<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.roadm.mapper.SmsCompanySignInfoMapper">

    <resultMap type="SmsCompanySignInfo" id="SmsCompanySignInfoResultMap">
        <id column="id" property="id" />
        <result column="signature" property="signature" />
        <result column="company_code" property="companyCode" />
        <result column="company_name" property="companyName" />
        <result column="sing_source" property="singSource" />
        <result column="sign_pic" property="signPic" />
        <result column="trademark_registration_num" property="trademarkRegistrationNum" />
        <result column="source_desc" property="sourceDesc" />
        <result column="sign_ext_prefix" property="signExtPrefix" />
        <result column="flag" property="flag" />
        <result column="sign_customize" property="signCustomize" />
        <result column="sign_ext_suffix_digit" property="signExtSuffixDigit" />
        <result column="app_store_publish" property="appStorePublish" />
        <result column="remark" property="remark" />
        <result column="legal_name" property="legalName"/>
        <result column="legal_cert_type" property="legalCertType"/>
        <result column="legal_cert_id" property="legalCertId"/>
        <result column="legal_identity_card_path" property="legalCertId"/>
        <result column="agency_name" property="agencyName"/>
        <result column="agency_cert_type" property="agencyCertType"/>
        <result column="agency_cert_id" property="agencyCertId"/>
        <result column="agency_phone" property="agencyPhone"/>
<!--        <result column="agency_identity_card_path" property="agencyPhone"/>-->
        <result column="agency_identity_card_path" property="agencyIdentityCardPath"/>
        <result column="business_pic_path" property="businessPicPath"/>
        <result column="companyRemark" property="companyRemark"/>
        <result column="lt_agency_name" property="ltAgencyName"/>
        <result column="lt_agency_cert_type" property="ltAgencyCertType"/>
        <result column="lt_agency_cert_id" property="ltAgencyCertId"/>
        <result column="lt_agency_phone" property="ltAgencyPhone"/>
        <result column="lt_agency_identity_card_path" property="ltAgencyIdentityCardPath"/>
        <result column="yd_agency_name" property="ydAgencyName"/>
        <result column="yd_agency_cert_type" property="ydAgencyCertType"/>
        <result column="yd_agency_cert_id" property="ydAgencyCertId"/>
        <result column="yd_agency_phone" property="ydAgencyPhone"/>
        <result column="yd_agency_identity_card_path" property="ydAgencyIdentityCardPath"/>
        <result column="dx_agency_name" property="dxAgencyName"/>
        <result column="dx_agency_cert_type" property="dxAgencyCertType"/>
        <result column="dx_agency_cert_id" property="dxAgencyCertId"/>
        <result column="dx_agency_phone" property="dxAgencyPhone"/>
        <result column="dx_agency_identity_card_path" property="dxAgencyIdentityCardPath"/>
        <result column="reason" property="reason"/>
        <result column="submit_uid" property="submitUid"/>
        <result column="audit_uid" property="auditUid"/>
    </resultMap>

    <select id="listPageCompanySignInfo" parameterType="SmsCompanySignInfo"
            resultMap="SmsCompanySignInfoResultMap">
        SELECT
        s.id, s.signature, s.company_code, s.sing_source, s.sign_pic, s.trademark_registration_num,s.source_desc,
        s.sign_ext_prefix, s.flag, s.sign_customize, s.sign_ext_suffix_digit, c.company_name, s.app_store_publish,
        s.remark, s.reason, s.submit_uid, s.audit_uid
        FROM sms_company_sign_info s
        LEFT JOIN sms_company_info c ON s.company_code = c.company_code
        <where>
            <if test="companyName != null and companyName != ''">
                AND c.company_name LIKE CONCAT('%', #{companyName}, '%')
            </if>
            <if test="companyCode != null and companyCode != ''">
                AND s.company_code LIKE CONCAT('%', #{companyCode}, '%')
            </if>
            <if test="signature != null and signature != ''">
                AND s.signature LIKE CONCAT('%', #{signature}, '%')
            </if>
            <if test="signExtPrefix != null">
                AND CAST(s.sign_ext_prefix AS CHAR) LIKE CONCAT('%', #{signExtPrefix}, '%')
            </if>
            <if test="flag != null">
                AND s.flag = #{flag}  <!-- 有参数时按参数筛选（0/1） -->
            </if>
            <if test="submitUid != null">
                AND s.submit_uid = #{submitUid}
            </if>
        </where>
        ORDER BY s.id DESC
    </select>

    <select id="getAllCompanySignList" resultMap="SmsCompanySignInfoResultMap">
        SELECT
        id, signature, company_code, sing_source, sign_pic, trademark_registration_num, source_desc,
        sign_ext_prefix, flag, sign_customize, sign_ext_suffix_digit, app_store_publish, remark
        FROM sms_company_sign_info
    </select>


    <select id="getCompanySignInfos" parameterType="SmsCompanySignInfo"
            resultMap="SmsCompanySignInfoResultMap">
        SELECT
        s.id, s.signature, s.company_code, s.sing_source, s.sign_pic, s.trademark_registration_num,s.source_desc,
        s.sign_ext_prefix, s.flag, s.sign_customize, s.sign_ext_suffix_digit, c.company_name, s.app_store_publish,
        s.remark, c.business_pic_path, c.remark AS company_remark,
        c.legal_name, c.legal_cert_type, c.legal_cert_id, c.legal_identity_card_path,
        c.agency_name, c.agency_cert_type, c.agency_cert_id, c.agency_phone, c.agency_identity_card_path,
        c.lt_agency_name, c.lt_agency_cert_type, c.lt_agency_cert_id, c.lt_agency_phone, c.lt_agency_identity_card_path,
        c.yd_agency_name, c.yd_agency_cert_type, c.yd_agency_cert_id, c.yd_agency_phone, c.yd_agency_identity_card_path,
        c.dx_agency_name, c.dx_agency_cert_type, c.dx_agency_cert_id, c.dx_agency_phone, c.dx_agency_identity_card_path
        FROM sms_company_sign_info s
        LEFT JOIN sms_company_info c ON s.company_code = c.company_code
        <where>
            <if test="companyName != null and companyName != ''">
                AND c.company_name  = #{companyName}
            </if>
            <if test="companyCode != null and companyCode != ''">
                AND s.company_code = #{companyCode}
            </if>
            <if test="signature != null and signature != ''">
                AND s.signature LIKE CONCAT('%', #{signature}, '%')
            </if>
            <if test="signExtPrefix != null">
                AND CAST(s.sign_ext_prefix AS CHAR) LIKE CONCAT('%', #{signExtPrefix}, '%')
            </if>
            <if test="flag == null">
                AND s.flag = 1  <!-- 无参数时默认查生效 -->
            </if>
            <if test="flag != null">
                AND s.flag = #{flag}  <!-- 有参数时按参数筛选（0/1） -->
            </if>
        </where>
        ORDER BY s.id DESC
    </select>


    <select id="getExportCompanySignInfos" parameterType="SmsCompanySignInfo"
            resultMap="SmsCompanySignInfoResultMap">
        SELECT
        s.id, s.signature, s.company_code, s.sing_source, s.sign_pic, s.trademark_registration_num,s.source_desc,
        s.sign_ext_prefix, s.flag, s.sign_customize, s.sign_ext_suffix_digit, c.company_name, s.app_store_publish,
        s.remark, c.business_pic_path, c.remark AS company_remark,
        c.legal_name, c.legal_cert_type, c.legal_cert_id, c.legal_identity_card_path,
        c.agency_name, c.agency_cert_type, c.agency_cert_id, c.agency_phone, c.agency_identity_card_path,
        c.lt_agency_name, c.lt_agency_cert_type, c.lt_agency_cert_id, c.lt_agency_phone, c.lt_agency_identity_card_path,
        c.yd_agency_name, c.yd_agency_cert_type, c.yd_agency_cert_id, c.yd_agency_phone, c.yd_agency_identity_card_path,
        c.dx_agency_name, c.dx_agency_cert_type, c.dx_agency_cert_id, c.dx_agency_phone, c.dx_agency_identity_card_path
        FROM sms_company_sign_info s
        LEFT JOIN sms_company_info c ON s.company_code = c.company_code
        <where>
            <if test="companyName != null and companyName != ''">
                AND c.company_name  = #{companyName}
            </if>
            <if test="companyCode != null and companyCode != ''">
                AND s.company_code = #{companyCode}
            </if>
            <if test="signature != null and signature != ''">
                AND s.signature LIKE CONCAT('%', #{signature}, '%')
            </if>
            <if test="signExtPrefix != null">
                AND CAST(s.sign_ext_prefix AS CHAR) LIKE CONCAT('%', #{signExtPrefix}, '%')
            </if>
            <if test="flag != null">
                AND s.flag = #{flag}  <!-- 有参数时按参数筛选（0/1） -->
            </if>
        </where>
        ORDER BY s.id DESC
    </select>

    <select id="getCompanySignInfoById" parameterType="Integer" resultMap="SmsCompanySignInfoResultMap">
        SELECT
            s.id, s.signature, s.company_code, s.sing_source, s.sign_pic, s.trademark_registration_num,s.source_desc,
            s.sign_ext_prefix, s.flag, s.sign_customize, s.sign_ext_suffix_digit, c.company_name, s.app_store_publish, c.business_pic_path,
            s.remark, c.company_code, c.legal_name, c.legal_cert_type, c.legal_cert_id, c.agency_name, c.agency_cert_type, c.agency_cert_id, c.agency_phone,
        c.lt_agency_name, c.lt_agency_cert_type, c.lt_agency_cert_id, c.lt_agency_phone, c.lt_agency_identity_card_path,
        c.yd_agency_name, c.yd_agency_cert_type, c.yd_agency_cert_id, c.yd_agency_phone, c.yd_agency_identity_card_path,
        c.dx_agency_name, c.dx_agency_cert_type, c.dx_agency_cert_id, c.dx_agency_phone, c.dx_agency_identity_card_path
        FROM sms_company_sign_info s
        LEFT JOIN sms_company_info c ON s.company_code = c.company_code
        WHERE s.id = #{id}
    </select>

    <select id="querySignInfobyAture" resultType="int">
        SELECT COUNT(1)
        FROM sms_company_sign_info
        WHERE signature = #{signature}
          AND sign_customize = #{signCustomize}
    </select>

    <select id="existsAutoGeneratedAndValidSign" resultType="int">
        SELECT COUNT(1)
        FROM sms_company_sign_info
        WHERE sign_customize = 1
        AND signature = #{signature}
        LIMIT 1
    </select>

    <select id="findMaxSignExtPrefixInValidSign" resultType="java.lang.Integer">
        SELECT MAX(sign_ext_prefix)
        FROM sms_company_sign_info
        WHERE
            sign_ext_prefix LIKE CONCAT(#{prefix}, '%')
    </select>

    <select id="existsBySignExtPrefix" resultType="int">
        SELECT COUNT(1)
        FROM sms_company_sign_info
        WHERE sign_ext_prefix = #{signExtPrefix}
            LIMIT 1
    </select>

    <insert id="addCompanySignInfo" parameterType="SmsCompanySignInfo" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO sms_company_sign_info (signature, company_code, sing_source, sign_pic, trademark_registration_num,
            source_desc, sign_ext_prefix, flag, sign_customize, sign_ext_suffix_digit, app_store_publish,
            remark, submit_uid, audit_uid,create_time)
        VALUES (#{signature}, #{companyCode}, #{singSource}, #{signPic}, #{trademarkRegistrationNum},
                #{sourceDesc}, #{signExtPrefix},#{flag}, #{signCustomize}, #{signExtSuffixDigit}, #{appStorePublish},
                #{remark}, #{submitUid}, #{auditUid},NOW())
    </insert>

    <update id="updateCompanySignInfo" parameterType="SmsCompanySignInfo">
        UPDATE sms_company_sign_info
        <set>
            <if test="signature != null and signature != ''">signature = #{signature},</if>
            <if test="companyCode != null and companyCode != ''">company_code = #{companyCode},</if>
            <if test="singSource != null and singSource != ''">sing_source = #{singSource},</if>
            <if test="signPic != null">sign_pic = #{signPic},</if>
            <if test="trademarkRegistrationNum != null and trademarkRegistrationNum != ''">trademark_registration_num = #{trademarkRegistrationNum},</if>
            <if test="sourceDesc != null and sourceDesc != ''">source_desc = #{sourceDesc},</if>
            <if test="signExtPrefix != null">sign_ext_prefix = #{signExtPrefix},</if>
            <if test="flag != null">flag = #{flag},</if>
            <if test="signCustomize != null">sign_customize = #{signCustomize},</if>
            <if test="signExtSuffixDigit != null">sign_ext_suffix_digit = #{signExtSuffixDigit},</if>
            <if test="appStorePublish != null">app_store_publish = #{appStorePublish},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="auditUid != null">audit_uid = #{auditUid},</if>
            update_time = NOW()
        </set>
        WHERE id = #{id}
    </update>

    <update id="deleteSmsCompanySignInfo" parameterType="int">
        UPDATE sms_company_sign_info
        set flag = 0
        where id = #{id}
    </update>

    <update id="batchUpdateStatus" parameterType="SmsCompanySignInfo">
        UPDATE sms_company_sign_info
        SET flag = #{flag}
        WHERE id IN (
        <foreach collection="ids" item="id" separator=",">
            #{id}
        </foreach>
        )
    </update>

    <update id="updateCompanySignStatus">
        update sms_company_sign_info set flag = #{status} where id = #{id}
    </update>

    <update id="updateCompanyCodeByCode">
        UPDATE sms_company_sign_info
        SET company_code = #{companyCodeEnd}
        WHERE company_code = #{companyCodeStart}
    </update>

    <update id="updateCompanySignAuditStatus">
        update sms_company_sign_info set flag = #{status}, reason = #{reason}, audit_uid = #{auditUid}, sign_ext_prefix = #{signExtPrefix} where id = #{id}
    </update>

    <update id="batchUpdateCompanySignAuditStatus">
        update sms_company_sign_info set flag = #{status}, reason = #{reason}, audit_uid = #{auditUid} where id in
        <foreach item="item" collection="idList" separator="," open="(" close=")" index="index">
            ${item}
        </foreach>
    </update>
</mapper>