serv\web-学习历史修改

This commit is contained in:
2025-10-27 13:42:34 +08:00
parent 74880b429e
commit e50de4a277
31 changed files with 3997 additions and 64 deletions

View File

@@ -0,0 +1,334 @@
<?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.LearningHistoryMapper">
<!-- 基础结果映射 -->
<resultMap id="BaseResultMap" type="org.xyzh.common.dto.study.TbLearningHistory">
<id column="id" property="id" jdbcType="VARCHAR"/>
<result column="user_id" property="userID" jdbcType="VARCHAR"/>
<result column="history_id" property="historyID" jdbcType="VARCHAR"/>
<result column="resource_type" property="resourceType" jdbcType="INTEGER"/>
<result column="resource_id" property="resourceID" jdbcType="VARCHAR"/>
<result column="course_id" property="courseID" jdbcType="VARCHAR"/>
<result column="chapter_id" property="chapterID" jdbcType="VARCHAR"/>
<result column="node_id" property="nodeID" jdbcType="VARCHAR"/>
<result column="task_id" property="taskID" jdbcType="VARCHAR"/>
<result column="start_time" property="startTime" jdbcType="TIMESTAMP"/>
<result column="end_time" property="endTime" jdbcType="TIMESTAMP"/>
<result column="duration" property="duration" jdbcType="INTEGER"/>
<result column="start_progress" property="startProgress" jdbcType="DECIMAL"/>
<result column="end_progress" property="endProgress" jdbcType="DECIMAL"/>
<result column="device_type" property="deviceType" jdbcType="VARCHAR"/>
<result column="ip_address" property="ipAddress" jdbcType="VARCHAR"/>
<result column="creator" property="creator" jdbcType="VARCHAR"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="deleted" property="deleted" jdbcType="BOOLEAN"/>
</resultMap>
<!-- VO结果映射带关联信息 -->
<resultMap id="VOResultMap" type="org.xyzh.common.vo.LearningHistoryVO">
<id column="id" property="id" jdbcType="VARCHAR"/>
<result column="user_id" property="userID" jdbcType="VARCHAR"/>
<result column="user_name" property="userName" jdbcType="VARCHAR"/>
<result column="history_id" property="historyID" jdbcType="VARCHAR"/>
<result column="resource_type" property="resourceType" jdbcType="INTEGER"/>
<result column="resource_id" property="resourceID" jdbcType="VARCHAR"/>
<result column="resource_title" property="resourceTitle" jdbcType="VARCHAR"/>
<result column="course_id" property="courseID" jdbcType="VARCHAR"/>
<result column="course_name" property="courseName" jdbcType="VARCHAR"/>
<result column="chapter_id" property="chapterID" jdbcType="VARCHAR"/>
<result column="chapter_name" property="chapterName" jdbcType="VARCHAR"/>
<result column="node_id" property="nodeID" jdbcType="VARCHAR"/>
<result column="node_name" property="nodeName" jdbcType="VARCHAR"/>
<result column="task_id" property="taskID" jdbcType="VARCHAR"/>
<result column="task_name" property="taskName" jdbcType="VARCHAR"/>
<result column="start_time" property="startTime" jdbcType="TIMESTAMP"/>
<result column="end_time" property="endTime" jdbcType="TIMESTAMP"/>
<result column="duration" property="duration" jdbcType="INTEGER"/>
<result column="start_progress" property="startProgress" jdbcType="DECIMAL"/>
<result column="end_progress" property="endProgress" jdbcType="DECIMAL"/>
<result column="device_type" property="deviceType" jdbcType="VARCHAR"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
</resultMap>
<!-- 基础字段 -->
<sql id="Base_Column_List">
id, user_id, history_id, resource_type, resource_id, course_id, chapter_id, node_id, task_id,
start_time, end_time, duration, start_progress, end_progress, device_type, ip_address,
creator, create_time, deleted
</sql>
<!-- 通用条件 -->
<sql id="Where_Clause">
<where>
deleted = 0
<if test="userID != null and userID != ''">
AND user_id = #{userID}
</if>
<if test="historyID != null and historyID != ''">
AND history_id = #{historyID}
</if>
<if test="resourceType != null">
AND resource_type = #{resourceType}
</if>
<if test="resourceID != null and resourceID != ''">
AND resource_id = #{resourceID}
</if>
<if test="courseID != null and courseID != ''">
AND course_id = #{courseID}
</if>
<if test="chapterID != null and chapterID != ''">
AND chapter_id = #{chapterID}
</if>
<if test="nodeID != null and nodeID != ''">
AND node_id = #{nodeID}
</if>
<if test="taskID != null and taskID != ''">
AND task_id = #{taskID}
</if>
<if test="deviceType != null and deviceType != ''">
AND device_type = #{deviceType}
</if>
</where>
</sql>
<!-- 插入学习历史 -->
<insert id="insertLearningHistory">
INSERT INTO tb_learning_history (
id, user_id, history_id, resource_type, resource_id, course_id, chapter_id, node_id, task_id,
start_time, end_time, duration, start_progress, end_progress, device_type, ip_address,
creator, create_time, deleted
) VALUES (
#{id}, #{userID}, #{historyID}, #{resourceType}, #{resourceID}, #{courseID}, #{chapterID}, #{nodeID}, #{taskID},
#{startTime}, #{endTime}, #{duration}, #{startProgress}, #{endProgress}, #{deviceType}, #{ipAddress},
#{creator}, #{createTime}, 0
)
</insert>
<!-- 更新学习历史 -->
<update id="updateLearningHistory">
UPDATE tb_learning_history
<set>
<if test="endTime != null">
end_time = #{endTime},
</if>
<if test="duration != null">
duration = #{duration},
</if>
<if test="startProgress != null">
start_progress = #{startProgress},
</if>
<if test="endProgress != null">
end_progress = #{endProgress},
</if>
<if test="deviceType != null">
device_type = #{deviceType},
</if>
<if test="ipAddress != null">
ip_address = #{ipAddress},
</if>
</set>
WHERE history_id = #{historyID} AND deleted = 0
</update>
<!-- 批量插入学习历史 -->
<insert id="batchInsertLearningHistories">
INSERT INTO tb_learning_history (
id, user_id, history_id, resource_type, resource_id, course_id, chapter_id, node_id, task_id,
start_time, end_time, duration, start_progress, end_progress, device_type, ip_address,
creator, create_time, deleted
) VALUES
<foreach collection="historyList" item="item" separator=",">
(
#{item.id}, #{item.userID}, #{item.historyID}, #{item.resourceType}, #{item.resourceID},
#{item.courseID}, #{item.chapterID}, #{item.nodeID}, #{item.taskID},
#{item.startTime}, #{item.endTime}, #{item.duration}, #{item.startProgress}, #{item.endProgress},
#{item.deviceType}, #{item.ipAddress}, #{item.creator}, #{item.createTime}, 0
)
</foreach>
</insert>
<!-- 根据ID查询 -->
<select id="selectById" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM tb_learning_history
WHERE id = #{id} AND deleted = 0
</select>
<!-- 根据用户ID查询 -->
<select id="selectByUserId" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM tb_learning_history
WHERE user_id = #{userId} AND deleted = 0
ORDER BY start_time DESC, create_time DESC
</select>
<!-- 根据条件查询学习历史列表 -->
<select id="selectLearningHistories" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM tb_learning_history
<include refid="Where_Clause"/>
ORDER BY start_time DESC, create_time DESC
</select>
<!-- 根据条件查询学习历史VO列表带关联信息 -->
<select id="selectLearningHistoriesWithDetails" resultMap="VOResultMap">
SELECT
lh.id,
lh.user_id,
u.username as user_name,
lh.history_id,
lh.resource_type,
lh.resource_id,
CASE
WHEN lh.resource_type = 1 THEN r.title
WHEN lh.resource_type = 2 THEN c.name
WHEN lh.resource_type = 3 THEN ch.name
WHEN lh.resource_type = 4 THEN n.name
END as resource_title,
lh.course_id,
c.name as course_name,
lh.chapter_id,
ch.name as chapter_name,
lh.node_id,
n.name as node_name,
lh.task_id,
t.name as task_name,
lh.start_time,
lh.end_time,
lh.duration,
lh.start_progress,
lh.end_progress,
lh.device_type,
lh.create_time
FROM tb_learning_history lh
LEFT JOIN tb_sys_user u ON lh.user_id = u.id
LEFT JOIN tb_resource r ON lh.resource_type = 1 AND lh.resource_id = r.resource_id
LEFT JOIN tb_course c ON lh.course_id = c.course_id
LEFT JOIN tb_course_chapter ch ON lh.chapter_id = ch.chapter_id
LEFT JOIN tb_course_node n ON lh.node_id = n.node_id
LEFT JOIN tb_learning_task t ON lh.task_id = t.task_id
<include refid="Where_Clause"/>
ORDER BY lh.start_time DESC, lh.create_time DESC
</select>
<!-- 分页查询学习历史 -->
<select id="selectLearningHistoriesPage" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM tb_learning_history
<include refid="Where_Clause"/>
ORDER BY start_time DESC, create_time DESC
LIMIT #{pageParam.offset}, #{pageParam.pageSize}
</select>
<!-- 统计学习历史总数 -->
<select id="countLearningHistories" resultType="long">
SELECT COUNT(*)
FROM tb_learning_history
<include refid="Where_Clause"/>
</select>
<!-- 根据时间段查询学习历史 -->
<select id="selectByUserIdAndTimeRange" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM tb_learning_history
WHERE user_id = #{userId}
AND start_time BETWEEN #{startTime} AND #{endTime}
AND deleted = 0
ORDER BY start_time DESC
</select>
<!-- 根据时间段和资源类型查询学习历史 -->
<select id="selectByUserIdAndResourceTypeAndTimeRange" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM tb_learning_history
WHERE user_id = #{userId}
AND resource_type = #{resourceType}
AND start_time BETWEEN #{startTime} AND #{endTime}
AND deleted = 0
ORDER BY start_time DESC
</select>
<!-- 统计用户某时间段的学习时长 -->
<select id="selectLearningStatisticsByTimeRange" resultType="map">
SELECT
SUM(duration) as totalDuration,
COUNT(*) as learnCount,
COUNT(DISTINCT DATE(start_time)) as learnDays,
COUNT(DISTINCT CASE WHEN resource_type = 1 THEN resource_id END) as resourceCount,
COUNT(DISTINCT CASE WHEN resource_type = 2 THEN course_id END) as courseCount
FROM tb_learning_history
WHERE user_id = #{userId}
AND start_time BETWEEN #{startTime} AND #{endTime}
AND deleted = 0
</select>
<!-- 统计用户某时间段各资源类型的学习时长 -->
<select id="selectLearningStatisticsByResourceType" resultType="map">
SELECT
resource_type as resourceType,
SUM(duration) as totalDuration,
COUNT(*) as learnCount,
COUNT(DISTINCT resource_id) as resourceCount
FROM tb_learning_history
WHERE user_id = #{userId}
AND start_time BETWEEN #{startTime} AND #{endTime}
AND deleted = 0
GROUP BY resource_type
ORDER BY resource_type
</select>
<!-- 获取用户学习的资源列表(按时间段) -->
<select id="selectLearnedResourcesByTimeRange" resultType="map">
SELECT
resource_type as resourceType,
resource_id as resourceId,
course_id as courseId,
chapter_id as chapterId,
node_id as nodeId,
SUM(duration) as totalDuration,
COUNT(*) as learnCount,
MAX(start_time) as lastLearnTime
FROM tb_learning_history
WHERE user_id = #{userId}
AND start_time BETWEEN #{startTime} AND #{endTime}
AND deleted = 0
GROUP BY resource_type, resource_id, course_id, chapter_id, node_id
ORDER BY lastLearnTime DESC
</select>
<!-- 删除学习历史(软删除) -->
<update id="deleteLearningHistory">
UPDATE tb_learning_history
SET deleted = 1, delete_time = NOW()
WHERE id = #{id}
</update>
<!-- 批量删除学习历史(软删除) -->
<update id="batchDeleteLearningHistories">
UPDATE tb_learning_history
SET deleted = 1, delete_time = NOW()
WHERE id IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</update>
<!-- 根据会话ID查询学习历史 -->
<select id="selectByhistoryID" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM tb_learning_history
WHERE history_id = #{historyID} AND deleted = 0
ORDER BY start_time DESC
</select>
<!-- 获取用户最近的学习历史 -->
<select id="selectRecentByUserId" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM tb_learning_history
WHERE user_id = #{userId} AND deleted = 0
ORDER BY start_time DESC, create_time DESC
LIMIT #{limit}
</select>
</mapper>

View File

@@ -0,0 +1,246 @@
<?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.LearningStatisticsDetailMapper">
<!-- 基础结果映射 -->
<resultMap id="BaseResultMap" type="org.xyzh.common.dto.study.TbLearningStatisticsDetail">
<id column="id" property="id" jdbcType="VARCHAR"/>
<result column="user_id" property="userID" jdbcType="VARCHAR"/>
<result column="stat_date" property="statDate" jdbcType="DATE"/>
<result column="resource_type" property="resourceType" jdbcType="INTEGER"/>
<result column="resource_id" property="resourceID" jdbcType="VARCHAR"/>
<result column="course_id" property="courseID" jdbcType="VARCHAR"/>
<result column="chapter_id" property="chapterID" jdbcType="VARCHAR"/>
<result column="total_duration" property="totalDuration" jdbcType="INTEGER"/>
<result column="learn_count" property="learnCount" jdbcType="INTEGER"/>
<result column="is_complete" property="isComplete" jdbcType="BOOLEAN"/>
<result column="complete_time" property="completeTime" jdbcType="TIMESTAMP"/>
<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="deleted" property="deleted" jdbcType="BOOLEAN"/>
</resultMap>
<!-- VO结果映射带关联信息 -->
<resultMap id="VOResultMap" type="org.xyzh.common.vo.LearningStatisticsDetailVO">
<result column="stat_date" property="statDate" jdbcType="DATE"/>
<result column="resource_type" property="resourceType" jdbcType="INTEGER"/>
<result column="resource_id" property="resourceID" jdbcType="VARCHAR"/>
<result column="resource_title" property="resourceTitle" jdbcType="VARCHAR"/>
<result column="course_id" property="courseID" jdbcType="VARCHAR"/>
<result column="course_name" property="courseName" jdbcType="VARCHAR"/>
<result column="chapter_id" property="chapterID" jdbcType="VARCHAR"/>
<result column="chapter_name" property="chapterName" jdbcType="VARCHAR"/>
<result column="total_duration" property="totalDuration" jdbcType="INTEGER"/>
<result column="learn_count" property="learnCount" jdbcType="INTEGER"/>
<result column="is_complete" property="isComplete" jdbcType="BOOLEAN"/>
<result column="complete_time" property="completeTime" jdbcType="TIMESTAMP"/>
</resultMap>
<!-- 基础字段 -->
<sql id="Base_Column_List">
id, user_id, stat_date, resource_type, resource_id, course_id, chapter_id,
total_duration, learn_count, is_complete, complete_time,
creator, updater, create_time, update_time, deleted
</sql>
<!-- 通用条件 -->
<sql id="Where_Clause">
<where>
deleted = 0
<if test="userID != null and userID != ''">
AND user_id = #{userID}
</if>
<if test="statDate != null">
AND stat_date = #{statDate}
</if>
<if test="resourceType != null">
AND resource_type = #{resourceType}
</if>
<if test="resourceID != null and resourceID != ''">
AND resource_id = #{resourceID}
</if>
<if test="courseID != null and courseID != ''">
AND course_id = #{courseID}
</if>
<if test="chapterID != null and chapterID != ''">
AND chapter_id = #{chapterID}
</if>
<if test="isComplete != null">
AND is_complete = #{isComplete}
</if>
</where>
</sql>
<!-- 插入统计明细 -->
<insert id="insertStatisticsDetail">
INSERT INTO tb_learning_statistics_detail (
id, user_id, stat_date, resource_type, resource_id, course_id, chapter_id,
total_duration, learn_count, is_complete, complete_time,
creator, create_time, deleted
) VALUES (
#{id}, #{userID}, #{statDate}, #{resourceType}, #{resourceID}, #{courseID}, #{chapterID},
#{totalDuration}, #{learnCount}, #{isComplete}, #{completeTime},
#{creator}, #{createTime}, 0
)
</insert>
<!-- 更新统计明细 -->
<update id="updateStatisticsDetail">
UPDATE tb_learning_statistics_detail
<set>
<if test="totalDuration != null">
total_duration = #{totalDuration},
</if>
<if test="learnCount != null">
learn_count = #{learnCount},
</if>
<if test="isComplete != null">
is_complete = #{isComplete},
</if>
<if test="completeTime != null">
complete_time = #{completeTime},
</if>
<if test="updater != null">
updater = #{updater},
</if>
update_time = NOW()
</set>
WHERE id = #{id}
</update>
<!-- 插入或更新统计明细ON DUPLICATE KEY UPDATE -->
<insert id="insertOrUpdateStatisticsDetail">
INSERT INTO tb_learning_statistics_detail (
id, user_id, stat_date, resource_type, resource_id, course_id, chapter_id,
total_duration, learn_count, is_complete, complete_time,
creator, create_time, deleted
) VALUES (
#{id}, #{userID}, #{statDate}, #{resourceType}, #{resourceID}, #{courseID}, #{chapterID},
#{totalDuration}, #{learnCount}, #{isComplete}, #{completeTime},
#{creator}, NOW(), 0
)
ON DUPLICATE KEY UPDATE
total_duration = total_duration + #{totalDuration},
learn_count = learn_count + #{learnCount},
is_complete = #{isComplete},
complete_time = #{completeTime},
updater = #{updater},
update_time = NOW()
</insert>
<!-- 根据条件查询统计明细 -->
<select id="selectStatisticsDetails" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM tb_learning_statistics_detail
<include refid="Where_Clause"/>
ORDER BY stat_date DESC, create_time DESC
</select>
<!-- 根据用户ID和日期查询统计明细 -->
<select id="selectByUserIdAndDate" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM tb_learning_statistics_detail
WHERE user_id = #{userId}
AND stat_date = #{statDate}
AND deleted = 0
ORDER BY resource_type, total_duration DESC
</select>
<!-- 根据用户ID和日期范围查询统计明细 -->
<select id="selectByUserIdAndDateRange" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM tb_learning_statistics_detail
WHERE user_id = #{userId}
AND stat_date BETWEEN #{startDate} AND #{endDate}
AND deleted = 0
ORDER BY stat_date DESC, resource_type, total_duration DESC
</select>
<!-- 根据用户ID和日期范围查询统计明细VO带关联信息 -->
<select id="selectStatisticsDetailsWithInfo" resultMap="VOResultMap">
SELECT
sd.stat_date,
sd.resource_type,
sd.resource_id,
CASE
WHEN sd.resource_type = 1 THEN r.title
WHEN sd.resource_type = 2 THEN c.name
WHEN sd.resource_type = 3 THEN ch.name
END as resource_title,
sd.course_id,
c.name as course_name,
sd.chapter_id,
ch.name as chapter_name,
sd.total_duration,
sd.learn_count,
sd.is_complete,
sd.complete_time
FROM tb_learning_statistics_detail sd
LEFT JOIN tb_resource r ON sd.resource_type = 1 AND sd.resource_id = r.resource_id
LEFT JOIN tb_course c ON sd.course_id = c.course_id
LEFT JOIN tb_course_chapter ch ON sd.chapter_id = ch.chapter_id
WHERE sd.user_id = #{userId}
AND sd.stat_date BETWEEN #{startDate} AND #{endDate}
AND sd.deleted = 0
ORDER BY sd.stat_date DESC, sd.total_duration DESC
</select>
<!-- 根据用户ID、资源类型和日期查询统计明细 -->
<select id="selectByUserIdAndResourceTypeAndDate" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM tb_learning_statistics_detail
WHERE user_id = #{userId}
AND resource_type = #{resourceType}
AND resource_id = #{resourceId}
AND stat_date = #{statDate}
AND deleted = 0
LIMIT 1
</select>
<!-- 删除统计明细(软删除) -->
<update id="deleteStatisticsDetail">
UPDATE tb_learning_statistics_detail
SET deleted = 1, delete_time = NOW()
WHERE id = #{id}
</update>
<!-- 批量删除统计明细(软删除) -->
<update id="batchDeleteStatisticsDetails">
UPDATE tb_learning_statistics_detail
SET deleted = 1, delete_time = NOW()
WHERE id IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</update>
<!-- 按日期汇总用户学习统计 -->
<select id="selectDailySummary" resultMap="BaseResultMap">
SELECT
NULL as id,
user_id,
stat_date,
NULL as resource_type,
NULL as resource_id,
NULL as course_id,
NULL as chapter_id,
SUM(total_duration) as total_duration,
SUM(learn_count) as learn_count,
NULL as is_complete,
NULL as complete_time,
NULL as creator,
NULL as updater,
MIN(create_time) as create_time,
MAX(update_time) as update_time,
0 as deleted
FROM tb_learning_statistics_detail
WHERE user_id = #{userId}
AND stat_date BETWEEN #{startDate} AND #{endDate}
AND deleted = 0
GROUP BY user_id, stat_date
ORDER BY stat_date DESC
</select>
</mapper>