<?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.cm.central.control.dao.customer.ICustomerDao">

    <resultMap id="customerDTO" type="com.cm.central.control.pojo.dtos.customer.CustomerDTO">
        <id column="customer_id" property="customerId"/>
        <result column="customer_name" property="customerName"/>
        <result column="customer_type" property="customerType"/>
        <result column="contact_person1" property="contactPerson1"/>
        <result column="contact_tel1" property="contactTel1"/>
        <result column="contact_phone1" property="contactPhone1"/>
        <result column="contact_email1" property="contactEmail1"/>
        <result column="contact_person2" property="contactPerson2"/>
        <result column="contact_tel2" property="contactTel2"/>
        <result column="contact_phone2" property="contactPhone2"/>
        <result column="contact_email2" property="contactEmail2"/>
        <result column="area1_id" property="area1Id"/>
        <result column="area1_name" property="area1Name"/>
        <result column="area1_code" property="area1Code"/>
        <result column="area2_id" property="area2Id"/>
        <result column="area2_name" property="area2Name"/>
        <result column="area2_code" property="area2Code"/>
        <result column="area3_id" property="area3Id"/>
        <result column="area3_name" property="area3Name"/>
        <result column="area3_code" property="area3Code"/>
        <result column="area4_id" property="area4Id"/>
        <result column="area4_name" property="area4Name"/>
        <result column="area4_code" property="area4Code"/>
        <result column="area5_id" property="area5Id"/>
        <result column="area5_name" property="area5Name"/>
        <result column="area5_code" property="area5Code"/>
        <result column="area_address" property="areaAddress"/>
        <result column="gmt_create" property="gmtCreate"/>
    </resultMap>

    <!-- 新增客户管理 -->
    <insert id="saveCustomer" parameterType="map" flushCache="true">
        INSERT INTO central_customer (
            customer_id,
            customer_name,
            customer_type,
            contact_person1,
            contact_tel1,
            contact_phone1,
            contact_email1,
            contact_person2,
            contact_tel2,
            contact_phone2,
            contact_email2,
            area1_id,
            area1_name,
            area1_code,
            area2_id,
            area2_name,
            area2_code,
            area3_id,
            area3_name,
            area3_code,
            area4_id,
            area4_name,
            area4_code,
            area5_id,
            area5_name,
            area5_code,
            area_address,
            creator,
            gmt_create,
            modifier,
            gmt_modified,
            is_delete
        ) VALUES(
            #{customerId},
            #{customerName},
            #{customerType},
            #{contactPerson1},
            #{contactTel1},
            #{contactPhone1},
            #{contactEmail1},
            #{contactPerson2},
            #{contactTel2},
            #{contactPhone2},
            #{contactEmail2},
            #{area1Id},
            #{area1Name},
            #{area1Code},
            #{area2Id},
            #{area2Name},
            #{area2Code},
            #{area3Id},
            #{area3Name},
            #{area3Code},
            #{area4Id},
            #{area4Name},
            #{area4Code},
            #{area5Id},
            #{area5Name},
            #{area5Code},
            #{areaAddress},
            #{creator},
            #{gmtCreate},
            #{modifier},
            #{gmtModified},
            #{isDelete}
        )
    </insert>

    <!-- 删除客户管理 -->
    <update id="removeCustomer" parameterType="map" flushCache="true">
        UPDATE
            central_customer
        SET
            is_delete = 1,
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            customer_id IN
        <foreach collection="customerIds" index="index" open="(" separator="," close=")">
            #{customerIds[${index}]}
        </foreach>
    </update>

    <!-- 删除客户管理（物理） -->
    <update id="deleteCustomer" parameterType="map" flushCache="true">
        DELETE FROM
            central_customer
        WHERE
            customer_id IN
        <foreach collection="customerIds" index="index" open="(" separator="," close=")">
            #{customerIds[${index}]}
        </foreach>
    </update>

    <!-- 修改客户管理 -->
    <update id="updateCustomer" parameterType="map" flushCache="true">
        UPDATE
            central_customer
        SET
        <if test="customerName != null and customerName != ''">
            customer_name = #{customerName},
        </if>
        <if test="customerType != null">
            customer_type = #{customerType},
        </if>
        <if test="contactPerson1 != null">
            contact_person1 = #{contactPerson1},
        </if>
        <if test="contactTel1 != null">
            contact_tel1 = #{contactTel1},
        </if>
        <if test="contactPhone1 != null">
            contact_phone1 = #{contactPhone1},
        </if>
        <if test="contactEmail1 != null">
            contact_email1 = #{contactEmail1},
        </if>
        <if test="contactPerson2 != null">
            contact_person2 = #{contactPerson2},
        </if>
        <if test="contactTel2 != null">
            contact_tel2 = #{contactTel2},
        </if>
        <if test="contactPhone2 != null">
            contact_phone2 = #{contactPhone2},
        </if>
        <if test="contactEmail2 != null">
            contact_email2 = #{contactEmail2},
        </if>
        <if test="area1Id != null">
            area1_id = #{area1Id},
        </if>
        <if test="area1Name != null">
            area1_name = #{area1Name},
        </if>
        <if test="area1Code != null">
            area1_code = #{area1Code},
        </if>
        <if test="area2Id != null">
            area2_id = #{area2Id},
        </if>
        <if test="area2Name != null">
            area2_name = #{area2Name},
        </if>
        <if test="area2Code != null">
            area2_code = #{area2Code},
        </if>
        <if test="area3Id != null">
            area3_id = #{area3Id},
        </if>
        <if test="area3Name != null">
            area3_name = #{area3Name},
        </if>
        <if test="area3Code != null">
            area3_code = #{area3Code},
        </if>
        <if test="area4Id != null">
            area4_id = #{area4Id},
        </if>
        <if test="area4Name != null">
            area4_name = #{area4Name},
        </if>
        <if test="area4Code != null">
            area4_code = #{area4Code},
        </if>
        <if test="area5Id != null">
            area5_id = #{area5Id},
        </if>
        <if test="area5Name != null">
            area5_name = #{area5Name},
        </if>
        <if test="area5Code != null">
            area5_code = #{area5Code},
        </if>
        <if test="areaAddress != null">
            area_address = #{areaAddress},
        </if>
            modifier = #{modifier},
            gmt_modified = #{gmtModified}
        WHERE
            customer_id = #{customerId}
    </update>

    <!-- 客户管理详情 -->
    <select id="getCustomer" parameterType="map" resultMap="customerDTO" useCache="true">
        SELECT
            t1.customer_name,
            t1.customer_type,
            t1.contact_person1,
            t1.contact_tel1,
            t1.contact_phone1,
            t1.contact_email1,
            t1.contact_person2,
            t1.contact_tel2,
            t1.contact_phone2,
            t1.contact_email2,
            t1.area1_id,
            t1.area1_name,
            t1.area1_code,
            t1.area2_id,
            t1.area2_name,
            t1.area2_code,
            t1.area3_id,
            t1.area3_name,
            t1.area3_code,
            t1.area4_id,
            t1.area4_name,
            t1.area4_code,
            t1.area5_id,
            t1.area5_name,
            t1.area5_code,
            t1.area_address,
            t1.customer_id
        FROM
            central_customer t1
        WHERE
            t1.is_delete = 0
        <if test="customerId != null and customerId != ''">
        AND
            t1.customer_id = #{customerId}
        </if>
    </select>

    <!-- 客户管理列表 -->
    <select id="listCustomer" parameterType="map" resultMap="customerDTO" useCache="true">
        SELECT
            t1.customer_name,
            t1.customer_type,
            t1.contact_person1,
            t1.contact_tel1,
            t1.contact_phone1,
            t1.contact_email1,
            t1.contact_person2,
            t1.contact_tel2,
            t1.contact_phone2,
            t1.contact_email2,
            t1.area1_name,
            t1.area1_code,
            t1.area2_name,
            t1.area2_code,
            t1.area3_name,
            t1.area3_code,
            t1.area4_name,
            t1.area4_code,
            t1.area5_name,
            t1.area5_code,
            t1.area_address,
            LEFT(t1.gmt_create, 19) gmt_create,
            t1.customer_id
        FROM
            central_customer t1
        WHERE
            t1.is_delete = 0
        <if test="keywords != null and keywords != ''">
        AND (
            t1.customer_name LIKE CONCAT('%', #{keywords}, '%')
        )
        </if>
        <if test="startTime != null and startTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ >= ]]> #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        AND
            LEFT(t1.gmt_create, 10) <![CDATA[ <= ]]> #{endTime}
        </if>
        <if test="customerIds != null and customerIds.size > 0">
        AND
            t1.customer_id IN
            <foreach collection="customerIds" index="index" open="(" separator="," close=")">
            #{customerIds[${index}]}
            </foreach>
        </if>
    </select>

    <!-- 客户管理统计  -->
    <select id="countCustomer" parameterType="map" resultType="Integer" useCache="true">
        SELECT
            COUNT(*)
        FROM
            central_customer t1
        WHERE
            t1.is_delete = 0
    </select>

</mapper>