<?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.report.mapper.AccountAttributionInfoMapper">

    <insert id="reloadData">
        replace into `jeecg-boot`.ctop_account_attribution_info(id, account_id, project_id, product_id, advertiser_id, company_id,
                                          account_name, project_name, advertiser_name, company_name, sale_id, user_id,
                                          org_code)
        select cua.account_id,
               cua.account_id,
               cp.id,
               product.id,
               ca.id,
               uc.company_id,
               cua.auth_name,
               cp.project_name,
               ca.name,
               uc.company_name,
               cp.sale_id,
               su.id,
               su.org_code
        from `jeecg-boot`.ctop_user_allocation cua
                 left join `jeecg-boot`.ctop_project cp on cua.project_id = cp.id
                 left join `jeecg-boot`.ctop_advertiser ca on ca.id = cua.advertiser_id
                 left join `jeecg-boot`.sys_user su on su.id = cua.user_id
                 left join `jeecg-boot`.user_company uc on uc.user_id = cua.user_id
                 left join `jeecg-boot`.ctop_product product on product.id = cp.product_id where cua.account_id is not null;
    </insert>
    <select id="selectByParams" resultType="cn.com.ctop.report.entity.AccountAttributionInfo">
        select
               caai.*,
               cua.auth_name,
               cua.user_name
        from `jeecg-boot`.ctop_user_allocation cua
        left join `jeecg-boot`.ctop_account_attribution_info caai on caai.account_id = cua.account_id
        where cua.media_id in
        <foreach collection="mediaList" item="item" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
        <if test="orgCode!=null">
            and caai.org_code like '${orgCode}'
        </if>
        <if test="projectIds!=null">
            and cua.project_id in
            <foreach collection="projectIds" item="projectId" separator=","
                     open="(" close=")">
                #{projectId}
            </foreach>
        </if>
        <if test="userIds!=null">
            and cua.user_id in
            <foreach collection="userIds" item="userId" separator=","
                     open="(" close=")">
                #{userId}
            </foreach>
        </if>
    </select>
    <select id="selectDesignAccuntsByParams" resultType="cn.com.ctop.report.entity.AccountAttributionInfo">
        select caai.*
        from `jeecg-boot`.ctop_user_allocation cua
                 left join `jeecg-boot`.ctop_account_attribution_info caai on cua.account_id = caai.account_id
        where cua.account_status = 0
        <if test="mediaList != null">
            and cua.media_id in
            <foreach collection="mediaList" item="item" separator=","
                     open="(" close=")">
                #{item}
            </foreach>
        </if>
          and cua.project_id in (select distinct project_id from ctop_project_member where user_id = #{designUserId})
    </select>

        <select id="queryTodaySumReportBy" resultType="com.alibaba.fastjson.JSONObject">
            SELECT
            ROUND(sum(charge),3) cost,
            sum(`show`) photoShow,
            sum(photo_click) photoClick,
            sum(aClick) as aClick,
            max(`hour`) maxHour
            FROM
            kuaishou_account_report_hourly_dw
            WHERE
            stat_date = #{statDate}
            AND account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </select>
        <select id="queryBytedanceTodaySumReportBy" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            ROUND(sum(cost),3) cost,
            sum(click) photoClick,
            sum(`show`) photoShow,
            sum(`convert`) as `convert`,
        max(`hour`) maxHour
        FROM
            bytedance_advertiser_report_hourly_dw
        WHERE
            stat_datetime = #{statDate}
        AND account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>


        <select id="queryTodaySumByHour" resultType="com.alibaba.fastjson.JSONObject">
            SELECT
            ROUND(sum(charge),2) cost,
            sum(`show`) photoShow,
            sum(photo_click) photoClick,
            sum(aClick) as aClick
            FROM
            kuaishou_account_report_hourly_dw
            WHERE
            stat_date = #{statDate}
            AND `hour` &lt;= #{hour}
            AND account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </select>
    <select id="queryBytedanceTodaySumByHour" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        IFNULL(ROUND(sum(cost),2),0)  cost,
        IFNULL(sum(`show`),0)  photoShow,
        IFNULL(sum(click),0)  photoClick,
        IFNULL(sum(`convert`),0)  as `convert`
        FROM
        bytedance_advertiser_report_hourly_dw
        WHERE
        stat_datetime = #{statDate}
        AND `hour` &lt;= #{hour}
        AND account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>



        <select id="queryTodayDetailReportBy" resultType="com.alibaba.fastjson.JSONObject">
            SELECT
            ROUND(sum(charge),3) cost,
            sum(`show`) photoShow,
            sum(photo_click) photoClick,
            sum(aClick) as aClick,
            `hour` as statHour
            FROM
            kuaishou_account_report_hourly_dw
            WHERE
            stat_date = #{statDate}
            AND account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
            GROUP BY
            `hour`
            ORDER BY
            `hour` ASC
        </select>
    <select id="queryBytedanceTodayDetailReportBy" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ROUND(sum(cost),3) cost,
        sum(`show`) photoShow,
        sum(click) photoClick,
        sum(`convert`) as `convert`,
        `hour` as statHour
        FROM
        bytedance_advertiser_report_hourly_dw
        WHERE
        stat_datetime = #{statDate}
        AND account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        GROUP BY
        `hour`
        ORDER BY
        `hour` ASC
    </select>



        <select id="querySumByStartEndDate" resultType="com.alibaba.fastjson.JSONObject">
            SELECT
            ROUND(sum(charge),3) cost,
            sum(`show`) photoShow,
            sum(photo_click) photoClick,
            sum(aClick) as aClick
            FROM kuaishou_account_report_daily_dw t1
            WHERE t1.stat_date &lt;= #{endDate}
            AND t1.stat_date &gt;= #{startDate}
            AND account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
        </select>
        <select id="queryBuytedanceSumByStartEndDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            sum(cost) cost,
            sum(`show`) photoShow,
            sum(click) photoClick,
            sum(`convert`) as `convert`
        FROM bytedance_advertiser_report_daily_dw t1
        WHERE t1.stat_datetime &lt;= #{endDate}
        AND t1.stat_datetime &gt;= #{startDate}
        AND account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>



        <select id="queryDetailByStartEndDate" resultType="com.alibaba.fastjson.JSONObject">
            SELECT
            ROUND(sum(charge),3) cost,
            sum(`show`) photoShow,
            sum(photo_click) photoClick,
            sum(aClick) as aClick,
            stat_date as statDate
            FROM
            kuaishou_account_report_daily_dw
            WHERE stat_date &lt;= #{endDate}
            AND stat_date &gt;= #{startDate}
            AND account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
            group by stat_date
            order by stat_date asc
        </select>

    <select id="queryBytedanceDetailByStartEndDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ROUND(sum(cost),3) cost,
        sum(`show`) photoShow,
        sum(click) photoClick,
        sum(`convert`) as `convert`,
        stat_datetime as stat_datetime
        FROM
        bytedance_advertiser_report_daily_dw
        WHERE stat_datetime &lt;= #{endDate}
        AND stat_datetime &gt;= #{startDate}
        AND account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        group by stat_datetime
        order by stat_datetime asc
    </select>



        <select id="queryDetailGroupMonthByDate" resultType="com.alibaba.fastjson.JSONObject">
            SELECT
            ROUND(sum(charge),3) cost,
            sum(`show`) photoShow,
            sum(photo_click) photoClick,
            sum(aClick) as aClick,
            DATE_FORMAT(t1.stat_date, '%Y-%m') statDate
            FROM kuaishou_account_report_daily_dw t1
            WHERE t1.stat_date &lt;= #{endDate}
            AND t1.stat_date &gt;= #{startDate}
            AND t1.account_id in
            <foreach item="item" index="index" collection="accountIds"
                     open="(" separator="," close=")">
                #{item}
            </foreach>
            group by DATE_FORMAT(t1.stat_date, '%Y-%m')
            order by t1.stat_datetime asc
        </select>

    <select id="queryBytedanceDetailGroupMonthByDate" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ROUND(sum(cost),3) cost,
        sum(`show`) photoShow,
        sum(click) photoClick,
        sum(`convert`) as `convert`,
        DATE_FORMAT(t1.stat_datetime, '%Y-%m') statDate
        FROM bytedance_advertiser_report_daily_dw t1
        WHERE t1.stat_datetime &lt;= #{endDate}
        AND t1.stat_datetime &gt;= #{startDate}
        AND t1.account_id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        group by DATE_FORMAT(t1.stat_datetime, '%Y-%m')
        order by t1.stat_datetime asc
    </select>


</mapper>