<?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="org.jeecg.ctop.material.mapper.MaterialStareMapper">

    <!-- 快手 时报  -->
    <select id="getKuaiShouCostHourlyByUserIdList" resultType="Map">
        SELECT
        CONCAT(t.`hour`,':00') `hour`,
        ROUND( SUM( charge ), 3 ) AS totalCost
        FROM
        application.kuaishou_account_report_hourly_dw t
        where
        stat_date &gt;= #{startDate}
        AND stat_date &lt;= #{endDate}
        <if test='list!=null'>
            AND t.user_id IN
            <foreach collection="list" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY t.`hour`
        ORDER BY t.`hour`
    </select>




    <!-- 快手-查询时报 消耗 前5 -->
    <select id="getKuaiShouCostByTop5HourList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        user_id,
        SUM( charge ) totalCost,
        (SELECT realname from `jeecg-boot`.sys_user u where u.id = user_id) as userName
        FROM
        application.kuaishou_account_report_hourly_dw
        WHERE
        stat_date &gt;= #{startDate}
        AND stat_date &lt;= #{endDate}
        <if test='list!=null'>
            AND user_id IN
            <foreach collection="list" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY user_id ORDER BY SUM(charge) DESC LIMIT 5
    </select>




<!--快手 -日报  -->
    <select id="getKuaiShouCostDailyByUserIdList" resultType="Map">
        SELECT DATE_FORMAT(t.stat_date,'%Y-%m-%d') as dateTime,
        ROUND(SUM(charge),2) as totalCost
        FROM application.kuaishou_account_report_daily_dw t
        WHERE
        t.stat_date &gt;= #{startDate}
        AND t.stat_date &lt;= #{endDate}
        <if test='list!=null'>
            AND t.user_id IN
            <foreach collection="list" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY t.stat_date
        ORDER BY t.stat_date;
    </select>


    <!-- 快手-查询日报 消耗 前5 -->
    <select id="getKuaiShouCostByTop5DailyList" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        user_id,
        SUM( charge ) totalCost,
        (SELECT realname from `jeecg-boot`.sys_user u where u.id = user_id) as userName
        FROM
        application.kuaishou_account_report_daily_dw
        WHERE
        stat_date &gt;= #{startDate}
        AND stat_date &lt;= #{endDate}
        <if test='list!=null'>
            AND user_id IN
            <foreach collection="list" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY user_id ORDER BY SUM(charge) DESC LIMIT 5
    </select>



    <!-- 快手 时报总消耗 及 账户数量 -->
    <select id="getKuaiShouTotalCost" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ROUND( SUM( charge ), 3 ) AS totalCost,
        COUNT(DISTINCT account_id) accountNum
        FROM
        application.kuaishou_account_report_hourly_dw
        where 1 = 1
        <if test="startDate != null and startDate != '' ">
            and stat_date &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != '' ">
            and stat_date &lt;= #{endDate}
        </if>
        <if test="nowHour != null and nowHour != '' ">
            AND hour &lt;= #{nowHour}
        </if>
        <if test='list!=null'>
            AND user_id IN
            <foreach collection="list" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>
    </select>


    <!-- 快手 日报总消耗 及 账户数量 -->
    <select id="getKuaiShouTotalCostDaily" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ROUND( SUM( charge ), 3 ) AS totalCost,
        COUNT(DISTINCT account_id) accountNum
        FROM
        application.kuaishou_account_report_daily_dw
        where charge > 0
        <if test="startDate != null and startDate != '' ">
            and stat_date &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != '' ">
            and stat_date &lt;= #{endDate}
        </if>
        <if test='list!=null'>
            AND user_id IN
            <foreach collection="list" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>
    </select>

    <!-- 查看 快手 有效 计划 组-->
    <select id="getKuaiShouAccountBaseByUserId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT abi.account_id ,
        SUM(abi.new_effect_num) as newEffectNum,
        SUM(abi.new_campaign_num) as newCampaignNum,
        SUM(abi.new_unit_num) as newUnitNum,
        SUM( abi.new_video_num ) AS newVideoNum,
        SUM( abi.new_hot_num ) AS newHotNum
        from application.app_kuaishou_account_base_info_d abi,
        `jeecg-boot`.ctop_user_allocation u
        WHERE abi.account_id = u.account_id
        and u.account_status = 0
        <if test="startDate != null and startDate != '' ">
            and abi.stat_date &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != '' ">
            and abi.stat_date &lt;= #{endDate}
        </if>
        <if test="userId != null and userId != '' ">
            and abi.user_id = #{userId}
        </if>
        <if test="projectId != null and projectId != '' ">
            and abi.project_id = #{projectId}
        </if>
        <if test="accountId != null and accountId != '' ">
            and abi.account_id = #{accountId}
        </if>

    </select>



    <!-- 快手 快手-查询账户数据信息 -->
    <select id="getKuaiShouOperateInfo" resultType="org.jeecg.ctop.material.entity.vo.KuaiShouOperateVo">
        SELECT
        SUM( d.charge ) totalCost,
        COUNT(DISTINCT d.account_id) accountNum,
        d.user_id as userId,
        (SELECT u.realname from `jeecg-boot`.sys_user u where u.id = d.user_id) as userName
        FROM
        application.kuaishou_account_report_daily_dw d
        LEFT JOIN `jeecg-boot`.ctop_user_allocation u on d.account_id = u.account_id
        WHERE
        d.charge > 0
        and u.account_status = 0
        <if test="startDate != null and startDate != '' ">
            and d.stat_date &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != '' ">
            and d.stat_date &lt;= #{endDate}
        </if>
        <if test='list!=null'>
            AND d.user_id IN
            <foreach collection="list" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY d.user_id
        ORDER BY SUM( d.charge ) DESC
    </select>

    <!-- 快手 -查询项目数据信息 -->
    <select id="getKuaiShouOperateProjectInfo" resultType="org.jeecg.ctop.material.entity.vo.KuaiShouOperateProjectVo">
        SELECT
        SUM( d.charge ) AS totalCost,
        COUNT( DISTINCT d.account_id ) AS accountNum,
        d.project_id AS projectId,
        d.project_name AS projectName,
        d.user_id AS userId,
        COUNT( DISTINCT d.user_id ) AS userNum,
        IFNULL(SUM( d.activation ),0)  AS activation,
        IF(SUM( d.charge ) / SUM( d.activation ) > 0.0001,ROUND( SUM( d.charge ) / SUM( d.activation ), 2 ),0) AS activationPrice,
        IF(SUM( d.event_next_day_stay ) / SUM( d.activation ) > 0.0001,CONCAT(ROUND(SUM( d.event_next_day_stay ) / SUM( d.activation ) * 100, 2 ),'%'),0)AS leave,
        IFNULL(SUM( d.event_pay ),0)  AS eventPay,
        IF(SUM( d.charge ) / SUM( d.event_pay ) > 0.0001,ROUND( SUM( d.charge ) / SUM( d.event_pay ), 2 ),0) AS eventPayCost,
        IF(SUM( d.event_pay ) / ( SUM( d.activation ) + SUM( d.form_count )) > 0.0001,CONCAT(ROUND( SUM( d.event_pay ) / ( SUM( d.activation ) + SUM( d.form_count )) * 100, 2 ),'%'),0) AS eventPayRate,
        IFNULL(SUM( d.event_new_user_pay ),0)  AS eventNewUserPay,
        IF(SUM( d.charge ) / SUM( d.event_new_user_pay) > 0.0001,ROUND( SUM( d.charge ) / SUM( d.event_new_user_pay ), 2 ),0)AS eventNewUserPayCost,
        IF(SUM( d.event_new_user_pay ) / ( SUM( d.activation ) + SUM( d.form_count )) > 0.0001 ,CONCAT(ROUND( SUM( d.event_new_user_pay ) / ( SUM( d.activation ) + SUM( d.form_count )) * 100, 2 ),'%'),0 ) AS eventNewUserPayRate,
        IFNULL(SUM( d.event_app_invoked ),0)  AS eventAppInvoked,
        IFNULL(ROUND( SUM( d.charge ) / SUM( d.event_app_invoked ), 2 ),0) AS eventAppInvokedCost,
        IFNULL(SUM( d.event_register ),0)  AS eventRegister,
        IFNULL(ROUND( SUM( d.charge ) / SUM( d.event_register ), 2 ),0) AS eventRegisterCost,
        IFNULL(SUM( d.key_action ),0)  AS keyAction,
        IFNULL(ROUND( SUM( d.charge ) / SUM( d.key_action ), 2 ),0) AS keyActionCost,
        IF(SUM( d.key_action )/ SUM( d.activation ) > 0.0001,CONCAT( ROUND(SUM( d.key_action )/ SUM( d.activation ) * 100 , 2 ), '%' ),0) AS keyActionRate,
        IFNULL(SUM( d.event_pay_purchase_amount_first_day ),0)  AS eventPayPurchaseAmountFirstDay,
        IFNULL(SUM( d.event_pay_first_day_roi ),0)  AS eventPayFirstDayRoi,
        IFNULL(SUM( d.form_count ),0) AS formCount,
        IFNULL(ROUND( SUM( d.charge ) / SUM( d.form_count ), 2 ),0) AS formCountPrice
        FROM
        application.kuaishou_account_report_daily_dw d
        LEFT JOIN `jeecg-boot`.ctop_user_allocation u on d.account_id = u.account_id
        WHERE
        d.charge > 0
        and u.account_status = 0
        <if test="startDate != null and startDate != '' ">
            and d.stat_date &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != '' ">
            and d.stat_date &lt;= #{endDate}
        </if>
        <if test="projectId != null and projectId != '' ">
            and d.project_id = #{projectId}
        </if>
        <if test='list!=null'>
            AND d.user_id IN
            <foreach collection="list" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY d.project_id
        ORDER BY SUM( d.charge ) DESC
    </select>



    <!-- 快手 -查询 账户 数据信息  -->
    <select id="getKuaiShouOperateAccountInfo" resultType="org.jeecg.ctop.material.entity.vo.KuaiShouOperateAccountVo">
        SELECT
        SUM( d.charge ) AS totalCost,
        d.account_id as accountId,
        d.account_name as accountName,
        d.project_id AS projectId,
        d.project_name AS projectName,
        d.user_id AS userId,
        COUNT( DISTINCT d.user_id ) AS userNum,
        ( SELECT u.realname FROM `jeecg-boot`.sys_user u WHERE u.id = d.user_id ) AS userName,
        (SELECT u.warning_amount FROM `jeecg-boot`.ctop_user_allocation u where u.account_id = d.account_id) as warningAmount,
        (SELECT u.balance FROM media_api.kuaishou_advertiser_fund_get u where u.advertiser_id = d.account_id ORDER BY stat_date DESC limit 1 ) as balance,
        IFNULL(SUM( d.activation ),0) AS activation,
        IFNULL(ROUND( SUM( d.charge ) / SUM( d.activation ), 2 ),0) AS activationPrice,
        IF(SUM( d.event_next_day_stay ) / SUM( d.activation ) > 0.0001,CONCAT(ROUND(SUM( d.event_next_day_stay ) / SUM( d.activation ) * 100, 2 ),'%'),0) AS leave,
        IFNULL(SUM( d.event_pay ),0) AS eventPay,
        IFNULL(ROUND( SUM( d.charge ) / SUM( d.event_pay ), 2 ),0) AS eventPayCost,
        IF(SUM( d.event_pay ) / ( SUM( d.activation ) + SUM( d.form_count )) > 0.0001,CONCAT(ROUND( SUM( d.event_pay ) / ( SUM( d.activation ) + SUM( d.form_count )) * 100, 2 ),'%'),0) AS eventPayRate,
        IFNULL(SUM( d.event_new_user_pay ),0) AS eventNewUserPay,
        IFNULL(ROUND( SUM( d.charge ) / SUM( d.event_new_user_pay ), 2 ),0) AS eventNewUserPayCost,
        IFNULL(ROUND( SUM( d.event_new_user_pay ) / ( SUM( d.activation ) + SUM( d.form_count )), 2 ),0) AS eventNewUserPayRate,
        IFNULL(SUM( d.event_app_invoked ),0) AS eventAppInvoked,
        IFNULL(ROUND( SUM( d.charge ) / SUM( d.event_app_invoked ), 2 ),0) AS eventAppInvokedCost,
        IFNULL(SUM( d.event_register ),0) AS eventRegister,
        IFNULL(ROUND( SUM( d.charge ) / SUM( d.event_register ), 2 ),0) AS eventRegisterCost,
        IFNULL(SUM( d.key_action ),0) AS keyAction,
        IFNULL(ROUND( SUM( d.charge ) / SUM( d.key_action ), 2 ),0) AS keyActionCost,
        IF(SUM( d.key_action )/ SUM( d.activation ) > 0.0001,CONCAT( ROUND(SUM( d.key_action )/ SUM( d.activation ) * 100 , 2 ), '%' ),0) AS keyActionRate,
        IFNULL(SUM( d.event_pay_purchase_amount_first_day ),0) AS eventPayPurchaseAmountFirstDay,
        IFNULL(SUM( d.event_pay_first_day_roi ),0) AS eventPayFirstDayRoi,
        IFNULL(SUM( d.form_count ),0) AS formCount,
        IFNULL(ROUND( SUM( d.charge ) / SUM( d.form_count ), 2 ),0) AS formCountPrice,
        IFNULL(SUM(d.`show`),0) as photoShow,
        IFNULL(SUM(d.photo_click),0) as photoClick
        FROM
        application.kuaishou_account_report_daily_dw d
        LEFT JOIN `jeecg-boot`.ctop_user_allocation u on d.account_id = u.account_id
        WHERE
        d.charge > 0
        and u.account_status = 0
        <if test="startDate != null and startDate != '' ">
            and d.stat_date &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != '' ">
            and d.stat_date &lt;= #{endDate}
        </if>
        <if test="projectId != null and projectId != '' ">
            and d.project_id = #{projectId}
        </if>
        <if test="accountId != null and accountId != '' ">
            and d.account_id = #{accountId}
        </if>
        <if test='list!=null'>
            AND d.user_id IN
            <foreach collection="list" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY d.account_id
        ORDER BY SUM( d.charge ) DESC
    </select>



    <!-- 快手 账户 余额 -->
    <select id="getKuaiShouAccountBalance" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            advertiser_id as accountId,
            balance
        FROM media_api.kuaishou_advertiser_fund_get
        where advertiser_id = #{accountId}

    </select>


    <!-- 快手 账户 余额 以及状态 -->
    <select id="getKuaiShouAccountTotalCost" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            u.account_id as accountId,
            u.auth_name as accountName,
            u.account_status as accountStatus,
            IFNULL((SELECT SUM( h.charge ) from application.kuaishou_account_report_hourly_dw h where u.account_id = h.account_id and h.stat_date = #{statDate}),0) as todayCost
        FROM
        `jeecg-boot`.`ctop_user_allocation` u
        WHERE u.account_id = #{accountId}

    </select>


    <!-- 快手-运营 月总消耗 -->
    <select id="getKuaiShouAccountCostMonth" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            SUM( d.charge ) cost
        FROM
            application.kuaishou_account_report_daily_dw d
        <where>
            <if test="userId != null and userId != '' ">
                and d.user_id = #{userId}
            </if>
            <if test="statDate != null and statDate != '' ">
                and d.stat_date &gt;= #{statDate}
            </if>
            <if test="endDate != null and endDate != '' ">
                and d.stat_date &lt;= #{endDate}
            </if>

        </where>

    </select>


    <!-- 用户 -->
    <select id="getKuaiShouUserByUserId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            realname as userName
        FROM
            `jeecg-boot`.sys_user
        WHERE id = #{userId}

    </select>




    <!-- 快手 账户趋势图-时报 -->
    <select id="getKuaiShouAccountInfoBrokenLineHour" resultType="com.alibaba.fastjson.JSONObject">
        SELECT t.*,y.*
        FROM
            (
                SELECT
                    h.`hour` as tHour,
                    <if test='type == "cost"'>
                        h.charge AS tData,
                    </if>
                    <if test='type == "activation"'>
                        IFNULL(h.activation,0) AS tData ,
                    </if>
                    <if test='type == "activationPrice"'>
                        IFNULL(ROUND( (h.charge  /  h.activation) , 2 ),0) AS tData,
                    </if>
                    <if test='type == "leave"'>
                        IF(( h.event_next_day_stay  /  h.activation ) > 0.0001,ROUND(( h.event_next_day_stay  / h.activation ) * 100, 2 ),0) AS tData,
                    </if>
                    <if test='type == "eventPay"'>
                        IFNULL( h.event_pay ,0) AS tData,
                    </if>
                    <if test='type == "eventPayCost"'>
                        IFNULL(ROUND(  (h.charge  /  h.event_pay ), 2 ),0) AS tData,
                    </if>
                    <if test='type == "eventPayRate"'>
                        IF(( h.event_pay  /   (h.activation  +  h.form_count) ) > 0.0001,ROUND( ( h.event_pay  /  (h.activation  +  h.form_count) ) * 100, 2 ),0) AS tData,
                    </if>
                    <if test='type == "eventNewUserPay"'>
                        IFNULL( h.event_new_user_pay ,0) AS tData,
                    </if>
                    <if test='type == "eventNewUserPayCost"'>
                        IFNULL(ROUND( ( h.charge  /  h.event_new_user_pay ), 2 ),0) AS tData,
                    </if>
                    <if test='type == "eventNewUserPayRate"'>
                        IFNULL(ROUND( ( h.event_new_user_pay  /   (h.activation  +  h.form_count) ) * 100, 2 ),0) AS tData,
                    </if>
                    <if test='type == "eventAppInvoked"'>
                        IFNULL( h.event_app_invoked ,0) AS tData,
                    </if>
                    <if test='type == "eventAppInvokedCost"'>
                        IFNULL(ROUND( ( h.charge  /  h.event_app_invoked ), 2 ),0) AS tData,
                    </if>
                    <if test='type == "eventRegister"'>
                        IFNULL( h.event_register ,0) AS tData,
                    </if>
                    <if test='type == "eventRegisterCost"'>
                        IFNULL(ROUND( ( h.charge  /  h.event_register) , 2 ),0) AS tData,
                    </if>
                    <if test='type == "keyAction"'>
                        IFNULL( h.key_action ,0) AS tData,
                    </if>
                    <if test='type == "keyActionCost"'>
                        IFNULL(ROUND( ( h.charge  /  h.key_action ), 2 ),0) AS tData,
                    </if>
                    <if test='type == "keyActionRate"'>
                        IF(( h.key_action /  h.activation ) > 0.0001,CONCAT( ( h.key_action / h.activation ) * 100 , 2 ),0) AS tData,
                    </if>
                    <if test='type == "eventPayPurchaseAmountFirstDay"'>
                        IFNULL( h.event_pay_purchase_amount_first_day ,0) AS tData,
                    </if>
                    <if test='type == "eventPayFirstDayRoi"'>
                        IFNULL( h.event_pay_first_day_roi ,0) AS tData,
                    </if>
                    <if test='type == "formCount"'>
                        IFNULL( h.form_count ,0) AS tData,
                    </if>
                    <if test='type == "formCountPrice"'>
                        IFNULL(ROUND( ( h.charge /  h.form_count ), 2 ),0) AS tData,
                    </if>
                    <if test='type == "photoShow"'>
                        IFNULL(h.`show`,0) as tData,
                    </if>
                    <if test='type == "photoClick"'>
                        IFNULL(h.photo_click,0) as tData,
                    </if>
                    h.account_id as tAccountId

                FROM
                    application.kuaishou_account_report_hourly_dw h
                <where>
                    <if test="accountId != null and accountId != '' ">
                        and h.account_id = #{accountId}
                    </if>
                    <if test="statDate != null and statDate != '' ">
                        and h.stat_date = #{statDate}
                    </if>
                </where>
                ORDER BY
                    h.hour) t
                RIGHT JOIN
            (
                SELECT
                    h.`hour` as yHour,
                    <if test='type == "cost"'>
                        h.charge AS yData,
                    </if>
                    <if test='type == "activation"'>
                        IFNULL(h.activation,0) AS yData ,
                    </if>
                    <if test='type == "activationPrice"'>
                        IFNULL(ROUND( (h.charge  /  h.activation) , 2 ),0) AS yData,
                    </if>
                    <if test='type == "leave"'>
                        IF(( h.event_next_day_stay  /  h.activation ) > 0.0001,ROUND(( h.event_next_day_stay  / h.activation ) * 100, 2 ),0) AS yData,
                    </if>
                    <if test='type == "eventPay"'>
                        IFNULL( h.event_pay ,0) AS yData,
                    </if>
                    <if test='type == "eventPayCost"'>
                        IFNULL(ROUND(  (h.charge  /  h.event_pay ), 2 ),0) AS yData,
                    </if>
                    <if test='type == "eventPayRate"'>
                        IF(( h.event_pay  /   (h.activation  +  h.form_count) ) > 0.0001,ROUND( ( h.event_pay  /  (h.activation  +  h.form_count) ) * 100, 2 ),0) AS yData,
                    </if>
                    <if test='type == "eventNewUserPay"'>
                        IFNULL( h.event_new_user_pay ,0) AS yData,
                    </if>
                    <if test='type == "eventNewUserPayCost"'>
                        IFNULL(ROUND( ( h.charge  /  h.event_new_user_pay ), 2 ),0) AS yData,
                    </if>
                    <if test='type == "eventNewUserPayRate"'>
                        IFNULL(ROUND( ( h.event_new_user_pay  /   (h.activation  +  h.form_count) ) * 100, 2 ),0) AS yData,
                    </if>
                    <if test='type == "eventAppInvoked"'>
                        IFNULL( h.event_app_invoked ,0) AS yData,
                    </if>
                    <if test='type == "eventAppInvokedCost"'>
                        IFNULL(ROUND( ( h.charge  /  h.event_app_invoked ), 2 ),0) AS yData,
                    </if>
                    <if test='type == "eventRegister"'>
                        IFNULL( h.event_register ,0) AS yData,
                    </if>
                    <if test='type == "eventRegisterCost"'>
                        IFNULL(ROUND( ( h.charge  /  h.event_register) , 2 ),0) AS yData,
                    </if>
                    <if test='type == "keyAction"'>
                        IFNULL( h.key_action ,0) AS yData,
                    </if>
                    <if test='type == "keyActionCost"'>
                        IFNULL(ROUND( ( h.charge  /  h.key_action ), 2 ),0) AS yData,
                    </if>
                    <if test='type == "keyActionRate"'>
                        IF(( h.key_action /  h.activation ) > 0.0001, ROUND(( h.key_action / h.activation ) * 100 , 2 ),0) AS yData,
                    </if>
                    <if test='type == "eventPayPurchaseAmountFirstDay"'>
                        IFNULL( h.event_pay_purchase_amount_first_day ,0) AS yData,
                    </if>
                    <if test='type == "eventPayFirstDayRoi"'>
                        IFNULL( h.event_pay_first_day_roi ,0) AS yData,
                    </if>
                    <if test='type == "formCount"'>
                        IFNULL( h.form_count ,0) AS yData,
                    </if>
                    <if test='type == "formCountPrice"'>
                        IFNULL(ROUND( ( h.charge /  h.form_count ), 2 ),0) AS yData,
                    </if>
                    <if test='type == "photoShow"'>
                        IFNULL(h.`show`,0) as yData,
                    </if>
                    <if test='type == "photoClick"'>
                        IFNULL(h.photo_click,0) as yData,
                    </if>
                    h.account_id as yAccountId
                FROM
                    application.kuaishou_account_report_hourly_dw h
                <where>
                    <if test="accountId != null and accountId != '' ">
                        and h.account_id = #{accountId}
                    </if>
                    <if test="endDate != null and endDate != '' ">
                        and h.stat_date = #{endDate}
                    </if>
                </where>
                ORDER BY
                    h.HOUR) y
            on t.tHour = y.yHour
    </select>



    <!-- 快手 账户趋势图-日报 -->
    <select id="getKuaiShouAccountInfoBrokenLineDaily" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            DATE_FORMAT(d.stat_date,'%Y-%m-%d') as dateTime,
            <if test='type == "cost"'>
                SUM( d.charge ) as data,
            </if>
            <if test='type == "activation"'>
                IFNULL(SUM( d.activation ),0) AS data,
            </if>
            <if test='type == "activationPrice"'>
                IFNULL(ROUND( SUM( d.charge ) / SUM( d.activation ), 2 ),0) AS data,
            </if>
            <if test='type == "leave"'>
                IF(SUM( d.event_next_day_stay ) / SUM( d.activation ) > 0.0001,ROUND(SUM( d.event_next_day_stay ) / SUM( d.activation ) * 100, 2 ),0) AS data,
            </if>
            <if test='type == "eventPay"'>
                IFNULL(SUM( d.event_pay ),0) AS data,
            </if>
            <if test='type == "eventPayCost"'>
                IFNULL(ROUND( SUM( d.charge ) / SUM( d.event_pay ), 2 ),0) AS data,
            </if>
            <if test='type == "eventPayRate"'>
                IF(SUM( d.event_pay ) / ( SUM( d.activation ) + SUM( d.form_count )) > 0.0001,ROUND( SUM( d.event_pay ) / ( SUM( d.activation ) + SUM( d.form_count )) * 100, 2 ),0) AS data,
            </if>
            <if test='type == "eventNewUserPay"'>
                IFNULL(SUM( d.event_new_user_pay ),0) AS data,
            </if>
            <if test='type == "eventNewUserPayCost"'>
                IFNULL(ROUND( SUM( d.charge ) / SUM( d.event_new_user_pay ), 2 ),0) AS data,
            </if>
            <if test='type == "eventNewUserPayRate"'>
                IFNULL(ROUND( (SUM( d.event_new_user_pay ) / ( SUM( d.activation ) + SUM( d.form_count ))) * 100, 2 ),0) AS data,
            </if>
            <if test='type == "eventAppInvoked"'>
                IFNULL(SUM( d.event_app_invoked ),0) AS data,
            </if>
            <if test='type == "eventAppInvokedCost"'>
                IFNULL(ROUND( SUM( d.charge ) / SUM( d.event_app_invoked ), 2 ),0) AS data,
            </if>
            <if test='type == "eventRegister"'>
                IFNULL(SUM( d.event_register ),0) AS data,
            </if>
            <if test='type == "eventRegisterCost"'>
                IFNULL(ROUND( SUM( d.charge ) / SUM( d.event_register ), 2 ),0) AS data,
            </if>
            <if test='type == "keyAction"'>
                IFNULL(SUM( d.key_action ),0) AS data,
            </if>
            <if test='type == "keyActionCost"'>
                IFNULL(ROUND( SUM( d.charge ) / SUM( d.key_action ), 2 ),0) AS data,
            </if>
            <if test='type == "keyActionRate"'>
                IF(SUM( d.key_action )/ SUM( d.activation ) > 0.0001, ROUND(SUM( d.key_action )/ SUM( d.activation ) * 100 , 2 ),0) AS data,
            </if>
            <if test='type == "eventPayPurchaseAmountFirstDay"'>
                IFNULL(SUM( d.event_pay_purchase_amount_first_day ),0) AS data,
            </if>
            <if test='type == "eventPayFirstDayRoi"'>
                IFNULL(SUM( d.event_pay_first_day_roi ),0) AS data,
            </if>
            <if test='type == "formCount"'>
                IFNULL(SUM( d.form_count ),0) AS data,
            </if>
            <if test='type == "formCountPrice"'>
                IFNULL(ROUND( SUM( d.charge ) / SUM( d.form_count ), 2 ),0) AS data,
            </if>
            <if test='type == "photoShow"'>
                IFNULL(SUM(d.`show`),0) as data
            </if>
            d.account_id as accountId
        FROM
            application.kuaishou_account_report_daily_dw d
        <where>
            <if test="accountId != null and accountId != '' ">
                and d.account_id = #{accountId}
            </if>
            <if test="startDate != null and startDate != '' ">
                and d.stat_date &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != '' ">
                and d.stat_date &lt;= #{endDate}
            </if>
        </where>
        GROUP BY
        d.stat_date
        ORDER BY
        d.stat_date
    </select>

    <!-- 快手 账户 素材上新-有效-爆款   searchType： 0-全部 1-上新 2-有效 3-爆款-->
    <select id="getKuaiShouAccountPieScene" resultType="org.jeecg.ctop.material.entity.vo.KuaiShouAccountPieVo">
        SELECT
        <if test='type == "cost"'>
            SUM( d.charge ) AS cost,
        </if>
        <if test='type == "activation"'>
            SUM( d.activation ) AS activation,
        </if>
        <if test='type == "activationPrice"'>
            IFNULL( ROUND( SUM( d.charge ) / SUM( d.activation ), 2 ), 0 ) AS activationPrice,
        </if>
        <if test='type == "eventPayRoi"'>
             SUM( d.event_pay_roi ) AS eventPayRoi,
        </if>
        <if test='type == "eventPay"'>
                SUM( d.event_pay ) AS eventPay,
        </if>
        <if test='type == "eventPayCost"'>
              IFNULL( ROUND( SUM( d.charge ) / SUM( d.event_pay ), 2 ), 0 ) AS eventPayCost,
        </if>
        <if test='type == "eventPayPurchaseAmountFirstDay"'>
              SUM( d.event_pay_purchase_amount_first_day ) AS eventPayPurchaseAmountFirstDay,
        </if>
        <if test='type == "eventNextDayStay"'>
            SUM( d.event_next_day_stay ) AS eventNextDayStay,
        </if>
        <if test='type == "leave"'>
             IFNULL( ROUND( SUM( d.event_next_day_stay ) / SUM( d.activation ), 2 ), 0 ) AS leave,
        </if>
        d.ad_scene AS adScene
        FROM
        application.app_kuaishou_account_ad_scene_report_d d
        <where>
            <if test="accountId != null and accountId != '' ">
                and d.account_id = #{accountId}
            </if>
            <if test="startDate != null and startDate != '' ">
                and d.stat_date &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != '' ">
                and d.stat_date &lt;= #{endDate}
            </if>
        </where>
        GROUP BY
        d.ad_scene
        ORDER BY
        d.ad_scene
    </select>





    <!-- 快手 账户饼图图-分版位 -->
    <select id="getKuaiShouAccountMaterialNewAndEffectAndFaddish" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        m.cover_url as coverUrl,
        DATE_FORMAT(m.media_time,'%Y-%m-%d') as dateTime,
        vd.signature,
        m.material_name as videoName,
        m.video_url as videoUrl,
        SUM( vd.charge ) AS totalCost,
        SUM( vd.activation ) AS activation,
        ROUND(( SUM( vd.charge ) / SUM( vd.activation ) ), 2 ) AS activationPrice,
        vd.stat_date AS statDate,
        m.media_time AS mediaTime,
        SUM(vd.show) as showNum
        FROM
        application.kuaishou_material_video_report_daily_dw vd,
        application.app_kuaishou_material_info m
        WHERE
        vd.signature = m.material_id
        AND m.media_time is not null
        AND vd.account_id = #{accountId}
        <if test="searchType == '0'.toString()">
            AND m.media_time &gt;= #{startDate}
            AND m.media_time &lt;= #{endDate}
        </if>
        <if test="searchType == '1'.toString()">
            and material_innovate = 2
        </if>
        <if test="searchType == '2'.toString()">
            AND m.effect_flag = 2
            AND m.effect_date &gt;= #{startDate}
            AND m.effect_date &lt;= #{endDate}
        </if>
         <if test="searchType == '3'.toString()">
             and m.faddish_flag = 2
             and m.faddish_date &gt;= #{startDate}
            AND m.faddish_date &lt;= #{endDate}
        </if>
        GROUP BY
            vd.signature
        ORDER BY
        m.media_time DESC, SUM( vd.charge ) DESC
    </select>


    <!-- 快手 账户饼图图-分年龄 -->
    <select id="getKuaiShouAccountPieAge" resultType="org.jeecg.ctop.material.entity.vo.KuaiShouAccountPieVo">
        SELECT
        <if test='type == "cost"'>
            SUM( d.charge ) AS cost,
        </if>
        <if test='type == "activation"'>
            SUM( d.activation ) AS activation,
        </if>
        <if test='type == "activationPrice"'>
            IFNULL( ROUND( SUM( d.charge ) / SUM( d.activation ), 2 ), 0 ) AS activationPrice,
        </if>
        <if test='type == "eventPayRoi"'>
            SUM( d.event_pay_roi ) AS eventPayRoi,
        </if>
        <if test='type == "eventPay"'>
            SUM( d.event_pay ) AS eventPay,
        </if>
        <if test='type == "eventPayCost"'>
            IFNULL( ROUND( SUM( d.charge ) / SUM( d.event_pay ), 2 ), 0 ) AS eventPayCost,
        </if>
        <if test='type == "eventPayPurchaseAmountFirstDay"'>
            SUM( d.event_pay_purchase_amount_first_day ) AS eventPayPurchaseAmountFirstDay,
        </if>
        <if test='type == "eventNextDayStay"'>
            SUM( d.event_next_day_stay ) AS eventNextDayStay,
        </if>
        <if test='type == "leave"'>
            IFNULL( ROUND( SUM( d.event_next_day_stay ) / SUM( d.activation ), 2 ), 0 ) AS leave,
        </if>
        d.age_segment AS ageSegment
        FROM
        media_api.kuaishou_audience_age_report_daily d
        <where>
            <if test="accountId != null and accountId != '' ">
                and d.advertiser_id = #{accountId}
            </if>
            <if test="startDate != null and startDate != '' ">
                and d.stat_date &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != '' ">
                and d.stat_date &lt;= #{endDate}
            </if>
        </where>
        GROUP BY
            d.age_segment
        ORDER BY
            d.age_segment
    </select>


    <!-- 快手 账户饼图图-分性别 -->
    <select id="getKuaiShouAccountPieGender" resultType="org.jeecg.ctop.material.entity.vo.KuaiShouAccountPieVo">
        SELECT
        <if test='type == "cost"'>
            SUM( d.charge ) AS cost,
        </if>
        <if test='type == "activation"'>
            SUM( d.activation ) AS activation,
        </if>
        <if test='type == "activationPrice"'>
            IFNULL( ROUND( SUM( d.charge ) / SUM( d.activation ), 2 ), 0 ) AS activationPrice,
        </if>
        <if test='type == "eventPayRoi"'>
            SUM( d.event_pay_roi ) AS eventPayRoi,
        </if>
        <if test='type == "eventPay"'>
            SUM( d.event_pay ) AS eventPay,
        </if>
        <if test='type == "eventPayCost"'>
            IFNULL( ROUND( SUM( d.charge ) / SUM( d.event_pay ), 2 ), 0 ) AS eventPayCost,
        </if>
        <if test='type == "eventPayPurchaseAmountFirstDay"'>
            SUM( d.event_pay_purchase_amount_first_day ) AS eventPayPurchaseAmountFirstDay,
        </if>
        <if test='type == "eventNextDayStay"'>
            SUM( d.event_next_day_stay ) AS eventNextDayStay,
        </if>
        <if test='type == "leave"'>
            IFNULL( ROUND( SUM( d.event_next_day_stay ) / SUM( d.activation ), 2 ), 0 ) AS leave,
        </if>
        d.gender AS gender
        FROM
        media_api.kuaishou_audience_gender_report_daily d
        <where>
            <if test="accountId != null and accountId != '' ">
                and d.advertiser_id = #{accountId}
            </if>
            <if test="startDate != null and startDate != '' ">
                and d.stat_date &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != '' ">
                and d.stat_date &lt;= #{endDate}
            </if>
        </where>
        GROUP BY
            d.gender
        ORDER BY
            d.gender
    </select>



    <!-- 快手 账户 框返 激励 消耗 -->
    <select id="getKuaiShouAccountCharged" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ROUND( SUM( total_charged_in_yuan ), 3 ) AS totalCost,
        ROUND( SUM( direct_rebate_real_charged_in_yuan ), 3 ) AS directCharged,
        ROUND( SUM( contract_rebate_real_charged_in_yuan ), 3 ) AS contractCharged
        FROM application.app_kuaishou_agent_report_d
        <where>
            <if test="startDate != null and startDate != '' ">
                and stat_date &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != '' ">
                and stat_date &lt;= #{endDate}
            </if>
            <if test='list!=null'>
                AND user_id IN
                <foreach collection="list" item="item" separator=","
                         open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>


    <!-- 快手 运营 组内消耗 排名 -->
    <select id="getKuaiShouPersonalGroupRank" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ROW_NUMBER() OVER ( ORDER BY SUM( total_charged_in_yuan ) DESC ) AS serialNumber,
        ROUND( SUM( total_charged_in_yuan ), 3 ) AS totalCost,
        user_id,
        user_name
        FROM
        application.app_kuaishou_agent_report_d
        <where>
            <if test="startDate != null and startDate != '' ">
                and stat_date &gt;= #{startDate}
            </if>
            <if test="endDate != null and endDate != '' ">
                and stat_date &lt;= #{endDate}
            </if>
            <if test="companyId != null and companyId != '' ">
                and company_id = #{companyId}
            </if>
        </where>
        GROUP BY user_id
        ORDER BY totalCost DESC
    </select>





    <!-- 快手 同公司所有下级运营人员 -->
    <select id="getKuaiShouAllOperators" resultType="java.lang.String">
        SELECT DISTINCT
        d.user_id
        FROM
        application.kuaishou_account_report_daily_dw d
        WHERE
         d.charge > 0
        <if test="startDate != null and startDate != '' ">
            and d.stat_date &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != '' ">
            and d.stat_date &lt;= #{endDate}
        </if>
        AND d.user_id in
        <foreach collection="userIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
    </select>


    <!-- 快手组实时数据查询-当天创建的组+当天有消耗的组 -->
    <select id="kuaiShouRealTimeUnitDateInfo" resultType="com.alibaba.fastjson.JSONObject">

    SELECT t.*,
           l.bid_type bidType,
           l.bid,
           l.cpa_bid cpaBid,
           l.put_status putStatus
    from (
        SELECT IFNULL(a.advertiser_id,b.dAccountId) accountId,
               IFNULL(a.uid,b.dunit_id) unit_id,
                a.*,b.*
        from (
            SELECT
            ad.advertiser_id as advertiser_id,
            ad.unit_id uid,
            ad.ocpx_action_type
            FROM
            media_api.kuaishou_ad_unit_list ad
            WHERE
            ad.advertiser_id = #{accountId}
            AND DATE_FORMAT( ad.create_time, "%Y%m%d" ) = #{nowDate}
            ) a RIGHT JOIN
            (
            SELECT
            d.advertiser_id dAccountId,
            d.campaign_id campaignId,
            d.campaign_name campaignName,
            d.unit_id dunit_id,
            d.unit_name unitName,
            IFNULL( SUM( d.charge ), 0 ) AS charge,
            IFNULL( SUM( d.activation ), 0 ) AS activation,
            IFNULL( ROUND( SUM( d.charge ) / SUM( d.activation ), 2 ), 0 ) AS activationPrice,
            IFNULL( ROUND( SUM( d.charge ) / SUM( d.activation ), 2 ), 0 ) AS activationCost,
            IF(SUM( d.event_next_day_stay ) / SUM( d.activation ) > 0.0001,CONCAT( ROUND( SUM( d.event_next_day_stay ) / SUM( d.activation ) * 100, 2 ), '%' ),0 ) AS `leave`,
            IFNULL( SUM( d.event_pay ), 0 ) AS eventPay,
            IFNULL( ROUND( SUM( d.charge ) / SUM( d.event_pay ), 2 ), 0 ) AS eventPayCost,
            IF(SUM( d.event_pay ) / (SUM( d.activation ) + SUM( d.form_count )) > 0.0001,CONCAT(ROUND( SUM( d.event_pay ) / ( SUM( d.activation ) + SUM( d.form_count )) * 100, 2),'%'),0 ) AS eventPayRate,
            IFNULL( SUM( d.event_new_user_pay ), 0 ) AS eventNewUserPay,
            IFNULL( ROUND( SUM( d.charge ) / SUM( d.event_new_user_pay ), 2 ), 0 ) AS eventNewUserPayCost,
            IFNULL( ROUND( SUM( d.event_new_user_pay ) / ( SUM( d.activation ) + SUM( d.form_count )), 2 ), 0 ) AS eventNewUserPayRate,
            IFNULL( SUM( d.event_app_invoked ), 0 ) AS eventAppInvoked,
            IFNULL( ROUND( SUM( d.charge ) / SUM( d.event_app_invoked ), 2 ), 0 ) AS eventAppInvokedCost,
            IFNULL( SUM( d.event_register ), 0 ) AS eventRegister,
            IFNULL( ROUND( SUM( d.charge ) / SUM( d.event_register ), 2 ), 0 ) AS eventRegisterCost,
            IFNULL( SUM( d.key_action ), 0 ) AS keyAction,
            IFNULL( ROUND( SUM( d.charge ) / SUM( d.key_action ), 2 ), 0 ) AS keyActionCost,
            IF(SUM( d.key_action )/ SUM( d.activation ) > 0.0001,CONCAT( ROUND( SUM( d.key_action )/ SUM( d.activation ) * 100, 2 ), '%' ),0) AS keyActionRate,
            IFNULL( SUM( d.event_pay_purchase_amount_first_day ), 0 ) AS eventPayPurchaseAmountFirstDay,
            IFNULL( SUM( d.event_pay_first_day_roi ), 0 ) AS eventPayFirstDayRoi,
            IFNULL( SUM( d.form_count ), 0 ) AS formCount,
            IFNULL( ROUND( SUM( d.charge ) / SUM( d.form_count ), 2 ), 0 ) AS formCountPrice,
            SUM(d.bclick) bClick
            FROM
            media_api.kuaishou_unit_report_hourly d
            WHERE
            d.advertiser_id = #{accountId}
            AND d.stat_date = #{nowDate}
            GROUP BY
            d.unit_id
            ) b
        on a.uid = b.dunit_id
        ) t
        LEFT JOIN media_api.kuaishou_ad_unit_list l
        on l.unit_id = t.unit_id
        WHERE l.advertiser_id = #{accountId}



    </select>











    <select id="getRoleCodeByUserId" resultType="java.lang.String">
        select c.role_code
        from `jeecg-boot`.sys_user a
                 left join `jeecg-boot`.sys_user_role b on a.id = b.user_id
                 left join `jeecg-boot`.sys_role c on b.role_id = c.id
        where
         a.id = #{userId}
            LIMIT 1
    </select>

    <select id="getCompanyAllUserId" resultType="java.lang.String">
        SELECT DISTINCT
            user_id
        FROM
            user_company
        WHERE
            company_id = #{companyId}
    </select>

    <select id="getUserCompanyByUserId" resultType="java.lang.String">
        SELECT
            company_id as companyId
        FROM
            user_company
        WHERE
            user_id = #{userId}
            LIMIT 1
    </select>

    <select id="getAffiliateId" resultType="java.lang.String">
        SELECT DISTINCT
            u.id
        FROM
        `jeecg-boot`.sys_user u,
        `jeecg-boot`.user_company c,
        `jeecg-boot`.sys_user_role r,
        `jeecg-boot`.sys_role sr
        WHERE
            u.id = c.user_id
            and c.user_id = r.user_id
            and r.role_id = sr.id
            AND sr.role_name LIKE '%运营%'
        <if test="leaderId != null and leaderId != '' ">
            AND u.leader_id = #{leaderId}
        </if>
        <if test="companyId != null and companyId != '' ">
              AND c.company_id = #{companyId}
        </if>
          AND u.STATUS = 1
          AND u.del_flag = 0
    </select>

    <select id="querySubordinateRecursive" resultType="java.lang.String">
        SELECT DISTINCT
        u.id
        FROM
        `jeecg-boot`.sys_user u,
        `jeecg-boot`.user_company c,
        `jeecg-boot`.sys_user_role r,
        `jeecg-boot`.sys_role sr
        WHERE
        u.id = c.user_id
        and c.user_id = r.user_id
        and r.role_id = sr.id
        AND sr.role_name LIKE '%运营%'
        AND u.STATUS = 1
        AND u.del_flag = 0
        <if test="companyId != null and companyId != '' ">
            AND c.company_id = #{companyId}
        </if>
        and u.leader_id in
        <foreach collection="leaderIds" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
    </select>




    <select id="getProjectByUserIdAndMediaIdsSubordinate" resultType="com.alibaba.fastjson.JSONObject">
        select
        t2.id as 'projectId',
        t2.project_name as 'projectName',
        t2.product_id as 'productId',
        (select name from ctop_advertiser where id = t2.advertiser_id) as 'advertiserName'
        from ctop_project_member t1
        left join ctop_project t2
        on t1.project_id = t2.id
        where 1 = 1
        <if test="userIds != null and userIds.size() != 0 ">
            and t1.user_id in
            <foreach collection="userIds" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>

        <if test="mediaIds != null and mediaIds.size() != 0 ">
            and t2.media_id in
            <foreach collection="mediaIds" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>
        group by t2.id
        order by t2.create_time desc
    </select>



    <select id="getAccountListByProjectListSubordinate" resultType="com.alibaba.fastjson.JSONObject">
        select
        t1.project_id as 'projectId',
        t1.account_id as 'accountId',
        (select realname from sys_user where id = t1.user_id ) as 'userName',
        t1.auth_name as 'authName',
        t1.account_status as 'accountStatus'
        from ctop_user_allocation t1
        where t1.account_status = 0

        <if test="list != null and list.size() != 0 ">
            and t1.project_id in
            <foreach collection="list" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>


        <if test="userIds != null and userIds.size() != 0 ">
            and t1.user_id in
            <foreach collection="userIds" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>

        order by t1.create_time desc
    </select>




</mapper>
