<?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.material.mapper.MaterialUploadTaskMapper">
    <insert id="replaceBatch">
        insert into ctop_material_upload_task(
        media_id,
        signature,
        account_id,
        kuaishou_user_id,
        shield_backward_switch,
        user_id,
        user_name,
        material_name,
        material_url,
        task_status,
        remarks
        )
        values
        <foreach collection="addList" item="add" separator=",">
            (
            #{add.mediaId},
            #{add.signature},
            #{add.accountId},
            #{add.kuaishouUserId},
            #{add.shieldBackwardSwitch},
            #{add.userId},
            #{add.userName},
            #{add.materialName},
            #{add.materialUrl},
            #{add.taskStatus},
            #{add.remarks}
            )
        </foreach>

    </insert>
    <insert id="addLong">
        insert into ctop_material_upload_bytedance_log(
        signature,
        project_id,
        video_id,
        account_id
        ) values (
         #{signature},
         #{projectId},
         #{videoId},
         #{accountId}
        )
    </insert>
    <update id="updateStatusById">
        UPDATE ctop_material_upload_task
        SET task_status = #{taskStatus},
        remarks = #{remarks},
        upload_time = #{uploadTime}
        WHERE
	    id = #{id}
    </update>
    <update id="syncBytedance">
        UPDATE ctop_material_info
        SET sync_bytedance = 2
        WHERE
	   `code` = #{signature}
    </update>
    <update id="syncKuaishou">
        UPDATE ctop_material_info
        SET sync_kuaishou = 2
        WHERE
	   `code` = #{signature}
    </update>

    <select id="getMediaList" resultType="java.lang.Integer">
        select media_id  from  ctop_material_upload_task
        WHERE signature = #{signature}
        AND task_status = 1
        GROUP BY media_id
    </select>
    <select id="getTokenByAccountId" resultType="java.lang.String">
        SELECT
	    access_token
        FROM
	    ctop_oauth_token
        WHERE
	    account_id = #{accountId}
    </select>
    <select id="getVideoSize" resultType="com.alibaba.fastjson.JSONObject">
    SELECT
	width,
	height
    FROM
	ctop_material_parameter
    WHERE
	material_id = #{signature}
	limit 1

    </select>
    <select id="getKuaiShouTokenByAccountId" resultType="java.lang.String">
     SELECT
       (CASE
		WHEN
		t2.access_token IS NOT NULL
		THEN
		t2.access_token
		ELSE t1.access_token
		END
		) as 'access_token'
		FROM
		ctop_oauth_token t1
		LEFT JOIN ctop_oauth_agent_token t2 ON t1.app_id = t2.app_id
	  WHERE
	    t1.account_id = #{accountId}
    </select>
    <select id="getProjectIdByAccoubt" resultType="java.lang.Long" parameterType="java.lang.Long">
        select project_id  from ctop_user_allocation where account_id = #{accountId} limit 1
    </select>
</mapper>