<?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.toutiao.modules.material.mapper.BytedanceEffectVideoInfoMapper">

    <update id="updateTotalCost">
        update
            ctop_bytedance_effect_video_info effect
        set
            effect.total_cost  = (
                select
                    sum(report.cost)
                from
                    ctop_bytedance_report_material_daily report
                left join ctop_bytedance_video_info video on report.material_id = video.material_id
                where
                    video.signature = effect.code
                and
                    report.image_mode in ('CREATIVE_IMAGE_MODE_VIDEO','CREATIVE_IMAGE_MODE_VIDEO_VERTICAL')
            )
        where effect.code is not null
    </update>
    <update id="updateFinalCostDate">
        update
            ctop_bytedance_effect_video_info effect
        set
            effect.final_cost_date  = (
                select
                    max(stat_datetime)
                from
                    ctop_bytedance_report_material_daily report
                left join ctop_bytedance_video_info video on report.material_id = video.material_id
                where
                    video.signature = effect.code
                and
                    report.image_mode in ('CREATIVE_IMAGE_MODE_VIDEO','CREATIVE_IMAGE_MODE_VIDEO_VERTICAL')
            )
        where effect.code is not null
    </update>
    <update id="updateTwoWeekCost">
        update
            ctop_bytedance_effect_video_info effect
        set
            effect.two_week_cost  = (
                select
                    sum(report.cost)
                from
                    ctop_bytedance_report_material_daily report
                left join ctop_bytedance_video_info video on report.material_id = video.material_id
                where
                    video.signature = effect.code
                and
                    report.image_mode in ('CREATIVE_IMAGE_MODE_VIDEO','CREATIVE_IMAGE_MODE_VIDEO_VERTICAL')
                and
                    report.stat_datetime&lt;= DATE_ADD(effect.initial_cost_date,INTERVAL 13 DAY)
            )
        where effect.code is not null
    </update>
    <update id="updateShot">
        update
            ctop_bytedance_effect_video_info effect
        set shot = (
            select
                ascr.shot_id
            from
                ctop_material_ascription ascr
            where
                ascr.material_id = effect.code
            limit 1 )
        where effect.code is not null
    </update>
    <update id="updateClip">
        update
            ctop_bytedance_effect_video_info effect
        set clip = (
            select
                ascr.clip_id
            from
                ctop_material_ascription ascr
            where
                ascr.material_id = effect.code
            limit 1 )
        where effect.code is not null
    </update>
    <update id="updatePlan">
        update
            ctop_bytedance_effect_video_info effect
        set plan = (
            select
                ascr.plan_id
            from
                ctop_material_ascription ascr
            where
                ascr.material_id = effect.code
            limit 1 )
        where effect.code is not null
    </update>
    <update id="updatePlane">
        update
            ctop_bytedance_effect_video_info effect
        set plane = (
            select
                ascr.plane_id
            from
                ctop_material_ascription ascr
            where
                ascr.material_id = effect.code
            limit 1 )
        where effect.code is not null
    </update>
    <update id="updateManager">
        update
            ctop_bytedance_effect_video_info effect
        set manager = (
            SELECT
                user.id
            FROM
                sys_user user
            LEFT JOIN sys_user_role userRole ON user.id = userRole.user_id
            LEFT JOIN sys_role role ON userRole.role_id = role.id
            WHERE
                user.org_code = (select org_code from sys_user where id = effect.clip)
            AND role.role_code = 'designTeamLeader')
        where effect.clip is not null
    </update>
</mapper>
