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


    <!-- 代理商列表-->
    <select id="companyReport" resultType="cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyAgentSum">
        select
        date_time as date,  <!-- 日期 -->
        sum(charged_campaign_count) as costCampaignCount,  <!-- 有消费计划数 -->
        sum(total_balance_in_yuan) as balance,  <!-- 总余额 -->
        sum(total_charged_in_yuan) as cost,  <!-- 总消耗 -->
        sum(real_charged_in_yuan) as xianjinCost,  <!-- 现金消耗 -->
        sum(total_rebate_real_charged_in_yuan) as fandianCost,  <!-- 后返消耗 -->
        sum(contract_rebate_real_charged_in_yuan) as kuangfanCost,  <!-- 框返消耗 -->
        sum(direct_rebate_real_charged_in_yuan) as jiliCost,  <!-- 激励账户消耗 -->
        sum(credit_real_charged_in_yuan) as xinyongCost,  <!-- 信用账户消耗 -->
        sum(ad_photo_impression) as fengmianShowCount,  <!-- 封面曝光数 -->
        sum(ad_photo_click) as fengmianClickCount,  <!-- 封面点击数 -->
        sum(ad_item_impression) as sucaiShowCount,  <!-- 素材曝光数 -->
        sum(ad_item_click) as convertCount,  <!-- 行为数 -->
        (case when sum(ad_photo_impression) != 0
        then sum(ad_photo_click) / sum(ad_photo_impression)
        else 0
        end
        ) as fengmianClickRate,  <!-- 封面点击率 -->
        (
        case when sum(ad_photo_click) != 0
        THEN sum(ad_item_click) / sum(ad_photo_click)
        else 0
        end
        ) as convertClickRate  <!-- 转化点击率 -->

        from
        ctop_kuaishou_daily_agent
        where

        date_time &gt;= #{startDate}
        and
        date_time &lt;= #{endDate}
        group by date_time
        order by date_time desc
    </select>


    <select id="companyReportSum" resultType="cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyAgentSum">
        select
        sum(charged_campaign_count) as costCampaignCount,  <!-- 有消费计划数 -->
        sum(total_balance_in_yuan) as balance,  <!-- 总余额 -->
        sum(total_charged_in_yuan) as cost,  <!-- 总消耗 -->
        sum(real_charged_in_yuan) as xianjinCost,  <!-- 现金消耗 -->
        sum(total_rebate_real_charged_in_yuan) as fandianCost,  <!-- 后返消耗 -->
        sum(contract_rebate_real_charged_in_yuan) as kuangfanCost,  <!-- 框返消耗 -->
        sum(direct_rebate_real_charged_in_yuan) as jiliCost,  <!-- 激励账户消耗 -->
        sum(credit_real_charged_in_yuan) as xinyongCost,  <!-- 信用账户消耗 -->
        sum(ad_photo_impression) as fengmianShowCount,  <!-- 封面曝光数 -->
        sum(ad_photo_click) as fengmianClickCount,  <!-- 封面点击数 -->
        sum(ad_item_impression) as sucaiShowCount,  <!-- 素材曝光数 -->
        sum(ad_item_click) as convertCount,  <!-- 行为数 -->
        (case when sum(ad_photo_impression) != 0
        then sum(ad_photo_click) / sum(ad_photo_impression)
        else 0
        end
        ) as fengmianClickRate,  <!-- 封面点击率 -->
        (
        case when sum(ad_photo_click) != 0
        then sum(ad_item_click) / sum(ad_photo_click)
        else 0
        end
        ) as convertClickRate  <!-- 转化点击率 -->
        from
        ctop_kuaishou_daily_agent
        where
        date_time &lt;= #{endDate} and
        date_time &gt;= #{startDate}
    </select>


    <select id="companyReportAccountDetailList"
            resultType="cn.com.ctop.kuaishou.modules.report.entity.KuaishouReportDailyAgent">
        select
        date_time as date,  <!-- 日期 -->
        account_id as accountId,  <!-- 账户id -->
        user_id as kid, <!-- 快手id -->
        corporation_name as advertiserName, <!-- 广告主名称 -->
        account_name as accountName, <!-- 账户名称 -->
        industry as industry, <!-- 一级行业 -->
        second_industry as secondIndustry, <!-- 二级行业 -->
        charge_day_on_day_percent as chargeDayOnDayPercent, <!-- 消耗环比 -->
        charged_campaign_count as costCampaignCount, <!-- 有消费计划数 -->
        total_balance_in_yuan as balance, <!-- 总余额 -->
        total_charged_in_yuan as cost, <!-- 总消耗 -->
        real_charged_in_yuan as xianjinCost, <!-- 现金消耗 -->
        total_rebate_real_charged_in_yuan as fandianCost, <!-- 后返消耗 -->
        contract_rebate_real_charged_in_yuan as kuangfanCost, <!-- 框返消耗 -->
        direct_rebate_real_charged_in_yuan as jiliCost, <!-- 激励账户消耗 -->
        credit_real_charged_in_yuan as xinyongCost, <!-- 信用账户消耗 -->
        ad_photo_impression as fengmianShowCount, <!-- 封面曝光数 -->
        ad_photo_click as fengmianClickCount, <!-- 封面点击数 -->
        ad_item_impression as sucaiShowCount, <!-- 素材曝光数 -->
        ad_item_click as convertCount, <!-- 行为数 -->
        photo_click_ratio as fengmianClickRate, <!-- 封面点击率 -->
        item_click_ratio as convertClickRate, <!-- 转化点击率 -->
        case
        when
        total_charged_in_yuan = 0
        then 0
        else 1
        end as isHasData
        from
        ctop_kuaishou_daily_agent
        where
        date_time = #{date}
        <if test="advertiserName != null and advertiserName != ''">
            and corporation_name like concat('%',#{advertiserName},'%')
        </if>
        <if test="isExportNullData == 0 or isExportNullData == '0'">  <!-- 判断是否导出公司明细中各字段总合为0的数据  1-导出 0-不导出 -->
            and
            total_charged_in_yuan > 0
        </if>
    </select>


    <insert id="insertSelective" parameterType="cn.com.ctop.kuaishou.modules.report.entity.KuaiShouDailyAgent">
        replace into ctop_kuaishou_daily_agent
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="agentId != null">
                agent_id,
            </if>
            <if test="accountId != null">
                account_id,
            </if>
            <if test="dateTime != null">
                date_time,
            </if>
            <if test="userId != null">
                user_id,
            </if>
            <if test="accountName != null">
                account_name,
            </if>
            <if test="totalChargedInYuan != null">
                total_charged_in_yuan,
            </if>
            <if test="totalBalanceInYuan != null">
                total_balance_in_yuan,
            </if>
            <if test="realChargedInYuan != null">
                real_charged_in_yuan,
            </if>
            <if test="totalRebateRealChargedInYuan != null">
                total_rebate_real_charged_in_yuan,
            </if>
            <if test="contractRebateRealChargedInYuan != null">
                contract_rebate_real_charged_in_yuan,
            </if>
            <if test="directRebateRealChargedInYuan != null">
                direct_rebate_real_charged_in_yuan,
            </if>
            <if test="creditRealChargedInYuan != null">
                credit_real_charged_in_yuan,
            </if>
            <if test="chargeDayOnDayPercent != null">
                charge_day_on_day_percent,
            </if>
            <if test="adPhotoImpression != null">
                ad_photo_impression,
            </if>
            <if test="adPhotoClick != null">
                ad_photo_click,
            </if>
            <if test="adItemImpression != null">
                ad_item_impression,
            </if>
            <if test="adItemClick != null">
                ad_item_click,
            </if>
            <if test="photoClickRatio != null">
                photo_click_ratio,
            </if>
            <if test="itemClickRatio != null">
                item_click_ratio,
            </if>
            <if test="chargedCampaignCount != null">
                charged_campaign_count,
            </if>
            <if test="productName != null">
                product_name,
            </if>
            <if test="corporationName != null">
                corporation_name,
            </if>
            <if test="firstCostDay != null">
                first_cost_day,
            </if>
            <if test="industry != null">
                industry,
            </if>
            <if test="secondIndustry != null">
                second_industry,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="agentId != null">
                #{agentId,jdbcType=BIGINT},
            </if>
            <if test="accountId != null">
                #{accountId,jdbcType=BIGINT},
            </if>
            <if test="dateTime != null">
                #{dateTime,jdbcType=VARCHAR},
            </if>
            <if test="userId != null">
                #{userId,jdbcType=BIGINT},
            </if>
            <if test="accountName != null">
                #{accountName,jdbcType=VARCHAR},
            </if>
            <if test="totalChargedInYuan != null">
                #{totalChargedInYuan,jdbcType=DECIMAL},
            </if>
            <if test="totalBalanceInYuan != null">
                #{totalBalanceInYuan,jdbcType=DECIMAL},
            </if>
            <if test="realChargedInYuan != null">
                #{realChargedInYuan,jdbcType=DECIMAL},
            </if>
            <if test="totalRebateRealChargedInYuan != null">
                #{totalRebateRealChargedInYuan,jdbcType=DECIMAL},
            </if>
            <if test="contractRebateRealChargedInYuan != null">
                #{contractRebateRealChargedInYuan,jdbcType=DECIMAL},
            </if>
            <if test="directRebateRealChargedInYuan != null">
                #{directRebateRealChargedInYuan,jdbcType=DECIMAL},
            </if>
            <if test="creditRealChargedInYuan != null">
                #{creditRealChargedInYuan,jdbcType=DECIMAL},
            </if>
            <if test="chargeDayOnDayPercent != null">
                #{chargeDayOnDayPercent,jdbcType=VARCHAR},
            </if>
            <if test="adPhotoImpression != null">
                #{adPhotoImpression,jdbcType=BIGINT},
            </if>
            <if test="adPhotoClick != null">
                #{adPhotoClick,jdbcType=BIGINT},
            </if>
            <if test="adItemImpression != null">
                #{adItemImpression,jdbcType=BIGINT},
            </if>
            <if test="adItemClick != null">
                #{adItemClick,jdbcType=BIGINT},
            </if>
            <if test="photoClickRatio != null">
                #{photoClickRatio,jdbcType=VARCHAR},
            </if>
            <if test="itemClickRatio != null">
                #{itemClickRatio,jdbcType=VARCHAR},
            </if>
            <if test="chargedCampaignCount != null">
                #{chargedCampaignCount,jdbcType=BIGINT},
            </if>
            <if test="productName != null">
                #{productName,jdbcType=VARCHAR},
            </if>
            <if test="corporationName != null">
                #{corporationName,jdbcType=VARCHAR},
            </if>
            <if test="firstCostDay != null">
                #{firstCostDay,jdbcType=VARCHAR},
            </if>
            <if test="industry != null">
                #{industry,jdbcType=VARCHAR},
            </if>
            <if test="secondIndustry != null">
                #{secondIndustry,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>
    <insert id="replaceBatch">
        replace into ctop_kuaishou_daily_agent(
        `date`,
        `account_id`,
        `kid`,
        `advertiser_name`,
        `advertiser_create_time`,
        `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`
        )
        values
        <foreach collection="list" item="agent" separator=",">
            (#{agent.date},
            #{agent.accountId},
            #{agent.kid},
            #{agent.advertiserName},
            #{agent.advertiserCreateTime},
            #{agent.costCampaignCount},
            #{agent.balance},
            #{agent.cost},
            #{agent.xianjinCost},
            #{agent.fandianCost},
            #{agent.kuangfanCost},
            #{agent.jiliCost},
            #{agent.xinyongCost},
            #{agent.fengmianShowCount},
            #{agent.fengmianClickCount},
            #{agent.sucaiShowCount},
            #{agent.convertCount},
            #{agent.fengmianClickRate},
            #{agent.convertClickRate})
        </foreach>

    </insert>

</mapper>
