<?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.live.mapper.LiveAutoTaskMapper">

    <resultMap type="LiveAutoTask" id="LiveAutoTaskResult">
        <result property="id"    column="id"    />
        <result property="liveId"    column="live_id"    />
        <result property="taskName"    column="task_name"    />
        <result property="taskType"    column="task_type"    />
        <result property="triggerType"    column="trigger_type"    />
        <result property="triggerValue"    column="trigger_value"    />
        <result property="absValue"    column="abs_value"    />
        <result property="content"    column="content"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="finishStatus"    column="finish_status"    />
    </resultMap>

    <sql id="selectLiveAutoTaskVo">
        select id, live_id, task_name, task_type, trigger_type, trigger_value, content, status, create_time, update_time,abs_value,finish_status from live_auto_task
    </sql>

    <select id="selectLiveAutoTaskList" parameterType="LiveAutoTask" resultMap="LiveAutoTaskResult">
        <include refid="selectLiveAutoTaskVo"/>
        <where>
            task_type != 3
            <if test="liveId != null "> and live_id = #{liveId}</if>
            <if test="taskName != null  and taskName != ''"> and task_name like concat('%', #{taskName}, '%')</if>

            <if test="triggerType != null "> and trigger_type = #{triggerType}</if>
            <if test="triggerValue != null"> and trigger_value = #{triggerValue}</if>
            <if test="absValue != null"> and abs_value = #{absValue}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="taskType != null "> and task_type = #{taskType}</if>
            <if test="createTime != null "> and create_time = #{createTime}</if>
            <if test="updateTime != null "> and update_time = #{updateTime}</if>
            <if test="finishStatus != null "> and finish_status = #{finishStatus}</if>
        </where>
    </select>

    <select id="selectLiveAutoTaskById" parameterType="Long" resultMap="LiveAutoTaskResult">
        <include refid="selectLiveAutoTaskVo"/>
        where id = #{id}
    </select>

    <insert id="insertLiveAutoTask" parameterType="LiveAutoTask" useGeneratedKeys="true" keyProperty="id">
        insert into live_auto_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="liveId != null">live_id,</if>
            <if test="taskName != null and taskName != ''">task_name,</if>
            <if test="taskType != null">task_type,</if>
            <if test="triggerType != null">trigger_type,</if>
            <if test="triggerValue != null">trigger_value,</if>
            <if test="absValue != null">abs_value,</if>
            <if test="content != null">content,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="finishStatus != null">finish_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="liveId != null">#{liveId},</if>
            <if test="taskName != null and taskName != ''">#{taskName},</if>
            <if test="taskType != null">#{taskType},</if>
            <if test="triggerType != null">#{triggerType},</if>
            <if test="triggerValue != null">#{triggerValue},</if>
            <if test="absValue != null">#{absValue},</if>
            <if test="content != null">#{content},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="finishStatus != null">#{finishStatus},</if>
         </trim>
    </insert>

    <update id="updateLiveAutoTask" parameterType="LiveAutoTask">
        update live_auto_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="liveId != null">live_id = #{liveId},</if>
            <if test="taskName != null and taskName != ''">task_name = #{taskName},</if>
            <if test="taskType != null">task_type = #{taskType},</if>
            <if test="triggerType != null">trigger_type = #{triggerType},</if>
            <if test="triggerValue != null">trigger_value = #{triggerValue},</if>
            <if test="absValue != null">abs_value = #{absValue},</if>
            <if test="content != null">content = #{content},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="finishStatus != null">finish_status = #{finishStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLiveAutoTaskById" parameterType="Long">
        delete from live_auto_task where id = #{id}
    </delete>

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

    <select id="selectLiveAutoTaskByLiveIds" resultMap="LiveAutoTaskResult">
        select * from live_auto_task where live_id in
        <foreach item="id" collection="liveIdLists" open="(" separator="," close=")">
            #{id}
        </foreach>
        and status=1 and finish_status = 0 and abs_value>#{now} order by abs_value
    </select>
    <insert id="batchInsertLiveAutoTask" parameterType="java.util.List">
        INSERT INTO live_auto_task (
        live_id,
        task_name,
        task_type,
        trigger_type,
        trigger_value,
        abs_value,
        content,
        status,
        finish_status,
        create_time,
        update_time
        )
        VALUES
        <foreach collection="list" item="task" separator=",">
            (
            #{task.liveId},
            #{task.taskName},
            #{task.taskType},
            #{task.triggerType},
            #{task.triggerValue},
            #{task.absValue},
            #{task.content},
            #{task.status},
            #{task.finishStatus},
            #{task.createTime},
            #{task.updateTime}
            )
        </foreach>
    </insert>

    <!-- 批量更新直播间自动化任务 -->
    <update id="batchUpdateLiveAutoTask">
        <foreach collection="list" item="task" separator=";">
            UPDATE live_auto_task
            <set>
                <!-- 1. 按需添加需更新的字段，非必更新字段用 if 判断（避免更新为 null） -->
                <if test="task.liveId != null">live_id = #{task.liveId},</if>
                <if test="task.taskName != null and task.taskName != ''">task_name = #{task.taskName},</if>
                <if test="task.taskType != null">task_type = #{task.taskType},</if>
                <if test="task.triggerType != null">trigger_type = #{task.triggerType},</if>
                <if test="task.triggerValue != null">trigger_value = #{task.triggerValue},</if>
                <if test="task.absValue != null">abs_value = #{task.absValue},</if>
                <if test="task.content != null and task.content != ''">content = #{task.content},</if>
                <if test="task.status != null">status = #{task.status},</if>
                <if test="task.finishStatus != null">finish_status = #{task.finishStatus},</if>
                <!-- 2. 若有更新时间字段，自动填充当前时间 -->
                update_time = NOW()
            </set>
            <!-- 3. 必须通过主键 id 定位数据（批量更新的核心） -->
            WHERE id = #{task.id}
        </foreach>
    </update>
</mapper>
