<?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.order.mapper.CustomersStartMapper">
    
    <resultMap type="com.dgtly.order.domain.CustomersStart" id="CustomersStartResult">
        <result property="customersCode"    column="customers_code"    />
        <result property="customersName"    column="customers_name"    />
        <result property="onlineTime"    column="online_time"    />
        <result property="customerType"    column="customer_type"    />
    </resultMap>

    <sql id="selectCustomersStartVo">
        select customers_code, customers_name,online_time,customer_type from customers_start
    </sql>

    <select id="selectCustomersStartList" parameterType="com.dgtly.order.domain.CustomersStart" resultMap="CustomersStartResult">
        <include refid="selectCustomersStartVo"/>
        <where>  
            <if test="customersCode != null  and customersCode != ''"> and customers_code like concat('%', #{customersCode}, '%')</if>
            <if test="customersName != null  and customersName != ''"> and customers_name like concat('%', #{customersName}, '%')</if>
            <if test="onlineTime != null"> and online_time = #{onlineTime}</if>
        </where>
        order by online_time desc
    </select>
    
    <select id="selectCustomersStartByCustomersCode" parameterType="String" resultMap="CustomersStartResult">
        <include refid="selectCustomersStartVo"/>
        where customers_code = #{customersCode}
    </select>
    <select id="selectcustomersList" resultType="com.dgtly.order.domain.OrderCustomers" parameterType="com.dgtly.order.domain.OrderCustomers">
        select id, chains_code as chainsCode, chains_name as chainsName from customers
        where is_delete = 0
        and chains_code is not null
        and chains_name is not null
        <if test="chainsCode != null and chainsCode != ''">
            AND chains_code like concat('%', #{chainsCode}, '%')
        </if>
        <if test="chainsName != null and chainsName != ''">
            AND chains_name like concat('%', #{chainsName}, '%')
        </if>
        GROUP BY chains_code,chains_name
    </select>
    <select id="selectCustomersById" resultType="com.dgtly.order.domain.OrderCustomers" parameterType="Long">
         select id, chains_name as chainsName, chains_code as chainsCode, state_name as stateName, city_name as cityName
         from customers
         where is_delete = 0
         and chains_code is not null
         and chains_name is not null
         and id = #{id}
    </select>

    <select id="selectCustomerAuthList" resultType="com.dgtly.order.domain.CustomerAuthModel">
        SELECT
            mds.sales_company companyCode,
            mds.sales_company_name companyName,
            mds.sales_office saleCode,
            mds.sales_office_name saleName,
            mds.sales_group officeCode,
            mds.sales_group_name officeName,
            mds.sales_group subOfficeCode,
            mds.sales_group_name subOfficeName,
            ce.chains_code customerCode,
            mds.CustomerName customerName,
            ce.org_attestation_time authTime
        FROM
            customers_ext ce

                LEFT JOIN data_master_customer mds on ce.chains_code=mds.CustomerCode

        WHERE
            ce.org_attestation_time IS NOT NULL
        GROUP BY
            ce.chains_code
        ORDER BY mds.sales_company DESC
    </select>

    <select id="selectCustomerExt" resultType="com.dgtly.order.domain.CustomerAuthModel">
        SELECT chains_code customerCode,org_attestation_time authTime FROM `customers_ext` where is_authentication=1
    </select>

    <select id="selectCustomerExtByid" resultType="com.dgtly.order.domain.CustomerAuthModel">
        SELECT chains_code customerCode,org_attestation_time authTime FROM `customers_ext` where is_authentication=1
        and chains_code =#{id} limit 1
    </select>

    <insert id="insertCustomersStart" parameterType="com.dgtly.order.domain.CustomersStart">
        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>
         </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>
         </trim>
    </insert>

    <update id="updateCustomersStart" parameterType="com.dgtly.order.domain.CustomersStart">
        update customers_start
        <trim prefix="SET" suffixOverrides=",">
            <if test="customersName != null  and customersName != ''">customers_name = #{customersName},</if>
        </trim>
        where customers_code = #{customersCode}
    </update>

    <delete id="deleteCustomersStartById" parameterType="String">
        delete from customers_start where customers_code = #{customersCode}
    </delete>

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

    <select id="selectCustomerNameByCustomerId" resultType="java.lang.String">
        select chains_name from customers where chains_code=#{customerId} limit 1
    </select>
</mapper>