refactor: 复习耗时聚合改为强类型结果并补充数据访问规范
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
package com.guo.learningprogresstracker.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 任务维度的复习耗时聚合结果(复习-07)。
|
||||
* <p>由 Mapper 层的聚合查询直接映射,避免用 Map 承载查询结果。</p>
|
||||
*/
|
||||
@Data
|
||||
public class TaskReviewDuration {
|
||||
|
||||
private String taskNum;
|
||||
|
||||
/** 该任务累计复习耗时(秒) */
|
||||
private Long reviewDurationSeconds;
|
||||
}
|
||||
@@ -1,9 +1,24 @@
|
||||
package com.guo.learningprogresstracker.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.guo.learningprogresstracker.dto.TaskReviewDuration;
|
||||
import com.guo.learningprogresstracker.entity.ReviewRecallRecordEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface ReviewRecallRecordMapper extends BaseMapper<ReviewRecallRecordEntity> {
|
||||
|
||||
/**
|
||||
* 按任务汇总复习耗时(秒,复习-07)。
|
||||
* <p>Wrapper 无法表达 SUM + GROUP BY,因此放在 Mapper 层用自定义 SQL + 显式结果类;
|
||||
* 语句见 {@code resources/mapper/ReviewRecallRecordMapper.xml}(逻辑删除条件已显式写出,
|
||||
* 租户条件由拦截器自动追加)。</p>
|
||||
*
|
||||
* @param taskNums 需要汇总的任务编号;为空时返回空列表
|
||||
*/
|
||||
List<TaskReviewDuration> sumReviewDurationByTask(@Param("taskNums") Collection<String> taskNums);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.guo.learningprogresstracker.service.impl;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
||||
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
||||
import com.guo.learningprogresstracker.dto.TaskReviewDuration;
|
||||
import com.guo.learningprogresstracker.entity.ReviewRecallRecordEntity;
|
||||
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
|
||||
import com.guo.learningprogresstracker.entity.StudyReportsEntity;
|
||||
@@ -382,43 +383,19 @@ public class ReviewServiceImpl implements ReviewService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 按任务汇总复习行为累计耗时(复习-07)。在 SQL 侧聚合,只把任务数量的行拉回内存,
|
||||
* 不随回忆记录条数增长。
|
||||
* 按任务汇总复习行为累计耗时(复习-07)。聚合在 SQL 侧完成,只把任务数量的行拉回内存,
|
||||
* 不随回忆记录条数增长;结果由 {@code TaskReviewDuration} 承载,不走 Map。
|
||||
*/
|
||||
private void fillReviewDuration(Map<String, ReviewTaskStats> statsByTaskNum) {
|
||||
List<Map<String, Object>> rows = reviewRecallRecordMapper.selectMaps(
|
||||
Wrappers.<ReviewRecallRecordEntity>query()
|
||||
.select("task_num AS taskNum",
|
||||
"COALESCE(SUM(review_duration_seconds), 0) AS reviewDurationSeconds")
|
||||
.in("task_num", statsByTaskNum.keySet())
|
||||
.groupBy("task_num"));
|
||||
|
||||
for (Map<String, Object> row : rows) {
|
||||
Object taskNum = columnValue(row, "taskNum");
|
||||
Object duration = columnValue(row, "reviewDurationSeconds");
|
||||
ReviewTaskStats stats = taskNum == null ? null : statsByTaskNum.get(taskNum.toString());
|
||||
if (stats != null && duration instanceof Number number) {
|
||||
stats.setReviewDurationSeconds(number.longValue());
|
||||
List<TaskReviewDuration> rows = reviewRecallRecordMapper.sumReviewDurationByTask(statsByTaskNum.keySet());
|
||||
for (TaskReviewDuration row : rows) {
|
||||
ReviewTaskStats stats = statsByTaskNum.get(row.getTaskNum());
|
||||
if (stats != null && row.getReviewDurationSeconds() != null) {
|
||||
stats.setReviewDurationSeconds(row.getReviewDurationSeconds());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取聚合结果的列:别名大小写随数据库实现不同(MySQL 保留、H2 会转为小写),这里统一不区分大小写。
|
||||
*/
|
||||
private static Object columnValue(Map<String, Object> row, String column) {
|
||||
Object direct = row.get(column);
|
||||
if (direct != null) {
|
||||
return direct;
|
||||
}
|
||||
for (Map.Entry<String, Object> entry : row.entrySet()) {
|
||||
if (entry.getKey() != null && entry.getKey().equalsIgnoreCase(column)) {
|
||||
return entry.getValue();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void fillReportAndFragmentCount(Map<String, String> sessionToTaskNum,
|
||||
Map<String, ReviewTaskStats> statsByTaskNum) {
|
||||
if (sessionToTaskNum.isEmpty()) {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?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.guo.learningprogresstracker.mapper.ReviewRecallRecordMapper">
|
||||
|
||||
<!-- 显式列映射:不依赖 map-underscore-to-camel-case,也不依赖数据库的标识符大小写规则 -->
|
||||
<resultMap id="TaskReviewDurationMap" type="com.guo.learningprogresstracker.dto.TaskReviewDuration">
|
||||
<result property="taskNum" column="task_num" jdbcType="VARCHAR"/>
|
||||
<result property="reviewDurationSeconds" column="review_duration_seconds" jdbcType="BIGINT"/>
|
||||
</resultMap>
|
||||
|
||||
<!--
|
||||
按任务汇总复习耗时(复习-07)。
|
||||
注意:自定义 SQL 不会被 @TableLogic 自动追加 deleted 条件,必须显式写出;
|
||||
租户条件 created_by 由 TenantLineInnerInterceptor 自动追加(有集成测试覆盖)。
|
||||
-->
|
||||
<select id="sumReviewDurationByTask" resultMap="TaskReviewDurationMap">
|
||||
SELECT task_num,
|
||||
COALESCE(SUM(review_duration_seconds), 0) AS review_duration_seconds
|
||||
FROM review_recall_records
|
||||
WHERE deleted = 0
|
||||
<if test="taskNums != null and !taskNums.isEmpty()">
|
||||
AND task_num IN
|
||||
<foreach collection="taskNums" item="num" open="(" separator="," close=")">
|
||||
#{num}
|
||||
</foreach>
|
||||
</if>
|
||||
GROUP BY task_num
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user