<?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="com.nbport.zgwl.partner.mapper.PartnerManageMapper">

    <select id="selectPartnerSeqNextVal" resultType="java.lang.Long">
        select SEQ_ILDM_FEE_CUSTOMER_INFO.NEXTVAL from dual
    </select>

    <select id="selectPartnerContactSeqNextVal" resultType="java.lang.Long">
        select SEQ_SEAL_PARTNER_CONTACT.NEXTVAL from dual
    </select>

    <resultMap id="PartnerManageResult" type="com.nbport.zgwl.partner.dto.PartnerManageDto">
        <id property="customerSysid" column="CUSTOMER_SYSID"/>
        <result property="customerRegno" column="CUSTOMER_REGNO"/>
        <result property="customerNo" column="CUSTOMER_NO"/>
        <result property="customerNameC" column="CUSTOMER_NAME_C"/>
        <result property="customerTaxno" column="CUSTOMER_TAXNO"/>
        <result property="customerAddress" column="CUSTOMER_ADDRESS"/>
        <result property="officephone" column="OFFICEPHONE"/>
        <result property="customerBank" column="CUSTOMER_BANK"/>
        <result property="customerBankaccount" column="CUSTOMER_BANKACCOUNT"/>
        <result property="customerRegaddr" column="CUSTOMER_REGADDR"/>
        <result property="emailAddress" column="EMAIL_ADDRESS"/>
        <result property="orgId" column="ORG_ID"/>
        <result property="oppositeId" column="OPPOSITE_ID"/>
        <result property="oppositeCharacter" column="OPPOSITE_CHARACTER"/>
        <result property="gatFlag" column="GAT_FLAG"/>
        <result property="groupFlag" column="GROUP_FLAG"/>
        <result property="country" column="COUNTRY"/>
        <result property="countryName" column="COUNTRY_NAME"/>
        <result property="legalPersonName" column="LEGAL_PERSON_NAME"/>
        <result property="certificateType" column="CERTIFICATE_TYPE"/>
        <result property="certificateNum" column="CERTIFICATE_NUM"/>
        <result property="tinCode" column="TIN_CODE"/>
        <result property="registeredCapital" column="REGISTERED_CAPITAL"/>
        <result property="regCapital" column="REG_CAPITAL"/>
        <result property="oppositeCode" column="OPPOSITE_CODE"/>
        <result property="oppositeStatus" column="OPPOSITE_STATUS"/>
        <result property="isSgat" column="IS_SGAT"/>
        <result property="isJtn" column="IS_JTN"/>
        <result property="natureOfEnterpriseOrg" column="NATURE_OF_ENTERPRISE_ORG"/>
        <result property="natureOfEnterpriseOrgLev" column="NATURE_OF_ENTERPRISE_ORG_LEV"/>
        <result property="relativeType" column="RELATIVE_TYPE"/>
        <result property="isDeleted" column="IS_DELETED"/>
        <result property="comments" column="COMMENTS"/>
        <result property="remark" column="REMARK"/>
        <result property="createBy" column="CREATE_BY"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="updateBy" column="UPDATE_BY"/>
        <result property="updateTime" column="UPDATE_TIME"/>
        <result property="syncStatus" column="sync_status"/>
        <result property="syncTime" column="sync_time"/>
        <result property="syncMessage" column="sync_message"/>
    </resultMap>

    <resultMap id="PartnerContactResult" type="com.nbport.zgwl.partner.dto.PartnerContactDto">
        <id property="id" column="id"/>
        <result property="partnerRecordId" column="partner_record_id"/>
        <result property="contactName" column="contact_name"/>
        <result property="contactPhone" column="contact_phone"/>
        <result property="postalAddress" column="postal_address"/>
        <result property="email" column="email"/>
        <result property="sortOrder" column="sort_order"/>
        <result property="delFlag" column="del_flag"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectPartnerColumns">
        select CUSTOMER_SYSID, CUSTOMER_REGNO, CUSTOMER_NO, CUSTOMER_NAME_C, CUSTOMER_TAXNO,
               CUSTOMER_ADDRESS, OFFICEPHONE, CUSTOMER_BANK, CUSTOMER_BANKACCOUNT, CUSTOMER_REGADDR,
               EMAIL_ADDRESS, ORG_ID, OPPOSITE_ID, OPPOSITE_CHARACTER, GAT_FLAG, GROUP_FLAG,
               COUNTRY, COUNTRY_NAME, LEGAL_PERSON_NAME, CERTIFICATE_TYPE,
               CERTIFICATE_NUM, TIN_CODE, REGISTERED_CAPITAL, REG_CAPITAL,
               OPPOSITE_CODE, OPPOSITE_STATUS, IS_SGAT, IS_JTN,
               NATURE_OF_ENTERPRISE_ORG, NATURE_OF_ENTERPRISE_ORG_LEV, RELATIVE_TYPE,
               IS_DELETED, COMMENTS, REMARK, CREATE_BY, CREATE_TIME,
               UPDATE_BY, UPDATE_TIME, sync_status, sync_time, sync_message
        from ILDM_FEE_CUSTOMER_INFO
    </sql>

    <select id="selectPartnerPage" resultMap="PartnerManageResult">
        <include refid="selectPartnerColumns"/>
        <where>
            IS_DELETED = 'N'
            <if test="query.customerNameC != null and query.customerNameC != ''">
                and CUSTOMER_NAME_C like CONCAT(CONCAT('%', #{query.customerNameC}), '%')
            </if>
            <if test="query.relativeType != null and query.relativeType != ''">
                and RELATIVE_TYPE = #{query.relativeType}
            </if>
            <if test="query.oppositeCharacter != null and query.oppositeCharacter != ''">
                and OPPOSITE_CHARACTER = #{query.oppositeCharacter}
            </if>
            <if test="query.groupFlag != null and query.groupFlag != ''">
                and GROUP_FLAG = #{query.groupFlag}
            </if>
            <if test="query.syncStatus != null and query.syncStatus != ''">
                and sync_status = #{query.syncStatus}
            </if>
        </where>
        order by CREATE_TIME desc, CUSTOMER_SYSID desc
    </select>

    <select id="selectPartnerById" resultMap="PartnerManageResult">
        <include refid="selectPartnerColumns"/>
        where CUSTOMER_SYSID = #{customerSysid}
          and IS_DELETED = 'N'
    </select>

    <insert id="insertPartner" parameterType="com.nbport.zgwl.partner.dto.PartnerManageDto">
        insert into ILDM_FEE_CUSTOMER_INFO
        (
            CUSTOMER_SYSID, CUSTOMER_NAME_C, CUSTOMER_TAXNO, CUSTOMER_REGADDR, EMAIL_ADDRESS,
            OPPOSITE_ID, OPPOSITE_CHARACTER, GAT_FLAG, GROUP_FLAG, COUNTRY,
            COUNTRY_NAME, LEGAL_PERSON_NAME, CERTIFICATE_TYPE, CERTIFICATE_NUM,
            TIN_CODE, REGISTERED_CAPITAL, REG_CAPITAL, OPPOSITE_CODE,
            OPPOSITE_STATUS, IS_SGAT, IS_JTN, NATURE_OF_ENTERPRISE_ORG,
            NATURE_OF_ENTERPRISE_ORG_LEV, RELATIVE_TYPE, IS_DELETED, COMMENTS,
            REMARK, CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME,
            sync_status, sync_time, sync_message, ORG_ID
        )
        values
        (
            #{customerSysid}, #{customerNameC}, #{customerTaxno}, #{customerRegaddr}, #{emailAddress},
            #{oppositeId}, #{oppositeCharacter}, #{gatFlag}, #{groupFlag}, #{country},
            #{countryName}, #{legalPersonName}, #{certificateType}, #{certificateNum},
            #{tinCode}, #{registeredCapital}, #{regCapital}, #{oppositeCode},
            #{oppositeStatus}, #{isSgat}, #{isJtn}, #{natureOfEnterpriseOrg},
            #{natureOfEnterpriseOrgLev}, #{relativeType}, #{isDeleted}, #{comments},
            #{remark}, #{createBy}, #{createTime}, #{updateBy}, #{updateTime},
            #{syncStatus}, #{syncTime}, #{syncMessage}, #{orgId}
        )
    </insert>

    <update id="updatePartner" parameterType="com.nbport.zgwl.partner.dto.PartnerManageDto">
        update ILDM_FEE_CUSTOMER_INFO
        <trim prefix="set" suffixOverrides=",">
            CUSTOMER_NAME_C = #{customerNameC},
            CUSTOMER_TAXNO = #{customerTaxno},
            CUSTOMER_REGADDR = #{customerRegaddr},
            EMAIL_ADDRESS = #{emailAddress},
            OPPOSITE_CHARACTER = #{oppositeCharacter},
            GAT_FLAG = #{gatFlag},
            GROUP_FLAG = #{groupFlag},
            COUNTRY = #{country},
            COUNTRY_NAME = #{countryName},
            LEGAL_PERSON_NAME = #{legalPersonName},
            CERTIFICATE_TYPE = #{certificateType},
            CERTIFICATE_NUM = #{certificateNum},
            TIN_CODE = #{tinCode},
            REGISTERED_CAPITAL = #{registeredCapital},
            REG_CAPITAL = #{regCapital},
            OPPOSITE_CODE = #{oppositeCode},
            OPPOSITE_STATUS = #{oppositeStatus},
            IS_SGAT = #{isSgat},
            IS_JTN = #{isJtn},
            NATURE_OF_ENTERPRISE_ORG = #{natureOfEnterpriseOrg},
            NATURE_OF_ENTERPRISE_ORG_LEV = #{natureOfEnterpriseOrgLev},
            RELATIVE_TYPE = #{relativeType},
            COMMENTS = #{comments},
            REMARK = #{remark},
            UPDATE_BY = #{updateBy},
            UPDATE_TIME = #{updateTime},
            sync_status = #{syncStatus},
            sync_time = #{syncTime},
            sync_message = #{syncMessage},
        </trim>
        where CUSTOMER_SYSID = #{customerSysid}
          and IS_DELETED = 'N'
    </update>

    <update id="deletePartnerByIds">
        update ILDM_FEE_CUSTOMER_INFO
        set IS_DELETED = #{isDeleted},
            UPDATE_BY = #{updateBy},
            UPDATE_TIME = #{updateTime}
        where CUSTOMER_SYSID in
        <foreach collection="customerSysids" item="customerSysid" open="(" separator="," close=")">
            #{customerSysid}
        </foreach>
    </update>

    <select id="selectPartnerContactsByPartnerIds" resultMap="PartnerContactResult">
        select id, partner_record_id, contact_name, contact_phone, postal_address,
               email, sort_order, del_flag, remark
        from SEAL_PARTNER_CONTACT
        where del_flag = '0'
          and partner_record_id in
        <foreach collection="partnerRecordIds" item="partnerRecordId" open="(" separator="," close=")">
            #{partnerRecordId}
        </foreach>
        order by partner_record_id asc, sort_order asc, id asc
    </select>

    <update id="markPartnerContactsDeletedByPartnerId">
        update SEAL_PARTNER_CONTACT
        set del_flag = #{delFlag},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where partner_record_id = #{partnerRecordId}
          and del_flag = '0'
    </update>

    <update id="markPartnerContactsDeletedByPartnerIds">
        update SEAL_PARTNER_CONTACT
        set del_flag = #{delFlag},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where partner_record_id in
        <foreach collection="partnerRecordIds" item="partnerRecordId" open="(" separator="," close=")">
            #{partnerRecordId}
        </foreach>
          and del_flag = '0'
    </update>

    <insert id="batchInsertPartnerContacts">
        INSERT ALL
        <foreach collection="contacts" item="item">
            INTO SEAL_PARTNER_CONTACT
            (
                id, partner_record_id, contact_name, contact_phone, postal_address, email,
                sort_order, del_flag, create_by, create_time, update_by, update_time, remark
            )
            VALUES
            (
                #{item.id}, #{item.partnerRecordId}, #{item.contactName}, #{item.contactPhone},
                #{item.postalAddress}, #{item.email}, #{item.sortOrder},
                #{delFlag}, #{createBy}, #{createTime}, #{updateBy}, #{updateTime}, #{item.remark}
            )
        </foreach>
        SELECT 1 FROM DUAL
    </insert>

    <update id="updatePartnerSyncInfo">
        update ILDM_FEE_CUSTOMER_INFO
        set OPPOSITE_ID = #{oppositeId},
            sync_status = #{syncStatus},
            sync_time = #{syncTime},
            sync_message = #{syncMessage},
            UPDATE_BY = #{updateBy},
            UPDATE_TIME = #{updateTime}
        where CUSTOMER_SYSID = #{customerSysid}
          and IS_DELETED = 'N'
    </update>
</mapper>
