<?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.fs.delivery.mapper.CtShortVideoMapper">
    
    <resultMap type="CtShortVideo" id="CtShortVideoResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyName" column="companyName"/>
        <result property="title"    column="title"    />
        <result property="groupId"    column="group_id"    />
        <result property="tags"    column="tags"    />
        <result property="taskType"    column="task_type"    />
        <result property="authorTypeId"    column="author_type_id"    />
        <result property="authorId"    column="author_id"    />
        <result property="authorName" column="authorName"/>
        <result property="isOriginal"    column="is_original"    />
        <result property="coverUrl"    column="cover_url"    />
        <result property="summary"    column="summary"    />
        <result property="videoUrl"    column="video_url"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectCtShortVideoVo">
        SELECT
        v.id,
        v.company_id,
        c.company_name AS companyName,

        v.title,
        v.group_id,
        v.tags,
        v.task_type,
        v.author_type_id,
        v.author_id,

        -- ✅ 关键：关联讲者表拿到姓名（用于前端回显）
        d.doctor_name AS authorName,

        v.is_original,
        v.cover_url,
        v.summary,
        v.video_url,
        v.status,
        v.create_by,
        v.create_time,
        v.update_by,
        v.update_time,
        v.del_flag
        FROM ct_short_video v
        LEFT JOIN company c ON c.company_id = v.company_id

        -- ✅ author_id 对应 doctor.id（你给的 doctor 表主键就是 id）
        -- ✅ 同时加 company_id 约束更安全（避免跨公司串数据）
        LEFT JOIN doctor d ON d.id = v.author_id AND d.company_id = v.company_id
    </sql>

    <select id="selectCtShortVideoList" parameterType="CtShortVideo" resultMap="CtShortVideoResult">
        <include refid="selectCtShortVideoVo"/>
        <where>
            <if test="companyId != null"> and v.company_id = #{companyId}</if>
            <if test="title != null and title != ''"> and v.title = #{title}</if>
            <if test="groupId != null"> and v.group_id = #{groupId}</if>
            <if test="tags != null and tags != ''"> and v.tags = #{tags}</if>
            <if test="taskType != null"> and v.task_type = #{taskType}</if>
            <if test="authorTypeId != null"> and v.author_type_id = #{authorTypeId}</if>
            <if test="authorId != null"> and v.author_id = #{authorId}</if>
            <if test="isOriginal != null"> and v.is_original = #{isOriginal}</if>
            <if test="coverUrl != null and coverUrl != ''"> and v.cover_url = #{coverUrl}</if>
            <if test="summary != null and summary != ''"> and v.summary = #{summary}</if>
            <if test="videoUrl != null and videoUrl != ''"> and v.video_url = #{videoUrl}</if>
            <if test="status != null"> and v.status = #{status}</if>
        </where>
        order by v.create_time desc
    </select>


    <select id="selectCtShortVideoById" parameterType="Long" resultMap="CtShortVideoResult">
        <include refid="selectCtShortVideoVo"/>
        where v.id = #{id}
    </select>
        
    <insert id="insertCtShortVideo" parameterType="CtShortVideo" useGeneratedKeys="true" keyProperty="id">
        insert into ct_short_video
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">title,</if>
            <if test="groupId != null">group_id,</if>
            <if test="tags != null">tags,</if>
            <if test="taskType != null">task_type,</if>
            <if test="authorTypeId != null">author_type_id,</if>
            <if test="authorId != null">author_id,</if>
            <if test="isOriginal != null">is_original,</if>
            <if test="coverUrl != null">cover_url,</if>
            <if test="summary != null">summary,</if>
            <if test="videoUrl != null">video_url,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="companyId != null">company_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">#{title},</if>
            <if test="groupId != null">#{groupId},</if>
            <if test="tags != null">#{tags},</if>
            <if test="taskType != null">#{taskType},</if>
            <if test="authorTypeId != null">#{authorTypeId},</if>
            <if test="authorId != null">#{authorId},</if>
            <if test="isOriginal != null">#{isOriginal},</if>
            <if test="coverUrl != null">#{coverUrl},</if>
            <if test="summary != null">#{summary},</if>
            <if test="videoUrl != null">#{videoUrl},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="companyId != null">#{companyId},</if>
         </trim>
    </insert>

    <update id="updateCtShortVideo" parameterType="CtShortVideo">
        update ct_short_video
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="groupId != null">group_id = #{groupId},</if>
            <if test="tags != null">tags = #{tags},</if>
            <if test="taskType != null">task_type = #{taskType},</if>
            <if test="authorTypeId != null">author_type_id = #{authorTypeId},</if>
            <if test="authorId != null">author_id = #{authorId},</if>
            <if test="isOriginal != null">is_original = #{isOriginal},</if>
            <if test="coverUrl != null">cover_url = #{coverUrl},</if>
            <if test="summary != null">summary = #{summary},</if>
            <if test="videoUrl != null">video_url = #{videoUrl},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCtShortVideoById" parameterType="Long">
        delete from ct_short_video where id = #{id}
    </delete>

    <delete id="deleteCtShortVideoByIds" parameterType="String">
        delete from ct_short_video where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>