<?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
        from ctop_bytedance_video_info video
        left join ctop_material_info cmi on video.signature = cmi.code and cmi.project_id = #{projectId}
        where account_id = #{accountId}
        and cmi.code is not null
        and cmi.offline_flag = 0
        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 desc
        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
        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.project_id = #{projectId}
        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})as video
        on a.material_id = video.material_id
        left join ctop_material_info cmi on cmi.code = video.signature and cmi.project_id = #{projectId}
        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 a.material_id desc
        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
        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.project_id = #{projectId}
                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})as video
                           on a.material_id = video.material_id
                 left join ctop_material_info cmi on cmi.code = video.signature and cmi.project_id = #{projectId}
        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 a.material_id desc
            limit #{videoCnt}
    </select>
</mapper>
