<?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.bytedance.mapper.MaterialReportMapper">


    <select id="getRoleCodeByUserId" parameterType="Object" resultType="java.lang.String">
	SELECT
    role_code
    FROM
    sys_role
    WHERE
    id = (
    SELECT
    t2.role_id
    FROM
    sys_user t1
    LEFT JOIN sys_user_role t2 ON t1.id = t2.user_id
    WHERE
    t1.id = #{userId,jdbcType=VARCHAR}
    )
	</select>


    <select id="selectMaxHourByDate" resultType="java.lang.Integer">
        select
        max(stat_hour)
       from
        ctop_kuaishou_report_hourly_creative_statistic
        where stat_date = #{statDate}

      </select>


    <select id="selectByMap" parameterType="Map" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        sum(t2.charge) cost, -- 花费
        sum(t2.photo_show) photoShow, -- 封面曝光数
        sum(t2.photo_click) photoClick, -- 封面点击数
        sum(t2.aclick) aclick, -- 素材曝光数
        sum(t2.bclick) bclick, -- 行为数
        (case
        when sum(photo_show) != 0
        then (sum(t2.photo_click) / sum(photo_show))
        else 0
        end
        ) photoClickRatio, -- 封面点击率

        (case
        when sum(aclick) != 0
        then (sum(t2.bclick) / sum(aclick))
        else 0
        end
        ) actionRatio, -- 行为率

        (case
        when sum(t2.photo_show) != 0
        then ((sum(t2.charge) / sum(t2.photo_show)) * 1000)
        else 0
        end
        ) impression1kCost, -- 均千次封面曝光花费

        (case
        when sum(t2.photo_click) != 0
        then (sum(t2.charge) / sum(t2.photo_click))
        else 0
        end
        ) photoClickCost, -- 平均封面点击单价

        (CASE
        WHEN sum(t2.photo_click) != 0
        THEN (sum(t2.charge) / sum(t2.photo_click))
        ELSE 0
        end
        ) actionCost, -- 平均行为单价

        (case
        WHEN sum(t2.form_count) != 0
        THEN sum(t2.form_count)
        WHEN sum(t2.activation) != 0
        THEN sum(t2.activation)
        ELSE 0
        END
        ) conversions, -- 转化数

        (
        CASE
        WHEN sum(t2.form_count) != 0
        THEN (sum(t2.charge) / sum(t2.form_count))
        WHEN sum(t2.activation) != 0
        THEN (sum(t2.charge) / sum(t2.activation))
        ELSE 0
        END
        ) conversionPrice, -- 转化单价

        t2.url url, -- 视频url
        t2.signature signature -- 视频md5
        from
        ctop_user_allocation t1
        left join
        ctop_kuaishou_report_hourly_creative_statistic t2
        on t1.account_id = t2.account_id
        <where>
            <if test="userId != null">
                and t1.user_id = #{userId,jdbcType=VARCHAR}
            </if>
            and t1.media_id = '2'
            and t1.account_id != ''
            and t2.signature != ''
            <if test="startDate != null">
                and t2.stat_date = #{startDate}
            </if>
            <if test="statHour != null">
                and t2.stat_hour &lt;= #{statHour}
            </if>
        </where>

        group by t2.signature
        order by sum(t2.charge) desc
        limit 100


    </select>


    <select id="selectChainRatioByMap" parameterType="Map" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        sum(charge) cost, -- 花费
        sum(photo_show) photoShow, -- 封面曝光数
        sum(photo_click) photoClick, -- 封面点击数
        sum(aclick) aclick, -- 素材曝光数
        sum(bclick) bclick, -- 行为数
        (case
        when sum(photo_show) != 0
        then (sum(photo_click) / sum(photo_show))
        else 0
        end
        ) photoClickRatio, -- 封面点击率

        (case
        when sum(aclick) != 0
        then (sum(bclick) / sum(aclick))
        else 0
        end
        ) actionRatio, -- 行为率

        (case
        when sum(photo_show) != 0
        then ((sum(charge) / sum(photo_show)) * 1000)
        else 0
        end
        ) impression1kCost, -- 均千次封面曝光花费

        (case
        when sum(photo_click) != 0
        then (sum(charge) / sum(photo_click))
        else 0
        end
        ) photoClickCost, -- 平均封面点击单价

        (CASE
        WHEN sum(photo_click) != 0
        THEN (sum(charge) / sum(photo_click))
        ELSE 0
        end
        ) actionCost, -- 平均行为单价

        (case
        WHEN sum(form_count) != 0
        THEN sum(form_count)
        WHEN sum(activation) != 0
        THEN sum(activation)
        ELSE 0
        END
        ) conversions, -- 转化数

        (
        CASE
        WHEN sum(form_count) != 0
        THEN (sum(charge) / sum(form_count))
        WHEN sum(activation) != 0
        THEN (sum(charge) / sum(activation))
        ELSE 0
        END
        ) conversionPrice -- 转化单价
        from
        ctop_kuaishou_report_hourly_creative_statistic

        <where>
            <if test="statDate != null">
                and stat_date = #{statDate}
            </if>
            <if test="statHour != null">
                and stat_hour &lt;= #{statHour}
            </if>
            <if test="signature != null">
                and signature = #{signature}
            </if>
            <if test="accountId != null">
                and account_id = #{accountId}
            </if>

        </where>
        group by signature

    </select>


    <select id="selectDailyByMap" parameterType="Map" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        sum(t2.charge) cost, -- 花费
        sum(t2.photo_show) photoShow, -- 封面曝光数
        sum(t2.photo_click) photoClick, -- 封面点击数
        sum(t2.aclick) aclick, -- 素材曝光数
        sum(t2.bclick) bclick, -- 行为数
        (case
        when sum(t2.photo_show) != 0
        then (sum(t2.photo_click) / sum(t2.photo_show))
        else 0
        end
        ) photoClickRatio, -- 封面点击率

        (case
        when sum(t2.aclick) != 0
        then (sum(t2.bclick) / sum(t2.aclick))
        else 0
        end
        ) actionRatio, -- 行为率

        (case
        when sum(t2.photo_show) != 0
        then ((sum(t2.charge) / sum(t2.photo_show)) * 1000)
        else 0
        end
        ) impression1kCost, -- 均千次封面曝光花费

        (case
        when sum(t2.photo_click) != 0
        then (sum(t2.charge) / sum(t2.photo_click))
        else 0
        end
        ) photoClickCost, -- 平均封面点击单价

        (CASE
        WHEN sum(t2.photo_click) != 0
        THEN (sum(t2.charge) / sum(t2.photo_click))
        ELSE 0
        end
        ) actionCost, -- 平均行为单价

        (case
        WHEN sum(t2.form_count) != 0
        THEN sum(t2.form_count)
        WHEN sum(t2.activation) != 0
        THEN sum(t2.activation)
        ELSE 0
        END
        ) conversions, -- 转化数

        (
        CASE
        WHEN sum(t2.form_count) != 0
        THEN (sum(t2.charge) / sum(t2.form_count))
        WHEN sum(t2.activation) != 0
        THEN (sum(t2.charge) / sum(t2.activation))
        ELSE 0
        END
        ) conversionPrice, -- 转化单价

        t2.url url, -- 视频url
        t2.signature signature -- 视频md5
        from
        ctop_user_allocation t1
        left join
        ctop_kuaishou_report_daily_creative_statistic t2
        on t1.account_id = t2.account_id
        <where>
            <if test="userId != null">
                and t1.user_id = #{userId,jdbcType=VARCHAR}
            </if>
            and t1.media_id = '2'
            and t1.account_id != ''
            and t2.signature != ''
            <if test="startDate != null">
                and t2.stat_date &lt;= #{startDate}
            </if>
            <if test="endDate != null">
                and stat_date &gt;= #{endDate}
            </if>
        </where>
        group by t2.signature
        order by sum(t2.charge) desc
        limit 100
    </select>


    <select id="selectDailyChainRatioByMap" parameterType="Map" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        sum(charge) cost, -- 花费
        sum(photo_show) photoShow, -- 封面曝光数
        sum(photo_click) photoClick, -- 封面点击数
        sum(aclick) aclick, -- 素材曝光数
        sum(bclick) bclick, -- 行为数
        (case
        when sum(photo_show) != 0
        then (sum(photo_click) / sum(photo_show))
        else 0
        end
        ) photoClickRatio, -- 封面点击率

        (case
        when sum(aclick) != 0
        then (sum(bclick) / sum(aclick))
        else 0
        end
        ) actionRatio, -- 行为率

        (case
        when sum(photo_show) != 0
        then ((sum(charge) / sum(photo_show)) * 1000)
        else 0
        end
        ) impression1kCost, -- 均千次封面曝光花费

        (case
        when sum(photo_click) != 0
        then (sum(charge) / sum(photo_click))
        else 0
        end
        ) photoClickCost, -- 平均封面点击单价

        (CASE
        WHEN sum(photo_click) != 0
        THEN (sum(charge) / sum(photo_click))
        ELSE 0
        end
        ) actionCost, -- 平均行为单价

        (case
        WHEN sum(form_count) != 0
        THEN sum(form_count)
        WHEN sum(activation) != 0
        THEN sum(activation)
        ELSE 0
        END
        ) conversions, -- 转化数

        (
        CASE
        WHEN sum(form_count) != 0
        THEN (sum(charge) / sum(form_count))
        WHEN sum(activation) != 0
        THEN (sum(charge) / sum(activation))
        ELSE 0
        END
        ) conversionPrice -- 转化单价
        from
        ctop_kuaishou_report_daily_creative_statistic
        <where>
            <if test="signature != null">
                and signature = #{signature}
            </if>
            <if test="statDate != null">
                and stat_date = #{statDate}
            </if>
            <if test="startDate != null">
                and stat_date &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                and stat_date &lt;= #{endDate}
            </if>

            <if test="accountId != null">
                and account_id = #{accountId}
            </if>
        </where>
        group by signature
    </select>


    <select id="selectHourMaterialList" parameterType="Map" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        sum(t2.charge) cost, -- 花费
        sum(t2.photo_show) photoShow, -- 封面曝光数
        sum(t2.photo_click) photoClick, -- 封面点击数
        sum(t2.aclick) aclick, -- 素材曝光数
        sum(t2.bclick) bclick, -- 行为数
        (case
        when sum(photo_show) != 0
        then (sum(t2.photo_click) / sum(photo_show))
        else 0
        end
        ) photoClickRatio, -- 封面点击率

        (case
        when sum(aclick) != 0
        then (sum(t2.bclick) / sum(aclick))
        else 0
        end
        ) actionRatio, -- 行为率

        (case
        when sum(t2.photo_show) != 0
        then ((sum(t2.charge) / sum(t2.photo_show)) * 1000)
        else 0
        end
        ) impression1kCost, -- 均千次封面曝光花费

        (case
        when sum(t2.photo_click) != 0
        then (sum(t2.charge) / sum(t2.photo_click))
        else 0
        end
        ) photoClickCost, -- 平均封面点击单价

        (CASE
        WHEN sum(t2.photo_click) != 0
        THEN (sum(t2.charge) / sum(t2.photo_click))
        ELSE 0
        end
        ) actionCost, -- 平均行为单价

        (case
        WHEN sum(t2.form_count) != 0
        THEN sum(t2.form_count)
        WHEN sum(t2.activation) != 0
        THEN sum(t2.activation)
        ELSE 0
        END
        ) conversions, -- 转化数

        (
        CASE
        WHEN sum(t2.form_count) != 0
        THEN (sum(t2.charge) / sum(t2.form_count))
        WHEN sum(t2.activation) != 0
        THEN (sum(t2.charge) / sum(t2.activation))
        ELSE 0
        END
        ) conversionPrice, -- 转化单价

        t2.url url, -- 视频url
        t2.signature signature, -- 视频md5
        t2.account_id accountId
        from
        ctop_oauth_token t1
        left join
        ctop_kuaishou_report_hourly_creative_statistic t2
        on t1.id = t2.account_id
        where t1.id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        and t1.media_id = '2'
        <if test="statHour != null">
            and t2.stat_hour &lt;= #{statHour}
        </if>
        <if test="statDate != null">
            and t2.stat_date = #{statDate}
        </if>
        and t2.signature != ''
        GROUP BY t2.signature
        order by sum(t2.charge) desc
        limit 100
    </select>


    <select id="selectDayMaterialList" parameterType="Map" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        sum(t2.charge) cost, -- 花费
        sum(t2.photo_show) photoShow, -- 封面曝光数
        sum(t2.photo_click) photoClick, -- 封面点击数
        sum(t2.aclick) aclick, -- 素材曝光数
        sum(t2.bclick) bclick, -- 行为数
        (case
        when sum(photo_show) != 0
        then (sum(t2.photo_click) / sum(photo_show))
        else 0
        end
        ) photoClickRatio, -- 封面点击率

        (case
        when sum(aclick) != 0
        then (sum(t2.bclick) / sum(aclick))
        else 0
        end
        ) actionRatio, -- 行为率

        (case
        when sum(t2.photo_show) != 0
        then ((sum(t2.charge) / sum(t2.photo_show)) * 1000)
        else 0
        end
        ) impression1kCost, -- 均千次封面曝光花费

        (case
        when sum(t2.photo_click) != 0
        then (sum(t2.charge) / sum(t2.photo_click))
        else 0
        end
        ) photoClickCost, -- 平均封面点击单价

        (CASE
        WHEN sum(t2.photo_click) != 0
        THEN (sum(t2.charge) / sum(t2.photo_click))
        ELSE 0
        end
        ) actionCost, -- 平均行为单价

        (case
        WHEN sum(t2.form_count) != 0
        THEN sum(t2.form_count)
        WHEN sum(t2.activation) != 0
        THEN sum(t2.activation)
        ELSE 0
        END
        ) conversions, -- 转化数

        (
        CASE
        WHEN sum(t2.form_count) != 0
        THEN (sum(t2.charge) / sum(t2.form_count))
        WHEN sum(t2.activation) != 0
        THEN (sum(t2.charge) / sum(t2.activation))
        ELSE 0
        END
        ) conversionPrice, -- 转化单价

        t2.url url, -- 视频url
        t2.signature signature, -- 视频md5
        t2.account_id accountId
        from
        ctop_oauth_token t1
        left join
        ctop_kuaishou_report_daily_creative_statistic t2
        on t1.id = t2.account_id
        where t1.id in
        <foreach item="item" index="index" collection="accountIds"
                 open="(" separator="," close=")">
            #{item}
        </foreach>
        and t1.media_id = '2'

        <if test="startDate != null">
            and t2.stat_date &gt;= #{startDate}
        </if>

        <if test="endDate != null">
            and t2.stat_date &lt;= #{endDate}
        </if>
        and t2.signature != ''
        GROUP BY t2.signature
        order by sum(t2.charge) desc
        limit 100
    </select>


</mapper>
