<?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.statistic.mapper.TsCustomerGenderStatisticMapper">

    <select id="getNewCustomerSexCount" resultType="com.huiju.estateagents.statistic.entity.GenderStatistic">
        SELECT
            org_id,realty_consultant,
            sum(if(sex = 1,1,0)) as man_sum,
            sum(if(sex = 1,1,0))/count(*) as man_pct,
            sum(if(sex = 2,1,0)) as woman_sum,
            sum(if(sex = 2,1,0))/count(*) as woman_pct,
            sum(if(sex != 1 and sex != 2,1,0)) as unknown_sum,
            sum(if(sex != 1 and sex != 2,1,0))/count(*) as unknown_pct
        FROM
            ta_recommend_customer
        WHERE
            verify_status = 1
            GROUP BY org_id,realty_consultant
    </select>
    <select id="getFollowUpSexCount" resultType="com.huiju.estateagents.statistic.entity.GenderStatistic">
        SELECT
            t.org_id,
            t.follower_id as realty_consultant,
            sum( IF ( t.customer_sex = 1, 1, 0 ) ) AS manSum,
            sum( IF ( t.customer_sex = 1, 1, 0 ) ) / count( * ) AS manPct,
            sum( IF ( t.customer_sex = 2, 1, 0 ) ) AS womanSum,
            sum( IF ( t.customer_sex = 2, 1, 0 ) ) / count( * ) AS womanPct,
            sum( IF ( t.customer_sex != 1 AND t.customer_sex != 2, 1, 0 ) ) AS unknownSum,
            sum( IF ( t.customer_sex != 1 AND t.customer_sex != 2, 1, 0 ) ) / count( * ) AS unknownPct
        FROM
            ( SELECT * FROM ta_customer_follow_up_record GROUP BY customer_id ) t
        GROUP BY
            t.org_id,t.follower_id
    </select>
    <select id="getVisiteSexCount" resultType="com.huiju.estateagents.statistic.entity.GenderStatistic">
        SELECT
            d.org_id,
            d.realty_consultant,
            sum( IF ( d.sex = 1, 1, 0 ) ) AS manSum,
            sum( IF ( d.sex = 1, 1, 0 ) ) / count( * ) AS manPct,
            sum( IF ( d.sex = 2, 1, 0 ) ) AS womanSum,
            sum( IF ( d.sex = 2, 1, 0 ) ) / count( * ) AS womanPct,
            sum( IF ( d.sex != 1 AND d.sex != 2, 1, 0 ) ) AS unknownSum,
            sum( IF ( d.sex != 1 AND d.sex != 2, 1, 0 ) ) / count( * ) AS unknownPct
        FROM
            (
                SELECT
                    t.enlist_id,
                    c.*
                FROM
                    ta_activity_dynamic_enlist t
                    LEFT JOIN ta_recommend_customer c ON t.person_id = c.person_id
                    AND t.building_id = c.building_id
                    AND t.org_id = c.org_id
                    AND c.verify_status = 1
                WHERE
                    t.is_checkin = 1
                GROUP BY
                    c.customer_id
            ) d
        GROUP BY
            d.org_id,d.realty_consultant
    </select>
</mapper>
