<?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.house365.ws.dao.mapper.LabelMapper">

    <resultMap id="infoMap" type="com.house365.beans.entity.LabelEntity">
        <result property="labelId" column="id" />
        <result property="colorId" column="colorId" />
        <result property="labelName" column="labelName" />
        <result property="createId" column="createId" />
        <result property="createName" column="createName" />
        <result property="updateTime" column="updateTime" />
        <result property="labelId" column="labelId" />
        <result property="cityCode" column="cityCode" />
        <result property="num" column="num" />
    </resultMap>

    <select id="getColorLabel" resultMap="infoMap" parameterType="map">
        select cl.id,cl.colorId,cl.labelName,cl.createId,cl.createName,cl.updateTime,c.num
        from color_label cl left join (select cl2.labelId, count(distinct cl2.customerId) num from customer_label cl2 where cl2.is_delete=0 group by cl2.labelId) c
        on cl.id=c.labelId
        where 1=1
        <if test="createId != null and createId != ''">
            and cl.createId =#{createId}
        </if>
        and cl.is_delete=0
        order by cl.updateTime desc
    </select>

    <select id="getColorLabel4MyCustomer" resultMap="infoMap" parameterType="map">
        select cl.id,cl.colorId,cl.createId,cl.labelName
        from color_label cl
        where 1=1
        <if test="createId != null and createId != ''">
            and cl.createId =#{createId}
        </if>
        <if test="cityCode != null and cityCode != ''">
            and cl.cityCode =#{cityCode}
        </if>
        and cl.is_delete=0
        order by cl.updateTime desc
    </select>

    <select id="getLabelList" resultMap="infoMap" parameterType="map">
        select cl.id,cl.colorId,cl.labelName,cl.createId,cl.createName,cl.updateTime,c.num
        from color_label cl left join (select cl2.labelId, count(distinct cl2.customerId) num from customer_label cl2 where cl2.is_delete=0 group by cl2.labelId) c
        on cl.id=c.labelId
        where 1=1
        <if test="labelName != null and labelName != ''">
            and cl.labelName like CONCAT('%', #{labelName},'%')
        </if>
        <if test="createId != null and createId != ''">
            and cl.createId =#{createId}
        </if>
        <if test="cityCode != null and cityCode != ''">
            and cl.cityCode =#{cityCode}
        </if>
        and cl.is_delete=0
        order by cl.updateTime desc
        limit #{PageNumber}, #{pageSize}
    </select>

    <select id="getLabelListCount" resultType="Integer" parameterType="map">
        select count(*)
        from color_label cl left join (select cl2.labelId, count(distinct cl2.customerId) num from customer_label cl2 where cl2.is_delete=0 group by cl2.labelId) c
        on cl.id=c.labelId
        where 1=1
        <if test="labelName != null and labelName != ''">
            and cl.labelName like CONCAT('', #{labelName},'%')
        </if>
        <if test="createId != null and createId != ''">
            and cl.createId =#{createId}
        </if>
        and cl.is_delete=0
    </select>

    <insert id="addLabel" parameterType="com.house365.beans.entity.LabelEntity">
        insert into color_label (colorId,labelName,createId,createName,updateTime,cityCode) values
        (
        #{colorId},#{labelName},#{createId},#{createName},#{updateTime},#{cityCode}
        )
    </insert>

    <update id="updateLabel" parameterType="com.house365.beans.entity.LabelEntity">
        UPDATE color_label
        SET
        colorId = #{colorId},labelName = #{labelName},updateTime = #{updateTime}
        WHERE id = #{id}
    </update>

    <update id="updateUserCustomerLabel" parameterType="com.house365.beans.entity.LabelEntity">
        UPDATE customer_label
        SET
        is_delete=1
        WHERE managerId = #{managerId} and customerId = #{customerId}
    </update>

    <update id="delLabel" parameterType="com.house365.beans.entity.LabelEntity">
        UPDATE color_label set is_delete=1,updateTime = #{updateTime} WHERE id = #{id}
    </update>

    <insert id="saveCustomersLabels" parameterType="List">
        insert into customer_label (customerId,managerId,labelId) values

        <foreach collection="list" item="entity" index="index" separator=",">
            (#{entity.customerId},#{entity.managerId},#{entity.labelId})
        </foreach>
    </insert>

    <select id="getCustomerLabelById" resultMap="infoMap" parameterType="com.house365.beans.entity.LabelEntity">
        select labelId from customer_label
        where is_delete =0
        <if test="managerId != null and managerId != ''">
            and managerId = #{managerId}
        </if>
        <if test="customerId != null and customerId != ''">
            and customerId =#{customerId}
        </if>
    </select>

    <select id="getCustomerLabelIds" resultMap="infoMap" parameterType="com.house365.beans.entity.LabelEntity">
        select labelId from customer_label
        where is_delete =0
        <if test="customerId != null and customerId != ''">
            and customerId =#{customerId}
        </if>
        <if test="labelId != null and labelId != ''">
            and labelId =#{labelId}
        </if>
    </select>

    <select id="queryUserCustomerLabel" resultMap="infoMap" parameterType="map">
        select labelId from customer_label
        where is_delete =0
        <if test="managerId != null and managerId != ''">
            and managerId = #{managerId}
        </if>
        <if test="customerId != null and customerId != ''">
            and customerId =#{customerId}
        </if>
    </select>

    <select id="queryUserLabel" resultMap="infoMap" parameterType="map">
        select * from color_label
        where  1=1
        and is_delete =0
        <if test="userId != null and userId != ''">
            and createId = #{userId}
        </if>
    </select>


    <update id="delCustomerLabel" parameterType="com.house365.beans.entity.LabelEntity">
        UPDATE customer_label set is_delete=1
        WHERE customerId = #{customerId}
        <if test="labelId != null and labelId != ''">
            and labelId = #{labelId}
        </if>
        <if test="managerId != null and managerId != ''">
            and managerId = #{managerId}
        </if>
    </update>

    <update id="updateCustomerId" parameterType="map">
        UPDATE customer_label set customerId= #{newCustomerId}
        WHERE customerId = #{customerId}
        <if test="managerId != null and managerId != ''">
            and managerId = #{oldManagerId}
        </if>
        and is_delete=0
    </update>

    <select id="queryCustomerLabel" resultMap="infoMap" parameterType="map">
        select * from (
        select c.labelId ,c.managerId,
        (select labelName from color_label cl where cl.id=c.labelId and cl.is_delete=0) labelName,
        (select colorId from color_label cl where cl.id=c.labelId and cl.is_delete=0) colorId
        from customer_label c
        where c.is_delete =0
        <if test="userId != null and userId != ''">
            and (c.managerId = #{userId}
            OR c.managerId IN ( SELECT id FROM user WHERE depturlpath LIKE ( SELECT concat( depturlpath, '/%' ) FROM user WHERE id = #{userId} ) ))
        </if>
        <if test="customerId != null and customerId != ''">
            and c.customerId =#{customerId}
        </if>
        ) t where t.labelName is not null and t.colorId is not null
    </select>

    <select id="getLabelColor" resultMap="infoMap" parameterType="map">
        select id,colorId,labelName,createId,createName,updateTime from color_label
        where 1=1
        <if test="createId != null and createId != ''">
            and createId =#{createId}
        </if>
        <if test="customerId != null and customerId != ''">
            and customerId =#{customerId}
        </if>
        and is_delete=0
        order by updateTime desc
    </select>

    <select id="getAllLabelsByOwn" resultMap="infoMap" parameterType="map">
        select id,colorId,labelName,createId,createName,updateTime from color_label
        where 1=1
        <if test="createId != null and createId != ''">
            and (createId = #{createId}
            OR createId IN ( SELECT id FROM user WHERE depturlpath LIKE ( SELECT concat( depturlpath, '/%' ) FROM user WHERE id = #{createId} ) ))
        </if>
        and is_delete=0
        order by updateTime desc
    </select>
</mapper>