<?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.dgtly.system.mapper.CustomersExtMapper">
    
    <resultMap type="CustomersExt" id="CustomersExtResult">
        <result property="chainsCode"    column="chains_code"    />
        <result property="creator"    column="creator"    />
        <result property="customersName"    column="customers_name"    />
        <result property="orgId"    column="org_id"    />
        <result property="idType"    column="id_type"    />
        <result property="idNumber"    column="id_number"    />
        <result property="orgLegalidNumber"    column="org_legalId_number"    />
        <result property="orgLegalName"    column="org_legal_name"    />
        <result property="isAuthentication"    column="is_authentication"    />
        <result property="orgAttestationTime"    column="org_attestation_time"    />
        <result property="receiveUrlMobileNo"    column="receive_url_mobile_no"    />
    </resultMap>

    <sql id="selectCustomersExtVo">
        select chains_code, creator, customers_name, org_id, id_type, id_number, org_legalId_number, org_legal_name, is_authentication, org_attestation_time,receive_url_mobile_no from customers_ext
    </sql>

    <select id="selectCustomersExtList" parameterType="CustomersExt" resultMap="CustomersExtResult">
        <include refid="selectCustomersExtVo"/>
        <where>  
            <if test="creator != null  and creator != ''"> and creator = #{creator}</if>
            <if test="customersName != null  and customersName != ''"> and customers_name like concat('%', #{customersName}, '%')</if>
            <if test="orgId != null  and orgId != ''"> and org_id = #{orgId}</if>
            <if test="idType != null  and idType != ''"> and id_type = #{idType}</if>
            <if test="idNumber != null  and idNumber != ''"> and id_number = #{idNumber}</if>
            <if test="orgLegalidNumber != null  and orgLegalidNumber != ''"> and org_legalId_number = #{orgLegalidNumber}</if>
            <if test="orgLegalName != null  and orgLegalName != ''"> and org_legal_name like concat('%', #{orgLegalName}, '%')</if>
            <if test="isAuthentication != null  and isAuthentication != ''"> and is_authentication = #{isAuthentication}</if>
            <if test="receiveUrlMobileNo != null  and receiveUrlMobileNo != ''"> and receive_url_mobile_no = #{receiveUrlMobileNo}</if>
        </where>
    </select>
    
    <select id="selectCustomersExtById" parameterType="String" resultMap="CustomersExtResult">
        <include refid="selectCustomersExtVo"/>
        where chains_code = #{chainsCode}
    </select>

    <!--根据订单id关联订单基础信息表查询经销商注册id-->
    <select id="selectOrgIdByOrderId" resultType="java.lang.String">
        SELECT
            s.org_id
        FROM
            sales_order_base t
        LEFT JOIN customers_ext s ON t.belong_to = s.chains_code
        WHERE t.is_delete=0
           and  t.order_number = #{orderId};
    </select>

    <!--根据订单id关联订单基础信息表查询经销商代号-->
    <select id="selectChainsCodeByOrderId" resultType="java.lang.String">
        SELECT
            org_id
        FROM
        customers_ext
        WHERE 1=1
        <if test="customerCode !=null">
        and chains_code=#{customerCode}
        </if>
    </select>

    <insert id="insertCustomersExt" parameterType="CustomersExt">
        insert into customers_ext
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="chainsCode != null  and chainsCode != ''">chains_code,</if>
            <if test="creator != null  and creator != ''">creator,</if>
            <if test="customersName != null  and customersName != ''">customers_name,</if>
            <if test="orgId != null  and orgId != ''">org_id,</if>
            <if test="idType != null  and idType != ''">id_type,</if>
            <if test="idNumber != null  and idNumber != ''">id_number,</if>
            <if test="orgLegalidNumber != null  and orgLegalidNumber != ''">org_legalId_number,</if>
            <if test="orgLegalName != null  and orgLegalName != ''">org_legal_name,</if>
            <if test="isAuthentication != null  and isAuthentication != ''">is_authentication,</if>
            <if test="receiveUrlMobileNo != null  and receiveUrlMobileNo != ''">receive_url_mobile_no,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="chainsCode != null  and chainsCode != ''">#{chainsCode},</if>
            <if test="creator != null  and creator != ''">#{creator},</if>
            <if test="customersName != null  and customersName != ''">#{customersName},</if>
            <if test="orgId != null  and orgId != ''">#{orgId},</if>
            <if test="idType != null  and idType != ''">#{idType},</if>
            <if test="idNumber != null  and idNumber != ''">#{idNumber},</if>
            <if test="orgLegalidNumber != null  and orgLegalidNumber != ''">#{orgLegalidNumber},</if>
            <if test="orgLegalName != null  and orgLegalName != ''">#{orgLegalName},</if>
            <if test="isAuthentication != null  and isAuthentication != ''">#{isAuthentication},</if>
            <if test="receiveUrlMobileNo != null  and receiveUrlMobileNo != ''">#{receiveUrlMobileNo},</if>
         </trim>
    </insert>

    <update id="updateCustomersExt" parameterType="CustomersExt">
        update customers_ext
        <trim prefix="SET" suffixOverrides=",">
            <if test="creator != null  and creator != ''">creator = #{creator},</if>
            <if test="customersName != null  and customersName != ''">customers_name = #{customersName},</if>
            <if test="orgId != null  and orgId != ''">org_id = #{orgId},</if>
            <if test="idType != null  and idType != ''">id_type = #{idType},</if>
            <if test="idNumber != null  and idNumber != ''">id_number = #{idNumber},</if>
            <if test="orgLegalidNumber != null  and orgLegalidNumber != ''">org_legalId_number = #{orgLegalidNumber},</if>
            <if test="orgLegalName != null  and orgLegalName != ''">org_legal_name = #{orgLegalName},</if>
            <if test="isAuthentication != null  and isAuthentication != ''">is_authentication = #{isAuthentication},</if>
            <if test="receiveUrlMobileNo != null  and receiveUrlMobileNo != ''">receive_url_mobile_no = #{receiveUrlMobileNo},</if>
        </trim>
        where chains_code = #{chainsCode}
    </update>

    <update id="updateCustomersIsAuthentication" parameterType="String">
        update customers_ext set is_authentication = #{isAuthentication}, org_attestation_time = now()
		where org_id = #{accountId}
    </update>

    <delete id="deleteCustomersExtById" parameterType="String">
        delete from customers_ext where chains_code = #{chainsCode}
    </delete>

    <delete id="deleteCustomersExtByIds" parameterType="String">
        delete from customers_ext where chains_code in 
        <foreach item="chainsCode" collection="array" open="(" separator="," close=")">
            #{chainsCode}
        </foreach>
    </delete>
    
</mapper>