<?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="com.backendsys.mapper.Ai.AiGenerateVideoMapper">

    <sql id="includeAiGenerateVideo">
        id,
        user_id,
        job_id,
        title,
        source,
        resolution,
        product_type,
        COALESCE(tts_per, '') tts_per,
        COALESCE(bg_music_url, '') bg_music_url,
        COALESCE(video_begin_url, '') video_begin_url,
        COALESCE(video_end_url, '') video_end_url,
        COALESCE(video_logo_url, '') video_logo_url,
        COALESCE(video_logo_location, '') video_logo_location,
        COALESCE(video_logo_margin, '') video_logo_margin,
        COALESCE(job_status, '') job_status,
        COALESCE(result_video_url, '') result_video_url,
        COALESCE(result_points, '') result_points,
        COALESCE(result_basic_duration, '') result_basic_duration,
        COALESCE(result_fail_reason, '') result_fail_reason,
        create_time,
        update_time
    </sql>

    <resultMap id="resultMapAiGenerateVideo" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="user_id" column="user_id" javaType="java.lang.Long" />
        <result property="job_id" column="job_id" />
        <result property="title" column="title" />
        <result property="source" column="source" />
        <result property="resolution" column="resolution" />
        <result property="product_type" column="product_type" />
        <result property="tts_per" column="tts_per" />
        <result property="bg_music_url" column="bg_music_url" />
        <result property="video_begin_url" column="video_begin_url" />
        <result property="video_end_url" column="video_end_url" />
        <result property="video_logo_url" column="video_logo_url" />
        <result property="video_logo_location" column="video_logo_location" />
        <result property="video_logo_margin" column="video_logo_margin" />
        <result property="job_status" column="job_status" javaType="java.lang.Integer" />
        <result property="result_video_url" column="result_video_url" />
        <result property="result_points" column="result_points" />
        <result property="result_basic_duration" column="result_basic_duration" />
        <result property="result_fail_reason" column="result_fail_reason" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <!-- 列表 -->
    <select id="queryAiGenerateVideoList" resultMap="resultMapAiGenerateVideo">
        SELECT <include refid="includeAiGenerateVideo" />
        FROM ai_generate_video_task
        <where>
            user_id = #{user_id}
            <if test="job_id != null and job_id != ''">
                AND job_id = #{job_id}
            </if>
            <if test="title != null and title != ''">
                AND title LIKE CONCAT('%', #{title}, '%')
            </if>
            <if test="product_type != null and product_type != ''">
                AND product_type = #{product_type}
            </if>
            <if test="job_status != null and job_status != ''">
                AND job_status = #{job_status}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>

    <!-- 详情 -->
    <select id="queryAiGenerateVideoDetail" resultMap="resultMapAiGenerateVideo">
        SELECT <include refid="includeAiGenerateVideo" />
        FROM ai_generate_video_task
        WHERE job_id = #{job_id} AND user_id = #{user_id}
    </select>

    <!-- 插入 -->
    <insert id="insertAiGenerateVideo" parameterType="com.backendsys.entity.Ai.AiGenerateVideoDTO"
        useGeneratedKeys="true" keyProperty="job_id">
        INSERT INTO ai_generate_video_task (
            user_id, job_id, title, source, product_type, resolution
            <if test="tts_per != null and tts_per != ''">, tts_per</if>
            <if test="bg_music_url != null and bg_music_url != ''">, bg_music_url</if>
            <if test="video_begin_url != null and video_begin_url != ''">, video_begin_url</if>
            <if test="video_end_url != null and video_end_url != ''">, video_end_url</if>
            <if test="video_logo_url != null and video_logo_url != ''">, video_logo_url</if>
            <if test="video_logo_location != null and video_logo_location != ''">, video_logo_location</if>
            <if test="video_logo_margin != null and video_logo_margin != ''">, video_logo_margin</if>
            <if test="job_status != null and job_status != ''">, job_status</if>
        )
        VALUES (
            #{user_id}, #{job_id}, #{title}, #{source}, #{product_type}, #{resolutionString}
            <if test="tts_per != null and tts_per != ''">, #{tts_per}</if>
            <if test="bg_music_url != null and bg_music_url != ''">, #{bg_music_url}</if>
            <if test="video_begin_url != null and video_begin_url != ''">, #{video_begin_url}</if>
            <if test="video_end_url != null and video_end_url != ''">, #{video_end_url}</if>
            <if test="video_logo_url != null and video_logo_url != ''">, #{video_logo_url}</if>
            <if test="video_logo_location != null and video_logo_location != ''">, #{video_logo_location}</if>
            <if test="video_logo_margin != null and video_logo_margin != ''">, #{video_logo_margin}</if>
            <if test="job_status != null and job_status != ''">, #{job_status}</if>
        )
    </insert>

    <!-- 更新 -->
    <update id="updateAiGenerateVideo" parameterType="com.backendsys.entity.Ai.AiGenerateVideoDTO"
        useGeneratedKeys="true" keyProperty="job_id">
        UPDATE ai_generate_video_task
        SET job_status = #{job_status}
            <if test="result_video_url != null and result_video_url != ''">, result_video_url = #{result_video_url}</if>
            <if test="result_points != null and result_points != ''">, result_points = #{result_points}</if>
            <if test="result_basic_duration != null and result_basic_duration != ''">, result_basic_duration = #{result_basic_duration}</if>
            <if test="result_fail_reason != null and result_fail_reason != ''">, result_fail_reason = #{result_fail_reason}</if>
        WHERE job_id = #{job_id} AND user_id = #{user_id}
    </update>

    <delete id="deleteAiGenerateVideo">
        DELETE FROM ai_generate_video_task WHERE job_id = #{job_id} AND user_id = #{user_id}
    </delete>

</mapper>