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

    <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"/>
    </resultMap>

    <sql id="sqlId">
        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
    </sql>

    <select id="queryById" resultMap="infoMap" parameterType="int">
        SELECT
         *
        FROM customer
        WHERE
        id = #{id}
    </select>

    <select id="queryCustomerPeriod" resultMap="infoMap"  parameterType="map">
        SELECT
        *
        FROM customer
        WHERE
        phone = #{phone}
        and cityCode = #{cityCode}
        and is_delete=0
        and customer_source_type=6
        and newCycle = 1
        and manager_id is not null
        order by create_time
    </select>

    <select id="queryCount" resultType="java.util.HashMap" parameterType="map">
        SELECT
        cityCode,
        phone,
        manager_id as managerId,
        manager_name as managerName,
        COUNT(*) as count
        FROM
        customer
        WHERE
        manager_id IS NOT NULL
        AND is_delete = 0
        GROUP BY manager_id
    </select>

    <select id="queryByManagerId" resultType="java.util.HashMap" parameterType="map">
        SELECT
        phone,
        manager_id,
        manager_name
        FROM
        customer
        WHERE
        manager_id IS NOT NULL
        AND is_delete = 0
        AND (isWaitCall = 0 OR isWaitCall is null)
        AND (status = 0 OR status = 11)
        AND (bind_time BETWEEN #{startTime} AND #{endTime})
        <if test="managerId != null and managerId != ''">
            AND manager_id = #{managerId}
        </if>
        <if test="idList != null and idList.size() > 0 ">
            AND manager_id in
            <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="type != null and type != '' ">
            AND buy_intention IS NOT NULL
            AND (buy_intention = '1' OR buy_intention = '2')
        </if>
    </select>

    <select id="queryCustomerLog" resultType="java.util.HashMap" parameterType="map">
        SELECT
        c.phone AS phone,
        c.manager_id AS managerId,
        c.id AS customerId
        FROM customer_callback_log cl
        LEFT JOIN customer c ON c.id = cl.customer_id
        WHERE c.manager_id IS NOT NULL
        AND c.is_delete = 0
        AND (c.isWaitCall = 0 OR c.isWaitCall is null)
        AND (c.status = 0 OR c.status = 11)
        <if test="managerId != null and managerId != ''">
            AND cl.manager_id = #{managerId}
        </if>
        <if test="idList != null and idList.size() > 0 ">
            AND cl.manager_id in
            <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        AND (cl.create_time BETWEEN #{startTime} AND #{endTime})
    </select>

    <select id="queryCustomerLogCount" resultType="Integer" parameterType="map">
        SELECT
        COUNT(DISTINCT(c.phone))
        FROM customer_callback_log cl
        LEFT JOIN customer c ON c.id = cl.customer_id
        WHERE c.manager_id IS NOT NULL
        AND c.is_delete = 0
        AND (c.isWaitCall = 0 OR c.isWaitCall is null)
        AND (c.status = 0 OR c.status = 11)
        <if test="managerId != null and managerId != ''">
            AND cl.manager_id = #{managerId}
        </if>
        <if test="idList != null and idList.size() > 0 ">
            AND cl.manager_id in
            <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        AND (cl.create_time BETWEEN #{startTime} AND #{endTime})
    </select>

    <select id="queryManagerByPhone" resultMap="infoMap" parameterType="map">
        Select
        name, phone, manager_id, manager_name, create_time
        FROM customer
        where 1=1
        and phone = #{phone}
        and cityCode = #{cityCode}
        and customer_source_type=#{customerSourceType}
        and is_delete=0
        and newCycle = 1
        and manager_id is not null
        order by create_time
    </select>

    <select id="queryCustomerByStatus" resultType="java.util.HashMap" parameterType="map">
        SELECT
        cl.customer_id,
        cl.customerManagerId,
        cl.from_status_id,
        c.phone
        FROM customer_status_log cl
        INNER JOIN customer c ON c.id = cl.customer_id
        WHERE customerManagerId IS NOT NULL
        AND c.is_delete = 0
        AND (c.isWaitCall = 0 OR c.isWaitCall is null)
        AND (c.status = 0 OR c.status = 11)
        AND cl.is_delete = 0
        AND cl.to_status_id = #{status}
        <if test="managerId != null and managerId != ''">
            AND cl.customerManagerId = #{managerId}
        </if>
        <if test="idList != null and idList.size() > 0 ">
            AND cl.customerManagerId in
            <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="status != null and status == 1">
            AND (cl.create_time BETWEEN #{startTime} AND #{endTime})
        </if>
        <if test="status != null and status > 1">
            AND (cl.occur_time BETWEEN #{startTime} AND #{endTime})
        </if>
    </select>

    <select id="queryCustomerByStatusCount" resultType="Integer" parameterType="map">
        SELECT
        COUNT(DISTINCT(c.phone))
        FROM customer_status_log cl
        INNER JOIN customer c ON c.id = cl.customer_id
        WHERE customerManagerId IS NOT NULL
        AND c.is_delete = 0
        AND (c.isWaitCall = 0 OR c.isWaitCall is null)
        AND (c.status = 0 OR c.status = 11)
        AND cl.is_delete = 0
        AND cl.to_status_id = #{status}
        <if test="managerId != null and managerId != ''">
            AND cl.customerManagerId = #{managerId}
        </if>
        <if test="idList != null and idList.size() > 0 ">
            AND cl.customerManagerId in
            <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="status != null and status == 1">
            AND (cl.create_time BETWEEN #{startTime} AND #{endTime})
        </if>
        <if test="status != null and status > 1">
            AND (cl.occur_time BETWEEN #{startTime} AND #{endTime})
        </if>
    </select>

    <select id="queryAllByCity" resultMap="infoMap" parameterType="map">
        SELECT
        act_source,
        id,
        cityCode
        FROM
        customer
        WHERE
        manager_id IS NOT NULL
        AND is_delete = 0
        AND act_source is NOT NULL
        limit 10000
    </select>

    <select id="queryByCondition" resultType="string" parameterType="map">
        SELECT
        phone
        FROM
        customer
        WHERE
        manager_id IS NOT NULL
        AND is_delete = 0
        AND act_source like CONCAT('', #{actSource},'%')
    </select>

    <select id="queryBySource" resultType="java.util.HashMap" parameterType="map">
        SELECT
        manager_id as managerId,
        id as customerId,
        phone
        FROM
        customer
        WHERE
        manager_id IS NOT NULL
        AND is_delete = 0
        AND act_source like CONCAT('', #{actSource},'%')
    </select>

    <select id="queryCustomerLogBySource" resultType="java.util.HashMap" parameterType="map">
        SELECT
        customer_id
        FROM  customer_callback_log
        WHERE customer_id IN
        (
        SELECT id AS customer_id FROM customer
        WHERE act_source = #{source}
        AND is_delete = 0  AND manager_id IS NOT NULL
        AND cityCode = #{cityCode}
        GROUP BY customer_id
        )
    </select>

    <select id="queryByStatusAndSource" resultType="java.util.HashMap" parameterType="map">
        SELECT
        customerManagerId
        FROM
        customer_status_log
        WHERE customerManagerId IN
        (
        SELECT manager_id AS customerManagerId FROM customer
        WHERE act_source = #{source}
        AND is_delete = 0 AND manager_id IS NOT NULL
        AND cityCode = #{cityCode}
        GROUP BY manager_id
        )
        <if test="status != 6 ">
            AND from_status_id = #{status}
        </if>
        <if test="telephone = 6 ">
            AND to_status_id = #{status}
        </if>

    </select>

    <select id="querySource" resultType="java.util.HashMap">
        SELECT
        act_source as actSource,
        cityCode
        FROM customer
    </select>

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

    <select id="queryByConditions" resultMap="infoMap" parameterType="map">
        SELECT
        <include refid="sqlId"/>
        FROM customer
        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="lastManager != null and lastManager != ''">
            AND  last_manager = #{lastManager}
        </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="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="createTime != null and createTime != '' ">
            AND create_time >= #{createTime}
        </if>
        <if test="isAdd != null and isAdd >=0">
            UNION
            SELECT * FROM customer
            WHERE
            STATUS = 11
            AND ( create_source = 'SYSTEM' OR create_source = 'WECHAT')
            AND DATE(create_time) >= DATE_SUB(CURDATE(), INTERVAL #{isProtect} DAY)
        </if>
    </select>


    <select id="queryMonthPlatCustomers" resultMap="infoMap">
        SELECT
        id,name,phone,creater,manager_id,manager_name,cityCode,customer_source_type,act_source
        FROM customer
        where is_delete = 0
        and create_source = 'PT'
        and customer_source_type = 8
        AND STATUS IN ( 11, 0, 99 )
        and crm_sync='0'
        and create_time &lt; CONCAT(DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 1 MONTH),'%Y-%m-%d 00:00:00'))

    </select>

    <update id="updateSync" parameterType="com.house365.beans.entity.CustomerEntity">
        UPDATE customer
        SET
        crm_sync = '1'
        WHERE id = #{id}
    </update>


    <select id="queryCustomers" 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 c
        where c.is_delete = 0
        <if test="city != null and city != '' ">
            AND cityCode = #{cityCode}
        </if>
        <if test="phone != null and phone >=0">
            AND c.phone = #{phone}
        </if>
        <if test="managerId != null and managerId >=0">
            AND c.manager_id = #{managerId}
        </if>
        <if test="isPrivate != null and isPrivate >=0">
            AND (c.isWaitCall = 0 OR c.isWaitCall IS NULL)
            AND (c.STATUS status in (11,0,99))
        </if>
        order by c.create_time
    </select>

    <select id="getCustomersById" resultType="int" parameterType="map">
        SELECT
        count(*)
        FROM customer_merge
        WHERE
        STATUS IN ( 11, 0, 99 )
        AND is_delete = 0
        AND ( isWaitCall = 0 OR isWaitCall IS NULL )
        and manager_id = #{managerId}
    </select>

    <select id="getDetailCustomers" 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
        FROM customer
        where manager_id= #{managerId}
        and phone= #{phone}
        and is_delete = 0
    </select>

    <update id="clearCustomerByManagerId" parameterType="map">
        UPDATE customer
        SET
        manager_id =null,
        manager_name =null,
        ocean_date = #{date},
        update_time = #{date},
        updater = #{updaterId},
        last_manager = #{lastManager}
        WHERE manager_id = #{managerId}
        AND is_delete = 0
    </update>

    <select id="queryOceanCustomer" parameterType="map" resultMap="infoMap">
        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
        WHERE (manager_id IS NULL OR manager_id = '')
        AND buy_intention = '5'
        AND TIMESTAMPDIFF(MONTH,update_time,CURDATE()) > 5 AND is_delete = 0
    </select>

    <select id="getCustomerIds" resultMap="infoMap" parameterType="map">
        select c1.id,c1.phone
        from customer c1
        where 1=1
        and c1.phone =
        (
        select phone
        from customer where id= #{id}
        )
        and c1.manager_id=
        (
        select manager_id
        from customer where id= #{id}
        )
        and is_delete=0
        and ( isWaitCall = 0 OR isWaitCall IS NULL )
        and cityCode= #{cityCode}
        and status!=98
    </select>

    <update id="noApproximateBackToSea" parameterType="java.util.HashMap">
        UPDATE customer a
        SET
        last_manager=manager_name,
        manager_id = null,
        manager_name=null,
        ocean_date = #{date},
        update_time =  #{date},
        isRecycled = #{isRecycled},
        updater = #{updaterId}
        WHERE
        1=1
        <if test="ids != null and ids.size() > 0 ">
            and a.id in
            <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        AND a.cityCode =#{city}
    </update>

    <update id="noApproximateBackToSea2" parameterType="com.house365.beans.entity.CustomerEntity">
        UPDATE customer a
        SET
        last_manager=#{lastManager},
        manager_id = null,
        manager_name=null,
        ocean_date = #{oceanDate},
        update_time =  #{updateTime},
        isRecycled = #{isRecycled},
        updater = #{updater}
        WHERE
        1=1
        and a.id =#{id}
        AND a.cityCode =#{cityCode}
    </update>

    <select id="backToSeaCustomerNoLimit" resultMap="infoMap" parameterType="Map">
        SELECT
        id,phone,manager_id,manager_name
        FROM
        (SELECT
        c1.id,
        c1.manager_id,
        c1.phone,
        c1.manager_name,
        IF (
        SUBSTRING_INDEX(
        GROUP_CONCAT(
        c2.create_time
        ORDER BY c2.create_time DESC
        ),
        ',',
        1
        ) IS NOT NULL,
        SUBSTRING_INDEX(
        GROUP_CONCAT(
        c2.create_time
        ORDER BY c2.create_time DESC
        ),
        ',',
        1
        ),
        c1.bind_time
        ) AS callbackTime
        FROM
        customer c1
        LEFT JOIN customer_callback_log c2
        ON c1.id = c2.customer_id
        WHERE 1=1
        AND c1.cityCode = #{city}
        AND c1.is_delete = 0
        AND c1.manager_id IS NOT NULL
        GROUP BY c1.id,
        c1.bind_time,
        c2.customer_id) t
        WHERE DATEDIFF(NOW(), callbackTime) >=#{days}
        limit 5000
    </select>

    <select id="backToSeaCustomer" resultMap="infoMap" parameterType="Map">
        select t.* from (
        SELECT
        c1.id,c1.manager_id,c1.manager_name,c1.phone,max(c2.create_time) create_time
        FROM
        customer c1 left join customer_callback_log c2 on c2.customer_id = c1.id
        left join (select customer_id from customer_project cp) cp2 on c1.id=cp2.customer_id
        where 1=1
        and c1.cityCode=#{city}
        AND c1.is_delete = 0
        AND ( c1.isWaitCall = 0 OR c1.isWaitCall IS NULL )
        AND c1.manager_id IS NOT NULL
        AND c1.STATUS IN ( 11, 0, 99 )

        group by c1.manager_id,c1.phone
        order by create_time desc
        ) t
        where  DATEDIFF(NOW(), t.create_time) >=#{days}
        limit 500
    </select>

    <select id="getCpsByPhone" resultMap="infoMap" parameterType="Map">
        select c.id
        from customer c,customer_project cp
        where c.manager_id=#{managerId}
        and c.phone=#{phone}
        and c.cityCode=#{city}
        and c.is_delete=0
        and (c.isWaitCall=0 or isWaitCall is null)
        and c.id=cp.customer_id
    </select>

    <update id="updateCustomerMerge" parameterType="com.house365.beans.entity.CustomerEntity">
        UPDATE customer_merge
        SET
        trace_status = #{traceStatus},
        manager_id = #{managerId},
        manager_name = #{managerName},
        buy_intention = #{buyIntention},
        update_time = #{updateTime},
        updater = #{updater},
        isRecycled = #{isRecycled},
        bind_time = #{bindTime}
        WHERE id = #{id}
    </update>

    <update id="updateOldCustomers" parameterType="map">
        UPDATE customer
        SET
        manager_id = null,
        manager_name = null,
        ocean_date = #{oceanDate},
        update_time = #{updateTime},
        updater = #{updater}
        WHERE manager_id = #{managerId}
        and phone = #{phone}
        and is_delete = 0
    </update>

    <update id="updateSeaCustomerByPhone" parameterType="map">
        UPDATE customer
        SET
        manager_id = #{managerId},
        manager_name = #{lastManagerName},
        update_time = #{updateTime},
        updater = #{updater}
        WHERE manager_id is null
        and manager_name is null
        and is_delete = #{isDelete}
        and last_manager = #{lastManagerName}
        and phone = #{phone}
    </update>

    <select id="getSeaCustomerByPhone" parameterType="map" resultMap="infoMap">
        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
        from customer
        WHERE manager_id is null
        and manager_name is null
        and is_delete = #{isDelete}
        and last_manager = #{lastManagerName}
        and phone = #{phone}
    </select>

    <update id="transferManagerInfo" parameterType="map">
        UPDATE customer
        SET
        manager_id = #{newManagerId},
        manager_name = #{newManagerName},
        updater = #{updater},
        isHandover = #{isHandover},
        update_time = #{updateTime}
        WHERE 1=1
        and is_delete = #{isDelete}
        and manager_id = #{oldManagerId}
        and phone = #{phone}
    </update>

    <select id="queryByOverTime" parameterType="map" resultMap="infoMap">
        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
        WHERE id IN
        (SELECT
        customerId
        FROM
        market_customer a
        WHERE a.active_id IN
        (SELECT
        id
        FROM
        marketing_activity
        WHERE finish_time BETWEEN #{date} AND NOW())
        )
        AND manager_id IS NOT NULL AND isWaitCall = 1
    </select>

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

    <select id="queryForAddHot" resultMap="infoMap" parameterType="map">
        SELECT id,phone FROM (
        SELECT
        id,phone
        FROM customer
        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
            WHERE
            STATUS in (0,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="queryCloudCount" resultMap="infoMap" parameterType="map">
        SELECT
        id,
        phone,
        manager_id
        FROM
        customer
        WHERE  is_delete = 0
        <if test="abType != null and abType > 0">
            AND buy_intention in (1,2)
        </if>
        <if test="idList != null and idList.size() > 0 ">
            AND id in
            <foreach collection="idList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY phone
    </select>

    <select id="querySeaList" parameterType="map" resultType="java.util.HashMap">
        SELECT
        id, name, phone, act_source AS actSource,buy_intention AS buyIntention,manager_id AS managerId,
        manager_name AS managerName,create_source AS createSource,last_manager AS lastManager,ocean_date AS oceanDate,
        create_time AS createTime
        FROM customer
        where cityCode = #{city} AND is_delete = 0 AND manager_id IS NULL
        <if test="phone != null and phone !='' ">
            AND phone LIKE CONCAT('',#{phone},'%')
        </if>
        <if test="name != null and name != '' ">
            AND name LIKE CONCAT('',#{name},'%')
        </if>
        <if test="lastManager != null and lastManager != ''">
            AND last_manager LIKE CONCAT('',#{lastManager},'%')
        </if>
        <if test="buyIntention != null and buyIntention != ''">
            AND buy_intention = #{buyIntention}
        </if>
        <if test="buyIntentionIsNull != null and buyIntentionIsNull != ''">
            AND buy_intention = ''
        </if>
        <if test="notBuyIntention != null and notBuyIntention != ''">
            AND buy_intention != #{notBuyIntention}
        </if>
        <if test="isRecycled != null and isRecycled >=0">
            AND isRecycled = #{isRecycled}
        </if>
        <if test="oceanDateStart != null and oceanDateStart != ''">
            AND ocean_date > #{oceanDateStart}
        </if>
        <if test="oceanDateEnd != null and oceanDateEnd != ''">
            AND  #{oceanDateEnd} > ocean_date
        </if>
        <if test="createSource != null and createSource != '' ">
            AND create_source = #{createSource}
        </if>
        ORDER BY ocean_date DESC
        <if test="start != null and start >= 0 ">
            limit #{start},#{limit}
        </if>
    </select>

    <select id="querySeaCount" resultType="Integer" parameterType="map">
        SELECT
        count(*)
        FROM customer
        where cityCode = #{city} AND is_delete = 0 AND manager_id IS NULL
        <if test="phone != null and phone !='' ">
            AND phone LIKE CONCAT('',#{phone},'%')
        </if>
        <if test="name != null and name != '' ">
            AND name LIKE CONCAT('',#{name},'%')
        </if>
        <if test="lastManager != null and lastManager != ''">
            AND last_manager LIKE CONCAT('',#{lastManager},'%')
        </if>
        <if test="buyIntention != null and buyIntention != ''">
            AND buy_intention = #{buyIntention}
        </if>
        <if test="buyIntentionIsNull != null and buyIntentionIsNull != ''">
            AND buy_intention = ''
        </if>
        <if test="notBuyIntention != null and notBuyIntention != ''">
            AND buy_intention != #{notBuyIntention}
        </if>
        <if test="isRecycled != null and isRecycled >=0">
            AND isRecycled = #{isRecycled}
        </if>
        <if test="oceanDateStart != null and oceanDateStart != ''">
            AND ocean_date > #{oceanDateStart}
        </if>
        <if test="oceanDateEnd != null and oceanDateEnd != ''">
            AND  #{oceanDateEnd} > ocean_date
        </if>
        <if test="createSource != null and createSource != '' ">
            AND create_source = #{createSource}
        </if>
    </select>

</mapper>