<?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="cn.com.ctop.kuaishou.modules.report.mapper.KuaishouAudienceReportDailyAccountMapper">

    <select id="queryPageLists" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        account_id as accountId,
        province,
        age_segment as ageSegment,
        business_interest_tags as businessInterestTags,
        city,
        client,
        gender,
        SUM(charge) as charge,
        SUM(activation) as activation,
        SUM(event_next_day_stay) as eventNextDayStay,
        IFNULL(ROUND(SUM(event_next_day_stay)/SUM(activation)*100,2),0) as eventNextDayStayRatio
        FROM ctop_kuaishou_audience_report_daily_account
        where 1=1
        <if test="accountIds != null and accountIds.size() > 0">
            and account_id in
            <foreach collection="accountIds" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="startTime != null">
            AND stat_date &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND stat_date &lt;= #{endTime}
        </if>
        <if test="audienceType == 'province' ">
            AND audience_type = #{audienceType}
            GROUP BY province
        </if>
        <if test="audienceType == 'city' ">
            AND audience_type = #{audienceType}
            GROUP BY city
        </if>
        <if test="audienceType == 'gender' ">
            AND audience_type = #{audienceType}
            GROUP BY gender
        </if>
        <if test="audienceType == 'ageSegment' ">
            AND audience_type = #{audienceType}
            GROUP BY age_segment
        </if>
        <if test="audienceType == 'clientId' ">
            AND audience_type = #{audienceType}
            GROUP BY client
        </if>
        <if test="audienceType == 'businessInterestTags' ">
            AND audience_type = #{audienceType}
            GROUP BY business_interest_tags
        </if>
        <if test="orderType == 'charge' ">
            ORDER BY charge
        </if>
        <if test="orderType == 'activation' ">
            ORDER BY activation
        </if>
        <if test="orderType == 'eventNextDayStayRatio'">
            ORDER BY eventNextDayStayRatio
        </if>
        <if test="orderType == 'eventNextDayStay'">
            ORDER BY eventNextDayStay
        </if>
        <if test="upOrDown == 'desc' || upOrDown == 'DESC'">
            DESC
        </if>
    </select>


    <select id="queryAllReportForm" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        stat_date as statDate,
        account_id as accountId,
        province,
        age_segment as ageSegment,
        business_interest_tags as businessInterestTags ,
        city,
        client,
        gender,
        SUM(photo_show) as photoShow,
        SUM(photo_click) as photoClick,
        SUM(aclick) as aclick,
        SUM(bclick) as bclick,
        IFNULL(ROUND(SUM(photo_click)/SUM(photo_show)*100,2),0) as photoClickRatio,
        IFNULL(ROUND(SUM(bclick)/SUM(aclick)*100,2),0) as actionRatio
        FROM ctop_kuaishou_audience_report_daily_account
        where 1=1
        <if test="accountIds != null and accountIds.size() > 0">
            and account_id in
            <foreach collection="accountIds" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="startTime != null">
            AND stat_date &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND stat_date &lt;= #{endTime}
        </if>
        <if test="audienceType == 'province' ">
            AND audience_type = #{audienceType}
            GROUP BY province
        </if>
        <if test="audienceType == 'city' ">
            AND audience_type = #{audienceType}
            GROUP BY city
        </if>
        <if test="audienceType == 'gender' ">
            AND audience_type = #{audienceType}
            GROUP BY gender
        </if>
        <if test="audienceType == 'ageSegment' ">
            AND audience_type = #{audienceType}
            GROUP BY age_segment
        </if>
        <if test="audienceType == 'clientId' ">
            AND audience_type = #{audienceType}
            GROUP BY client
        </if>
        <if test="audienceType == 'businessInterestTags' ">
            AND audience_type = #{audienceType}
            GROUP BY business_interest_tags
        </if>
    </select>

    <select id="querybusinessTypeReportForm" resultType="com.alibaba.fastjson.JSONObject">
        (SELECT
        stat_date as statDate,
        account_id as accountId,
        business_interest_tags as businessInterestTags ,
        SUM(photo_show) as photoShow,
        SUM(photo_click) as photoClick,
        sum(charge) as totalCost,
        SUM(aclick) as aclick,
        SUM(bclick) as bclick,
        IFNULL(ROUND(SUM(photo_click)/SUM(photo_show)*100,2),0) as photoClickRatio,
        IFNULL(ROUND(SUM(bclick)/SUM(aclick)*100,2),0) as actionRatio
        FROM ctop_kuaishou_audience_report_daily_account
        where 1=1
        <if test="busTypes !=null and busTypes.size() == 9 ">
            AND business_interest_tags != '其他'
        </if>
        <if test="accountIds != null and accountIds.size() > 0">
            and account_id in
            <foreach collection="accountIds" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="startTime != null">
            AND stat_date &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND stat_date &lt;= #{endTime}
        </if>
        AND audience_type = 'businessInterestTags'
        GROUP BY business_interest_tags
        ORDER BY totalCost desc
        limit 9
        )
        <if test="busTypes !=null and busTypes.size() == 9 ">
            union all
            SELECT
            statDate as statDate,
            accountId as accountId,
            businessInterestTags as businessInterestTags,
            SUM(photoShow) as photoShow,
            SUM(photoClick) as photoClick,
            sum(charge) AS totalCost,
            SUM(aclick) as aclick,
            SUM(bclick) as bclick,
            ROUND(SUM(photoClick)/SUM(photoShow)*100,2) as photoClickRatio,
            ROUND(SUM(bclick)/SUM(aclick)*100,2) as actionRatio
            FROM (
            SELECT
            stat_date as statDate,
            account_id as accountId,
            '其他' as businessInterestTags,
            SUM(photo_show) as photoShow,
            SUM(photo_click) as photoClick,
            sum(charge) AS charge,
            SUM(aclick) as aclick,
            SUM(bclick) as bclick
            FROM ctop_kuaishou_audience_report_daily_account t
            where 1=1
            <if test="accountIds != null and accountIds.size() > 0">
                and account_id in
                <foreach collection="accountIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="startTime != null">
                AND stat_date &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                AND stat_date &lt;= #{endTime}
            </if>
            AND audience_type = 'businessInterestTags'
            AND business_interest_tags not in
            <foreach collection="busTypes" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
            GROUP BY business_interest_tags
            ) t1
        </if>
    </select>


    <select id="querybusinessTypeCount" resultType="java.lang.String">
        SELECT businessInterestTags FROM (
        SELECT
        sum(charge) as totalCost,
        business_interest_tags as businessInterestTags
        FROM ctop_kuaishou_audience_report_daily_account
        where audience_type = 'businessInterestTags'
        AND business_interest_tags != '其他'
        <if test="accountIds != null and accountIds.size() > 0">
            and account_id in
            <foreach collection="accountIds" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="startTime != null">
            AND stat_date &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            AND stat_date &lt;= #{endTime}
        </if>
        GROUP BY business_interest_tags
        ORDER BY totalCost desc
        limit 9
        ) t1
    </select>
</mapper>
