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()) {
|
||||
|
||||
Reference in New Issue
Block a user