<?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.yijia.label.mapper.CustomerLabelMapper">
    <resultMap type="CustomerLabel" id="CustomerLabelResult">
        <result property="id"    column="id"    />
        <result property="phone"    column="phone"    />
        <result property="labelId"    column="label_id"    />
        <result property="stationId"    column="station_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="delFlag"    column="del_flag"    />
        <result property="customerName"    column="customer_name"    />
    </resultMap>

    <sql id="selectCustomerLabelVo">
        select id, phone, label_id, station_id, create_by, create_time, update_by, update_time, del_flag from customer_label
    </sql>

    <select id="selectCustomerLabelList" parameterType="CustomerLabel" resultMap="CustomerLabelResult">
        <include refid="selectCustomerLabelVo"/>
        <where>
            <if test="phone != null  and phone != ''"> and phone like concat('%', #{phone}, '%')</if>
            <if test="labelId != null "> and label_id = #{labelId}</if>
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="delFlag != null "> and del_flag = #{delFlag}</if>
        </where>
    </select>
    <select id="selectNotCustomerLabelInfo" parameterType="CustomerLabel" resultMap="CustomerLabelResult">
        SELECT	* FROM(
        SELECT DISTINCT ( m.phone_number ) AS phone,customer_name,pay_date
        FROM customer_manage m
        LEFT JOIN app_user_info a ON m.mina_user_id = a.user_id
        LEFT JOIN ( SELECT max( pay_date ) AS pay_date, po.consumer_id FROM pay_order po
        <where>
            <if test="stationId != null "> and po.station_id = #{stationId}</if>
        </where>
          GROUP BY consumer_id ) c ON c.consumer_id = a.user_id
        <where>
            <if test="stationId != null "> and m.station_id = #{stationId}</if>
            <if test="phone != null  and phone != ''"> and m.phone_number like concat('%', #{phone}, '%')</if>
            <if test="customerName != null  and customerName != ''"> and m.customer_name like concat('%', #{customerName}, '%')</if>
        </where>
        AND m.phone_number NOT IN ( SELECT phone FROM customer_label b
        <where>
            <if test="stationId != null "> and b.station_id = #{stationId}</if>
            <if test="delFlag != null "> and del_flag = #{delFlag}</if>
            <if test="phone != null  and phone != ''"> and b.phone like concat('%', #{phone}, '%')</if>
        </where>
         )) po
        ORDER BY  po.pay_date DESC
    </select>
    <select id="selectPhonelist" parameterType="CustomerLabel" resultMap="CustomerLabelResult">
        SELECT DISTINCT phone,label_id,c.station_id,m.customer_name from  customer_label	c
        left join  customer_manage m on c.phone = m.phone_number and c.station_id = m.station_id
        <where>
            <if test="stationId != null "> and c.station_id = #{stationId}</if>
            <if test="labelId != null "> and label_id = #{labelId}</if>
            <if test="delFlag != null "> and del_flag = #{delFlag}</if>
            <if test="phone != null  and phone != ''"> and c.phone like concat('%', #{phone}, '%')</if>
            <if test="customerName != null  and customerName != ''"> and m.customer_name like concat('%', #{customerName}, '%')</if>
        </where>
    </select>
    <select id="selectCustomerLabelById" parameterType="Integer" resultMap="CustomerLabelResult">
        <include refid="selectCustomerLabelVo"/>
        where id = #{id}
    </select>

    <select id="selectCustomerLabelInfo" parameterType="CustomerLabel" resultMap="CustomerLabelResult">
        <include refid="selectCustomerLabelVo"/>
        <where>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="labelId != null "> and label_id = #{labelId}</if>
            <if test="stationId != null "> and station_id = #{stationId}</if>
            <if test="delFlag != null "> and del_flag = #{delFlag}</if>
        </where>
         limit 1
    </select>

    <insert id="insertCustomerLabel" parameterType="CustomerLabel" useGeneratedKeys="true" keyProperty="id">
        insert into customer_label
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="phone != null">phone,</if>
            <if test="labelId != null">label_id,</if>
            <if test="stationId != null">station_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="phone != null">#{phone},</if>
            <if test="labelId != null">#{labelId},</if>
            <if test="stationId != null">#{stationId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateCustomerLabel" parameterType="CustomerLabel">
        update customer_label
        <trim prefix="SET" suffixOverrides=",">
            <if test="phone != null">phone = #{phone},</if>
            <if test="labelId != null">label_id = #{labelId},</if>
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCustomerLabelById" parameterType="Integer">
        delete from customer_label where id = #{id}
    </delete>

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

</mapper>
