<?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.bytedance.advertise.mapper.ByteDanceVideoInfoMapper">
    <insert id="replaceBatch">
        replace into ctop_bytedance_video_info(
        id,
        vid,
        advertiser_id,
        account_id,
        material_id,
        url,
        material_upload_time,
        filename,
        format,
        poster_url,
        bit_rate,
        source,
        file_id,
        width,
        height,
        duration,
        size,
        video_url,
        status,
        create_time,
        update_time,
        signature)
        values
        <foreach collection="videoList" item="video" separator=",">
            (
            #{video.id},
            #{video.vid},
            #{video.advertiserId},
            #{video.accountId},
            #{video.materialId},
            #{video.url},
            #{video.materialUploadTime},
            #{video.filename},
            #{video.format},
            #{video.posterUrl},
            #{video.bitRate},
            #{video.source},
            #{video.fileId},
            #{video.width},
            #{video.height},
            #{video.duration},
            #{video.size},
            #{video.videoUrl},
            #{video.status},
            #{video.createTime},
            #{video.updateTime},
            #{video.signature}
            )
        </foreach>
    </insert>
    <update id="updateVideoVerticalType">
        update
            ctop_bytedance_video_info
        set type = 2
        where height &gt; width and type is null
        and (height between 1280 and 2560)  and (width between 720 and 1440)
    </update>
    <update id="updateVideoHorizonType">
        update ctop_bytedance_video_info
            set type = 1
        where height &lt; width and type is null
        and (width between 1280 and 2560)  and (height between 720 and 1440)
    </update>
    <select id="getNewVideoByParams"
            resultType="org.jeecg.modules.bytedance.advertise.entity.ByteDanceVideoInfo">
        select distinct video.*,
        (select count(1) from ctop_bytedance_ai_video_creative_log l where l.video_code = video.signature and l.account_id = video.account_id and l.state_date = #{now}) as creativeNum,
        (select count(1) from ctop_bytedance_creative c where c.video_id = video.vid and c.account_id = #{accountId}) as creativeCnt,
        (select count(1) from ctop_bytedance_video_slogen_info s where s.video_code = video.signature) as slogens,
         'video' As materialType
        from ctop_bytedance_video_info video
        left join ctop_material_info cmi on video.signature = cmi.code
        <if test="projectId !=null and projectId != ''">
            and cmi.project_id = #{projectId}
        </if>
        where account_id = '${accountId}'
        and cmi.code is not null
        and cmi.status = 1
        and cmi.offline_flag = 0
        and video.status = 1
        and video.type = #{imageMode}
        and material_upload_time &gt;= #{startTime}
        and material_upload_time &lt;= #{endTime}
        having creativeCnt &lt;= #{creativeCnt} and slogens >0 and creativeNum = 0
        order by material_upload_time
        limit #{videoCnt}
    </select>
    <select id="topMatByParams" resultType="org.jeecg.modules.bytedance.advertise.entity.ByteDanceVideoInfo">
        select distinct video.*,
        a.totalCost,
        (select count(1) from ctop_bytedance_ai_video_creative_log l where l.video_code = video.signature and l.account_id = video.account_id and l.state_date = #{now}) as creativeNum,
        (select count(1) from ctop_bytedance_creative c where c.video_id = video.vid and c.account_id = #{accountId}) as creativeCnt,
        (select count(1) from ctop_bytedance_video_slogen_info s where s.video_code = video.signature ) as slogens,
         'video' As materialType
        from (select report.material_id, sum(cost) as totalCost
        from ctop_bytedance_report_material_daily report
        left join ctop_user_allocation cua on report.account_id = cua.account_id
        where cua.account_id = '${accountId}'
        and report.stat_datetime &gt;= #{startTime}
        and report.stat_datetime &lt;= #{endTime}
        group by report.material_id
        order by sum(report.cost) desc ) a
        left join (select distinct a.* from ctop_bytedance_video_info a where a.account_id = '${accountId}' and a.type = #{imageMode} and a.status = 1)as video
        on a.material_id = video.material_id
        left join ctop_material_info cmi on cmi.code = video.signature
        <if test="projectId !=null and projectId != ''">
            and cmi.project_id = #{projectId}
        </if>
        where cmi.id is not null
          and video.id is not null
          and cmi.offline_flag = 0
        and a.totalCost &gt;= 500
        having creativeCnt &lt;= 100
           and creativeNum = 0
        and slogens >0
        order by material_upload_time
        limit #{videoCnt}
    </select>

    <select id="historyMatByParams" resultType="org.jeecg.modules.bytedance.advertise.entity.ByteDanceVideoInfo">
        select distinct video.*,
               a.totalCost,
               (select count(1) from ctop_bytedance_ai_video_creative_log l where l.video_code = video.signature and l.account_id = video.account_id and l.state_date = #{now}) as creativeNum,
               (select count(1) from ctop_bytedance_creative c where c.video_id = video.vid and c.account_id = #{accountId}) as creativeCnt,
               (select count(1) from ctop_bytedance_video_slogen_info s where s.video_code = video.signature) as slogens,
               'video' As materialType
        from (select report.material_id, sum(cost) as totalCost
              from ctop_bytedance_report_material_daily report
                       left join ctop_user_allocation cua on report.account_id = cua.account_id
              where cua.account_id = '${accountId}'
                and report.stat_datetime &gt;= #{startTime}
                and report.stat_datetime &lt;= #{endTime}
              group by report.material_id
              order by sum(report.cost) desc ) a
                 left join (select distinct a.* from ctop_bytedance_video_info a where a.account_id = '${accountId}' and a.type = #{imageMode} and a.status = 1)as video
                           on a.material_id = video.material_id
                 left join ctop_material_info cmi on cmi.code = video.signature
        <if test="projectId !=null and projectId != ''">
            and cmi.project_id = #{projectId}
        </if>
        where cmi.id is not null
          and video.id is not null
          and cmi.offline_flag = 0
          and a.totalCost &lt;= 100
        having creativeCnt &lt; 20
           and creativeNum = 0
           and slogens >0
        order by material_upload_time
            limit #{videoCnt}
    </select>


    <!-- 图片 上新 遗漏 -->
    <select id="imageListByParams" resultType="org.jeecg.modules.bytedance.advertise.entity.ByteDanceVideoInfo">
        select distinct image.*,
        (select count(1) from ctop_bytedance_ai_video_creative_log l
        where l.video_code = image.signature
        and l.account_id = image.account_id
        and l.state_date = #{now}) as creativeNum,
        (select count(1) from ctop_bytedance_creative c
        where SUBSTRING_INDEX(SUBSTRING_INDEX(image_ids,'["',-1),'"]',1) in(image.id)
        and c.account_id = '${accountId}') as creativeCnt,
        'image' as materialType
        from ctop_bytedance_image_info image
        where account_id = #{accountId}
        <if test="imageMode == '1'.toString()">
            and image.type = '1'
        </if>
        <if test="imageMode == '2'.toString()">
            and (image.type = '2' or image.type = '3')
        </if>
        <if test="imageMode == '3'.toString()">
            and image.type = '3'
        </if>
        and image.image_create_time &gt;= #{startTime}
        and image.image_create_time &lt;= #{endTime}
        having creativeCnt &lt;= #{creativeCnt}
        and creativeNum = 0
        order by image.image_create_time
            limit #{videoCnt}
    </select>


    <!-- 图片 跑量-->
    <select id="imageTopMatByParams" resultType="org.jeecg.modules.bytedance.advertise.entity.ByteDanceVideoInfo">
        select distinct image.*,
        a.totalCost,
        (select count(1) from ctop_bytedance_ai_video_creative_log l
        where l.video_code = image.signature
        and l.account_id = image.account_id
        and l.state_date =#{now}) as creativeNum,
        (select count(1)
        from ctop_bytedance_creative c
        where SUBSTRING_INDEX(SUBSTRING_INDEX(image_ids,'["',-1),'"]',1) in(image.id)
        and c.account_id = '${accountId}')  as creativeCnt
        from (select report.material_id, sum(cost) as totalCost,
         'image' as materialType
        from ctop_bytedance_report_image_daily report
        left join ctop_user_allocation cua on report.account_id = cua.account_id
        where cua.account_id = #{accountId}
        and report.stat_datetime &gt;= #{startTime}
        and report.stat_datetime &lt;= #{endTime}
        group by report.material_id
        order by sum(report.cost) desc ) a
        left join (select distinct a.* from ctop_bytedance_image_info a where 1 = 1  and a.account_id = #{accountId}
        <if test="imageMode == '1'.toString()">
            and a.type = '1'
        </if>
        <if test="imageMode == '2'.toString()">
            and (a.type = '2' or a.type = '3')
        </if>
        <if test="imageMode == '3'.toString()">
            and a.type = '3'
        </if>)as image
        on a.material_id = image.material_id
        where 1=1
        and a.totalCost &gt; 500
        having creativeCnt &lt;= 100
        and creativeNum = 0
        order by image_create_time
            limit #{videoCnt}
    </select>


    <!-- 图片 打捞-->
    <select id="imageHistoryMatByParams" resultType="org.jeecg.modules.bytedance.advertise.entity.ByteDanceVideoInfo">
        select distinct image.*,
        a.totalCost,
        (select count(1)
        from ctop_bytedance_ai_video_creative_log l
        where l.video_code = image.signature
        and l.account_id = image.account_id
        and l.state_date = #{now}) as creativeNum,
        (select count(1)
        from ctop_bytedance_creative c
        where SUBSTRING_INDEX(SUBSTRING_INDEX(image_ids,'["',-1),'"]',1) in(image.id)
        and c.account_id = '${accountId}') as creativeCnt
        from (select report.material_id, sum(cost) as totalCost,
        'image' as materialType
        from ctop_bytedance_report_image_daily report
        left join ctop_user_allocation cua on report.account_id = cua.account_id
        where cua.account_id = #{accountId}
        and report.stat_datetime &gt;= #{startTime}
        and report.stat_datetime &lt;= #{endTime}
        group by report.material_id
        order by sum(report.cost) desc) a
        left join (select distinct a.* from ctop_bytedance_image_info a where 1 = 1 and a.account_id =#{accountId}
        <if test="imageMode == '1'.toString()">
            and a.type = '1'
        </if>
        <if test="imageMode == '2'.toString()">
            and (a.type = '2' or a.type = '3')
        </if>
        <if test="imageMode == '3'.toString()">
            and a.type = '3'
        </if>) as image
        on a.material_id = image.material_id
        where 1=1
        and a.totalCost &lt;= 100
        having creativeCnt &lt; 20
        and creativeNum = 0
        order by image_create_time
            limit #{videoCnt}
    </select>


</mapper>
