feat: 复习总览分页与全局汇总
This commit is contained in:
@@ -2,8 +2,10 @@ package com.guo.learningprogresstracker.controller;
|
|||||||
|
|
||||||
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
||||||
import com.guo.learningprogresstracker.dto.ReviewSortPreference;
|
import com.guo.learningprogresstracker.dto.ReviewSortPreference;
|
||||||
|
import com.guo.learningprogresstracker.dto.ReviewSummary;
|
||||||
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
||||||
import com.guo.learningprogresstracker.dto.StudyReportDetail;
|
import com.guo.learningprogresstracker.dto.StudyReportDetail;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.guo.learningprogresstracker.dto.request.UpdateStandardMindMapRequest;
|
import com.guo.learningprogresstracker.dto.request.UpdateStandardMindMapRequest;
|
||||||
import com.guo.learningprogresstracker.dto.request.ReviewFeedbackRequest;
|
import com.guo.learningprogresstracker.dto.request.ReviewFeedbackRequest;
|
||||||
import com.guo.learningprogresstracker.dto.request.UpdateStudyReportRequest;
|
import com.guo.learningprogresstracker.dto.request.UpdateStudyReportRequest;
|
||||||
@@ -85,11 +87,29 @@ public class ReviewController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取任务维度的复习统计汇总
|
* 复习总览的全局汇总(任务/报告/碎片总数)。
|
||||||
|
*
|
||||||
|
* <p>列表接口分页后,前端只能看到当前页;把每页相加会得到「本页合计」而不是总数,
|
||||||
|
* 因此汇总单独出接口,口径不受分页影响。</p>
|
||||||
|
*/
|
||||||
|
@GetMapping("/summary")
|
||||||
|
public CommonResult<ReviewSummary> summary() {
|
||||||
|
return CommonResult.success(reviewService.summary());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页获取任务维度的复习统计汇总。
|
||||||
|
*
|
||||||
|
* <p>分页不只为了排版:聚合(时长/报告数/碎片数)只算当前这一页的任务,
|
||||||
|
* 不再对全部任务做 IN 聚合。{@code markerName} 把「点标签即筛选」下推到服务端,
|
||||||
|
* 否则客户端只能筛当前页、total 也会不对。</p>
|
||||||
*/
|
*/
|
||||||
@GetMapping("/tasks")
|
@GetMapping("/tasks")
|
||||||
public CommonResult<List<ReviewTaskStats>> getReviewTaskStats() {
|
public CommonResult<Page<ReviewTaskStats>> getReviewTaskStats(
|
||||||
return CommonResult.success(reviewService.getReviewTaskStats());
|
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
|
||||||
|
@RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize,
|
||||||
|
@RequestParam(value = "markerName", required = false) String markerName) {
|
||||||
|
return CommonResult.success(reviewService.getReviewTaskStats(pageNum, pageSize, markerName));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.guo.learningprogresstracker.dto;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 复习总览的全局汇总。
|
||||||
|
*
|
||||||
|
* <p>刻意单独出接口:任务列表已分页,客户端只能看到当前页,
|
||||||
|
* 再把每页的报告数/碎片数相加会得到一个「看起来是总数、其实是本页合计」的错数字。</p>
|
||||||
|
*
|
||||||
|
* <p>口径与 {@code /review/tasks} 的列表完全一致(列表逐页相加 == 这里的汇总):
|
||||||
|
* 报告按「报告 → 会话 → 任务」归属,碎片按内容归属({@code owner_task_num})统计。
|
||||||
|
* 因此待归类碎片不计入 {@code fragmentTotal}(由总览的「待归类内容」独立区块展示),
|
||||||
|
* 任务删除后残留的报告也不计入。</p>
|
||||||
|
*/
|
||||||
|
public record ReviewSummary(
|
||||||
|
@Schema(description = "任务总数")
|
||||||
|
long taskTotal,
|
||||||
|
@Schema(description = "学习报告总数")
|
||||||
|
long reportTotal,
|
||||||
|
@Schema(description = "学习碎片总数")
|
||||||
|
long fragmentTotal) {
|
||||||
|
}
|
||||||
@@ -27,6 +27,17 @@ public interface StudyReportFragmentsMapper extends BaseMapper<StudyReportFragme
|
|||||||
*/
|
*/
|
||||||
List<TaskContentCount> countFragmentsByTask(@Param("taskNums") Collection<String> taskNums);
|
List<TaskContentCount> countFragmentsByTask(@Param("taskNums") Collection<String> taskNums);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 复习总览汇总:当前用户全量「已归属」碎片数(不含待归类)。
|
||||||
|
*
|
||||||
|
* <p>口径必须与 {@link #countFragmentsByTask} 一致——按内容归属({@code owner_task_num})统计,
|
||||||
|
* 只算仍指向现存(未删除)任务的碎片。待归类碎片由总览的「待归类内容」独立区块展示,
|
||||||
|
* 不能在这里重复计数,否则汇总与列表逐页相加的结果对不上。</p>
|
||||||
|
*
|
||||||
|
* <p>语句见 {@code resources/mapper/StudyReportFragmentsMapper.xml}。</p>
|
||||||
|
*/
|
||||||
|
long countOwnedFragments();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 取知识碎片的主键区间(复习-03):供随机取样计算随机起点。
|
* 取知识碎片的主键区间(复习-03):供随机取样计算随机起点。
|
||||||
* <p>语句见 {@code resources/mapper/StudyReportFragmentsMapper.xml};无数据时两列为 null。</p>
|
* <p>语句见 {@code resources/mapper/StudyReportFragmentsMapper.xml};无数据时两列为 null。</p>
|
||||||
|
|||||||
@@ -28,6 +28,17 @@ public interface StudyReportsMapper extends BaseMapper<StudyReportsEntity> {
|
|||||||
* @param taskNums 需要统计的任务编号;为空时返回空列表
|
* @param taskNums 需要统计的任务编号;为空时返回空列表
|
||||||
*/
|
*/
|
||||||
List<TaskContentCount> countReportsByTask(@Param("taskNums") Collection<String> taskNums);
|
List<TaskContentCount> countReportsByTask(@Param("taskNums") Collection<String> taskNums);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 复习总览汇总:当前用户全量报告数。
|
||||||
|
*
|
||||||
|
* <p>口径必须与 {@link #countReportsByTask} 一致——按「报告 → 会话 → 任务」归属,
|
||||||
|
* 只统计仍归属于现存(未删除)任务的报告。否则任务删除后残留的报告会被算进总览,
|
||||||
|
* 与列表逐页相加的结果对不上。</p>
|
||||||
|
*
|
||||||
|
* <p>语句见 {@code resources/mapper/StudyReportsMapper.xml}。</p>
|
||||||
|
*/
|
||||||
|
long countOwnedReports();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ package com.guo.learningprogresstracker.mapper;
|
|||||||
|
|
||||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author guo
|
* @author guo
|
||||||
@@ -13,6 +15,18 @@ import org.apache.ibatis.annotations.Mapper;
|
|||||||
@Mapper
|
@Mapper
|
||||||
public interface TasksMapper extends BaseMapper<TaskEntity> {
|
public interface TasksMapper extends BaseMapper<TaskEntity> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按用户标签分页筛选任务(点标签即筛选)。
|
||||||
|
*
|
||||||
|
* <p>标记过滤直接下推到 SQL({@code EXISTS} 子查询),由数据库在一次查询里算出命中任务,
|
||||||
|
* 不再「先查全部命中对象号、再拼 {@code IN} 回查」——命中量大时 {@code IN} 会膨胀,
|
||||||
|
* 优化器也拿不到好的执行计划。排序与任务列表一致(优先级倒序、id 兜底保证翻页稳定)。</p>
|
||||||
|
*
|
||||||
|
* <p>语句见 {@code resources/mapper/TasksMapper.xml};租户条件由拦截器给 tasks 与 markers 分别追加。</p>
|
||||||
|
*
|
||||||
|
* @param markerName 用户标签名({@code kind=USER});空白时调用方不应走这个分支
|
||||||
|
*/
|
||||||
|
IPage<TaskEntity> selectPageByMarker(IPage<TaskEntity> page, @Param("markerName") String markerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
package com.guo.learningprogresstracker.service;
|
package com.guo.learningprogresstracker.service;
|
||||||
|
|
||||||
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
||||||
|
import com.guo.learningprogresstracker.dto.ReviewSummary;
|
||||||
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
||||||
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
|
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
|
||||||
import com.guo.learningprogresstracker.entity.StudyReportsEntity;
|
import com.guo.learningprogresstracker.entity.StudyReportsEntity;
|
||||||
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
|
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -19,9 +21,18 @@ public interface ReviewService {
|
|||||||
List<ReviewFeedItem> getReviewFeed(int limit, String mode);
|
List<ReviewFeedItem> getReviewFeed(int limit, String mode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取所有任务的复习统计数据
|
* 分页获取任务的复习统计数据(优先级倒序,同优先级按 id 倒序保证翻页稳定)。
|
||||||
|
*
|
||||||
|
* @param markerName 可选:只返回带该用户标签的任务(单个标记筛选,不做组合)
|
||||||
*/
|
*/
|
||||||
List<ReviewTaskStats> getReviewTaskStats();
|
Page<ReviewTaskStats> getReviewTaskStats(int pageNum, int pageSize, String markerName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 复习总览的全局汇总(任务/报告/碎片总数)。
|
||||||
|
*
|
||||||
|
* <p>不能由前端把分页后的每页数字相加:那样得到的是「本页合计」而不是总数。</p>
|
||||||
|
*/
|
||||||
|
ReviewSummary summary();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取指定任务的复习统计数据
|
* 获取指定任务的复习统计数据
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package com.guo.learningprogresstracker.service.impl;
|
package com.guo.learningprogresstracker.service.impl;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.guo.learningprogresstracker.dto.IdRange;
|
import com.guo.learningprogresstracker.dto.IdRange;
|
||||||
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
||||||
|
import com.guo.learningprogresstracker.dto.ReviewSummary;
|
||||||
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
||||||
import com.guo.learningprogresstracker.dto.StudyReportDetail;
|
import com.guo.learningprogresstracker.dto.StudyReportDetail;
|
||||||
import com.guo.learningprogresstracker.dto.TaskRecallRatio;
|
import com.guo.learningprogresstracker.dto.TaskRecallRatio;
|
||||||
@@ -247,17 +250,40 @@ public class ReviewServiceImpl implements ReviewService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ReviewTaskStats> getReviewTaskStats() {
|
public ReviewSummary summary() {
|
||||||
List<TaskEntity> tasks = tasksMapper.selectList(
|
// 口径必须与 /review/tasks 的列表一致(列表逐页相加 == 这里的汇总):
|
||||||
Wrappers.<TaskEntity>lambdaQuery()
|
// 报告按「报告 → 会话 → 任务」归属,碎片按内容归属(owner_task_num)统计。
|
||||||
.orderByDesc(TaskEntity::getCalculatedPriority));
|
// 不能用裸表的 selectCount:那样「学习残片」会把待归类碎片也算进来,
|
||||||
|
// 与列表逐页相加的结果对不上(待归类在总览里有独立区块展示)。
|
||||||
|
return new ReviewSummary(
|
||||||
|
tasksMapper.selectCount(null),
|
||||||
|
studyReportsMapper.countOwnedReports(),
|
||||||
|
studyReportFragmentsMapper.countOwnedFragments());
|
||||||
|
}
|
||||||
|
|
||||||
List<ReviewTaskStats> stats = tasks.stream()
|
@Override
|
||||||
|
public Page<ReviewTaskStats> getReviewTaskStats(int pageNum, int pageSize, String markerName) {
|
||||||
|
// 点标签即筛选:过滤下推到 SQL(EXISTS 子查询),由数据库一次算出命中任务并分页;
|
||||||
|
// 不再「先查全部命中对象号、再拼 IN 回查」——命中量大时 IN 会膨胀。
|
||||||
|
IPage<TaskEntity> taskPage;
|
||||||
|
if (StringUtils.hasText(markerName)) {
|
||||||
|
taskPage = tasksMapper.selectPageByMarker(new Page<>(pageNum, pageSize), markerName.trim());
|
||||||
|
} else {
|
||||||
|
taskPage = tasksMapper.selectPage(new Page<>(pageNum, pageSize),
|
||||||
|
Wrappers.<TaskEntity>lambdaQuery()
|
||||||
|
.orderByDesc(TaskEntity::getCalculatedPriority)
|
||||||
|
// 优先级相同的任务之间要有稳定次序,否则翻页会重复或漏掉
|
||||||
|
.orderByDesc(TaskEntity::getId));
|
||||||
|
}
|
||||||
|
List<ReviewTaskStats> stats = taskPage.getRecords().stream()
|
||||||
.map(this::newTaskStats)
|
.map(this::newTaskStats)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
// 聚合只算这一页的任务,不再对全部任务做 IN 聚合
|
||||||
fillReviewTaskStats(stats);
|
fillReviewTaskStats(stats);
|
||||||
return stats;
|
|
||||||
|
Page<ReviewTaskStats> result = new Page<>(taskPage.getCurrent(), taskPage.getSize(), taskPage.getTotal());
|
||||||
|
result.setRecords(stats);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -63,4 +63,17 @@
|
|||||||
</if>
|
</if>
|
||||||
GROUP BY owner_task_num
|
GROUP BY owner_task_num
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
复习总览汇总:全量「已归属」碎片数。
|
||||||
|
口径与 countFragmentsByTask 完全一致(按 owner_task_num 分组),只是不再限定任务列表,
|
||||||
|
因此 join tasks 只保留仍指向现存任务的碎片;待归类(owner_task_num 为空)与已删任务残留的不计入。
|
||||||
|
租户条件由拦截器给每张表分别追加;自定义 SQL 需显式写 deleted = 0。
|
||||||
|
-->
|
||||||
|
<select id="countOwnedFragments" resultType="long">
|
||||||
|
SELECT COUNT(f.id)
|
||||||
|
FROM study_report_fragments f
|
||||||
|
JOIN tasks t ON t.task_num = f.owner_task_num AND t.deleted = 0
|
||||||
|
WHERE f.deleted = 0
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
@@ -55,4 +55,18 @@
|
|||||||
</if>
|
</if>
|
||||||
GROUP BY ss.task_num
|
GROUP BY ss.task_num
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
复习总览汇总:全量报告数。
|
||||||
|
口径与 countReportsByTask 完全一致(报告 → 会话 → 任务),只是不再限定任务列表,
|
||||||
|
因此 join tasks 只保留仍归属于现存任务的报告;任务删除后残留的报告不计入。
|
||||||
|
租户条件由拦截器给每张表分别追加;自定义 SQL 需显式写 deleted = 0。
|
||||||
|
-->
|
||||||
|
<select id="countOwnedReports" resultType="long">
|
||||||
|
SELECT COUNT(r.id)
|
||||||
|
FROM study_reports r
|
||||||
|
JOIN study_sessions ss ON ss.session_num = r.session_num AND ss.deleted = 0
|
||||||
|
JOIN tasks t ON t.task_num = ss.task_num AND t.deleted = 0
|
||||||
|
WHERE r.deleted = 0
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
@@ -27,4 +27,26 @@
|
|||||||
created_time,created_by,last_modified_time,
|
created_time,created_by,last_modified_time,
|
||||||
last_modified_by
|
last_modified_by
|
||||||
</sql>
|
</sql>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
按用户标签分页筛选任务(点标签即筛选):过滤下推到 SQL。
|
||||||
|
用 EXISTS 而不是 join markers:markers 的唯一键包含 marker_value,
|
||||||
|
而用户标签的 marker_value 为 NULL(MySQL 唯一键不约束 NULL),理论上可能有重复行;
|
||||||
|
EXISTS 天然去重,也避免分页 count 被 join 放大。
|
||||||
|
自定义 SQL 不会被 @TableLogic 追加条件,故显式写 deleted = 0;
|
||||||
|
租户条件由拦截器给外层 tasks 与子查询里的 markers 分别追加。
|
||||||
|
-->
|
||||||
|
<select id="selectPageByMarker" resultType="com.guo.learningprogresstracker.entity.TaskEntity">
|
||||||
|
SELECT t.*
|
||||||
|
FROM tasks t
|
||||||
|
WHERE t.deleted = 0
|
||||||
|
AND EXISTS (SELECT 1
|
||||||
|
FROM markers m
|
||||||
|
WHERE m.object_num = t.task_num
|
||||||
|
AND m.object_type = 'TASK'
|
||||||
|
AND m.kind = 'USER'
|
||||||
|
AND m.marker_name = #{markerName}
|
||||||
|
AND m.deleted = 0)
|
||||||
|
ORDER BY t.calculated_priority DESC, t.id DESC
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
+32
-5
@@ -1,6 +1,8 @@
|
|||||||
package com.guo.learningprogresstracker.controller;
|
package com.guo.learningprogresstracker.controller;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
||||||
|
import com.guo.learningprogresstracker.dto.ReviewSummary;
|
||||||
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
||||||
import com.guo.learningprogresstracker.dto.request.UpdateStandardMindMapRequest;
|
import com.guo.learningprogresstracker.dto.request.UpdateStandardMindMapRequest;
|
||||||
import com.guo.learningprogresstracker.entity.CommonResult;
|
import com.guo.learningprogresstracker.entity.CommonResult;
|
||||||
@@ -54,14 +56,39 @@ class ReviewControllerUnitTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getReviewTaskStats_noArg_returnsAllTaskStats() {
|
void summary_returnsTotals() {
|
||||||
List<ReviewTaskStats> stats = List.of(new ReviewTaskStats());
|
ReviewSummary summary = new ReviewSummary(45, 30, 33);
|
||||||
when(reviewService.getReviewTaskStats()).thenReturn(stats);
|
when(reviewService.summary()).thenReturn(summary);
|
||||||
|
|
||||||
CommonResult<List<ReviewTaskStats>> result = controller.getReviewTaskStats();
|
CommonResult<ReviewSummary> result = controller.summary();
|
||||||
|
|
||||||
assertEquals(200, result.getCode());
|
assertEquals(200, result.getCode());
|
||||||
assertSame(stats, result.getData());
|
assertEquals(45, result.getData().taskTotal());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getReviewTaskStats_paged_returnsTaskPage() {
|
||||||
|
ReviewTaskStats stats = new ReviewTaskStats();
|
||||||
|
Page<ReviewTaskStats> page = new Page<>(1, 20, 1);
|
||||||
|
page.setRecords(List.of(stats));
|
||||||
|
when(reviewService.getReviewTaskStats(1, 20, null)).thenReturn(page);
|
||||||
|
|
||||||
|
CommonResult<Page<ReviewTaskStats>> result = controller.getReviewTaskStats(1, 20, null);
|
||||||
|
|
||||||
|
assertEquals(200, result.getCode());
|
||||||
|
assertSame(page, result.getData());
|
||||||
|
assertEquals(1, result.getData().getTotal());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getReviewTaskStats_passesMarkerFilterToService() {
|
||||||
|
Page<ReviewTaskStats> empty = new Page<>(1, 20, 0);
|
||||||
|
when(reviewService.getReviewTaskStats(2, 50, "英语")).thenReturn(empty);
|
||||||
|
|
||||||
|
controller.getReviewTaskStats(2, 50, "英语");
|
||||||
|
|
||||||
|
// 标记筛选必须下推到服务端,否则只能筛当前页
|
||||||
|
verify(reviewService).getReviewTaskStats(2, 50, "英语");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
+45
-2
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|||||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||||
import com.guo.learningprogresstracker.dto.IdRange;
|
import com.guo.learningprogresstracker.dto.IdRange;
|
||||||
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
import com.guo.learningprogresstracker.dto.ReviewFeedItem;
|
||||||
|
import com.guo.learningprogresstracker.dto.ReviewSummary;
|
||||||
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
||||||
import com.guo.learningprogresstracker.dto.TaskContentCount;
|
import com.guo.learningprogresstracker.dto.TaskContentCount;
|
||||||
import com.guo.learningprogresstracker.dto.TaskRecallRatio;
|
import com.guo.learningprogresstracker.dto.TaskRecallRatio;
|
||||||
@@ -20,6 +21,7 @@ import com.guo.learningprogresstracker.mapper.StudyReportsMapper;
|
|||||||
import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
|
import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
|
||||||
import com.guo.learningprogresstracker.mapper.TasksMapper;
|
import com.guo.learningprogresstracker.mapper.TasksMapper;
|
||||||
import com.guo.learningprogresstracker.mapper.UserReviewPreferencesMapper;
|
import com.guo.learningprogresstracker.mapper.UserReviewPreferencesMapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -32,7 +34,11 @@ import java.time.LocalDateTime;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
@@ -163,7 +169,7 @@ class ReviewServiceImplTest {
|
|||||||
sessionStats.setTodayEffectiveTime(600D);
|
sessionStats.setTodayEffectiveTime(600D);
|
||||||
sessionStats.setWeekEffectiveTime(600D);
|
sessionStats.setWeekEffectiveTime(600D);
|
||||||
|
|
||||||
when(tasksMapper.selectList(any())).thenReturn(List.of(task));
|
when(tasksMapper.selectPage(any(), any())).thenReturn(taskPage(task));
|
||||||
when(studySessionsMapper.sumStudyStatsByTask(any(), any(), any())).thenReturn(List.of(sessionStats));
|
when(studySessionsMapper.sumStudyStatsByTask(any(), any(), any())).thenReturn(List.of(sessionStats));
|
||||||
when(studyReportsMapper.countReportsByTask(any())).thenReturn(List.of(count("T1", 2L)));
|
when(studyReportsMapper.countReportsByTask(any())).thenReturn(List.of(count("T1", 2L)));
|
||||||
when(studyReportFragmentsMapper.countFragmentsByTask(any())).thenReturn(List.of(count("T1", 3L)));
|
when(studyReportFragmentsMapper.countFragmentsByTask(any())).thenReturn(List.of(count("T1", 3L)));
|
||||||
@@ -172,7 +178,7 @@ class ReviewServiceImplTest {
|
|||||||
reviewDuration("T1", 420L),
|
reviewDuration("T1", 420L),
|
||||||
reviewDuration("T_UNKNOWN", 100L)));
|
reviewDuration("T_UNKNOWN", 100L)));
|
||||||
|
|
||||||
List<ReviewTaskStats> stats = reviewService.getReviewTaskStats();
|
List<ReviewTaskStats> stats = reviewService.getReviewTaskStats(1, 20, null).getRecords();
|
||||||
|
|
||||||
assertEquals(1, stats.size());
|
assertEquals(1, stats.size());
|
||||||
assertEquals(600, stats.get(0).getEffectiveTime());
|
assertEquals(600, stats.get(0).getEffectiveTime());
|
||||||
@@ -184,6 +190,43 @@ class ReviewServiceImplTest {
|
|||||||
assertEquals(420L, stats.get(0).getReviewDurationSeconds());
|
assertEquals(420L, stats.get(0).getReviewDurationSeconds());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getReviewTaskStats_markerFilter_pushesFilterIntoSql() {
|
||||||
|
// 过滤下推到 SQL:命中任务由 selectPageByMarker 一次查出,
|
||||||
|
// 不再先取全部命中对象号再 IN,也不会在没有命中时退化成「不过滤」
|
||||||
|
Page<TaskEntity> empty = new Page<>(1, 20, 0);
|
||||||
|
when(tasksMapper.selectPageByMarker(any(), eq("没有的标签"))).thenReturn(empty);
|
||||||
|
|
||||||
|
Page<ReviewTaskStats> result = reviewService.getReviewTaskStats(1, 20, "没有的标签");
|
||||||
|
|
||||||
|
assertEquals(0, result.getTotal());
|
||||||
|
assertTrue(result.getRecords().isEmpty());
|
||||||
|
verify(tasksMapper).selectPageByMarker(any(), eq("没有的标签"));
|
||||||
|
// 带筛选时必须走专用查询,不能退化成不带筛选的全量分页
|
||||||
|
verify(tasksMapper, never()).selectPage(any(), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void summary_countsAllInsteadOfCurrentPage() {
|
||||||
|
when(tasksMapper.selectCount(null)).thenReturn(45L);
|
||||||
|
when(studyReportsMapper.countOwnedReports()).thenReturn(30L);
|
||||||
|
when(studyReportFragmentsMapper.countOwnedFragments()).thenReturn(28L);
|
||||||
|
|
||||||
|
ReviewSummary summary = reviewService.summary();
|
||||||
|
|
||||||
|
assertEquals(45L, summary.taskTotal());
|
||||||
|
assertEquals(30L, summary.reportTotal());
|
||||||
|
// 28 而不是裸表总数:汇总只算已归属碎片,与列表逐页相加同口径(待归类另有区块)
|
||||||
|
assertEquals(28L, summary.fragmentTotal());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 分页结果:与分页拖控的同一形状,聚合只对该页的任务做 */
|
||||||
|
private static Page<TaskEntity> taskPage(TaskEntity... tasks) {
|
||||||
|
Page<TaskEntity> page = new Page<>(1, 20, tasks.length);
|
||||||
|
page.setRecords(List.of(tasks));
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
|
||||||
private static IdRange idRange(int minId, int maxId) {
|
private static IdRange idRange(int minId, int maxId) {
|
||||||
IdRange range = new IdRange();
|
IdRange range = new IdRange();
|
||||||
range.setMinId(minId);
|
range.setMinId(minId);
|
||||||
|
|||||||
+129
@@ -1,16 +1,25 @@
|
|||||||
package com.guo.learningprogresstracker.service.impl;
|
package com.guo.learningprogresstracker.service.impl;
|
||||||
|
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
|
import com.guo.learningprogresstracker.dto.ReviewSummary;
|
||||||
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
import com.guo.learningprogresstracker.dto.ReviewTaskStats;
|
||||||
import com.guo.learningprogresstracker.dto.TaskContentCount;
|
import com.guo.learningprogresstracker.dto.TaskContentCount;
|
||||||
import com.guo.learningprogresstracker.dto.TaskSessionStats;
|
import com.guo.learningprogresstracker.dto.TaskSessionStats;
|
||||||
|
import com.guo.learningprogresstracker.entity.MarkerEntity;
|
||||||
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
|
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
|
||||||
import com.guo.learningprogresstracker.entity.StudyReportsEntity;
|
import com.guo.learningprogresstracker.entity.StudyReportsEntity;
|
||||||
import com.guo.learningprogresstracker.entity.StudySessionsEntity;
|
import com.guo.learningprogresstracker.entity.StudySessionsEntity;
|
||||||
|
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||||
|
import com.guo.learningprogresstracker.enums.MarkerKindEnum;
|
||||||
|
import com.guo.learningprogresstracker.enums.MarkerObjectTypeEnum;
|
||||||
import com.guo.learningprogresstracker.enums.StudySessionStateEnum;
|
import com.guo.learningprogresstracker.enums.StudySessionStateEnum;
|
||||||
|
import com.guo.learningprogresstracker.mapper.MarkerMapper;
|
||||||
import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper;
|
import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper;
|
||||||
import com.guo.learningprogresstracker.mapper.StudyReportsMapper;
|
import com.guo.learningprogresstracker.mapper.StudyReportsMapper;
|
||||||
import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
|
import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
|
||||||
|
import com.guo.learningprogresstracker.mapper.TasksMapper;
|
||||||
|
import com.guo.learningprogresstracker.service.MarkerService;
|
||||||
import com.guo.learningprogresstracker.service.ReviewService;
|
import com.guo.learningprogresstracker.service.ReviewService;
|
||||||
import com.guo.learningprogresstracker.utils.GenerateNumTool;
|
import com.guo.learningprogresstracker.utils.GenerateNumTool;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
@@ -54,6 +63,15 @@ class ReviewStatsAggregationIntegrationTest {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private StudyReportFragmentsMapper studyReportFragmentsMapper;
|
private StudyReportFragmentsMapper studyReportFragmentsMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TasksMapper tasksMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MarkerService markerService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MarkerMapper markerMapper;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
StpUtil.login(USER, "test_device");
|
StpUtil.login(USER, "test_device");
|
||||||
@@ -146,11 +164,122 @@ class ReviewStatsAggregationIntegrationTest {
|
|||||||
ReviewTaskStats otherStats = reviewService.getReviewTaskStats(taskNum);
|
ReviewTaskStats otherStats = reviewService.getReviewTaskStats(taskNum);
|
||||||
assertEquals(0, otherStats.getSessionCount(), "统计聚合必须受租户隔离约束");
|
assertEquals(0, otherStats.getSessionCount(), "统计聚合必须受租户隔离约束");
|
||||||
assertEquals(0D, otherStats.getEffectiveTime(), 0.001);
|
assertEquals(0D, otherStats.getEffectiveTime(), 0.001);
|
||||||
|
// 汇总同样不能跨租户:join 出来的 tasks 也会被拦截器追加 created_by,
|
||||||
|
// 否则会把别人的报告/碎片算进自己的总览
|
||||||
|
ReviewSummary otherSummary = reviewService.summary();
|
||||||
|
assertEquals(0L, otherSummary.taskTotal(), "汇总必须受租户隔离约束");
|
||||||
|
assertEquals(0L, otherSummary.reportTotal(), "汇总必须受租户隔离约束");
|
||||||
|
assertEquals(0L, otherSummary.fragmentTotal(), "汇总必须受租户隔离约束");
|
||||||
|
|
||||||
StpUtil.login(USER, "test_device");
|
StpUtil.login(USER, "test_device");
|
||||||
assertEquals(1, reviewService.getReviewTaskStats(taskNum).getSessionCount());
|
assertEquals(1, reviewService.getReviewTaskStats(taskNum).getSessionCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 总览汇总必须与按任务列表同口径:列表逐页相加 == summary。
|
||||||
|
*
|
||||||
|
* <p>裸表 count 会把「待归类」碎片也算进「学习残片」,而列表的碎片数按内容归属聚合,
|
||||||
|
* 两者对不上(待归类在总览里另有独立区块展示)。这里额外造一条待归类碎片,
|
||||||
|
* 一旦汇总退回裸表计数,断言就会失败。</p>
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void summaryMatchesListTotalsAcrossAllPagesAndExcludesPendingFragments() {
|
||||||
|
String taskNum = saveTask();
|
||||||
|
StudySessionsEntity session = saveSession(taskNum, 60, LocalDateTime.now());
|
||||||
|
insertReport(session.getSessionNum());
|
||||||
|
insertFragment(session.getSessionNum(), taskNum);
|
||||||
|
// 待归类碎片:不属于任何任务,因此不进列表,也不该进汇总的「学习残片」
|
||||||
|
insertFragment(session.getSessionNum(), null);
|
||||||
|
|
||||||
|
ReviewSummary summary = reviewService.summary();
|
||||||
|
|
||||||
|
long listReports = 0L;
|
||||||
|
long listFragments = 0L;
|
||||||
|
int pageSize = 100;
|
||||||
|
long total = reviewService.getReviewTaskStats(1, pageSize, null).getTotal();
|
||||||
|
long pages = (total + pageSize - 1) / pageSize;
|
||||||
|
for (int page = 1; page <= pages; page++) {
|
||||||
|
Page<ReviewTaskStats> pageResult = reviewService.getReviewTaskStats(page, pageSize, null);
|
||||||
|
for (ReviewTaskStats stats : pageResult.getRecords()) {
|
||||||
|
listReports += stats.getReportCount();
|
||||||
|
listFragments += stats.getFragmentCount();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(listReports, summary.reportTotal(), "汇总报告数必须等于列表逐页相加");
|
||||||
|
assertEquals(listFragments, summary.fragmentTotal(), "汇总碎片数必须等于列表逐页相加(不含待归类)");
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 真实的任务行:列表按 tasks 表分页,因此汇总的 join 必须能匹配到它 */
|
||||||
|
private String saveTask() {
|
||||||
|
String taskNum = uniqueTaskNum();
|
||||||
|
TaskEntity task = new TaskEntity();
|
||||||
|
task.setTaskNum(taskNum);
|
||||||
|
task.setTaskName("统计聚合集成测试任务");
|
||||||
|
tasksMapper.insert(task);
|
||||||
|
return taskNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标签筛选下推到 SQL:命中任务由一次 EXISTS 查询算出,且子查询也受租户隔离约束。
|
||||||
|
*
|
||||||
|
* <p>「同一 task_num 上存在别人打的同名标签」这种编号碰撞下,若子查询没带 created_by,
|
||||||
|
* 就会把别人标记的任务算成自己的命中——这里用直插 marker 的方式构造该场景守住它。</p>
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void markerFilterIsPushedIntoSqlAndStaysTenantScoped() throws Exception {
|
||||||
|
String untaggedTask = saveTask();
|
||||||
|
String taggedTask = saveTask();
|
||||||
|
markerService.addUserTag(MarkerObjectTypeEnum.TASK, taggedTask, "筛选标签");
|
||||||
|
|
||||||
|
// 本用户:只命中自己打过标签的那一个
|
||||||
|
Page<ReviewTaskStats> page = reviewService.getReviewTaskStats(1, 20, "筛选标签");
|
||||||
|
assertEquals(1, page.getTotal());
|
||||||
|
assertEquals(taggedTask, page.getRecords().get(0).getTaskNum());
|
||||||
|
|
||||||
|
// 没有命中时返回空页,不能退化成「不过滤」
|
||||||
|
Page<ReviewTaskStats> none = reviewService.getReviewTaskStats(1, 20, "不存在的标签");
|
||||||
|
assertEquals(0, none.getTotal());
|
||||||
|
assertTrue(none.getRecords().isEmpty());
|
||||||
|
|
||||||
|
// 另一用户在同一个 task_num 上插同名标签(模拟编号碰撞)
|
||||||
|
StpUtil.login("统计聚合集成测试标记用户", "test_device");
|
||||||
|
MarkerEntity foreign = new MarkerEntity();
|
||||||
|
foreign.setObjectType(MarkerObjectTypeEnum.TASK.name());
|
||||||
|
foreign.setObjectNum(untaggedTask);
|
||||||
|
foreign.setKind(MarkerKindEnum.USER.name());
|
||||||
|
foreign.setMarkerName("筛选标签");
|
||||||
|
markerMapper.insert(foreign);
|
||||||
|
|
||||||
|
// 本用户仍只看到自己标记的任务:别人的标记不能让他未打标签的任务命中
|
||||||
|
StpUtil.login(USER, "test_device");
|
||||||
|
Page<ReviewTaskStats> after = reviewService.getReviewTaskStats(1, 20, "筛选标签");
|
||||||
|
assertEquals(1, after.getTotal(), "标签筛选必须受租户隔离约束");
|
||||||
|
assertEquals(taggedTask, after.getRecords().get(0).getTaskNum());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页总数必须受租户隔离:分页拦截器要注册在多租户之后,
|
||||||
|
* 否则 count 语句是从「还没追加租户条件」的原句派生的,records 正确但 total 会跨租户。
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void pagedTaskListTotalIsTenantScoped() {
|
||||||
|
StpUtil.login(USER, "test_device");
|
||||||
|
long mineBefore = reviewService.getReviewTaskStats(1, 20, null).getTotal();
|
||||||
|
|
||||||
|
// 另一用户新建任务:若 count 没带租户条件,本用户看到的 total 会被抬高
|
||||||
|
StpUtil.login("统计聚合集成测试分页用户", "test_device");
|
||||||
|
long theirsBefore = reviewService.getReviewTaskStats(1, 20, null).getTotal();
|
||||||
|
saveTask();
|
||||||
|
saveTask();
|
||||||
|
assertEquals(theirsBefore + 2, reviewService.getReviewTaskStats(1, 20, null).getTotal(),
|
||||||
|
"分页 count 必须只算自己的任务");
|
||||||
|
|
||||||
|
StpUtil.login(USER, "test_device");
|
||||||
|
assertEquals(mineBefore, reviewService.getReviewTaskStats(1, 20, null).getTotal(),
|
||||||
|
"分页 count 必须带租户条件,别人的任务不能算进我的总数");
|
||||||
|
}
|
||||||
|
|
||||||
private StudySessionsEntity saveSession(String taskNum, long effectiveSeconds, LocalDateTime startTime) {
|
private StudySessionsEntity saveSession(String taskNum, long effectiveSeconds, LocalDateTime startTime) {
|
||||||
StudySessionsEntity session = new StudySessionsEntity();
|
StudySessionsEntity session = new StudySessionsEntity();
|
||||||
session.setSessionNum(GenerateNumTool.generateNum("SESSIONAGG"));
|
session.setSessionNum(GenerateNumTool.generateNum("SESSIONAGG"));
|
||||||
|
|||||||
Reference in New Issue
Block a user