<?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.CustomerMergeMapper">

    <resultMap id="infoMap" type="com.house365.beans.entity.CustomerEntity">
        <result property="id" column="id" />
        <result property="name" column="name" />
        <result property="phone" column="phone" />
        <result property="bakPhone1" column="bak_phone1" />
        <result property="bakPhone2" column="bak_phone2" />
        <result property="bakPhone3" column="bak_phone3" />
        <result property="sex" column="sex" />
        <result property="customerType" column="customer_type" />
        <result property="actSource" column="act_source" />
        <result property="firstSource" column="first_source" />
        <result property="latestSource" column="latest_source" />
        <result property="crmLatestCall" column="crm_latest_call" />
        <result property="crmLatestCallResult" column="crm_latest_call_result" />
        <result property="status" column="status" />
        <result property="createSource" column="create_source" />
        <result property="createTime" column="create_time" />
        <result property="creater" column="creater" />
        <result property="buyIntention" column="buy_intention" />
        <result property="lastVisitTime" column="last_visit_time" />
        <result property="lastCallTime" column="last_call_time" />
        <result property="managerId" column="manager_id" />
        <result property="managerName" column="manager_name" />
        <result property="updateTime" column="update_time" />
        <result property="updater" column="updater" />
        <result property="cityCode" column="cityCode" />
        <result property="cityName" column="cityName" />
        <result property="crmId" column="crmId" />
        <result property="bizPort" column="biz_port" />
        <result property="actSourceId" column="act_source_id" />
        <result property="traceStatus" column="trace_status" />
        <result property="remark" column="remark" />
        <result property="crmSync" column="crm_sync" />
        <result property="hotlineCallTime" column="hotline_call_time" />
        <result property="hotlineCallSpend" column="hotline_call_spend" />
        <result property="oceanDate" column="ocean_date" />
        <result property="isDelete" column="is_delete" />
        <result property="lastManager" column="last_manager" />
        <result property="bindTime" column="bind_time" />
        <result property="customerSourceType" column="customer_source_type" />
        <result property="customerRelId" column="customer_rel_id" />
        <result property="isWaitCall" column="isWaitCall" />
        <result property="singupTime" column="singupTime" />
        <result property="marketActId" column="marketActId" />
        <result property="marketActName" column="marketActName" />
        <result property="isPeer" column="is_peer" />
        <result property="isActive" column="is_active"/>
        <result property="isHandover" column="isHandover"/>
        <result property="totalNum" column="totalNum"/>
    </resultMap>

    <resultMap id="count" type="java.lang.Integer" autoMapping="true">
        <result column="count"  jdbcType="INTEGER" javaType="Integer" />
    </resultMap>

    <update id="deleteCustomerMergeById" parameterType="map">
        delete from customer_merge
        WHERE id in
        <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
            #{item,jdbcType=VARCHAR}
        </foreach>
    </update>

    <update id="deleteSingleCustomerMerge" parameterType="com.house365.beans.entity.CustomerEntity">
        delete from customer_merge
        WHERE id = #{id}
    </update>

    <update id="deleteSingleCustomerMerge2" parameterType="com.house365.beans.entity.CustomerEntity">
        delete from customer_merge
        WHERE manager_id = #{oldManagerId} and phone=#{phone}
    </update>

    <update id="deleteCustomerMergeByManager" parameterType="map">
        delete from customer_merge
        WHERE manager_id = #{managerId}
    </update>

    <select id="queryById" resultMap="infoMap" parameterType="int">
        SELECT
        id, name, phone, bak_phone1, bak_phone2, bak_phone3, sex, customer_type, act_source,
        first_source, latest_source, crm_latest_call, crm_latest_call_result, status, create_source,
        create_time, creater, buy_intention, last_visit_time, last_call_time, manager_id, manager_name,
        update_time, updater, cityCode,cityName, biz_port,crmId,act_source_id, trace_status, remark, crm_sync,
        hotline_call_time,hotline_call_spend,ocean_date,is_delete,last_manager,bind_time, customer_source_type,
        customer_rel_id, isWaitCall, singupTime,marketActId, marketActName,is_peer, starCustomer, isHandover,is_active, isRecycled
        FROM customer_merge
        WHERE is_delete = 0
        AND  id = #{id}
    </select>

    <select id="getCityPeriod" resultType="Integer" parameterType="String">
        SELECT
              period
        from city_period
        where cityCode = #{cityCode}
    </select>

    <update id="updateCustomerStar" parameterType="map">
        UPDATE customer_merge
        SET
        starCustomer = #{star}
        WHERE id = #{id}
    </update>

    <select id="getCustomerMerge" resultMap="infoMap" parameterType="map">
        Select
              id, name, phone, bak_phone1, bak_phone2, bak_phone3, sex, customer_type, act_source,
        first_source, latest_source, crm_latest_call, crm_latest_call_result, status, create_source,
        create_time, creater, buy_intention, last_visit_time, last_call_time, manager_id, manager_name,
        update_time, updater, cityCode,cityName, biz_port,crmId,act_source_id, trace_status, remark, crm_sync,
        hotline_call_time,hotline_call_spend,ocean_date,is_delete,last_manager,bind_time, customer_source_type,
         customer_rel_id, isWaitCall, singupTime,marketActId, marketActName,is_peer, starCustomer, isHandover, is_active,isRecycled
        FROM customer_merge
        where 1=1
        AND manager_id = #{managerId}
        and phone = #{phone}
    </select>

    <select id="getCustomerMergeById" resultMap="infoMap" parameterType="map">
        Select
        manager_id
        FROM customer_merge
        where 1=1
        AND id = #{oldCustomerId}
    </select>

    <select id="getMergeCustomerId" resultMap="infoMap" parameterType="map">
        Select
            id, name, phone, bak_phone1, bak_phone2, bak_phone3, sex, customer_type, act_source,
            first_source, latest_source, crm_latest_call, crm_latest_call_result, status, create_source,
            create_time, creater, buy_intention, last_visit_time, last_call_time, manager_id, manager_name,
            update_time, updater, cityCode,cityName, biz_port,crmId,act_source_id, trace_status, remark, crm_sync,
            hotline_call_time,hotline_call_spend,ocean_date,is_delete,last_manager,bind_time, customer_source_type,
            customer_rel_id, isWaitCall, singupTime,marketActId, marketActName,is_peer, starCustomer, isHandover, isRecycled,is_active
        FROM customer_merge
        where 1=1
        AND manager_id = #{managerId}
        and phone = #{phone}
        AND is_delete=0
    </select>

    <select id="queryManagerByPhone" resultMap="infoMap" parameterType="map">
        Select
        name, phone, manager_id, manager_name, create_time
        FROM customer_merge
        where 1=1
        and phone = #{phone}
        and cityCode = #{cityCode}
    </select>

    <select id="getCustomerMergeList" resultMap="infoMap" parameterType="map">
        Select
        id, name, phone, bak_phone1, bak_phone2, bak_phone3, sex, customer_type, act_source,
        first_source, latest_source, crm_latest_call, crm_latest_call_result, status, create_source,
        create_time, creater, buy_intention, last_visit_time, last_call_time, manager_id, manager_name,
        update_time, updater, cityCode,cityName, biz_port,crmId,act_source_id, trace_status, remark, crm_sync,
        hotline_call_time,hotline_call_spend,ocean_date,is_delete,last_manager,bind_time, customer_source_type,
        customer_rel_id, isWaitCall, singupTime,marketActId, marketActName,is_peer, starCustomer, isHandover,is_active, isRecycled
        FROM customer_merge
        where 1=1 and phone in
        <foreach collection="phones" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and is_delete=0
        and ( isWaitCall = 0 OR isWaitCall IS NULL )
        and cityCode= #{cityCode}
        AND (manager_id = #{managerId} or manager_id in (select id from user where depturlpath like (select concat(depturlpath,'/%') from user where id = #{managerId})))
    </select>

    <select id="getCustomerList" resultMap="infoMap" parameterType="map">
        Select
        id, name, phone, bak_phone1, bak_phone2, bak_phone3, sex, customer_type, act_source,
        first_source, latest_source, crm_latest_call, crm_latest_call_result, status, create_source,
        create_time, creater, buy_intention, last_visit_time, last_call_time, manager_id, manager_name,
        update_time, updater, cityCode,cityName, biz_port,crmId,act_source_id, trace_status, remark, crm_sync,
        hotline_call_time,hotline_call_spend,ocean_date,is_delete,last_manager,bind_time, customer_source_type,
        customer_rel_id, isWaitCall, singupTime,marketActId, marketActName,is_peer, starCustomer, isHandover,is_active, isRecycled
        FROM customer
        where 1=1
        AND manager_id = #{managerId}
        and phone = #{phone}
        and is_delete =0
    </select>

    <select id="getCustomerManagementList4Label" resultMap="infoMap" parameterType="map">
        SELECT
        c.id, c.name, c.phone,c.status, c.create_time, c.creater,c.buy_intention, c.last_call_time, c.manager_id,c.manager_name, c.update_time,c.create_source,
        c.cityCode, c.cityName, c.trace_status, c.crm_sync,c.bind_time, c.customer_source_type, c.isWaitCall,c.is_peer, c.starCustomer, c.isHandover
        FROM customer_merge c
        <if test='tabType =="9" || (callbackLog != null and callbackLog != "") || (customerSourceType2 != null and customerSourceType2 != "" )'>
            inner join (
            SELECT
            c3.manager_id,c3.phone
            FROM
            customer_callback_log c2 right join customer c3 on c2.customer_id = c3.id
            where 1=1
            and c3.cityCode=#{cityCode}
            AND c3.is_delete = 0
            AND ( c3.isWaitCall = 0 OR c3.isWaitCall IS NULL )
            AND c3.manager_id IS NOT NULL
            AND c3.STATUS IN ( 11, 0, 99 )
            <if test='tabType =="9"'>
                and c3.customer_source_type=7
            </if>
            <if test="customerSourceType2 != null and customerSourceType2 != '' ">
                and EXISTS (
                    select customer_source_type from customer c8
                    where c8.manager_id=c3.manager_id and c8.phone=c3.phone
                    and c8.customer_source_type=#{customerSourceType2}
                )
            </if>
            <if test="callbackLog != null and callbackLog != '' ">
                and c2.content like CONCAT('%', #{callbackLog},'%')
            </if>
            group by c3.manager_id,c3.phone
            ) t1 on c.manager_id=t1.manager_id and c.phone=t1.phone
        </if>
        <if test="(projectName != null and projectName != '') or (projectStatus != null and projectStatus != '')">
            LEFT JOIN (
            SELECT c1.manager_id, c1.phone FROM customer c1 inner join customer_project p1 on c1.id=p1.customer_id
            WHERE 1=1 and (c1.manager_id = #{userId} OR c1.manager_id IN ( SELECT id FROM user WHERE depturlpath LIKE ( SELECT concat( depturlpath, '/%' ) FROM user WHERE id = #{userId} ) ))
            and c1.cityCode = #{cityCode} AND c1.is_delete = 0 AND c1.STATUS IN ( 11, 0, 99 ) AND ( c1.isWaitCall = 0 OR c1.isWaitCall IS NULL )
            <if test="projectName != null and projectName != '' ">
                and p1.house_name LIKE CONCAT( '%', #{projectName}, '%' )
            </if>
            <if test="projectStatus != null and projectStatus != '' ">
                and p1.STATUS = #{projectStatus}
            </if>
            <if test="phone != null and phone != '' ">
                AND (c1.phone LIKE CONCAT('%', #{phone},'%') OR c1.bak_phone1 LIKE CONCAT('%', #{phone},'%') OR
                c1.bak_phone2 LIKE CONCAT('%', #{phone},'%') OR c1.bak_phone3 LIKE CONCAT('%', #{phone},'%') )
            </if>
            group by c1.manager_id,c1.phone
            )t2 on c.manager_id = t2.manager_id AND c.phone = t2.phone
        </if>
        <if test="labelIdList != null and labelIdList.size() > 0 ">
            inner join (select distinct customerId from customer_label cl where cl.labelId in
            <foreach item="item" index="index" collection="labelIdList" open="(" separator="," close=")">
                #{item,jdbcType=VARCHAR}
            </foreach>
            and cl.is_delete=0) m
            on c.id=m.customerId
        </if>
        <if test="sourceName != null and sourceName != '' ">
            left join (
            SELECT ct1.manager_id, ct1.phone FROM customer ct1 WHERE 1=1
            and ct1.cityCode=#{cityCode} and ct1.is_delete = 0 AND ct1.STATUS IN ( 11, 0, 99 )
            and (ct1.manager_id=#{userId} OR ct1.manager_id IN ( SELECT id FROM user WHERE depturlpath LIKE ( SELECT concat( depturlpath, '/%' ) FROM user WHERE id = #{userId} ) ))
            AND ct1.act_source LIKE concat( '%', #{sourceName}, '%' )
            group by ct1.manager_id,ct1.phone
            ) t4 on c.manager_id = t4.manager_id AND c.phone = t4.phone
        </if>
        <if test="customTimeTypeTwo != null and customTimeTypeTwo != '' ">
            left join (
            select ct2.manager_id,ct2.phone
            from customer ct2 inner join customer_status_log csl on ct2.id=csl.customer_id
            where csl.is_delete = 0 and csl.to_status_id = 1
            AND csl.create_time &gt;=#{customTimeStart}
            AND csl.create_time &lt;=#{customTimeEnd}
            group by ct2.manager_id,ct2.phone
            ) t5 on c.manager_id = t5.manager_id AND c.phone = t5.phone
        </if>
        <if test="customTimeTypeThree != null and customTimeTypeThree != '' ">
            left join (
            select ct2.manager_id,ct2.phone
            from customer ct2 inner join customer_status_log csl on ct2.id=csl.customer_id
            where csl.is_delete = 0 and csl.to_status_id = 2
            AND csl.occur_time &gt;=#{customTimeStart}
            AND csl.occur_time &lt;=#{customTimeEnd}
            group by ct2.manager_id,ct2.phone
            ) t5 on c.manager_id = t5.manager_id AND c.phone = t5.phone
        </if>
        <if test="customTimeTypeFour != null and customTimeTypeFour != '' ">
            left join (
            select ct2.manager_id,ct2.phone
            from customer ct2 inner join customer_status_log csl on ct2.id=csl.customer_id
            where csl.is_delete = 0 and csl.to_status_id = 3
            AND csl.occur_time &gt;=#{customTimeStart}
            AND csl.occur_time &lt;=#{customTimeEnd}
            group by ct2.manager_id,ct2.phone
            ) t5 on c.manager_id = t5.manager_id AND c.phone = t5.phone
        </if>
        <if test="customTimeTypeFive != null and customTimeTypeFive != '' ">
            left join (
            select ct2.manager_id,ct2.phone
            from customer ct2 inner join customer_status_log csl on ct2.id=csl.customer_id
            where csl.is_delete = 0 and csl.to_status_id = 4
            AND csl.occur_time &gt;=#{customTimeStart}
            AND csl.occur_time &lt;=#{customTimeEnd}
            group by ct2.manager_id,ct2.phone
            ) t5 on c.manager_id = t5.manager_id AND c.phone = t5.phone
        </if>
        <if test="customTimeTypeSix != null and customTimeTypeSix != '' ">
            left join (
            select ct2.manager_id,ct2.phone
            from customer ct2 inner join customer_status_log csl on ct2.id=csl.customer_id
            where csl.is_delete = 0 and csl.to_status_id = 5
            AND csl.occur_time &gt;=#{customTimeStart}
            AND csl.occur_time &lt;=#{customTimeEnd}
            group by ct2.manager_id,ct2.phone
            ) t5 on c.manager_id = t5.manager_id AND c.phone = t5.phone
        </if>
        where 1=1
        <if test='tabType =="9" || (callbackLog != null and callbackLog != "") || (customerSourceType2 != null and customerSourceType2 != "" )'>
            and t1.phone is not null
        </if>
        <if test="phone != null and phone != '' ">
            AND (c.phone LIKE CONCAT('%', #{phone},'%') OR c.bak_phone1 LIKE CONCAT('%', #{phone},'%') OR c.bak_phone2 LIKE CONCAT('%', #{phone},'%') OR c.bak_phone3 LIKE CONCAT('%', #{phone},'%') )
        </if>
        <if test="(projectName != null and projectName != '') or (projectStatus != null and projectStatus != '')">
            and t2.phone is not null
        </if>
        <if test="sourceName != null and sourceName != '' ">
            and t4.phone is not null
        </if>
        <if test="isNotBack != null and isNotBack != '' ">
            and c.last_call_time is null
        </if>
        <if test="hasBack != null and hasBack != '' ">
            and c.last_call_time is not null
        </if>
        <if test='tabType =="1"'>
            and c.last_call_time is not null
        </if>
        <if test="(customTimeTypeTwo != null and customTimeTypeTwo != '') or (customTimeTypeThree != null and customTimeTypeThree != '') or (customTimeTypeFour != null and customTimeTypeFour != '')
                    or (customTimeTypeFive != null and customTimeTypeFive != '') or (customTimeTypeSix != null and customTimeTypeSix != '')">
            and t5.phone is not null
        </if>

        <if test="cityCode != null and cityCode != '' ">
            AND c.cityCode = #{cityCode}
        </if>
        AND c.is_delete = 0
        AND c.STATUS IN ( 11, 0, 99 )
        AND ( c.isWaitCall = 0 OR c.isWaitCall IS NULL )
        <if test="name != null and name != '' ">
            AND c.NAME like CONCAT('%', #{name},'%')
        </if>
        <if test="createSourceList1 != null and createSourceList1 != '' and createSourceList2 != null and createSourceList2 != ''">
            AND (c.create_source = #{createSourceList1} or c.create_source = #{createSourceList2})
        </if>
        <if test="createSourceList3 != null and createSourceList3 != ''">
            AND c.create_source = #{createSourceList3}
        </if>
        <if test="buyIntention != null and buyIntention != '' ">
            AND c.buy_intention = #{buyIntention}
        </if>
        <if test="buyIntentionIsNull != null and buyIntentionIsNull != '' ">
            AND (c.buy_intention is null or c.buy_intention='')
        </if>
        <if test="managerName != null and managerName != '' ">
            AND c.manager_name like CONCAT('%', #{managerName},'%')
        </if>
        <if test="createTimeStart != null and createTimeStart != '' ">
            AND c.create_time &gt;=#{createTimeStart}
        </if>
        <if test="createTimeEnd != null and createTimeEnd != '' ">
            AND c.create_time &lt;=#{createTimeEnd}
        </if>
        <if test="userId != null and userId != '' ">
            AND (
            c.manager_id = #{userId}
            OR c.manager_id IN ( SELECT id FROM user WHERE depturlpath LIKE ( SELECT concat( depturlpath, '/%' ) FROM user WHERE id = #{userId} ) )
            )
        </if>
        <if test="bindTimeStart != null and bindTimeStart != '' ">
            AND c.bind_time &gt;=#{bindTimeStart}
        </if>
        <if test="bindTimeEnd != null and bindTimeEnd != '' ">
            AND c.bind_time &lt;=#{bindTimeEnd}
        </if>
        <!--<if test="customerSourceType2 != null and customerSourceType2 != '' ">-->
        <!--AND c.customer_source_type =#{customerSourceType2}-->
        <!--</if>-->

        <if test="isNotBackFour != null and isNotBackFour != '' ">
            and datediff( CURRENT_DATE, c.last_call_time ) &gt;= 3
            AND datediff( CURRENT_DATE, c.last_call_time ) &lt;= 7
        </if>
        <if test="isNotBackFive != null and isNotBackFive != '' ">
            and datediff( CURRENT_DATE, c.last_call_time ) &gt;= 7
            AND datediff( CURRENT_DATE, c.last_call_time ) &lt;= 14
        </if>
        <if test="isNotBackSix != null and isNotBackSix != '' ">
            and datediff( CURRENT_DATE, c.last_call_time ) &gt;= 14
        </if>
        <if test="isNotBackSeven != null and isNotBackSeven != '' ">
            and datediff( CURRENT_DATE, c.last_call_time ) &gt;= 30
        </if>
        <if test="isNotBackEight != null and isNotBackEight != '' ">
            and datediff( CURRENT_DATE, c.last_call_time ) &gt;= 50
        </if>
        <if test="customTimeTypeOne != null and customTimeTypeOne != '' ">
            AND c.last_call_time &gt;=#{customTimeStart}
            AND c.last_call_time &lt;=#{customTimeEnd}
        </if>

        <if test="isStar != null and isStar != '' ">
            AND c.starCustomer =1
        </if>
        <if test="isNotStar != null and isNotStar != '' ">
            AND c.starCustomer =0
        </if>

        <if test="starCustomer != null and starCustomer != '' ">
            AND c.starCustomer =#{starCustomer}
        </if>

        <if test="isHandover != null and isHandover != '' ">
            AND c.isHandover = #{isHandover}
        </if>
        <if test="isActive != null and isActive != '' ">
            AND c.is_active = 1
        </if>

        <if test="isRecycled != null and isRecycled != '' ">
            AND c.isRecycled =#{isRecycled}
        </if>
        <if test='tabType =="8"'>
            AND ( c.is_active = 1 )
        </if>
        <if test='tabType =="7"'>
            AND ( c.isHandover = 1 )
        </if>
        <if test='tabType =="4"'>
            AND ( c.trace_status = 4 )
        </if>
        <if test='tabType =="3"'>
            and (c.trace_status = 2 or c.trace_status = 3)
        </if>
        <if test='tabType =="2"'>
            and c.trace_status = 1
        </if>

        <if test='tabType =="0"'>
            AND ( SELECT count( * ) FROM customer_callback_log ccl WHERE ccl.customer_id = c.id ) = 0
            AND ( SELECT count( * ) FROM customer_status_log csl WHERE csl.customer_id = c.id ) = 0
        </if>
        <if test="orderBy != null and orderBy != '' ">
            order by ${orderBy}
        </if>

        limit #{PageNumber}, #{pageSize};
    </select>

    <select id="getCustomerManagementList4LabelCount" resultType="Integer" parameterType="map">
        SELECT
        count(c.id)
        FROM customer_merge c
        <if test='tabType =="9" || (callbackLog != null and callbackLog != "") || (customerSourceType2 != null and customerSourceType2 != "" )'>
            inner join (
            SELECT
            c3.manager_id,c3.phone
            FROM
            customer_callback_log c2 right join customer c3 on c2.customer_id = c3.id
            where 1=1
            and c3.cityCode=#{cityCode}
            AND c3.is_delete = 0
            AND ( c3.isWaitCall = 0 OR c3.isWaitCall IS NULL )
            AND c3.manager_id IS NOT NULL
            AND c3.STATUS IN ( 11, 0, 99 )
            <if test='tabType =="9"'>
                and c3.customer_source_type=7
            </if>
            <if test="customerSourceType2 != null and customerSourceType2 != '' ">
                and EXISTS (
                select customer_source_type from customer c8
                where c8.manager_id=c3.manager_id and c8.phone=c3.phone
                and c8.customer_source_type=#{customerSourceType2}
                )
            </if>
            <if test="callbackLog != null and callbackLog != '' ">
                and c2.content like CONCAT('%', #{callbackLog},'%')
            </if>
            group by c3.manager_id,c3.phone
            ) t1 on c.manager_id=t1.manager_id and c.phone=t1.phone
        </if>
        <if test="(projectName != null and projectName != '') or (projectStatus != null and projectStatus != '')">
            LEFT JOIN (
            SELECT c1.manager_id, c1.phone FROM customer c1 inner join customer_project p1 on c1.id=p1.customer_id
            WHERE 1=1 and (c1.manager_id = #{userId} OR c1.manager_id IN ( SELECT id FROM user WHERE depturlpath LIKE ( SELECT concat( depturlpath, '/%' ) FROM user WHERE id = #{userId} ) ))
            and c1.cityCode = #{cityCode} AND c1.is_delete = 0 AND c1.STATUS IN ( 11, 0, 99 ) AND ( c1.isWaitCall = 0 OR c1.isWaitCall IS NULL )
            <if test="projectName != null and projectName != '' ">
                and p1.house_name LIKE CONCAT( '%', #{projectName}, '%' )
            </if>
            <if test="projectStatus != null and projectStatus != '' ">
                and p1.STATUS = #{projectStatus}
            </if>
            <if test="phone != null and phone != '' ">
                AND (c1.phone LIKE CONCAT('%', #{phone},'%') OR c1.bak_phone1 LIKE CONCAT('%', #{phone},'%') OR
                c1.bak_phone2 LIKE CONCAT('%', #{phone},'%') OR c1.bak_phone3 LIKE CONCAT('%', #{phone},'%') )
            </if>
            group by c1.manager_id,c1.phone
            )t2 on c.manager_id = t2.manager_id AND c.phone = t2.phone
        </if>
        <if test="labelIdList != null and labelIdList.size() > 0 ">
            inner join (select distinct customerId from customer_label cl where cl.labelId in
            <foreach item="item" index="index" collection="labelIdList" open="(" separator="," close=")">
                #{item,jdbcType=VARCHAR}
            </foreach>
            and cl.is_delete=0) m
            on c.id=m.customerId
        </if>
        <if test="sourceName != null and sourceName != '' ">
            left join (
            SELECT ct1.manager_id, ct1.phone FROM customer ct1 WHERE 1=1
            and ct1.cityCode=#{cityCode} and ct1.is_delete = 0 AND ct1.STATUS IN ( 11, 0, 99 )
            and (ct1.manager_id=#{userId} OR ct1.manager_id IN ( SELECT id FROM user WHERE depturlpath LIKE ( SELECT concat( depturlpath, '/%' ) FROM user WHERE id = #{userId} ) ))
            AND ct1.act_source LIKE concat( '%', #{sourceName}, '%' )
            group by ct1.manager_id,ct1.phone
            ) t4 on c.manager_id = t4.manager_id AND c.phone = t4.phone
        </if>
        <if test="customTimeTypeTwo != null and customTimeTypeTwo != '' ">
            left join (
            select ct2.manager_id,ct2.phone
            from customer ct2 inner join customer_status_log csl on ct2.id=csl.customer_id
            where csl.is_delete = 0 and csl.to_status_id = 1
            AND csl.create_time &gt;=#{customTimeStart}
            AND csl.create_time &lt;=#{customTimeEnd}
            group by ct2.manager_id,ct2.phone
            ) t5 on c.manager_id = t5.manager_id AND c.phone = t5.phone
        </if>
        <if test="customTimeTypeThree != null and customTimeTypeThree != '' ">
            left join (
            select ct2.manager_id,ct2.phone
            from customer ct2 inner join customer_status_log csl on ct2.id=csl.customer_id
            where csl.is_delete = 0 and csl.to_status_id = 2
            AND csl.occur_time &gt;=#{customTimeStart}
            AND csl.occur_time &lt;=#{customTimeEnd}
            group by ct2.manager_id,ct2.phone
            ) t5 on c.manager_id = t5.manager_id AND c.phone = t5.phone
        </if>
        <if test="customTimeTypeFour != null and customTimeTypeFour != '' ">
            left join (
            select ct2.manager_id,ct2.phone
            from customer ct2 inner join customer_status_log csl on ct2.id=csl.customer_id
            where csl.is_delete = 0 and csl.to_status_id = 3
            AND csl.occur_time &gt;=#{customTimeStart}
            AND csl.occur_time &lt;=#{customTimeEnd}
            group by ct2.manager_id,ct2.phone
            ) t5 on c.manager_id = t5.manager_id AND c.phone = t5.phone
        </if>
        <if test="customTimeTypeFive != null and customTimeTypeFive != '' ">
            left join (
            select ct2.manager_id,ct2.phone
            from customer ct2 inner join customer_status_log csl on ct2.id=csl.customer_id
            where csl.is_delete = 0 and csl.to_status_id = 4
            AND csl.occur_time &gt;=#{customTimeStart}
            AND csl.occur_time &lt;=#{customTimeEnd}
            group by ct2.manager_id,ct2.phone
            ) t5 on c.manager_id = t5.manager_id AND c.phone = t5.phone
        </if>
        <if test="customTimeTypeSix != null and customTimeTypeSix != '' ">
            left join (
            select ct2.manager_id,ct2.phone
            from customer ct2 inner join customer_status_log csl on ct2.id=csl.customer_id
            where csl.is_delete = 0 and csl.to_status_id = 5
            AND csl.occur_time &gt;=#{customTimeStart}
            AND csl.occur_time &lt;=#{customTimeEnd}
            group by ct2.manager_id,ct2.phone
            ) t5 on c.manager_id = t5.manager_id AND c.phone = t5.phone
        </if>
        where 1=1
        <if test='tabType =="9" || (callbackLog != null and callbackLog != "") || (customerSourceType2 != null and customerSourceType2 != "" )'>
            and t1.phone is not null
        </if>
        <if test="phone != null and phone != '' ">
            AND (c.phone LIKE CONCAT('%', #{phone},'%') OR c.bak_phone1 LIKE CONCAT('%', #{phone},'%') OR c.bak_phone2 LIKE CONCAT('%', #{phone},'%') OR c.bak_phone3 LIKE CONCAT('%', #{phone},'%') )
        </if>
        <if test="(projectName != null and projectName != '') or (projectStatus != null and projectStatus != '')">
            and t2.phone is not null
        </if>
        <if test="sourceName != null and sourceName != '' ">
            and t4.phone is not null
        </if>
        <if test="isNotBack != null and isNotBack != '' ">
            and c.last_call_time is null
        </if>
        <if test="hasBack != null and hasBack != '' ">
            and c.last_call_time is not null
        </if>
        <if test='tabType =="1"'>
            and c.last_call_time is not null
        </if>
        <if test="(customTimeTypeTwo != null and customTimeTypeTwo != '') or (customTimeTypeThree != null and customTimeTypeThree != '') or (customTimeTypeFour != null and customTimeTypeFour != '')
                    or (customTimeTypeFive != null and customTimeTypeFive != '') or (customTimeTypeSix != null and customTimeTypeSix != '')">
            and t5.phone is not null
        </if>
        <if test="cityCode != null and cityCode != '' ">
            AND c.cityCode = #{cityCode}
        </if>
        AND c.is_delete = 0
        AND c.STATUS IN ( 11, 0, 99 )
        AND ( c.isWaitCall = 0 OR c.isWaitCall IS NULL )
        <if test="name != null and name != '' ">
            AND c.NAME like CONCAT('%', #{name},'%')
        </if>
        <if test="createSourceList1 != null and createSourceList1 != '' and createSourceList2 != null and createSourceList2 != ''">
            AND (c.create_source = #{createSourceList1} or c.create_source = #{createSourceList2})
        </if>
        <if test="createSourceList3 != null and createSourceList3 != ''">
            AND c.create_source = #{createSourceList3}
        </if>
        <if test="buyIntention != null and buyIntention != '' ">
            AND c.buy_intention = #{buyIntention}
        </if>
        <if test="buyIntentionIsNull != null and buyIntentionIsNull != '' ">
            AND (c.buy_intention is null or c.buy_intention='')
        </if>
        <if test="managerName != null and managerName != '' ">
            AND c.manager_name like CONCAT('%', #{managerName},'%')
        </if>
        <if test="createTimeStart != null and createTimeStart != '' ">
            AND c.create_time &gt;=#{createTimeStart}
        </if>
        <if test="createTimeEnd != null and createTimeEnd != '' ">
            AND c.create_time &lt;=#{createTimeEnd}
        </if>
        <if test="userId != null and userId != '' ">
            AND (
            c.manager_id = #{userId}
            OR c.manager_id IN ( SELECT id FROM user WHERE depturlpath LIKE ( SELECT concat( depturlpath, '/%' ) FROM user WHERE id = #{userId} ) )
            )
        </if>
        <if test="bindTimeStart != null and bindTimeStart != '' ">
            AND c.bind_time &gt;=#{bindTimeStart}
        </if>
        <if test="bindTimeEnd != null and bindTimeEnd != '' ">
            AND c.bind_time &lt;=#{bindTimeEnd}
        </if>
        <if test="isNotBackFour != null and isNotBackFour != '' ">
            and datediff( CURRENT_DATE, c.last_call_time ) &gt;= 3
            AND datediff( CURRENT_DATE, c.last_call_time ) &lt;= 7
        </if>
        <if test="isNotBackFive != null and isNotBackFive != '' ">
            and datediff( CURRENT_DATE, c.last_call_time ) &gt;= 7
            AND datediff( CURRENT_DATE, c.last_call_time ) &lt;= 14
        </if>
        <if test="isNotBackSix != null and isNotBackSix != '' ">
            and datediff( CURRENT_DATE, c.last_call_time ) &gt;= 14
        </if>
        <if test="isNotBackSeven != null and isNotBackSeven != '' ">
            and datediff( CURRENT_DATE, c.last_call_time ) &gt;= 30
        </if>
        <if test="isNotBackEight != null and isNotBackEight != '' ">
            and datediff( CURRENT_DATE, c.last_call_time ) &gt;= 50
        </if>
        <if test="customTimeTypeOne != null and customTimeTypeOne != '' ">
            AND c.last_call_time &gt;=#{customTimeStart}
            AND c.last_call_time &lt;=#{customTimeEnd}
        </if>
        <if test="isStar != null and isStar != '' ">
            AND c.starCustomer =1
        </if>
        <if test="isNotStar != null and isNotStar != '' ">
            AND c.starCustomer =0
        </if>
        <if test="starCustomer != null and starCustomer != '' ">
            AND c.starCustomer =#{starCustomer}
        </if>

        <if test="isHandover != null and isHandover != '' ">
            AND c.isHandover = #{isHandover}
        </if>
        <if test="isActive != null and isActive != '' ">
            AND c.is_active = 1
        </if>
        <if test="isRecycled != null and isRecycled != '' ">
            AND c.isRecycled =#{isRecycled}
        </if>
        <if test='tabType =="8"'>
            AND ( c.is_active = 1 )
        </if>
        <if test='tabType =="7"'>
            AND ( c.isHandover = 1 )
        </if>
        <if test='tabType =="4"'>
            AND ( c.trace_status = 4 )
        </if>
        <if test='tabType =="3"'>
            and (c.trace_status = 2 or c.trace_status = 3)
        </if>
        <if test='tabType =="2"'>
            and c.trace_status = 1
        </if>
        <if test='tabType =="0"'>
            AND ( SELECT count( * ) FROM customer_callback_log ccl WHERE ccl.customer_id = c.id ) = 0
            AND ( SELECT count( * ) FROM customer_status_log csl WHERE csl.customer_id = c.id ) = 0
        </if>
    </select>

    <insert id="saveCustomerMerge" parameterType="com.house365.beans.entity.CustomerEntity">
        insert into   customer_merge
        (id,name,phone,bak_phone1,bak_phone2,
        bak_phone3,sex,customer_type,act_source,first_source,latest_source,crm_latest_call,crm_latest_call_result,status,create_source,
        create_time,creater,buy_intention,last_visit_time,last_call_time,manager_id,manager_name,update_time,updater,cityCode,cityName,biz_port,crmId,act_source_id,trace_status,remark,crm_sync,hotline_call_time,
        hotline_call_spend,ocean_date,
        last_manager,is_delete,bind_time,
        customer_source_type,customer_rel_id,isWaitCall,
        singupTime,marketActId, marketActName, newCycle)
        values
        (
        #{id},#{name},#{phone},#{bakPhone1},#{bakPhone2},
        #{bakPhone3},#{sex},#{customerType},#{actSource},#{firstSource},#{latestSource},#{crmLatestCall},#{crmLatestCallResult},#{status},#{createSource},
        #{createTime},#{creater},#{buyIntention},#{lastVisitTime},#{lastCallTime},#{managerId},#{managerName},#{updateTime},#{updater},#{cityCode},#{cityName},#{bizPort},#{crmId},#{actSourceId},#{traceStatus},#{remark},#{crmSync},#{hotlineCallTime},
        #{hotlineCallSpend},#{oceanDate},
        #{lastManager},#{isDelete},#{bindTime},
        #{customerSourceType},#{customerRelId},#{isWaitCall},
        #{singupTime},#{marketActId}, #{marketActName}, #{newCycle}
        )
    </insert>

    <update id="updateCustomerMerge" parameterType="com.house365.beans.entity.CustomerEntity">
        UPDATE customer_merge
        SET
        trace_status = #{traceStatus},
        buy_intention = #{buyIntention},
        create_time = #{createTime},
        update_time = #{updateTime},
        bind_time = #{bindTime},
        manager_id = #{managerId},
        manager_name = #{managerName},
        updater = #{updater},
        isHandover = #{isHandover},
        <if test="isActive != null and isActive >= 0 ">
            is_active = #{isActive},
        </if>
        last_call_time = #{lastCallTime},
        status = #{status}
        WHERE id = #{id}
    </update>


    <update id="updateCustomerMerge2" parameterType="com.house365.beans.entity.CustomerEntity">
        UPDATE customer_merge
        SET
        trace_status = #{traceStatus},
        buy_intention = #{buyIntention},
        create_time = #{createTime},
        update_time = #{updateTime},
        bind_time = #{bindTime},
        manager_id = #{managerId},
        manager_name = #{managerName},
        updater = #{updater},
        isHandover = #{isHandover},
        <if test="isActive != null and isActive >= 0 ">
            is_active = #{isActive},
        </if>
        last_call_time = #{lastCallTime},
        status = #{status}
        WHERE manager_id = #{oldManagerId} and phone=#{phone}
    </update>

    <!-- 添加回访更新客户信息 -->
    <update id="updateCustomerMerge4AddCall" parameterType="com.house365.beans.entity.CustomerEntity">
        UPDATE customer_merge
        SET
        trace_status = #{traceStatus},
        buy_intention = #{buyIntention},
        update_time = #{updateTime},
        manager_id = #{managerId},
        manager_name = #{managerName},
        updater = #{updater},
        isHandover = #{isHandover},
        last_call_time = #{lastCallTime},
        status = #{status}
        WHERE id = #{id}
    </update>

    <update id="update" parameterType="Map">
        UPDATE customer_merge
        SET
        <if test="name != null and name != ''">
            name = #{name},
        </if>
        <if test="sex != null and sex >= 0">
            sex = #{sex},
        </if>
        <if test="managerId != null and managerId >= 0">
            manager_id = #{managerId},
        </if>
        <if test="phone != null and phone != ''">
            phone = #{phone},
        </if>
        <if test="bakPhone1 != null and bakPhone1 != ''">
            bak_phone1 = #{bakPhone1},
        </if>
        <if test="traceStatus != null and traceStatus >= 0">
            trace_status = #{traceStatus},
        </if>
        <if test="buyIntention != null and buyIntention != ''">
            buy_intention = #{buyIntention},
        </if>
        <if test="isHandover != null and isHandover >= 0 ">
            isHandover = #{isHandover},
        </if>
        <if test="isActive != null and isActive >= 0 ">
            is_active = #{isActive},
        </if>
        <if test="activeTime != null and activeTime != ''">
            active_time = #{activeTime},
        </if>
        <if test="lastCallTime != null and lastCallTime != ''">
            last_call_time = #{lastCallTime},
        </if>
        update_time = #{updateTime},
        updater = #{updater}
        WHERE id = #{id}
    </update>

    <update id="clearCustomerMerge" parameterType="java.util.HashMap">
        DELETE 	from customer_merge
        where 1=1
        <if test="ids != null and ids.size() > 0 ">
            and id in
            <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </update>

    <update id="clearCustomerMerge2" parameterType="com.house365.beans.entity.CustomerEntity">
        DELETE 	from customer_merge
        where 1=1
        and id =#{id}
    </update>

    <select id="queryByConditions" resultMap="infoMap" parameterType="java.util.HashMap">
        SELECT
        id, name, phone, bak_phone1, bak_phone2, bak_phone3, sex, customer_type, act_source,
        first_source, latest_source, crm_latest_call, crm_latest_call_result, status, create_source,
        create_time, creater, buy_intention, last_visit_time, last_call_time, manager_id, manager_name,
        update_time, updater, cityCode,cityName, biz_port,crmId,act_source_id, trace_status, remark, crm_sync,
        hotline_call_time,hotline_call_spend,ocean_date,is_delete,last_manager,bind_time, customer_source_type,
        customer_rel_id, isWaitCall, singupTime,marketActId, marketActName,is_peer, starCustomer, isHandover, isRecycled,is_active
        FROM customer_merge
        where is_delete = 0
        <if test="phone != null and phone >=0">
            AND phone = #{phone}
        </if>
        <if test="managerId != null and managerId >=0">
            AND manager_id = #{managerId}
        </if>
        <if test="cityCode != null and cityCode != ''">
            AND cityCode = #{cityCode}
        </if>
        <if test="isPrivate != null and isPrivate >=0">
            AND (isWaitCall = 0 OR isWaitCall IS NULL)
            AND (STATUS = 0 OR STATUS = 11 OR status = 99)
        </if>
        <if test="createTime != null and createTime != ''">
            AND create_time > #{createTime}
        </if>
        <if test="customerSourceType != null and customerSourceType >=0">
            AND customer_source_type = #{customerSourceType}
        </if>
        <if test="createIdList != null and createIdList.size() > 0 ">
            AND  creater IN
            <foreach collection="createIdList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="limit != null and limit > 0 ">
            limit 10
        </if>
    </select>

    <update id="updatePeer" parameterType="map">
        UPDATE customer_merge
        SET
        is_peer = #{isPeer},
        update_time = #{updateTime}
        WHERE id = #{id}
    </update>

    <update id="updateActiveStatus" parameterType="map">
        UPDATE customer_merge
        SET is_active = 0
        WHERE is_active = 1
        AND cityCode = #{city}
        AND DATE_SUB(CURDATE(), INTERVAL #{hour} HOUR) >= DATE(active_time)
        AND active_time != '2014-01-01 00:00:00'
    </update>

    <select id="queryForAddHot" resultMap="infoMap" parameterType="java.util.HashMap">
        SELECT id,phone FROM (
        SELECT
        id,phone
        FROM customer_merge
        where is_delete = 0
        <if test="status != null and status >=0">
            AND status = #{status}
        </if>
        <if test="createSource != null and createSource != '' ">
            AND create_source = #{createSource}
        </if>
        <if test="isProtect != null and isProtect >=0">
            AND DATE(create_time) >= DATE_SUB(CURDATE(), INTERVAL #{isProtect} DAY)
        </if>
        <if test="city != null and city != '' ">
            AND cityCode = #{city}
        </if>
        <if test="isAdd != null and isAdd >=0">
            UNION
            SELECT id,phone FROM customer_merge
            WHERE
            STATUS = 11 AND is_delete = 0
            AND ( create_source = 'SYSTEM' OR create_source = 'WECHAT')
            AND DATE(create_time) >= DATE_SUB(CURDATE(), INTERVAL #{isProtect} DAY)
        </if>
        ) AS  temp
        GROUP BY phone
    </select>

    <select id="queryCount" resultType="Integer" parameterType="java.util.HashMap">
        SELECT
        count(*)
        FROM customer_merge
        where is_delete = 0
        <if test="phone != null and phone >=0">
            AND phone = #{phone}
        </if>
        <if test="managerId != null and managerId >=0">
            AND manager_id = #{managerId}
        </if>
        <if test="cityCode != null and cityCode != ''">
            AND cityCode = #{cityCode}
        </if>
    </select>


    <select id="countCustomer" resultType="Integer" parameterType="map">
        select count(cm.id) from customer_merge cm
        <if test="projectStatus != null and projectStatus !=''">
            LEFT JOIN (
            SELECT c1.manager_id, c1.phone FROM customer c1 inner join customer_project p1 on c1.id=p1.customer_id
            WHERE 1=1 and (c1.manager_id = #{userId} OR c1.manager_id IN ( SELECT id FROM user WHERE depturlpath LIKE ( SELECT concat( depturlpath, '/%' ) FROM user WHERE id = #{userId} ) ))
            and c1.cityCode = #{cityCode} AND c1.is_delete = 0 AND c1.STATUS IN ( 11, 0, 99 ) AND ( c1.isWaitCall = 0 OR c1.isWaitCall IS NULL )

            <if test="projectStatus != null and projectStatus != '' ">
                and p1.STATUS = #{projectStatus}
            </if>
            group by c1.manager_id,c1.phone
            )t2 on cm.manager_id = t2.manager_id AND cm.phone = t2.phone
        </if>

        where cm.manager_Id = #{managerId}
        <if test="buyIntention != null and buyIntention !='' and buyIntention !='23'">
            and cm.buy_intention=#{buyIntention}
        </if>
        <if test="buyIntention =='23'">
            and (cm.buy_intention IS NULL OR cm.buy_intention = '')
        </if>
        <if test="isStar != null and isStar !=''">
            and cm.starCustomer=#{isStar}
        </if>

        AND cm.cityCode =#{cityCode}
        and cm.is_delete = 0
        AND cm.STATUS IN ( 11, 0, 99 )
        AND ( cm.isWaitCall = 0 OR cm.isWaitCall IS NULL )
        <if test="projectStatus != null and projectStatus !=''">
            and t2.phone is not null
        </if>

    </select>

    <select id="countCustomer2" resultType="Integer" parameterType="map">
        select count(*) from (
        select count(cm.id),count(cm.manager_id),cm.phone
        from customer cm,customer_project cp
        where cm.id=cp.customer_id
        and cm.is_delete=0
        and cm.status in (0,11,99)
        and (cm.isWaitCall is null or cm.isWaitCall=0)
        and cm.manager_Id = #{managerId}
        <if test="buyIntention != null and buyIntention !='' and buyIntention !='23'">
            and cm.buy_intention=#{buyIntention}
        </if>
        <if test="buyIntention =='23'">
            and (cm.buy_intention IS NULL OR cm.buy_intention = '')
        </if>
        <if test="isStar != null and isStar !=''">
            and cm.starCustomer=#{isStar}
        </if>
        <if test="projectStatus != null and projectStatus !=''">
            and cp.status=#{projectStatus}
        </if>
        group by cm.phone
        ) t

    </select>

    <select id="countCustomer3" resultType="Integer" parameterType="map">
        select
        count(*) from (
        select count(cm.id),count(cm.manager_id),cm.phone from customer cm,customer_project cp,customer_callback_log ccl
        where cm.id=cp.customer_id
        and cm.manager_Id = #{managerId}
        and cm.is_delete=0
        and cm.status in (0,11,99)
        and (cm.isWaitCall is null or cm.isWaitCall=0)
        and cm.id=ccl.customer_id
        <if test="buyIntention != null and buyIntention !='' and buyIntention !='23'">
            and cm.buy_intention=#{buyIntention}
        </if>
        <if test="buyIntention =='23'">
            and (cm.buy_intention IS NULL OR cm.buy_intention = '')
        </if>
        <if test="isStar != null and isStar !=''">
            and cm.starCustomer=#{isStar}
        </if>
        <if test="projectStatus != null and projectStatus !=''">
            and cp.status=#{projectStatus}
        </if>
        <if test="callbackLog != null and callbackLog !=''">
            and ccl.content like concat('%', #{callbackLog}, '%')
        </if>
        group by cm.phone
        ) t

    </select>

    <select id="countCustomer4" resultType="Integer" parameterType="map">
        select count(cm.id)
        from customer_merge cm left JOIN (
        SELECT
        c3.manager_id,
        c3.phone
        FROM
        customer_callback_log c2
        right JOIN customer c3 ON c2.customer_id = c3.id
        WHERE
        1 = 1
        AND c3.cityCode =#{cityCode}
        AND c3.is_delete = 0
        AND ( c3.isWaitCall = 0 OR c3.isWaitCall IS NULL )
        AND c3.manager_id IS NOT NULL
        AND c3.STATUS IN ( 11, 0, 99 )
        AND c2.content LIKE CONCAT( '%', #{callbackLog}, '%' )
        GROUP BY
        c3.manager_id,
        c3.phone
        ) t1 ON cm.manager_id = t1.manager_id
        AND cm.phone = t1.phone
        <if test="projectStatus != null and projectStatus != '' ">
            LEFT JOIN (
            SELECT c1.manager_id, c1.phone FROM customer c1 inner join customer_project p1 on c1.id=p1.customer_id
            WHERE 1=1 and (c1.manager_id = #{userId} OR c1.manager_id IN ( SELECT id FROM user WHERE depturlpath LIKE ( SELECT concat( depturlpath, '/%' ) FROM user WHERE id = #{userId} ) ))
            and c1.cityCode = #{cityCode} AND c1.is_delete = 0 AND c1.STATUS IN ( 11, 0, 99 ) AND ( c1.isWaitCall = 0 OR c1.isWaitCall IS NULL )
            and p1.STATUS = #{projectStatus}
            <if test="phone != null and phone != '' ">
                AND (c1.phone LIKE CONCAT('%', #{phone},'%') OR c1.bak_phone1 LIKE CONCAT('%', #{phone},'%') OR
                c1.bak_phone2 LIKE CONCAT('%', #{phone},'%') OR c1.bak_phone3 LIKE CONCAT('%', #{phone},'%') )
            </if>
            GROUP BY c1.manager_id,c1.phone
            )t3 on cm.manager_id = t3.manager_id AND cm.phone = t3.phone
        </if>


        where 1=1
        and t1.phone is not null
        <if test="projectStatus != null and projectStatus != '' ">
            and t3.phone is not null
        </if>
        AND cm.cityCode =#{cityCode}
        and cm.is_delete=0
        and cm.status in (0,11,99)
        and (cm.isWaitCall is null or cm.isWaitCall=0)
        and cm.manager_Id = #{managerId}
        <if test="buyIntention != null and buyIntention !='' and buyIntention !='23'">
            and cm.buy_intention=#{buyIntention}
        </if>
        <if test="buyIntention =='23'">
            and (cm.buy_intention IS NULL OR cm.buy_intention = '')
        </if>
        <if test="isStar != null and isStar !=''">
            and cm.starCustomer=#{isStar}
        </if>
        <if test="projectStatus != null and projectStatus !=''">
            AND cm.id in ( SELECT customer_id FROM customer_project cp WHERE cp.STATUS = #{projectStatus} )
        </if>
    </select>

    <select id="getAllCustomer" resultType="com.house365.beans.entity.CustomerEntity" parameterType="map">
        select cm.id,cm.manager_id,cm.phone
        from customer_merge cm
        <if test="projectStatus != null and projectStatus !=''">
            LEFT JOIN (
            SELECT c1.manager_id, c1.phone FROM customer c1 inner join customer_project p1 on c1.id=p1.customer_id
            WHERE 1=1 and (c1.manager_id = #{userId} OR c1.manager_id IN ( SELECT id FROM user WHERE depturlpath LIKE ( SELECT concat( depturlpath, '/%' ) FROM user WHERE id = #{userId} ) ))
            and c1.cityCode = #{cityCode} AND c1.is_delete = 0 AND c1.STATUS IN ( 11, 0, 99 ) AND ( c1.isWaitCall = 0 OR c1.isWaitCall IS NULL )

            <if test="projectStatus != null and projectStatus != '' ">
                and p1.STATUS = #{projectStatus}
            </if>
            group by c1.manager_id,c1.phone
            )t2 on cm.manager_id = t2.manager_id AND cm.phone = t2.phone
        </if>
        where cm.manager_Id = #{managerId}
        <if test="buyIntention != null and buyIntention !='' and buyIntention !='23'">
            and cm.buy_intention=#{buyIntention}
        </if>
        <if test="buyIntention =='23'">
            and (cm.buy_intention IS NULL OR cm.buy_intention = '')
        </if>
        <if test="isStar != null and isStar !=''">
            and cm.starCustomer=#{isStar}
        </if>
        and cm.cityCode =#{cityCode}
        and cm.is_delete = 0
        and cm.STATUS IN ( 11, 0, 99 )
        and ( cm.isWaitCall = 0 OR cm.isWaitCall IS NULL )
        <if test="projectStatus != null and projectStatus !=''">
            and t2.phone is not null
        </if>
    </select>

    <select id="getAllCustomer2" resultType="com.house365.beans.entity.CustomerEntity" parameterType="map">
        select t.id,t.manager_id,t.phone from (
        select cm.id,cm.manager_id,cm.phone
        from customer cm,customer_project cp
        where cm.id=cp.customer_id
        and cm.is_delete=0
        and cm.status in (0,11,99)
        and (cm.isWaitCall is null or cm.isWaitCall=0)
        and cm.manager_Id = #{managerId}
        <if test="buyIntention != null and buyIntention !='' and buyIntention !='23'">
            and cm.buy_intention=#{buyIntention}
        </if>
        <if test="buyIntention =='23'">
            and (cm.buy_intention IS NULL OR cm.buy_intention = '')
        </if>
        <if test="isStar != null and isStar !=''">
            and cm.starCustomer=#{isStar}
        </if>
        <if test="projectStatus != null and projectStatus !=''">
            and cp.status=#{projectStatus}
        </if>
        ) t
        group by t.manager_Id,t.phone
    </select>

    <select id="getAllCustomer3" resultType="com.house365.beans.entity.CustomerEntity" parameterType="map">
        select
        t.id,t.manager_id,t.phone from (
        select cm.id,cm.manager_Id,cm.phone from customer cm,customer_project cp,customer_callback_log ccl
        where cm.id=cp.customer_id
        and cm.manager_Id = #{managerId}
        and cm.is_delete=0
        and cm.status in (0,11,99)
        and (cm.isWaitCall is null or cm.isWaitCall=0)
        and cm.id=ccl.customer_id
        <if test="buyIntention != null and buyIntention !='' and buyIntention !='23'">
            and cm.buy_intention=#{buyIntention}
        </if>
        <if test="buyIntention =='23'">
            and (cm.buy_intention IS NULL OR cm.buy_intention = '')
        </if>
        <if test="isStar != null and isStar !=''">
            and cm.starCustomer=#{isStar}
        </if>
        <if test="projectStatus != null and projectStatus !=''">
            and cp.status=#{projectStatus}
        </if>
        <if test="callbackLog != null and callbackLog !=''">
            and ccl.content like concat('%', #{callbackLog}, '%')
        </if>
        ) t
        group by t.manager_Id,t.phone
    </select>

    <select id="getAllCustomer4" resultType="com.house365.beans.entity.CustomerEntity" parameterType="map">
        select cm.id,cm.manager_Id,cm.phone
        from customer_merge cm left JOIN (
        SELECT
        c3.manager_id,
        c3.phone
        FROM
        customer_callback_log c2
        right JOIN customer c3 ON c2.customer_id = c3.id
        WHERE
        1 = 1
        AND c3.cityCode =#{cityCode}
        AND c3.is_delete = 0
        AND ( c3.isWaitCall = 0 OR c3.isWaitCall IS NULL )
        AND c3.manager_id IS NOT NULL
        AND c3.STATUS IN ( 11, 0, 99 )
        AND c2.content LIKE CONCAT( '%', #{callbackLog}, '%' )
        GROUP BY
        c3.manager_id,
        c3.phone
        ) t1 ON cm.manager_id = t1.manager_id AND cm.phone = t1.phone
        <if test="projectStatus != null and projectStatus != '' ">
            LEFT JOIN (
            SELECT c1.manager_id, c1.phone FROM customer c1 inner join customer_project p1 on c1.id=p1.customer_id
            WHERE 1=1 and (c1.manager_id = #{userId} OR c1.manager_id IN ( SELECT id FROM user WHERE depturlpath LIKE ( SELECT concat( depturlpath, '/%' ) FROM user WHERE id = #{userId} ) ))
            and c1.cityCode = #{cityCode} AND c1.is_delete = 0 AND c1.STATUS IN ( 11, 0, 99 ) AND ( c1.isWaitCall = 0 OR c1.isWaitCall IS NULL )
            and p1.STATUS = #{projectStatus}
            <if test="phone != null and phone != '' ">
                AND (c1.phone LIKE CONCAT('%', #{phone},'%') OR c1.bak_phone1 LIKE CONCAT('%', #{phone},'%') OR
                c1.bak_phone2 LIKE CONCAT('%', #{phone},'%') OR c1.bak_phone3 LIKE CONCAT('%', #{phone},'%') )
            </if>
            GROUP BY c1.manager_id,c1.phone
            )t3 on cm.manager_id = t3.manager_id AND cm.phone = t3.phone
        </if>
        where 1=1
        and t1.phone is not null
        <if test="projectStatus != null and projectStatus != '' ">
            and t3.phone is not null
        </if>
        and cm.cityCode =#{cityCode}
        and cm.is_delete=0
        and cm.status in (0,11,99)
        and (cm.isWaitCall is null or cm.isWaitCall=0)
        and cm.manager_Id = #{managerId}
        <if test="buyIntention != null and buyIntention !='' and buyIntention !='23'">
            and cm.buy_intention=#{buyIntention}
        </if>
        <if test="buyIntention =='23'">
            and (cm.buy_intention IS NULL OR cm.buy_intention = '')
        </if>
        <if test="isStar != null and isStar !=''">
            and cm.starCustomer=#{isStar}
        </if>
        <if test="projectStatus != null and projectStatus !=''">
            AND cm.id in ( SELECT customer_id FROM customer_project cp WHERE cp.STATUS = #{projectStatus} )
        </if>
    </select>

</mapper>
