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

    <!-- 公司报表sql -->
    <select id="companyReport" resultType="cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyAgentSum">
            select
            date_format(date, '%Y-%m-%d') as date,  <!-- 日期 -->
            cost_campaign_count as costCampaignCount,  <!-- 有消费计划数 -->
            balance as balance,  <!-- 总余额 -->
            cost as cost,  <!-- 总消耗 -->
            xianjin_cost as xianjinCost,  <!-- 现金消耗 -->
            fandian_cost as fandianCost,  <!-- 后返消耗 -->
            kuangfan_cost as kuangfanCost,  <!-- 框返消耗 -->
            jili_cost as jiliCost,  <!-- 激励账户消耗 -->
            xinyong_cost as xinyongCost,  <!-- 信用账户消耗 -->
            fengmian_show_count as fengmianShowCount,  <!-- 封面曝光数 -->
            fengmian_click_count as fengmianClickCount,  <!-- 封面点击数 -->
            sucai_show_count as sucaiShowCount,  <!-- 素材曝光数 -->
            convert_count as convertCount,  <!-- 行为数 -->
            fengmian_click_rate as fengmianClickRate,  <!-- 封面点击率 -->
            convert_click_rate as convertClickRate  <!-- 转化点击率 -->
        from
        ctop_kuaishou_report_daily_agent_sum
        where
        date &lt;= #{endDate} and
        date &gt;= #{startDate}
        order by date desc
    </select>

    <select id="companyReportSum" resultType="cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyAgentSum">
        select
        sum(cost_campaign_count) as costCampaignCount,  <!-- 有消费计划数 -->
        sum(balance) as balance,  <!-- 总余额 -->
        sum(cost) as cost,  <!-- 总消耗 -->
        sum(xianjin_cost) as xianjinCost,  <!-- 现金消耗 -->
        sum(fandian_cost) as fandianCost,  <!-- 后返消耗 -->
        sum(kuangfan_cost) as kuangfanCost,  <!-- 框返消耗 -->
        sum(jili_cost) as jiliCost,  <!-- 激励账户消耗 -->
        sum(xinyong_cost) as xinyongCost,  <!-- 信用账户消耗 -->
        sum(fengmian_show_count) as fengmianShowCount,  <!-- 封面曝光数 -->
        sum(fengmian_click_count) as fengmianClickCount,  <!-- 封面点击数 -->
        sum(sucai_show_count) as sucaiShowCount,  <!-- 素材曝光数 -->
        sum(convert_count) as convertCount,  <!-- 行为数 -->
        sum(fengmian_click_rate) as fengmianClickRate,  <!-- 封面点击率 -->
        sum(convert_click_rate) as convertClickRate  <!-- 转化点击率 -->
        from
        ctop_kuaishou_report_daily_agent_sum
        where
        date &lt;= #{endDate} and
        date &gt;= #{startDate}
    </select>

    <!-- 根据传入时间获取公司报表账户的列表 -->
    <select id="companyReportAccountDetailList" resultType="cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyAgent">
            select
            date_format(date, '%Y-%m-%d') as date,  <!-- 日期 -->
            account_id as accountId,  <!-- 账户id -->
            kid as kid, <!-- 快手id -->
            advertiser_name as advertiserName, <!-- 广告主名称 -->
            advertiser_create_time as advertiserCreateTime, <!-- 广告主创建时间 -->
            cost_campaign_count as costCampaignCount, <!-- 有消费计划数 -->
            balance as balance, <!-- 总余额 -->
            cost as cost, <!-- 总消耗 -->
            xianjin_cost as xianjinCost, <!-- 现金消耗 -->
            fandian_cost as fandianCost, <!-- 后返消耗 -->
            kuangfan_cost as kuangfanCost, <!-- 框返消耗 -->
            jili_cost as jiliCost, <!-- 激励账户消耗 -->
            xinyong_cost as xinyongCost, <!-- 信用账户消耗 -->
            fengmian_show_count as fengmianShowCount, <!-- 封面曝光数 -->
            fengmian_click_count as fengmianClickCount, <!-- 封面点击数 -->
            sucai_show_count as sucaiShowCount, <!-- 素材曝光数 -->
            convert_count as convertCount, <!-- 行为数 -->
            fengmian_click_rate as fengmianClickRate, <!-- 封面点击率 -->
            convert_click_rate as convertClickRate, <!-- 转化点击率 -->
            case
                when
                cost_campaign_count + balance + cost + xianjin_cost + fandian_cost + kuangfan_cost + jili_cost +
                xinyong_cost + fengmian_show_count + fengmian_click_count + sucai_show_count + convert_count +
                fengmian_click_rate + convert_click_rate = 0 then 0
                else 1
                end as isHasData
        from
        ctop_kuaishou_report_daily_agent
        where
        date_format(date, '%Y-%m-%d') = #{date}
        <if test="advertiserName != null and advertiserName != ''">
            and advertiser_name like concat('%',#{advertiserName},'%')
        </if>
        <if test="isExportNullData == 0 or isExportNullData == '0'">  <!-- 判断是否导出公司明细中各字段总合为0的数据  1-导出 0-不导出 -->
            and
            (cost_campaign_count + balance + cost + xianjin_cost + fandian_cost + kuangfan_cost + jili_cost +
            xinyong_cost + fengmian_show_count + fengmian_click_count + sucai_show_count + convert_count +
            fengmian_click_rate + convert_click_rate) != 0
        </if>
    </select>

    <!-- 根据传入开始时间和结束获取公司报表账户的列表 -->
    <select id="companyReportAccountDetailListByDateRange" resultType="cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyAgent">
        select
        date as date,  <!-- 日期 -->
        account_id as accountId,  <!-- 账户id -->
        kid as kid, <!-- 快手id -->
        advertiser_name as advertiserName, <!-- 广告主名称 -->
        advertiser_create_time as advertiserCreateTime, <!-- 广告主创建时间 -->
        cost_campaign_count as costCampaignCount, <!-- 有消费计划数 -->
        balance as balance, <!-- 总余额 -->
        cost as cost, <!-- 总消耗 -->
        xianjin_cost as xianjinCost, <!-- 现金消耗 -->
        fandian_cost as fandianCost, <!-- 后返消耗 -->
        kuangfan_cost as kuangfanCost, <!-- 框返消耗 -->
        jili_cost as jiliCost, <!-- 激励账户消耗 -->
        xinyong_cost as xinyongCost, <!-- 信用账户消耗 -->
        fengmian_show_count as fengmianShowCount, <!-- 封面曝光数 -->
        fengmian_click_count as fengmianClickCount, <!-- 封面点击数 -->
        sucai_show_count as sucaiShowCount, <!-- 素材曝光数 -->
        convert_count as convertCount, <!-- 行为数 -->
        fengmian_click_rate as fengmianClickRate, <!-- 封面点击率 -->
        convert_click_rate as convertClickRate <!-- 转化点击率 -->
        from
        ctop_kuaishou_report_daily_agent
        where
        date_format(date, '%Y-%m-%d') &gt; #{startDate} and
        date_format(date, '%Y-%m-%d') &lt; #{endDate}
    </select>

</mapper>
