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

    <!-- 按照项目, 置业分组统计, 注意查询与分组条件, 必须与下面的汇总，导出一致 -->
    <select id="stsKPIDaily" resultType="com.huiju.estateagents.entity.TsConsultantKpi">
        SELECT * FROM (
            SELECT
                1 AS serial_no,
                '2020' AS statis_date,
                t.org_id,
                t.building_id,
                t.building_name,
                t.user_id,
                t.user_name,
                t.phone,
                IFNULL(sum( t.new_persons ),0) AS new_persons,
                IFNULL(sum( t.share_num ),0) AS share_num,
                IFNULL(sum( t.visit_persons ),0) AS visit_persons,
                IFNULL(sum( t.visit_num ),0) AS visit_num,
                IFNULL(sum( t.share_persons ),0) AS share_persons,
                IFNULL(sum( t.home_page_persons ),0) AS home_page_persons,
                IFNULL(sum( t.home_page_nums ),0) AS home_page_nums,
                IFNULL(sum( t.chat_persons ),0) AS chat_persons,
                IFNULL(sum( t.favor_num ),0) AS favor_num,
                (select count(DISTINCT s.customer_id) from ta_recommend_customer s
                 where s.org_id = t.org_id and (s.realty_consultant = t.user_id or s.realty_consultant = m.person_id)
                 and (s.building_id = t.building_id or IFNULL(t.building_id, '') = '')
                 and s.status > 0
                 AND s.create_date BETWEEN STR_TO_DATE( #{startDate}, '%Y%m%d' )
                 AND  STR_TO_DATE(concat(#{endDate}, '235959'), '%Y%m%d%H%i%s')) as total_persons
            FROM
                ts_consultant_kpi t
            LEFT JOIN ta_person m on m.org_id = #{orgId} and m.user_id = t.user_id
            WHERE
                t.org_id = #{orgId}
                AND t.statis_date BETWEEN #{startDate} AND #{endDate}
            <if test="buildingIds != null">
                AND t.building_id in
                <foreach item="buildingId" index="index" collection="buildingIds"
                         open="(" separator="," close=")">
                    #{buildingId}
                </foreach>
            </if>
            GROUP BY
                t.user_id,
                t.building_id
        ) a

        <if test="asc != null and asc != ''">
            ORDER BY ${asc} asc
        </if>
        <if test="desc != null and desc != ''">
            ORDER BY ${desc} desc
        </if>
    </select>

    <!-- 按照小程序分组统计, 注意查询与分组条件, 必须与之前的汇总，导出一致 -->
    <select id="stsKPITotalByOrg" resultType="com.huiju.estateagents.entity.TsConsultantKpi">
        SELECT
            1 AS serial_no,
            '2020' AS statis_date,
            t.org_id,
            '' as building_id,
            '-' as building_name,
            0 as user_id,
            '-' as user_name,
            IFNULL(sum( t.new_persons ),0) AS new_persons,
            IFNULL(sum( t.share_num ),0) AS share_num,
            IFNULL(sum( t.visit_persons ),0) AS visit_persons,
            IFNULL(sum( t.visit_num ),0) AS visit_num,
            IFNULL(sum( t.share_persons ),0) AS share_persons,
            IFNULL(sum( t.home_page_persons ),0) AS home_page_persons,
            IFNULL(sum( t.home_page_nums ),0) AS home_page_nums,
            IFNULL(sum( t.chat_persons ),0) AS chat_persons,
            IFNULL(sum( t.favor_num ),0) AS favor_num,

            <!-- 这一列单独统计 -->
            0 as total_persons
        FROM
            ts_consultant_kpi t
        WHERE
            t.org_id = #{orgId}
            AND t.statis_date BETWEEN #{startDate} AND #{endDate}
        <if test="buildingIds != null">
            AND t.building_id in
            <foreach item="buildingId" index="index" collection="buildingIds"
                     open="(" separator="," close=")">
                #{buildingId}
            </foreach>
        </if>
    </select>

    <!-- 导出 -->
    <select id="stsKPIDailyExport" resultType="com.huiju.estateagents.excel.ConsultantKPIExport">
        SELECT
            1 AS serial_no,
            '2020' AS statis_date,
            t.org_id,
            t.building_id,
            t.building_name,
            t.user_id,
            t.user_name,
            t.phone,
            IFNULL(sum( t.new_persons ),0) AS new_persons,
            IFNULL(sum( t.share_num ),0) AS share_num,
            IFNULL(sum( t.visit_persons ),0) AS visit_persons,
            IFNULL(sum( t.visit_num ),0) AS visit_num,
            IFNULL(sum( t.share_persons ),0) AS share_persons,
            IFNULL(sum( t.home_page_persons ),0) AS home_page_persons,
            IFNULL(sum( t.home_page_nums ),0) AS home_page_nums,
            IFNULL(sum( t.chat_persons ),0) AS chat_persons,
            IFNULL(sum( t.favor_num ),0) AS favor_num,
            (select count(DISTINCT s.customer_id) from ta_recommend_customer s
             where s.org_id = t.org_id and (s.realty_consultant = t.user_id or s.realty_consultant = m.person_id) and (s.building_id = t.building_id or IFNULL(t.building_id, '') = '') and s.status > 0) as total_persons
        FROM
            ts_consultant_kpi t
        LEFT JOIN ta_person m on m.org_id = #{orgId} and m.user_id = t.user_id
        WHERE
            t.org_id = #{orgId}
        AND t.statis_date BETWEEN #{startDate} AND #{endDate}
    <if test="buildingIds != null">
        AND t.building_id in
        <foreach item="buildingId" index="index" collection="buildingIds"
                 open="(" separator="," close=")">
            #{buildingId}
        </foreach>
    </if>
        GROUP BY
            t.user_id,
            t.building_id
        ORDER BY sum( new_persons ) desc
    </select>

    <select id="stsAllCustomersByOrg" resultType="java.lang.Integer">
        SELECT
            count(DISTINCT p.customer_id)
        FROM
            ta_recommend_customer p
        INNER JOIN ta_person s ON s.org_id = p.org_id
            AND ( p.realty_consultant = s.user_id OR p.realty_consultant = s.person_id )
        INNER JOIN ts_consultant_kpi t ON t.org_id = s.org_id
            AND t.user_id = s.user_id
            AND ( t.building_id = p.building_id OR IFNULL( t.building_id, '' ) = '' )
        WHERE
            p.org_id = #{orgId}
            AND p.`status` > 0
            AND p.create_date BETWEEN STR_TO_DATE( #{startDate}, '%Y%m%d')
            AND  STR_TO_DATE(concat(#{endDate}, '235959'), '%Y%m%d%H%i%s')
        <if test="buildingIds != null">
            AND t.building_id in
            <foreach item="buildingId" index="index" collection="buildingIds"
                     open="(" separator="," close=")">
                #{buildingId}
            </foreach>
        </if>
    </select>

    <select id="getConsultantShareCustomers" resultType="com.huiju.estateagents.entity.TaPerson">
        SELECT
            t.*,
            e.create_date as visit_time
        FROM ta_customer_from e
        INNER JOIN ta_person t ON t.person_id = e.person_id
        INNER JOIN ta_person s ON s.person_id = e.share_person_id
        WHERE e.org_id = #{orgId}
        <if test="buildingId != null and buildingId !=''">
            AND e.building_id = #{buildingId}
        </if>
        AND e.target_type is not null
        AND e.create_date BETWEEN #{startDate} and #{endDate}
        AND e.is_project_first = 1
        AND s.user_id = #{userId}

        AND t.person_type != 'Realty Consultant'

<!--        为了与存储过程一致 -->
<!--        AND t.`status` = 1-->
        ORDER BY e.create_date DESC
    </select>

    <select id="getConsultantHomePagePersons" resultType="com.huiju.estateagents.entity.TaPerson">
        SELECT
            t.*,
            count( * ) as visit_times,
            max(f.visit_time) as visit_time
        FROM
            ta_person t
        INNER JOIN ta_person_visit_record f ON f.org_id = #{orgId} AND f.person_id = t.person_id
        INNER JOIN ta_person s on s.person_id = f.target_id
        WHERE t.org_id = #{orgId}

<!--        为了与存储过程一致 -->
<!--            AND t.`status` = 1-->
        <if test="buildingId != null and buildingId !=''">
            AND f.building_id = #{buildingId}
        </if>
            AND f.event_type = 'card'
            AND f.visit_time BETWEEN #{startDate} and #{endDate}
            AND s.user_id = #{userId}

<!--        为了与存储过程一致 -->
<!--            AND s.`status` = 1-->
        GROUP BY t.person_id, DATE_FORMAT(f.visit_time, '%Y%m%d')
        ORDER BY f.visit_time DESC
    </select>

    <select id="getConsultantHomePageTimes" resultType="com.huiju.estateagents.entity.TaPerson">
        SELECT
            t.*,
            f.visit_time as visit_time
        FROM
            ta_person t
        INNER JOIN ta_person_visit_record f ON f.org_id = #{orgId} AND f.person_id = t.person_id
        INNER JOIN ta_person s on s.person_id = f.target_id
        WHERE t.org_id = #{orgId}

<!--        为了与存储过程一致 -->
<!--            AND t.`status` = 1-->
        <if test="buildingId != null and buildingId !=''">
            AND f.building_id = #{buildingId}
        </if>
            AND f.event_type = 'card'
            AND f.visit_time BETWEEN #{startDate} and #{endDate}
            AND s.user_id = #{userId}

<!--        为了与存储过程一致 -->
<!--            AND s.`status` = 1-->
        <if test="personId != null and personId !=''">
            AND t.person_id = #{personId}
        </if>
        ORDER BY f.visit_time DESC
    </select>

    <select id="getConsultantChatPersons" resultType="com.huiju.estateagents.entity.TaPerson">
        SELECT
            t.*,
            max(h.create_date) AS visit_time
        FROM
            ta_person t
        INNER JOIN ta_chat h ON h.send_person = t.person_id
        INNER JOIN ta_person s ON s.person_id = h.receive_person
        WHERE t.org_id = #{orgId}

<!--        为了与存储过程一致 -->
<!--            AND t.`status` = 1-->
            AND h.create_date BETWEEN #{startDate} and #{endDate}
            AND s.user_id = #{userId}

<!--        为了与存储过程一致 -->
<!--            AND s.`status` = 1-->
        GROUP BY
            t.person_id, DATE_FORMAT(h.create_date, '%Y%m%d')
        ORDER BY
            h.create_date DESC
    </select>

    <select id="getConsultantFavor" resultType="com.huiju.estateagents.entity.TaPerson">
        SELECT
            t.*,
            i.create_date AS visit_time
        FROM
            ta_person t
        INNER JOIN ta_favor i ON i.person_id = t.person_id
        INNER JOIN ta_person s ON s.person_id = i.be_favor
        WHERE t.org_id = #{orgId}

<!--        为了与存储过程一致 -->
<!--            AND t.`status` = 1-->
            AND i.tagert_type = 'consultant'
            AND i.create_date BETWEEN #{startDate} and #{endDate}
            AND s.user_id = #{userId}

<!--        为了与存储过程一致 -->
<!--            AND s.`status` = 1-->
        ORDER BY
            i.create_date DESC
    </select>

    <select id="getConsultantShareTargets" resultType="com.huiju.estateagents.entity.TaPerson">
        SELECT
            s.*,
            t.tagert_type as target_type,
            t.target_name,
            t.be_share as target_id,
            t.create_date as visit_time
        FROM
            ta_share_count t
        INNER JOIN ta_person s ON s.person_id = t.person_id
        WHERE
            t.org_id = #{orgId}

        <!-- 没有设置 targetType 查询条件, 则只查询固定的几个 -->
        <if test="targetType == null or targetType ==''">
            AND (
            t.tagert_type IN ( 'consultant', 'project', 'activity', 'group', 'h5', 'help', 'news', 'main' )
            OR t.tagert_type like 'house%'
            OR t.tagert_type like 'live%'
            )
        </if>
        <if test="targetType != null and targetType =='card_share'">
            AND t.tagert_type = 'consultant'
        </if>
        <if test="targetType != null and targetType !='' and targetType != 'card_share'">
            AND t.tagert_type like concat(#{targetType}, '%')
        </if>

            AND t.create_date BETWEEN #{startDate} and #{endDate}
        <if test="targetName != null and targetName !=''">
            AND t.target_name LIKE concat('%', #{targetName}, '%')
        </if>
            AND s.user_id = #{userId}

<!--        为了与存储过程一致 -->
<!--            AND s.`status` = 1-->
        ORDER BY t.create_date DESC
    </select>

    <select id="getConsultantSharePersons" resultType="com.huiju.estateagents.entity.TaPerson">
        SELECT
            t.*,
            count(*) as visit_times,
            max(e.create_date) as visit_time
        FROM ta_share_person_from e
        INNER JOIN ta_person t ON t.person_id = e.person_id
        WHERE e.org_id = #{orgId}
            AND e.share_person = #{userId}
            AND e.create_date BETWEEN #{startDate} and #{endDate}
            AND (
                e.target_type IN ( 'card_share', 'building_share', 'dynamic_share', 'group_share', 'h5_share', 'help_share', 'house_share', 'live_share', 'news_share', 'poster' )
                OR ( e.target_type = 'share' AND e.target_id = 'index' )
            )

        <if test="buildingId != null and buildingId !=''">
            AND e.building_id = #{buildingId}
        </if>

<!--            AND t.`status` = 1-->
        GROUP BY t.person_id, DATE_FORMAT(e.create_date, '%Y%m%d')
        ORDER BY e.create_date DESC
    </select>

    <select id="getConsultantShareTimes" resultType="com.huiju.estateagents.entity.TaPerson">
        SELECT
            t.*,
            e.create_date as visit_time,
            e.target_type,
            e.target_id,
            e.target_name
        FROM ta_share_person_from e
        INNER JOIN ta_person t ON t.person_id = e.person_id
        WHERE e.org_id = #{orgId}
            AND e.share_person = #{userId}

        <!-- 没有设置 targetType 查询条件, 则只查询固定的几个 -->
        <if test="targetType == null or targetType ==''">
            AND (
            e.target_type IN ( 'card_share', 'building_share', 'dynamic_share', 'group_share', 'h5_share', 'help_share', 'house_share', 'live_share', 'news_share', 'poster' )
            OR ( e.target_type = 'share' AND e.target_id = 'index' )
            )
        </if>
        <if test="targetType != null and targetType !=''">
            AND e.target_type = #{targetType}
        </if>
        <if test="targetType != null and targetType =='share'">
            AND e.target_type = 'share'
            AND e.target_id = 'index'
        </if>
        <if test="targetName != null and targetName !=''">
            AND e.target_name like concat('%', #{targetName} ,'%')
        </if>

            AND e.create_date BETWEEN #{startDate} and #{endDate}

        <if test="buildingId != null and buildingId !=''">
            AND e.building_id = #{buildingId}
        </if>

        <if test="personId != null and personId !=''">
            AND t.person_id = #{personId}
        </if>
        <if test="userName != null and userName !=''">
            AND IFNULL(t.name, t.nickname) like concat('%', #{userName} ,'%')
        </if>
            <!--            AND t.`status` = 1-->
        ORDER BY e.create_date DESC
    </select>
</mapper>
