<?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.LearningTaskMapper">

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.study.TbLearningTask">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="task_id" property="taskID" jdbcType="VARCHAR"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="description" property="description" jdbcType="LONGVARCHAR"/>
        <result column="start_time" property="startTime" jdbcType="TIMESTAMP"/>
        <result column="end_time" property="endTime" jdbcType="TIMESTAMP"/>
        <result column="status" property="status" jdbcType="INTEGER"/>
        <result column="creator" property="creator" jdbcType="VARCHAR"/>
        <result column="updater" property="updater" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="delete_time" property="deleteTime" jdbcType="TIMESTAMP"/>
        <result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, task_id, name, description, start_time, end_time, status, 
        creator, updater, create_time, update_time, delete_time, deleted
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            deleted = 0
            <if test="taskID != null and taskID != ''">
                AND task_id = #{taskID}
            </if>
            <if test="name != null and name != ''">
                AND name LIKE CONCAT('%', #{name}, '%')
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
        </where>
    </sql>

    <!-- selectLearningTasks -->
    <select id="selectLearningTasks" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_learning_task
        <include refid="Where_Clause"/>
        ORDER BY create_time DESC
    </select>

    <!-- 根据任务ID查询任务信息 -->
    <select id="selectByTaskId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_task
        WHERE task_id = #{taskId} AND deleted = 0
    </select>

    <!-- 根据用户ID查询学习任务列表 -->
    <select id="selectByUserId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_task
        WHERE deleted = 0
        ORDER BY create_time DESC
    </select>

    <!-- 根据任务名称查询任务 -->
    <select id="selectByName" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_task
        WHERE name = #{name} AND deleted = 0
    </select>

    <!-- 根据状态查询学习任务列表 -->
    <select id="selectByStatus" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_task
        WHERE status = #{status} AND deleted = 0
        ORDER BY create_time DESC
    </select>

    <!-- 根据优先级查询学习任务列表 -->
    <select id="selectByPriority" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_task
        WHERE deleted = 0
        ORDER BY create_time DESC
    </select>

    <!-- 查询用户待完成任务列表 -->
    <select id="selectPendingTasksByUserId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_task
        WHERE status = 0 AND deleted = 0
        ORDER BY create_time DESC
    </select>

    <!-- 查询用户已完成任务列表 -->
    <select id="selectCompletedTasksByUserId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_task
        WHERE status = 1 AND deleted = 0
        ORDER BY create_time DESC
    </select>

    <!-- 检查任务名称是否存在 -->
    <select id="countByName" resultType="int">
        SELECT COUNT(1)
        FROM tb_learning_task
        WHERE name = #{name} AND deleted = 0
        <if test="excludeId != null and excludeId != ''">
            AND id != #{excludeId}
        </if>
    </select>

    <!-- 插入学习任务 -->
    <insert id="insertLearningTask" parameterType="org.xyzh.common.dto.study.TbLearningTask">
        INSERT INTO tb_learning_task (
            id, task_id, name, description, start_time, end_time, status,
            creator, updater, create_time, update_time, delete_time, deleted
        ) VALUES (
            #{id}, #{taskID}, #{name}, #{description}, #{startTime}, #{endTime}, #{status},
            #{creator}, #{updater}, #{createTime}, #{updateTime}, #{deleteTime}, #{deleted}
        )
    </insert>

    <!-- 更新学习任务 -->
    <update id="updateLearningTask" parameterType="org.xyzh.common.dto.study.TbLearningTask">
        UPDATE tb_learning_task
        <set>
            <if test="taskID != null and taskID != ''">
                task_id = #{taskID},
            </if>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="description != null and description != ''">
                description = #{description},
            </if>
            <if test="startTime != null">
                start_time = #{startTime},
            </if>
            <if test="endTime != null">
                end_time = #{endTime},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="updater != null and updater != ''">
                updater = #{updater},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="deleteTime != null">
                delete_time = #{deleteTime},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 删除学习任务 -->
    <delete id="deleteLearningTask" parameterType="org.xyzh.common.dto.study.TbLearningTask">
        DELETE FROM tb_learning_task
        WHERE id = #{id}
    </delete>

    <!-- 批量插入学习任务 -->
    <insert id="batchInsertLearningTasks" parameterType="java.util.List">
        INSERT INTO tb_learning_task (
            id, task_id, name, description, start_time, end_time, status,
            creator, updater, create_time, update_time, delete_time, deleted
        ) VALUES
        <foreach collection="learningTaskList" item="item" separator=",">
            (
                #{item.id}, #{item.taskID}, #{item.name}, #{item.description}, #{item.startTime}, 
                #{item.endTime}, #{item.status}, #{item.creator}, #{item.updater}, 
                #{item.createTime}, #{item.updateTime}, #{item.deleteTime}, #{item.deleted}
            )
        </foreach>
    </insert>

    <!-- 批量删除学习任务 -->
    <delete id="batchDeleteLearningTasks">
        DELETE FROM tb_learning_task
        WHERE id IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- 分页查询学习任务 -->
    <select id="selectLearningTasksPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_task
        <include refid="Where_Clause" />
        ORDER BY create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计学习任务总数 -->
    <select id="countLearningTasks" resultType="long">
        SELECT COUNT(1)
        FROM tb_learning_task
        <include refid="Where_Clause" />
    </select>

</mapper>
