<?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.huiju.estateagents.mapper.TaRecommendCustomerMapper">
    <select id="getMyCutsomerNum" resultType="int">
        SELECT
        count(*) AS total
        FROM
        ta_recommend_customer t
        WHERE
        t.recommend_person = #{personId}
        AND t.status &gt; -1
    </select>
    <select id="getCustomerPersonId" resultType="java.lang.String">
        SELECT
        person_id
        FROM
        ta_customer_person
        WHERE
        customer_id = #{customerId}
    </select>

    <select id="getCustomerDetail" resultType="com.huiju.estateagents.entity.TaRecommendCustomer">
        SELECT
        a.*,
        (select c.visit_time FROM ta_person_visit_record c where c.person_id = #{personId} ORDER BY c.visit_time asc LIMIT 1) as visitTime,
        (SELECT SUM(d.visit_duration) FROM ta_person_visit_record d WHERE d.person_id= #{personId}) as duration
        FROM
        ta_recommend_customer a
        LEFT JOIN ta_person_visit_record c ON a.person_id = c.person_id
        where a.customer_id = #{customerId}
        GROUP BY a.customer_id
    </select>

    <select id="getRecCustomerList" resultType="com.huiju.estateagents.entity.TaRecommendCustomer">
        SELECT
        a.*,
        a.customer_id as customerId,
        c.phone as recommendTel,
        c.nickname as recommendName
        FROM
        ta_recommend_customer a
        LEFT JOIN ta_person c on a.recommend_person = c.person_id
        <where>
            a.status > 0
            and a.report_recommend_status = 2
            <if test="orgId != null">
                and a.org_id = #{orgId}
            </if>
            <if test="building != null and building !=''">
                and a.building_id = #{building}
            </if>
            <if test="name != null and name !=''">
                and a.name like CONCAT('%',#{name}, '%')
            </if>
            <if test="tel != null and tel!=''">
                and a.phone like CONCAT('%',#{tel}, '%')
            </if>
            <if test="consultName != null and consultName !=''">
                and c.nickname like CONCAT('%',#{consultName}, '%')
            </if>
            <if test="consultTel != null and consultTel !=''">
                and c.phone like CONCAT('%',#{consultTel}, '%')
            </if>
            <if test="entryType != null and entryType !=''">
                and a.entry_type = #{entryType}
            </if>
            <if test="verifyStatus != null and verifyStatus !=''">
                and a.verify_status = #{verifyStatus}
            </if>
            <if test="sex != null and sex !=''">
                and a.sex = #{sex}
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND a.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
        </where>
        order by a.create_date desc
    </select>

    <select id="getIndependentAgents" resultType="com.huiju.estateagents.entity.TaPerson">
        SELECT
        a.*,
        tc.channel_name as channelName
        FROM
        ta_person a
        left join ta_channel_person tcp on a.person_id = tcp.person_id and tcp.status = 1
        left join ta_channel tc on tc.channel_id = tcp.channel_id
        <where>
            a.person_type in ('estate agent', 'channel agent')
            and a.status >0
            <if test="name != null and name !=''">
                and (a.nickname like concat('%', #{name}, '%') or  a.name like concat('%', #{name}, '%'))
            </if>
            <if test="tel != null and tel!=''">
                and a.phone like concat('%', #{tel}, '%')
            </if>
            <if test="channelId != null and channelId != '' ">
                and tc.channel_id = #{channelId}
            </if>
            <if test="orgId != null">
                and a.org_id = #{orgId}
            </if>

        </where>

        ORDER BY
        create_date DESC
    </select>

    <select id="getRepCustomerList" resultType="com.huiju.estateagents.entity.TaRecommendCustomer">
        SELECT
        a.*,
        a.customer_id AS customerId,
        IFNULL(d.user_name, b.name) as consultantName,
        IFNULL(d.phone, b.tel) as consultTel,
        c.building_name as intentionName
        FROM
        ta_recommend_customer a
        LEFT JOIN ta_person b ON a.realty_consultant = b.person_id
        LEFT JOIN ta_user d on a.realty_consultant = d.user_id
        LEFT JOIN ta_building c ON a.building_id = c.building_id
        <where>
            a.status > 0
            and a.report_recommend_status = 1
            <if test="orgId != null">
                and a.org_id = #{orgId}
            </if>
            <if test="building != null and building !=''">
                and a.building_id = #{building}
            </if>
            <if test="name != null and name !=''">
                and a.name = #{name}
            </if>
            <if test="tel != null and tel!=''">
                and a.phone like concat(concat("%",#{tel}),"%")
            </if>
            <if test="consultName != null and consultName !=''">
                and IFNULL(d.user_name, b.name) like CONCAT('%',#{consultName}, '%')
            </if>
            <if test="consultTel != null and consultTel !=''">
                and IFNULL(d.phone, b.tel) = #{consultTel}
            </if>
            <if test="entryType != null and entryType !=''">
                and a.entry_type = #{entryType}
            </if>
            <if test="verifyStatus != null and verifyStatus !=''">
                and a.verify_status = #{verifyStatus}
            </if>
            <if test="sex != null">
                and a.sex = #{sex}
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND a.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
        </where>
        order by a.create_date desc
    </select>
    <select id="getCustomerList" resultType="com.huiju.estateagents.entity.TaRecommendCustomer">
        SELECT
        a.*,
        IFNULL( t2.building_name, '暂无' ) AS buildingName,
        a.customer_id as customerId,
        IFNULL(b.name,d.user_name) as consultantName,
        IFNULL(b.tel,d.phone) as consultTel,
        c.nickname as recommendName,
        c.tel as recommendTel,
        IFNULL(c.name,c.nickname) AS sharePersonName
        FROM
        ta_recommend_customer a
        LEFT JOIN ta_person b ON a.realty_consultant = b.person_id
        LEFT JOIN ta_user d on d.user_id = a.realty_consultant
        LEFT JOIN ta_person_from_record p ON a.person_id = p.person_id AND p.is_first_time = 1
        <if test="orgId != null">
            and p.org_id = #{orgId}
        </if>
        --         LEFT JOIN ta_person c on p.share_person_id = c.person_id
        --         LEFT JOIN td_wx_dict w ON p.scene_id = w.scene_id
        LEFT JOIN td_wx_dict w ON p.scene_id = w.scene_id
        LEFT JOIN ta_person p2 ON a.person_id = p2.person_id
        LEFT JOIN ta_person c ON p2.recommend_person = c.person_id
        LEFT JOIN ta_building t2 ON a.building_id = t2.building_id
        <where>
            a.status > 0
            and a.verify_status = 1
            and a.realty_consultant is not null
            and a.realty_consultant != ''
            <if test="orgId != null">
                and a.org_id = #{orgId}
            </if>
            <if test="building != null and building !=''">
                and a.building_id = #{building}
            </if>
            <if test="name != null and name !=''">
                and a.name like CONCAT('%',#{name}, '%')
            </if>
            <if test="tel != null and tel!=''">
                and a.phone like CONCAT('%',#{tel}, '%')
            </if>
            <if test="consultName != null and consultName !=''">
                and IFNULL(b.name,d.user_name) like CONCAT('%',#{consultName}, '%')
            </if>
            <if test="consultTel != null and consultTel !=''">
                and IFNULL(b.tel,d.phone) like CONCAT('%',#{consultTel}, '%')
            </if>
            <if test="entryType != null and entryType !=''">
                and a.entry_type = #{entryType}
            </if>
            <if test="verifyStatus != null and verifyStatus !=''">
                and a.verify_status = #{verifyStatus}
            </if>
            <if test="status != null and status !=''">
                and a.status = #{status}
            </if>
            <if test="sex != null and sex !=''">
                and a.sex = #{sex}
            </if>
            <if test="startCreateDate != null and startCreateDate !=''">
                and a.create_Date >= #{startCreateDate}
            </if>
            <if test="endCreateDate != null and endCreateDate !=''">
                and a.create_Date &lt;= #{endCreateDate}
            </if>
            <if test="sharePersonName != null and sharePersonName !=''">
                and (c.nickName like CONCAT('%',#{sharePersonName}, '%') or c.name like CONCAT('%',#{sharePersonName},
                '%'))
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND a.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
            <if test="sceneType !=null and sceneType !=''">
                and p.scene_type = #{sceneType}
            </if>
        </where>
        order by a.create_date desc
    </select>
    <select id="getCustomerById" resultType="com.huiju.estateagents.entity.TaRecommendCustomer">
        SELECT
        a.*,
        b.name as consultantName
        FROM
        ta_recommend_customer a
        LEFT JOIN ta_person b ON a.realty_consultant = b.person_id
        WHERE a.customer_id = #{customerId}
    </select>

    <select id="getPublicCustomerList" resultType="com.huiju.estateagents.entity.TaRecommendCustomer">
        SELECT
        t.*,
        IFNULL( t2.building_name, '暂无' ) AS buildingName,
        ifnull( t3.nickname, t3.NAME ) AS sharePersonName
        FROM
        ta_recommend_customer t
        LEFT JOIN ta_building t2 ON t.building_id = t2.building_id
        LEFT JOIN ta_person t6 ON t.person_id = t6.person_id
        LEFT JOIN ta_person t3 ON t6.recommend_person = t3.person_id
        LEFT JOIN ta_person_from_record t4 ON t.person_id = t4.person_id AND t4.is_first_time = 1 AND t4.org_id = #{orgId}
        LEFT JOIN td_wx_dict t5 ON t4.scene_id = t5.scene_id
        <where>
            t.`status` > 0
            AND t.verify_status = 1
            AND ( t.realty_consultant IS NULL OR t.realty_consultant = '' )
            AND t.org_id = #{orgId}
            <if test="buildingId != null and buildingId !=''">
                and t.building_id = #{buildingId}
            </if>
            <if test="name != null and name !=''">
                and t.name like CONCAT('%',#{name}, '%')
            </if>
            <if test="tel != null and tel!=''">
                and t.phone like CONCAT('%',#{tel}, '%')
            </if>
            <if test="entryType != null and entryType !=''">
                and t.entry_type = #{entryType}
            </if>
            <if test="verifyStatus != null and verifyStatus !=''">
                and t.verify_status = #{verifyStatus}
            </if>
            <if test="sex != null and sex !=''">
                and t.sex = #{sex}
            </if>
            <if test="startCreateDate != null and startCreateDate !=''">
                and t.create_Date >= #{startCreateDate}
            </if>
            <if test="endCreateDate != null and endCreateDate !=''">
                and t.create_Date &lt;= #{endCreateDate}
            </if>
            <if test="sharePersonName != null and sharePersonName !=''">
                and (t3.nickName like CONCAT('%',#{sharePersonName}, '%') or t3.name like CONCAT('%',#{sharePersonName},
                '%'))
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND t.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
            <if test="sceneType !=null and sceneType !=''">
                and t4.scene_type = #{sceneType}
            </if>
            <if test="belongStatus == 0">
                and (t.building_id = ''  OR t.building_id is null )
            </if>
            <if test="belongStatus == 1">
                and t.building_id is not null
            </if>
        </where>
        order by t.create_date desc
    </select>

    <select id="getRecCustomerExport" resultType="com.huiju.estateagents.excel.ExcelRecommendCustomer">
        SELECT
        a.name as name,
        a.phone as phone,
        a.intention as intention,
        a.create_date as createDate,
        if(a.sex = 1, '男',if(a.sex = 2, '女', '未知')) as sex,
        c.nickname as recommend,
        c.phone as recommendTel,
        if(a.verify_status = 0, '待审核', if(a.verify_status = 1, '已通过', if(a.verify_status = 2, '未通过', ''))) as
        verifyStatusName
        FROM
        ta_recommend_customer a
        LEFT JOIN ta_person c on a.recommend_person = c.person_id
        <where>
            a.status > 0
            and a.report_recommend_status = 2
            <if test="orgId != null">
                and a.org_id = #{orgId}
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND a.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
        </where>
        order by a.create_date desc
        limit #{pageCode}, #{pageSize}
    </select>

    <select id="getRecCustomerExportCount" resultType="java.lang.Integer">
        SELECT
        count(*)
        FROM
        ta_recommend_customer a
        LEFT JOIN ta_person c on a.recommend_person = c.person_id
        <where>
            a.status > 0
            and a.report_recommend_status = 2
            <if test="orgId != null">
                and a.org_id = #{orgId}
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND a.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getRepCustomerReportCount" resultType="java.lang.Integer">
        SELECT
        count(*)
        FROM
        ta_recommend_customer a
        LEFT JOIN ta_person b ON a.realty_consultant = b.person_id
        LEFT JOIN ta_building c ON a.building_id = c.building_id
        <where>
            a.status > 0
            and a.report_recommend_status = 1
            <if test="orgId != null">
                and a.org_id = #{orgId}
            </if>
            <if test="building != null and building !=''">
                and a.building_id = #{building}
            </if>
            <if test="name != null and name !=''">
                and a.name = #{name}
            </if>
            <if test="tel != null and tel!=''">
                and a.phone like concat(concat("%",#{tel}),"%")
            </if>
            <if test="consultName != null and consultName !=''">
                and b.name like CONCAT('%',#{consultName}, '%')
            </if>
            <if test="consultTel != null and consultTel !=''">
                and b.tel = #{consultTel}
            </if>
            <if test="entryType != null and entryType !=''">
                and a.entry_type = #{entryType}
            </if>
            <if test="verifyStatus != null and verifyStatus !=''">
                and a.verify_status = #{verifyStatus}
            </if>
            <if test="sex != null and sex !=''">
                and a.sex = #{sex}
                and a.org_id = #{orgid}
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND a.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
        </where>
        order by a.create_date desc
    </select>

    <select id="getRepCustomerReportExport" resultType="com.huiju.estateagents.excel.ReporRecommendCustomer">
        SELECT
        a.name as name,
        a.phone as phone,
        c.building_name as intentionName,
        if(a.sex = 1, '男',if(a.sex = 2, '女', '未知')) as sex,
        IFNULL(d.user_name, b.name) as consultantName,
        IFNULL(d.phone, b.tel) as consultTel
        FROM
        ta_recommend_customer a
        LEFT JOIN ta_person b ON a.realty_consultant = b.person_id
        LEFT JOIN ta_user d on a.realty_consultant = d.user_id
        LEFT JOIN ta_building c ON a.building_id = c.building_id
        <where>
            a.status > 0
            and a.report_recommend_status = 1
            <if test="orgId != null">
                and a.org_id = #{orgId}
            </if>
            <if test="building != null and building !=''">
                and a.building_id = #{building}
            </if>
            <if test="name != null and name !=''">
                and a.name = #{name}
            </if>
            <if test="tel != null and tel!=''">
                and a.phone like concat(concat("%",#{tel}),"%")
            </if>
            <if test="consultName != null and consultName !=''">
                and IFNULL(d.user_name, b.name) like CONCAT('%',#{consultName}, '%')
            </if>
            <if test="consultTel != null and consultTel !=''">
                and IFNULL(d.phone, b.tel) = #{consultTel}
            </if>
            <if test="entryType != null and entryType !=''">
                and a.entry_type = #{entryType}
            </if>
            <if test="verifyStatus != null and verifyStatus !=''">
                and a.verify_status = #{verifyStatus}
            </if>
            <if test="sex != null and sex !=''">
                and a.sex = #{sex}
                and a.org_id = #{orgid}
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND a.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
        </where>
        order by a.create_date desc
        limit #{pageCode}, #{pageSize}
    </select>


    <select id="getIndependentAgentsCount" resultType="java.lang.Integer">
        SELECT
        count(*)
        FROM
        ta_person a
        left join ta_channel_person tcp on a.person_id = tcp.person_id
        left join ta_channel tc on tc.channel_id = tcp.channel_id
        <where>
            a.person_type = 'estate agent'
            and a.status >0
            <if test="orgId != null">
                and a.org_id = #{orgId}
            </if>
        </where>

        ORDER BY
        a.create_date DESC
    </select>

    <select id="getIndependentAgentsExport" resultType="com.huiju.estateagents.excel.AgentsRecommendCustomer">
        SELECT
        ifnull( a.name, a.nickname) as nickname,
        a.phone as phone,
        if(a.gender = '1', '男', if(a.gender = '2', '女', '未知')) as gender,
        '专业经纪人' as personType,
        tc.channel_name as channelName,
        (
        SELECT
        count( 1 )
        FROM
        ta_person tp
        WHERE
        tp.recommend_agent = a.person_id
        ) AS agentCount,
        (
        SELECT
        count( 1 )
        FROM
        ta_recommend_customer tarc
        WHERE
        tarc.recommend_person = a.person_id
        and tarc.org_id = #{orgId}
        ) AS recommedCount
        FROM
        ta_person a
        left join ta_channel_person tcp on a.person_id = tcp.person_id and tcp.status = 1
        left join ta_channel tc on tc.channel_id = tcp.channel_id
        <where>
            ifnull(a.person_type, '') in ('channel agent', 'estate agent')
            and a.status >0
            <if test="orgId != null">
                and a.org_id = #{orgId}
            </if>
            <if test="name != null and name != ''">
                and (a.nickname like concat('%', #{name}, '%') or  a.name like concat('%', #{name}, '%'))
            </if>
            <if test="tel != null and tel != null">
                and a.phone like concat('%', #{tel}, '%')
            </if>
            <if test="channelId != null and channelId != ''">
                and tc.channel_id = #{channelId}
            </if>

        </where>

        ORDER BY
        a.create_date DESC
        limit #{pageCode}, #{pageSize}
    </select>


    <select id="getPublicCustomerExportListCount" resultType="Integer">
        SELECT
        count(1)
        FROM
        ta_person b
        <where>
            b.status > 0
            AND IFNULL(b.person_type, '') in ('estate agent', 'customer')
            <if test="name != null and name !=''">
                and b.nickname like CONCAT('%',#{name}, '%')
            </if>
            <if test="tel != null and tel!=''">
                and b.phone like CONCAT('%',#{tel}, '%')
            </if>
            <if test="entryType != null and entryType !=''">
                and b.entry_type = #{entryType}
            </if>
            <if test="sex != null and sex !=''">
                and b.sex = #{sex}
            </if>
            <if test="orgId != null">
                and b.org_id = #{orgId}
            </if>
            <if test="consultTel != null and consultTel !=''">
                and b.tel like CONCAT('%',#{consultTel}, '%')
            </if>
            and NOT EXISTS ( select * from ta_recommend_customer a where a.org_id = #{orgId} and a.recommend_person =
            b.person_id)
        </where>
        order by b.create_date desc
    </select>

    <select id="getPublicCustomerExportList" resultType="com.huiju.estateagents.excel.PublicCustomerExport">
        SELECT
        t.NAME AS nickname,
        t.phone,
        IF( t.sex = 1, '男', IF ( t.sex = 2, '女', '未知' ) ) AS sex,
        t.create_date,
        concat( t.country, t.province ) AS province,
        z.intention,
        IF(t.building_id is null or t.building_id = '','否','是') AS belongStatus,
        IFNULL( t2.building_name, '暂无' ) AS buildingName,
        t5.scene_alias as sceneType,
        ifnull( t3.nickname, t3.NAME ) AS sharePersonName ,
        t6.points
        FROM
        ta_recommend_customer t
        LEFT JOIN ta_building t2 ON t.building_id = t2.building_id
        LEFT JOIN ta_person t7 ON t.person_id = t7.person_id
        LEFT JOIN ta_person t3 ON t7.recommend_person = t3.person_id
        LEFT JOIN ta_person_from_record t4 ON t.person_id = t4.person_id AND t4.is_first_time = 1 AND t4.org_id =
        #{orgId}
        LEFT JOIN td_wx_dict t5 ON t4.scene_id = t5.scene_id
        LEFT JOIN (select sum(points_amount) as points,person_id from ta_points_records where org_id = #{orgId} GROUP BY
        person_id) t6 on t.person_id = t6.person_id
        left join (select sum(ti.intention) as intention,ti.person_id From ta_person_intention_record ti where ti.org_id = #{orgId} group by ti.person_id) z on z.person_id = t.person_id
        WHERE
        t.`status` > 0
        AND t.verify_status = 1
        AND ( t.realty_consultant IS NULL OR t.realty_consultant = '' )
        AND t.org_id = #{orgId}
        <if test="buildingId != null and buildingId !=''">
            and t.building_id = #{buildingId}
        </if>
        <if test="name != null and name !=''">
            and t.name like CONCAT('%',#{name}, '%')
        </if>
        <if test="tel != null and tel!=''">
            and t.phone like CONCAT('%',#{tel}, '%')
        </if>
        <if test="entryType != null and entryType !=''">
            and t.entry_type = #{entryType}
        </if>
        <if test="verifyStatus != null and verifyStatus !=''">
            and t.verify_status = #{verifyStatus}
        </if>
        <if test="sex != null and sex !=''">
            and t.sex = #{sex}
        </if>
        <if test="startCreateDate != null and startCreateDate !=''">
            and t.create_Date >= #{startCreateDate}
        </if>
        <if test="endCreateDate != null and endCreateDate !=''">
            and t.create_Date &lt;= #{endCreateDate}
        </if>
        <if test="personBuildingList != null and personBuildingList.size > 0">
            AND t.building_id in
            <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                #{personBuilding.buildingId}
            </foreach>
        </if>
        <if test="sceneType !=null and sceneType !=''">
            and t4.scene_type = #{sceneType}
        </if>
        <if test="belongStatus == 0">
            and (t.building_id = ''  OR t.building_id is null )
        </if>
        <if test="belongStatus == 1">
            and t.building_id is not null
        </if>
        order by t.create_date desc
    </select>
    <!--    limit #{pageCode}, #{pageSize}-->

    <select id="getCustomerExportListCount" resultType="Integer">
        SELECT
        count(1)
        FROM
        ta_recommend_customer a
        LEFT JOIN ta_person b ON a.realty_consultant = b.person_id
        LEFT JOIN ta_person c on a.recommend_person = c.person_id
        <where>
            a.status > 0
            and a.verify_status = 1
            and realty_consultant is not null
            <if test="orgId != null">
                and a.org_id = #{orgId}
            </if>
            <if test="building != null and building !=''">
                and a.building_id = #{building}
            </if>
            <if test="name != null and name !=''">
                and a.name like CONCAT('%',#{name}, '%')
            </if>
            <if test="tel != null and tel!=''">
                and a.phone like CONCAT('%',#{tel}, '%')
            </if>
            <if test="consultName != null and consultName !=''">
                and b.name like CONCAT('%',#{consultName}, '%')
            </if>
            <if test="consultTel != null and consultTel !=''">
                and b.tel like CONCAT('%',#{consultTel}, '%')
            </if>
            <if test="entryType != null and entryType !=''">
                and a.entry_type = #{entryType}
            </if>
            <if test="verifyStatus != null and verifyStatus !=''">
                and a.verify_status = #{verifyStatus}
            </if>
            <if test="status != null and status !=''">
                and a.status = #{status}
            </if>
            <if test="sex != null and sex !=''">
                and a.sex = #{sex}
            </if>
            <if test="startCreateDate != null and startCreateDate !=''">
                and a.create_Date >= #{startCreateDate}
            </if>
            <if test="endCreateDate != null and endCreateDate !=''">
                and a.create_Date &lt;= #{endCreateDate}
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND a.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
        </where>
        order by a.create_date desc
    </select>


    <select id="getCustomerExportList" resultType="com.huiju.estateagents.excel.PrivateCustomerExport">
        SELECT
        a.name AS name,
        a.phone as phone,
        if(a.sex = 1, '男', if(a.sex = 2, '女', '未知')) as sex,
        CASE
        WHEN b.NAME IS NULL THEN
        d.user_name ELSE b.NAME
        END AS consultantName,
        CASE
        WHEN b.tel IS NULL THEN
        d.phone ELSE b.tel
        END AS consultTel,
        t.building_name,
        z.intention,
        a.create_date,
        concat(tp.country, tp.province) as province,
        w.scene_alias as scene_type,
        IFNULL( c.NAME, c.nickname ) AS sharePersonName,
        s.points
        FROM
        ta_recommend_customer a
        left join ta_person tp on tp.person_id = a.person_id
        LEFT JOIN ta_person b ON a.realty_consultant = b.person_id
        LEFT JOIN ta_user d on d.user_id = a.realty_consultant
        LEFT JOIN ta_person_from_record p ON a.person_id = p.person_id AND p.is_first_time = 1 and p.org_id = #{orgId}
        LEFT JOIN td_wx_dict w ON p.scene_id = w.scene_id
        LEFT JOIN ta_person p2 ON a.person_id = p2.person_id
        LEFT JOIN ta_person c ON p2.recommend_person = c.person_id
        left join ta_building t on a.building_id = t.building_id
        left JOIN (select sum(points_amount) as points,person_id from ta_points_records where org_id = #{orgId} GROUP BY
        person_id) s on a.person_id = s.person_id
        left join (select sum(t.intention) as intention,t.person_id From ta_person_intention_record t where t.org_id =
        #{orgId} group by t.person_id) z on z.person_id = a.person_id
        <where>
            a.status > 0
            and a.verify_status = 1
            and a.realty_consultant is not null
            and a.realty_consultant != ''
            <if test="orgId != null">
                and a.org_id = #{orgId}
            </if>
            <if test="building != null and building !=''">
                and a.building_id = #{building}
            </if>
            <if test="name != null and name !=''">
                and a.name like CONCAT('%',#{name}, '%')
            </if>
            <if test="tel != null and tel!=''">
                and a.phone like CONCAT('%',#{tel}, '%')
            </if>
            <if test="consultName != null and consultName !=''">
                and b.name like CONCAT('%',#{consultName}, '%')
            </if>
            <if test="consultTel != null and consultTel !=''">
                and b.tel like CONCAT('%',#{consultTel}, '%')
            </if>
            <if test="entryType != null and entryType !=''">
                and a.entry_type = #{entryType}
            </if>
            <if test="verifyStatus != null and verifyStatus !=''">
                and a.verify_status = #{verifyStatus}
            </if>
            <if test="status != null and status !=''">
                and a.status = #{status}
            </if>
            <if test="sex != null and sex !=''">
                and a.sex = #{sex}
            </if>
            <if test="startCreateDate != null and startCreateDate !=''">
                and a.create_Date >= #{startCreateDate}
            </if>
            <if test="endCreateDate != null and endCreateDate !=''">
                and a.create_Date &lt;= #{endCreateDate}
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND a.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
            <if test="sceneType !=null and sceneType !=''">
                and p.scene_type = #{sceneType}
            </if>
        </where>
        group by a.customer_id,a.building_id
        order by a.create_date desc
    </select>
    <!--    limit #{pageCode}, #{pageSize}-->

    <select id="getMyCustStatistics" resultType="java.util.Map">
        SELECT
        sum( IF ( STATUS = 4 AND verify_status = 1, 1, 0 ) ) AS clinchSum,
        sum( IF ( STATUS != 4 AND verify_status = 1, 1, 0 ) ) AS followSum
        FROM
        ta_recommend_customer
        WHERE
        org_id = #{orgId}
        AND ( realty_consultant = #{personId}
        <if test="userId != null and userId != ''">
            OR realty_consultant = #{userId}
        </if>
        )
    </select>

    <select id="getMyCustFollowOrClinch" resultType="com.huiju.estateagents.entity.TaRecommendCustomer">
        SELECT
        *
        FROM
        ta_recommend_customer
        WHERE
        org_id = #{orgId}
        AND ( realty_consultant = #{personId} OR realty_consultant = #{userId} )
        <if test="type == 'follow'">
            AND STATUS != 4
            AND verify_status = 1
            <if test="name != null and name !=''">
                and (`name` like CONCAT('%',#{name}, '%') or phone like CONCAT('%',#{name}, '%'))
            </if>
            <if test="phone != null and phone !=''">
                and phone like CONCAT('%',#{phone}, '%')
            </if>
            <if test="status != null and status !=''">
                and status = #{status}
            </if>
            <if test="startReportDate != null and startReportDate !=''">
                and date_format( report_date, '%Y-%m-%d' ) >= #{startReportDate}
            </if>
            <if test="endReportDate != null and endReportDate !=''">
                and date_format( report_date, '%Y-%m-%d' ) &lt;= #{endReportDate}
            </if>
            <if test="startArrivalDate != null and startArrivalDate !=''">
                and date_format( arrival_date, '%Y-%m-%d' ) >= #{startArrivalDate}
            </if>
            <if test="endArrivalDate != null and endArrivalDate !=''">
                and date_format( arrival_date, '%Y-%m-%d' ) &lt;= #{endArrivalDate}
            </if>
        </if>
        <if test="type == 'clinch'">
            AND STATUS = 4
        </if>
    </select>

    <select id="getMyCustDetailById" resultType="com.huiju.estateagents.entity.TaRecommendCustomer">
        SELECT
        *
        FROM
        ta_recommend_customer
        where customer_id = #{customerId}
    </select>

    <select id="getCustomerIntentions" resultType="com.huiju.estateagents.entity.PersonIntention">
        SELECT
        SUM( a.intention ) AS intention,
        a.building_id,
        b.building_name
        FROM
        ta_person_intention_record a
        left join ta_building b on a.building_id = b.building_id
        WHERE
        a.person_id = #{personId}
        <if test="personBuildingList != null and personBuildingList.size > 0">
            AND a.building_id in
            <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                #{personBuilding.buildingId}
            </foreach>
        </if>
        GROUP BY
        a.building_id
        ORDER BY
        intention DESC
    </select>

    <select id="getCustomersIRecommended" resultType="com.huiju.estateagents.entity.TaRecommendCustomer">
        select a.*,b.avatarurl from ta_recommend_customer a left join ta_person b on a.person_id = b.person_id
        where a.recommend_person = #{customerId}
        and a.status &gt;= #{status}
        and a.org_id = #{orgId}
        <if test="personBuildingList != null and personBuildingList.size > 0">
            AND a.building_id in
            <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                #{personBuilding.buildingId}
            </foreach>
        </if>
    </select>

    <select id="getCustomerListOfConsultant" resultType="com.huiju.estateagents.entity.TaRecommendCustomer">
        select s.*, t.avatarurl, s.report_date as visit_time
        from ta_recommend_customer s
        left join ta_person t on t.person_id = s.person_id
        where s.org_id = #{orgId}
        and (s.realty_consultant = #{userId}
        <if test="personId != null">
            or s.realty_consultant = #{personId}
        </if>
        )
        <if test="startDate != null">
            and s.create_date BETWEEN #{startDate} and #{endDate}
        </if>
        <if test="buildingId != null and buildingId !=''">
            and s.building_id = #{buildingId}
        </if>
        and s.status &gt; 0
        order by s.report_date desc
    </select>

    <select id="getCustomerByProjectOrOrg" resultType="com.huiju.estateagents.entity.TaRecommendCustomer">
        SELECT
        *
        FROM
        ta_recommend_customer t
        WHERE
        t.org_id = #{orgId}
        AND t.person_id = #{personId}
        ORDER BY
        IF( t.building_id = #{buildingId}, 0, 1 ) ASC,
        IF( ifnull( t.building_id, '' ) = '', 0, 1 ) ASC,
        t.create_date ASC
        LIMIT 1
    </select>

    <select id="getConsultantCustomerList" resultType="com.huiju.estateagents.entity.TaRecommendCustomer">
        SELECT
        *
        FROM
        ta_recommend_customer t
        WHERE
        t.org_id = #{orgId}
        AND t.STATUS > 0
        AND t.verify_status = 1
        AND t.realty_consultant IN ( #{userId}, #{personId})
    </select>

    <select id="checkCustomerBy" resultType="java.util.Map">
        SELECT
            count( 1 ) AS orgNum,

        <!--   sum( IF ( t.building_id IN ( 'foo', 'bar' ), 1, 0 ) ) AS consultNum,     -->
        <choose>
            <when test="consultBuildingIds != null and consultBuildingIds.size > 0">
                COALESCE(sum( IF ( t.building_id IN <foreach collection="consultBuildingIds" item="id" open="(" close=")" separator=",">#{id}</foreach>
            </when>
            <otherwise>
                COALESCE(sum( IF ( t.building_id IN ('****')
            </otherwise>
        </choose>
            , 1, 0 ) ), 0) AS consultBuildingNum,

        <choose>
            <when test="consultBuildingIds != null and consultBuildingIds.size > 0">
                COALESCE(sum( IF ( t.building_id IN <foreach collection="consultBuildingIds" item="id" open="(" close=")" separator=",">#{id}</foreach>
            </when>
            <otherwise>
                COALESCE(sum( IF ( t.building_id IN ('****')
            </otherwise>
        </choose>

              AND IFNULL(t.realty_consultant, '') != '', 1, 0 ) ), 0) AS consultNum,

            COALESCE(sum( IF ( t.building_id = #{buildingId}, 1, 0 ) ), 0) AS buildingNum

        FROM
            ta_recommend_customer t
        WHERE
            t.org_id = #{orgId}
            AND t.person_id = #{personId}
            AND t.status > -1
    </select>
</mapper>
