<?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.yunzhi.marketing.xlk.mapper.PcStatisticsMapper">
    <select id="selectAllPerson" resultType="java.lang.Integer">
        select count(1) from ta_person where org_id = #{params.orgId}
    </select>

    <select id="selectRegisterPerson" resultType="java.lang.Integer">
        select count(1) from ta_person where  org_id = #{params.orgId} and phone is not null
    </select>
    <select id="selectTodayVisitNum" resultType="java.lang.Integer">
        SELECT
            COUNT(1)
        FROM
            ta_person_visit_record tp
        where tp.org_id = #{params.orgId} AND
            tp.event  = 'start'
        AND DATE_FORMAT(tp.visit_time,'%Y-%m-%d') = DATE_FORMAT(#{params.todayTime},'%Y-%m-%d')
    </select>
    <select id="selectTodayAddPerson" resultType="java.lang.Integer">
        SELECT
            COUNT(1)
        FROM
        ta_person tp
        where tp.org_id = #{params.orgId}
        AND DATE_FORMAT(tp.create_date,'%Y-%m-%d') = DATE_FORMAT(#{params.todayTime},'%Y-%m-%d')
    </select>
    <select id="selectChannelNum" resultType="java.lang.Integer">
        SELECT
            COUNT(1)
        FROM
            ta_channel t
        WHERE
            t.org_id = #{params.orgId}
        <if test="params.institutionIds != null and params.institutionIds.size > 0">
            AND t.institution_id in
            <foreach collection="params.institutionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>
    <select id="selectEstateAgentNum" resultType="java.lang.Integer">
        SELECT
            COUNT(1)
        FROM
        ta_person tp
        where tp.org_id = #{params.orgId}
        and tp.person_type in ('estate agent','channel agent')
    </select>
    <select id="getUserOriginStatistics" resultType="com.yunzhi.marketing.xlk.vo.PcStatisticsVO">
        SELECT
            sum(if(recommend_person_type = 'Realty Consultant', 1, 0)) as consultant_num,
            sum(if(recommend_person_type = 'customer', 1, 0)) as customer_num,
            sum(if(recommend_person_type in ('estate agent','channel agent'), 1, 0)) as estage_num,
            count(1) as all_person_num
        from ta_person
        where org_id = #{params.orgId}
    </select>
    <select id="getuserGenderStatistics" resultType="com.yunzhi.marketing.xlk.vo.PcStatisticsGenderVO">
        SELECT
            sum(if(gender = '1', 1, 0)) as male_num,
            sum(if(gender = '2', 1, 0)) as female_num
        from ta_person
    </select>
    <select id="getGkLeaderboard" resultType="com.yunzhi.marketing.xlk.vo.PcStatisticsLeaderboardVO">
        SELECT
            count(1) as num,
            b.building_id,
            d.building_name
        FROM
            ta_recommend_customer b
            LEFT JOIN ta_building d ON b.building_id = d.building_id
        WHERE
            b.STATUS > 0
            AND b.org_id = #{params.orgId}
            AND b.verify_status = 1
            AND ( b.realty_consultant IS NULL OR b.realty_consultant = '' )
            AND b.building_id IS NOT NULL
            <if test="params.cityId != null">
                and d.city_id = #{params.cityId}
            </if>
            <if test="params.startTime != null">
                and  TO_DAYS(b.create_date) >= TO_DAYS(#{params.startTime})
            </if>
            <if test="params.endTime != null">
                and  TO_DAYS(b.create_date) &lt;= TO_DAYS(#{params.endTime})
            </if>
            <if test="params.institutionIds != null and params.institutionIds.size > 0">
                AND b.institution_id in
                <foreach collection="params.institutionIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        GROUP BY b.building_id
        ORDER BY num desc
        LIMIT 6
    </select>
    <select id="getSkLeaderboard" resultType="com.yunzhi.marketing.xlk.vo.PcStatisticsLeaderboardVO">
        SELECT
            count(1) as num,
            b.building_id,
            d.building_name
        FROM
            ta_recommend_customer b
        LEFT JOIN ta_building d ON b.building_id = d.building_id
        WHERE
            b.STATUS > 0
        AND b.org_id = #{params.orgId}
        AND b.verify_status = 1
        AND b.realty_consultant IS NOT NULL
        AND b.realty_consultant != ''
        AND b.building_id IS NOT NULL
        <if test="params.cityId != null">
            and d.city_id = #{params.cityId}
        </if>
        <if test="params.startTime != null">
            and  TO_DAYS(b.create_date) >= TO_DAYS(#{params.startTime})
        </if>
        <if test="params.endTime != null">
            and  TO_DAYS(b.create_date) &lt;= TO_DAYS(#{params.endTime})
        </if>
        <if test="params.institutionIds != null and params.institutionIds.size > 0">
            AND b.institution_id in
            <foreach collection="params.institutionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        GROUP BY b.building_id
        ORDER BY num desc
        LIMIT 6
    </select>
    <select id="getVisitNumLeaderboard" resultType="com.yunzhi.marketing.xlk.vo.PcStatisticsLeaderboardVO">
        SELECT
            count( * ) AS num,
            a.be_uv AS building_id,
            d.building_name
        FROM
            ta_uv a
            LEFT JOIN ta_building d ON a.be_uv = d.building_id
        WHERE
            a.tagert_type = 'project'
            AND d.org_id = #{params.orgId}
            AND a.be_uv != "undefined"
        <if test="params.cityId != null">
            and d.city_id = #{params.cityId}
        </if>
        <if test="params.startTime != null">
            and  TO_DAYS(a.create_date) >= TO_DAYS(#{params.startTime})
        </if>
        <if test="params.endTime != null">
            and  TO_DAYS(a.create_date) &lt;= TO_DAYS(#{params.endTime})
        </if>
        <if test="params.institutionIds != null and params.institutionIds.size > 0">
            AND d.institution_id in
            <foreach collection="params.institutionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        GROUP BY
            a.be_uv
        ORDER BY
            num DESC
            LIMIT 6
    </select>
    <select id="getSuccessLeaderboard" resultType="com.yunzhi.marketing.xlk.vo.PcStatisticsLeaderboardVO">
        SELECT
            count(1) as num,
            b.building_id,
            d.building_name
        FROM
            ta_recommend_customer b
        LEFT JOIN ta_building d ON b.building_id = d.building_id
        LEFT JOIN xlk_institution i ON b.institution_id = i.institution_id
        WHERE
            b.STATUS > 3
        AND b.org_id = #{params.orgId}
        AND b.verify_status = 1
        AND b.building_id IS NOT NULL
        <if test="params.cityId != null">
            and d.city_id = #{params.cityId}
        </if>
        <if test="params.startTime != null">
            and  TO_DAYS(b.create_date) >= TO_DAYS(#{params.startTime})
        </if>
        <if test="params.endTime != null">
            and  TO_DAYS(b.create_date) &lt;= TO_DAYS(#{params.endTime})
        </if>
        <if test="params.institutionIds != null and params.institutionIds.size > 0">
            AND b.institution_id in
            <foreach collection="params.institutionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        GROUP BY b.building_id
        ORDER BY num desc
        LIMIT 6
    </select>
    <select id="selectActivityList" resultType="com.yunzhi.marketing.xlk.vo.PcStatisticsActivityVO">
        SELECT
            p.visit_num,
            v.visit_person_num,
            d.sign_num,
            t.title,
            t.dynamic_id,
            t.building_id
        FROM
        ta_building_dynamic t
        LEFT JOIN (
            SELECT
            count( * ) AS visit_num,
            a.target_id
            FROM
            ta_person_visit_record a
            WHERE
            a.event_type = 'activity'
            AND a.`event` = 'detail'
            AND a.org_id = #{params.orgId}
            GROUP BY
            a.target_id
        ) p ON t.dynamic_id = p.target_id
        LEFT JOIN (
        SELECT
        count( DISTINCT ( a.person_id ) ) AS visit_person_num,
        a.target_id
        FROM
        ta_person_visit_record a
        WHERE
        a.event_type = 'activity'
        AND a.`event` = 'detail'
        AND a.org_id = #{params.orgId}
        GROUP BY
        a.target_id
        ) v ON t.dynamic_id = v.target_id
        LEFT JOIN ( SELECT COUNT( 1 ) AS sign_num, dynamic_id FROM ta_activity_dynamic_enlist GROUP BY dynamic_id ) d ON t.dynamic_id = d.dynamic_id
        LEFT JOIN xlk_institution i ON t.institution_id = i.institution_id
        where
        t.org_id = #{params.orgId}
        <if test="params.startTime != null">
            and  TO_DAYS(t.create_date) >= TO_DAYS(#{params.startTime})
        </if>
        <if test="params.endTime != null">
            and  TO_DAYS(t.create_date) &lt;= TO_DAYS(#{params.endTime})
        </if>
        <if test="params.buildingId != null and params.buildingId != ''">
            and  t.building_id = #{params.buildingId}
        </if>
        <if test="params.institutionIds != null and params.institutionIds.size > 0">
            AND t.institution_id in
            <foreach collection="params.institutionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        order by
        <if test="params.sortField != null and params.sortField != ''">
            ${params.sortField} ${params.sortType},
        </if>
        t.create_date desc
    </select>
</mapper>