<?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.CtLongVideoMapper">
    
    <resultMap type="CtLongVideo" id="CtLongVideoResult">
        <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>

    <!-- ✅ 关联公司表：查 companyName -->
    <sql id="selectCtLongVideoVo">
        SELECT
        t.id,
        t.company_id,
        c.company_name AS companyName,

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

        -- ✅ 关键：讲者姓名
        d.doctor_name AS authorName,

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

        -- ✅ 关联讲者表（防串公司）
        LEFT JOIN doctor d
        ON d.id = t.author_id
        AND d.company_id = t.company_id
    </sql>

    <select id="selectCtLongVideoList" parameterType="CtLongVideo" resultMap="CtLongVideoResult">
        <include refid="selectCtLongVideoVo"/>
        <where>
            <!-- ✅ 公司筛选 -->
            <if test="companyId != null"> AND t.company_id = #{companyId}</if>

            <!-- 你原来的条件（我顺手把 title 改成 like，通常更好用；不想改就换回 =） -->
            <if test="title != null and title != ''"> AND t.title LIKE CONCAT('%', #{title}, '%')</if>

            <if test="groupId != null"> AND t.group_id = #{groupId}</if>
            <if test="tags != null and tags != ''"> AND t.tags = #{tags}</if>
            <if test="taskType != null"> AND t.task_type = #{taskType}</if>
            <if test="authorTypeId != null"> AND t.author_type_id = #{authorTypeId}</if>
            <if test="authorId != null"> AND t.author_id = #{authorId}</if>
            <if test="isOriginal != null"> AND t.is_original = #{isOriginal}</if>
            <if test="coverUrl != null and coverUrl != ''"> AND t.cover_url = #{coverUrl}</if>
            <if test="summary != null and summary != ''"> AND t.summary = #{summary}</if>
            <if test="videoUrl != null and videoUrl != ''"> AND t.video_url = #{videoUrl}</if>
            <if test="status != null"> AND t.status = #{status}</if>

            <!-- ✅ 软删过滤（如果你们 del_flag=0 才是正常数据；按你系统实际来） -->
            <if test="delFlag != null"> AND t.del_flag = #{delFlag}</if>
        </where>

        <!-- 可选：按时间倒序 -->
        ORDER BY t.create_time DESC
    </select>


    <select id="selectCtLongVideoById" parameterType="Long" resultMap="CtLongVideoResult">
        <include refid="selectCtLongVideoVo"/>
        WHERE t.id = #{id}
    </select>
        
    <insert id="insertCtLongVideo" parameterType="CtLongVideo" useGeneratedKeys="true" keyProperty="id">
        insert into ct_long_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="updateCtLongVideo" parameterType="CtLongVideo">
        update ct_long_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="deleteCtLongVideoById" parameterType="Long">
        delete from ct_long_video where id = #{id}
    </delete>

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