<?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.CustomerMapper">
    
    <resultMap type="Customer" id="CustomersResult">
        <result property="id"    column="id"    />
        <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="isDelete"    column="is_delete"    />
        <result property="chainsName"    column="chains_name"    />
        <result property="chainsCode"    column="chains_code"    />
        <result property="stateName"    column="state_name"    />
        <result property="cityName"    column="city_name"    />
        <result property="districtName"    column="district_name"    />
        <result property="userAccount"    column="user_account"    />
        <result property="userName"    column="user_name"    />
        <result property="userNo"    column="user_no"    />
        <result property="creditScore"    column="credit_score"    />
        <result property="currentScore"    column="current_score"    />
        <result property="code"    column="code"    />
        <result property="lineId"    column="line_id"    />
    </resultMap>

    <sql id="selectCustomersVo">
        select id, create_by, create_time, update_by, update_time, is_delete, chains_name, chains_code, state_name, city_name, district_name, user_account, user_name, user_no, credit_score, current_score, code, line_id from customers
    </sql>

    <select id="selectCustomersList" parameterType="Customers" resultMap="CustomersResult">
        <include refid="selectCustomersVo"/>
        <where>  
            <if test="isDelete != null "> and is_delete = #{isDelete}</if>
            <if test="chainsName != null  and chainsName != ''"> and chains_name like concat('%', #{chainsName}, '%')</if>
            <if test="chainsCode != null  and chainsCode != ''"> and chains_code = #{chainsCode}</if>
            <if test="stateName != null  and stateName != ''"> and state_name like concat('%', #{stateName}, '%')</if>
            <if test="cityName != null  and cityName != ''"> and city_name like concat('%', #{cityName}, '%')</if>
            <if test="districtName != null  and districtName != ''"> and district_name like concat('%', #{districtName}, '%')</if>
            <if test="userAccount != null  and userAccount != ''"> and user_account = #{userAccount}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="userNo != null  and userNo != ''"> and user_no = #{userNo}</if>
            <if test="creditScore != null "> and credit_score = #{creditScore}</if>
            <if test="currentScore != null "> and current_score = #{currentScore}</if>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="lineId != null  and lineId != ''"> and line_id = #{lineId}</if>
        </where>
    </select>
    
    <select id="selectCustomersById" parameterType="Long" resultMap="CustomersResult">
        <include refid="selectCustomersVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertCustomers" parameterType="Customers" useGeneratedKeys="true" keyProperty="id">
        insert into customers
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="updateTime != null ">update_time,</if>
            <if test="isDelete != null ">is_delete,</if>
            <if test="chainsName != null  and chainsName != ''">chains_name,</if>
            <if test="chainsCode != null  and chainsCode != ''">chains_code,</if>
            <if test="stateName != null  and stateName != ''">state_name,</if>
            <if test="cityName != null  and cityName != ''">city_name,</if>
            <if test="districtName != null  and districtName != ''">district_name,</if>
            <if test="userAccount != null  and userAccount != ''">user_account,</if>
            <if test="userName != null  and userName != ''">user_name,</if>
            <if test="userNo != null  and userNo != ''">user_no,</if>
            <if test="creditScore != null ">credit_score,</if>
            <if test="currentScore != null ">current_score,</if>
            <if test="lineId != null  and lineId != ''">line_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null ">#{updateTime},</if>
            <if test="isDelete != null ">#{isDelete},</if>
            <if test="chainsName != null  and chainsName != ''">#{chainsName},</if>
            <if test="chainsCode != null  and chainsCode != ''">#{chainsCode},</if>
            <if test="stateName != null  and stateName != ''">#{stateName},</if>
            <if test="cityName != null  and cityName != ''">#{cityName},</if>
            <if test="districtName != null  and districtName != ''">#{districtName},</if>
            <if test="userAccount != null  and userAccount != ''">#{userAccount},</if>
            <if test="userName != null  and userName != ''">#{userName},</if>
            <if test="userNo != null  and userNo != ''">#{userNo},</if>
            <if test="creditScore != null ">#{creditScore},</if>
            <if test="currentScore != null ">#{currentScore},</if>
            <if test="code != null  and code != ''">#{code},</if>
            <if test="lineId != null  and lineId != ''">#{lineId},</if>
         </trim>
    </insert>

    <update id="updateCustomers" parameterType="Customers">
        update customers
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null  and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="updateBy != null  and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null ">update_time = #{updateTime},</if>
            <if test="isDelete != null ">is_delete = #{isDelete},</if>
            <if test="chainsName != null  and chainsName != ''">chains_name = #{chainsName},</if>
            <if test="chainsCode != null  and chainsCode != ''">chains_code = #{chainsCode},</if>
            <if test="stateName != null  and stateName != ''">state_name = #{stateName},</if>
            <if test="cityName != null  and cityName != ''">city_name = #{cityName},</if>
            <if test="districtName != null  and districtName != ''">district_name = #{districtName},</if>
            <if test="userAccount != null  and userAccount != ''">user_account = #{userAccount},</if>
            <if test="userName != null  and userName != ''">user_name = #{userName},</if>
            <if test="userNo != null  and userNo != ''">user_no = #{userNo},</if>
            <if test="creditScore != null ">credit_score = #{creditScore},</if>
            <if test="currentScore != null ">current_score = #{currentScore},</if>
            <if test="code != null  and code != ''">code = #{code},</if>
            <if test="lineId != null  and lineId != ''">line_id = #{lineId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCustomersById" parameterType="Long">
        delete from customers where id = #{id}
    </delete>

    <delete id="deleteCustomersByIds" parameterType="String">
        delete from customers where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectCustomerMapByCode" resultMap="CustomersResult">
       select code ,chains_name,chains_code from customers where code is not null
    </select>
    <select id="selectCustomerMapByChainsCode" resultMap="CustomersResult">
       select chains_code ,chains_name  from customers where chains_code is not null
    </select>

    <select id="selectCustomersByChainsCode" parameterType="String" resultMap="CustomersResult">
        <include refid="selectCustomersVo"/>
        where chains_code = #{chainsCode} limit 1
    </select>
    
    <select id="selectCustomerCodeByNotStart" resultType="com.dgtly.system.domain.DataMasterCustomerVO">
        SELECT DISTINCT
            CustomerCode,
            CustomerName
        FROM
            data_master_customer
        WHERE
            1 = 1
          AND (CustomerTypeCode5 = 'SSB' or (IsFreeze='否' and CustomerTypeCode1='D01' and IsBMDCustomer='否'))
          AND CustomerCode NOT IN (
            SELECT
                customers_code
            FROM
                customers_start)
    </select>

    <insert id="insertCustomersStart" >
        insert into customers_start
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="customersCode != null  and customersCode != ''">customers_code,</if>
            <if test="customersName != null  and customersName != ''">customers_name,</if>
            <if test="onlineTime != null">online_time,</if>
            <if test="customerType !=null and customerType !='' ">customer_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="customersCode != null  and customersCode != ''">#{customersCode},</if>
            <if test="customersName != null  and customersName != ''">#{customersName},</if>
            <if test="onlineTime != null">#{onlineTime},</if>
            <if test="customerType !=null and customerType != '' ">#{customerType},</if>
        </trim>
    </insert>
    
</mapper>