<?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="org.xyzh.study.mapper.TaskCourseMapper">

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.study.TbTaskCourse">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="task_id" property="taskID" jdbcType="VARCHAR"/>
        <result column="course_id" property="courseID" jdbcType="VARCHAR"/>
        <result column="required" property="required" jdbcType="BOOLEAN"/>
        <result column="order_num" property="orderNum" jdbcType="INTEGER"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, task_id, course_id, required, order_num, creator, create_time
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            <if test="taskID != null and taskID != ''">
                AND task_id = #{taskID}
            </if>
            <if test="courseID != null and courseID != ''">
                AND course_id = #{courseID}
            </if>
            <if test="required != null">
                AND required = #{required}
            </if>
        </where>
    </sql>

    <!-- selectTaskCourses -->
    <select id="selectTaskCourses" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_task_course
        <include refid="Where_Clause"/>
        ORDER BY order_num ASC, create_time ASC
    </select>

    <!-- 根据关联ID查询关联信息 -->
    <select id="selectByRelationId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_task_course
        WHERE id = #{relationId}
    </select>

    <!-- 根据任务ID查询课程关联列表 -->
    <select id="selectByTaskId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_task_course
        WHERE task_id = #{taskId}
        ORDER BY order_num ASC, create_time ASC
    </select>

    <!-- 根据课程ID查询任务关联列表 -->
    <select id="selectByCourseId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_task_course
        WHERE course_id = #{courseId}
        ORDER BY order_num ASC, create_time ASC
    </select>

    <!-- 根据任务ID和课程ID查询关联信息 -->
    <select id="selectByTaskIdAndCourseId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_task_course
        WHERE task_id = #{taskId} AND course_id = #{courseId}
    </select>

    <!-- 检查任务课程关联是否存在 -->
    <select id="countByTaskIdAndCourseId" resultType="int">
        SELECT COUNT(1)
        FROM tb_task_course
        WHERE task_id = #{taskId} AND course_id = #{courseId}
        <if test="excludeId != null and excludeId != ''">
            AND id != #{excludeId}
        </if>
    </select>

    <!-- 插入任务课程关联 -->
    <insert id="insertTaskCourse" parameterType="org.xyzh.common.dto.study.TbTaskCourse">
        INSERT INTO tb_task_course (
            id, task_id, course_id, required, order_num, creator, create_time
        ) VALUES (
            #{id}, #{taskID}, #{courseID}, #{required}, #{orderNum}, #{creator}, #{createTime}
        )
    </insert>

    <!-- 更新任务课程关联 -->
    <update id="updateTaskCourse" parameterType="org.xyzh.common.dto.study.TbTaskCourse">
        UPDATE tb_task_course
        <set>
            <if test="taskID != null and taskID != ''">
                task_id = #{taskID},
            </if>
            <if test="courseID != null and courseID != ''">
                course_id = #{courseID},
            </if>
            <if test="required != null">
                required = #{required},
            </if>
            <if test="orderNum != null">
                order_num = #{orderNum},
            </if>
            <if test="creator != null and creator != ''">
                creator = #{creator},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 删除任务课程关联 -->
    <delete id="deleteTaskCourse" parameterType="org.xyzh.common.dto.study.TbTaskCourse">
        DELETE FROM tb_task_course
        WHERE id = #{id}
    </delete>

    <!-- 批量插入任务课程关联 -->
    <insert id="batchInsertTaskCourses" parameterType="java.util.List">
        INSERT INTO tb_task_course (
            id, task_id, course_id, required, order_num, creator, create_time
        ) VALUES
        <foreach collection="taskCourseList" item="item" separator=",">
            (
                #{item.id}, #{item.taskID}, #{item.courseID}, #{item.required}, 
                #{item.orderNum}, #{item.creator}, #{item.createTime}
            )
        </foreach>
    </insert>

    <!-- 批量删除任务课程关联 -->
    <delete id="batchDeleteTaskCourses">
        DELETE FROM tb_task_course
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 根据任务ID批量删除课程关联 -->
    <delete id="deleteByTaskId">
        DELETE FROM tb_task_course
        WHERE task_id = #{taskId}
    </delete>

    <!-- 根据课程ID批量删除任务关联 -->
    <delete id="deleteByCourseId">
        DELETE FROM tb_task_course
        WHERE course_id = #{courseId}
    </delete>

    <!-- 分页查询任务课程关联 -->
    <select id="selectTaskCoursesPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_task_course
        <include refid="Where_Clause" />
        ORDER BY order_num ASC, create_time ASC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计任务课程关联总数 -->
    <select id="countTaskCourses" resultType="long">
        SELECT COUNT(1)
        FROM tb_task_course
        <include refid="Where_Clause" />
    </select>

</mapper>
