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

    <!-- 基础结果映射 -->
    <resultMap id="BaseResultMap" type="org.xyzh.common.dto.study.TbLearningRecord">
        <id column="id" property="id" jdbcType="VARCHAR"/>
        <result column="user_id" property="userID" jdbcType="VARCHAR"/>
        <result column="resource_type" property="resourceType" jdbcType="INTEGER"/>
        <result column="resource_id" property="resourceID" jdbcType="VARCHAR"/>
        <result column="task_id" property="taskID" jdbcType="VARCHAR"/>
        <result column="duration" property="duration" jdbcType="INTEGER"/>
        <result column="progress" property="progress" jdbcType="DECIMAL"/>
        <result column="is_complete" property="isComplete" jdbcType="BOOLEAN"/>
        <result column="complete_time" property="completeTime" jdbcType="TIMESTAMP"/>
        <result column="last_learn_time" property="lastLearnTime" jdbcType="TIMESTAMP"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 基础字段 -->
    <sql id="Base_Column_List">
        id, user_id, resource_type, resource_id, task_id, duration, progress, 
        is_complete, complete_time, last_learn_time, create_time, update_time
    </sql>

    <!-- 通用条件 -->
    <sql id="Where_Clause">
        <where>
            <if test="userID != null and userID != ''">
                AND user_id = #{userID}
            </if>
            <if test="resourceType != null">
                AND resource_type = #{resourceType}
            </if>
            <if test="resourceID != null and resourceID != ''">
                AND resource_id = #{resourceID}
            </if>
            <if test="taskID != null and taskID != ''">
                AND task_id = #{taskID}
            </if>
            <if test="isComplete != null">
                AND is_complete = #{isComplete}
            </if>
        </where>
    </sql>

    <!-- selectLearningRecords -->
    <select id="selectLearningRecords" resultMap="BaseResultMap">
        SELECT 
        <include refid="Base_Column_List"/>
        FROM tb_learning_record
        <include refid="Where_Clause"/>
        ORDER BY last_learn_time DESC, create_time DESC
    </select>

    <!-- 根据记录ID查询记录信息 -->
    <select id="selectByRecordId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_record
        WHERE id = #{recordId}
    </select>

    <!-- 根据用户ID查询学习记录列表 -->
    <select id="selectByUserId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_record
        WHERE user_id = #{userId}
        ORDER BY last_learn_time DESC, create_time DESC
    </select>

    <!-- 根据课程ID查询学习记录列表 -->
    <select id="selectByCourseId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_record
        WHERE resource_id = #{courseId}
        ORDER BY last_learn_time DESC, create_time DESC
    </select>

    <!-- 根据用户ID和课程ID查询学习记录 -->
    <select id="selectByUserIdAndCourseId" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_record
        WHERE user_id = #{userId} AND resource_id = #{courseId}
    </select>

    <!-- 根据状态查询学习记录列表 -->
    <select id="selectByStatus" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_record
        WHERE is_complete = #{status}
        ORDER BY last_learn_time DESC, create_time DESC
    </select>

    <!-- 查询用户学习统计信息 -->
    <select id="selectLearningStatistics" resultMap="BaseResultMap">
        SELECT
        user_id,
        COUNT(*) as total_records,
        SUM(duration) as total_duration,
        AVG(progress) as avg_progress,
        COUNT(CASE WHEN is_complete = 1 THEN 1 END) as completed_count
        FROM tb_learning_record
        WHERE user_id = #{userId}
        GROUP BY user_id
    </select>

    <!-- 插入学习记录 -->
    <insert id="insertLearningRecord" parameterType="org.xyzh.common.dto.study.TbLearningRecord">
        INSERT INTO tb_learning_record (
            id, user_id, resource_type, resource_id, task_id, duration, progress,
            is_complete, complete_time, last_learn_time, create_time, update_time
        ) VALUES (
            #{id}, #{userID}, #{resourceType}, #{resourceID}, #{taskID}, #{duration}, #{progress},
            #{isComplete}, #{completeTime}, #{lastLearnTime}, #{createTime}, #{updateTime}
        )
    </insert>

    <!-- 更新学习记录 -->
    <update id="updateLearningRecord" parameterType="org.xyzh.common.dto.study.TbLearningRecord">
        UPDATE tb_learning_record
        <set>
            <if test="userID != null and userID != ''">
                user_id = #{userID},
            </if>
            <if test="resourceType != null">
                resource_type = #{resourceType},
            </if>
            <if test="resourceID != null and resourceID != ''">
                resource_id = #{resourceID},
            </if>
            <if test="taskID != null and taskID != ''">
                task_id = #{taskID},
            </if>
            <if test="duration != null">
                duration = #{duration},
            </if>
            <if test="progress != null">
                progress = #{progress},
            </if>
            <if test="isComplete != null">
                is_complete = #{isComplete},
            </if>
            <if test="completeTime != null">
                complete_time = #{completeTime},
            </if>
            <if test="lastLearnTime != null">
                last_learn_time = #{lastLearnTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!-- 删除学习记录 -->
    <delete id="deleteLearningRecord" parameterType="org.xyzh.common.dto.study.TbLearningRecord">
        DELETE FROM tb_learning_record
        WHERE id = #{id}
    </delete>

    <!-- 批量插入学习记录 -->
    <insert id="batchInsertLearningRecords" parameterType="java.util.List">
        INSERT INTO tb_learning_record (
            id, user_id, resource_type, resource_id, task_id, duration, progress,
            is_complete, complete_time, last_learn_time, create_time, update_time
        ) VALUES
        <foreach collection="learningRecordList" item="item" separator=",">
            (
                #{item.id}, #{item.userID}, #{item.resourceType}, #{item.resourceID}, #{item.taskID}, 
                #{item.duration}, #{item.progress}, #{item.isComplete}, #{item.completeTime}, 
                #{item.lastLearnTime}, #{item.createTime}, #{item.updateTime}
            )
        </foreach>
    </insert>

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

    <!-- 分页查询学习记录 -->
    <select id="selectLearningRecordsPage" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM tb_learning_record
        <include refid="Where_Clause" />
        ORDER BY last_learn_time DESC, create_time DESC
        LIMIT #{pageParam.pageSize} OFFSET #{pageParam.offset}
    </select>

    <!-- 统计学习记录总数 -->
    <select id="countLearningRecords" resultType="long">
        SELECT COUNT(1)
        FROM tb_learning_record
        <include refid="Where_Clause" />
    </select>

</mapper>
