<?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.modules.ctop.mapper.AdvertiserMapper">

    <select id="getXXlStatisticInfo" resultType="org.jeecg.modules.excelutil.entity.KuaishouXxlEntity">
        select
        report.stat_date as date,
        round(sum(report.charge),2) as cost,
        round(sum(
        case
        when g.deep_conversion_bid != 0
        then report.charge
        else 0
        end),2) as doubleBid,
        round(sum(
        case
        when g.deep_conversion_bid = 0
        then report.charge
        else 0
        end),2) as singleBid,
        sum(activation) as active,
        (case
        when sum(report.charge)!=0 and sum(activation)!=0
        then round(sum(report.charge) / sum(activation), 2)
        else 0
        end
        ) as activeCost,
        (
        select
        count(1)
        from
        ctop_kuaishou_group g2
        left join ctop_user_allocation allocation2 on g2.account_id = allocation2.account_id
        where
        1=1
        <if test="projectId != null">
            and allocation2.project_id = #{projectId}
        </if>
        <if test="systemType != null">
            and allocation2.system_type = #{systemType}
        </if>
        <if test="accountId != null">
            and allocation2.account_id = #{accountId}
        </if>
        and
        date_format(g2.group_create_time, '%Y-%m-%d') = report.stat_date
        ) as planNum,
        '' as mat
        from
        ctop_kuaishou_report_daily_group report
        left join ctop_kuaishou_group g on g.unit_id = report.unit_id
        left join ctop_user_allocation allocation on allocation.account_id = report.account_id
        where
        1=1
        <if test="projectId != null">
            and allocation.project_id = #{projectId}
        </if>
        <if test="systemType != null">
            and allocation.system_type = #{systemType}
        </if>
        <if test="accountId != null">
            and allocation.account_id = #{accountId}
        </if>
        <if test="groupBy != null">
            group by report.stat_date desc
        </if>

    </select>
    <select id="getYdWeekStatisticInfo" resultType="org.jeecg.modules.excelutil.entity.YdWeekAggregateEntity">
        select
            concat(#{start},'至',#{end}) as date,
            round(sum(cost),2) as cost,
            sum(active) as active,
            (
                case
                    when sum(cost)!=0 and sum(active)!=0
                    then round(sum(cost)/sum(active),2)
                    else 0
                end)    as activeAvg
        from
            ctop_bytedance_report_advertiser_daily daily
        left join ctop_user_allocation allocation on allocation.account_id = daily.advertiser_id
        where
            allocation.project_id = #{projectId}
        and
            allocation.account_status = 0
        and
            daily.stat_datetime &gt;= #{start}
        and
            daily.stat_datetime &lt;= #{end}

    </select>
    <select id="getYdWeekPlanStatisticInfoAll"
            resultType="org.jeecg.modules.excelutil.entity.YdPlanAggregateEntity">
        select
            allocation.auth_name as accountName,
            concat(#{start}, '至', #{end})as date,
            daily.campaign_name as planName,
            sum(daily.cost) as totalCost,
            sum(daily.show_num) as showNum,
            sum(daily.click) as clickNum,
            (case
                when sum(daily.click)!=0 and sum(daily.show_num)!=0
                then concat(round(sum(daily.click)/sum(daily.show_num)*100,2),'%')
                else '0%'
                end
            )as clickRate,
            (case
                when sum(daily.cost)!=0 and sum(daily.click)!=0
                then round(sum(daily.cost)/sum(daily.click)*100,2)
                else 0
                end
            )as priceAvg,
            (case
                when sum(daily.cost)!=0 and sum(daily.show_num)!=0
                then round(sum(daily.cost)/sum(daily.show_num)*1000,2)
                else 0
                end
            )as ctr,
            sum(daily.convert_num) as convertNum,
            (case
                when sum(daily.convert_num)!=0 and sum(daily.cost)!=0
                then round(sum(daily.cost)/sum(daily.convert_num),2)
                else 0
                end
            )as convertPrice,
            (case
                when sum(daily.convert_num)!=0 and sum(daily.click)!=0
                then concat(round(sum(daily.convert_num)/sum(daily.click)*100,2),'%')
                else '0%'
                end
            )as convertRate,
            (
                select
                    group_concat(distinct plan.gender)
                from
                    ctop_bytedance_advertise_plan plan
                    where plan.campaign_id = daily.campaign_id
            )as sex,
            (
                select
                    group_concat(distinct plan.platform)
                from
                    ctop_bytedance_advertise_plan plan
                where
                    plan.campaign_id = daily.campaign_id
            )as platform
        from ctop_bytedance_report_campaign_daily daily
        left join ctop_user_allocation allocation on allocation.account_id = daily.advertiser_id
        where allocation.account_id = #{accountId}
        and allocation.account_status = 0
        and daily.stat_datetime &gt;= #{start}
        and daily.stat_datetime &lt;= #{end}
        group by daily.campaign_id order by sum(daily.cost) desc
    </select>
    <select id="getAllocationsBySaleId" resultType="cn.com.ctop.common.module.entity.UserAllocation">
        select
            allocation.*
        from
            ctop_user_allocation allocation
         left join ctop_project project on project.id = allocation.project_id
         left join ctop_advertiser advertiser on advertiser.id = project.advertiser_id
        where advertiser.sale_id = #{saleId}
    </select>
    <select id="getAllocationsByAdvertiserId" resultType="cn.com.ctop.common.module.entity.UserAllocation">
        select
            allocation.*
        from
            ctop_user_allocation allocation
         left join ctop_project project on project.id = allocation.project_id
         where project.advertiser_id = #{adId}
    </select>
    <select id="getAdvertiserByMap" resultType="com.alibaba.fastjson.JSONObject">
        select id,name from ctop_advertiser
        where id in
        <foreach collection="ids" item="ids" separator="," open="(" close=")">
            #{ids}
        </foreach>
    </select>
</mapper>
