<?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.MaterialTopMapper">
    <select id="selectMapsPage" resultType="org.jeecg.modules.ctop.vo.MaterialTopVO">
        SELECT
            url,
            cover_url,
            name,
            stat_date ,
            sum(cost) as cost,
            sum(click) as click,
            sum(show_num) as show_num,
            sum(active) as active,
            sum(register) as register,
            sum(pay) as pay,
            sum(phone_convert) as phone_convert,
            sum(form_convert) as form_convert,
            convert_rate,
            sum(residue) as residue,
            sum(play3s) as play3s,
            sum(play10s) as play10s
        FROM
            ctop_material_report_total_result
        where
            ${ew.sqlSegment}
    </select>

    <insert id="insertByteDanceMaterialReportAccountByDate">
        INSERT INTO ctop_material_report_total_result (
            CODE,
            url,
            cover_url,
            NAME,
            stat_date,
            cost,
            click,
            show_num,
            active,
            register,
            pay,
            phone_convert,
            form_convert,
            residue,
            play3s,
            play10s,
            media_type
        ) SELECT
              t2.signature,
              IFNULL((SELECT
                          info.url
                      FROM
                          ctop_material_info info
                      WHERE
                          info.CODE = t2.signature
                      ORDER BY
                          create_time DESC
                      LIMIT 1),'') AS url,
              IFNULL((SELECT
                          info.cover_url
                      FROM
                          ctop_material_info info
                      WHERE
                          info.CODE = t2.signature
                      ORDER BY
                          create_time DESC
                      LIMIT 1),'') AS cover_url,
              IFNULL((
                         SELECT
                             info.material_name
                         FROM
                             ctop_material_info info
                         WHERE
                             info.CODE = t2.signature
                         ORDER BY
                             create_time DESC
                         LIMIT 1
                     ),'')
                                   AS NAME,
              t1.stat_datetime AS statDate,
              sum(t1.cost) AS cost,
              sum(t1.click) click,
              0 AS showNum,
              sum(t1.active) AS active,
              sum(t1.register) AS register,
              sum(t1.pay_count) AS pay,
              sum(t1.phone_confirm) AS phoneConvert,
              sum(t1.form) AS formConvert,
              sum(t1.next_day_open) AS residue,
              sum(t1.play_duration_3s) AS play3s,
              sum(t1.play_duration_10s) AS play3s,
              1 AS mediaType
        FROM
            ctop_bytedance_report_material_daily t1
                LEFT JOIN ctop_bytedance_video_info t2 ON t1.material_id = t2.material_id
        WHERE
            1 = 1
          AND t1.stat_datetime = #{statDate}
          AND t1.image_mode in ('CREATIVE_IMAGE_MODE_VIDEO','CREATIVE_IMAGE_MODE_VIDEO_VERTICAL')
          AND t2.signature IS NOT NULL
          AND t2.signature != ""
        GROUP BY
            t2.signature,
            t1.stat_datetime;
    </insert>

    <insert id ="insertKuaiShouMaterialReportAccountByDate">
        INSERT INTO ctop_material_report_total_result (
            CODE,
            url,
            cover_url,
            NAME,
            stat_date,
            cost,
            click,
            show_num,
            active,
            register,
            form_convert,
            media_type
        ) SELECT
              t1.signature AS 'code',
              IFNULL((SELECT
                          info.url
                      FROM
                          ctop_material_info info
                      WHERE
                          info.CODE = t2.signature
                      ORDER BY
                          create_time DESC
                      LIMIT 1),'') AS url,
              IFNULL((SELECT
                          info.cover_url
                      FROM
                          ctop_material_info info
                      WHERE
                          info.CODE = t2.signature
                      ORDER BY
                          create_time DESC
                      LIMIT 1),'') AS cover_url,
              IFNULL((
                         SELECT
                             info.material_name
                         FROM
                             ctop_material_info info
                         WHERE
                             info.CODE = t2.signature
                         ORDER BY
                             create_time DESC
                         LIMIT 1
                     ),'')
                  AS NAME,
              t1.stat_date AS 'stat_date',
              sum(t1.charge) AS 'cost',
              sum(t1.photo_click) AS 'click',
              sum(t1.photo_show) AS 'show_num',
              sum(t1.activation) AS 'active',
              sum(t1.event_register) AS 'register',
              sum(t1.form_count) AS 'form_convert',
              2 AS 'media_type'
        FROM
            ctop_kuaishou_report_daily_creative t1
        WHERE
            t1.signature IS NOT NULL
          AND t1.stat_date = #{statDate}
        GROUP BY
            t1.stat_date,
            t1.signature;
    </insert>

    <delete id="deletedMaterialReportAccountByDate">
        DELETE FROM ctop_material_report_total_result where stat_date= #{statDate}
    </delete>

</mapper>
