perf: 批量更新优先级与标签改名,按范围收敛隐私清算
This commit is contained in:
+13
-2
@@ -46,6 +46,12 @@ Status: implemented
|
||||
`tagsChanged` 里逐篇回忆两次全表校验 → 整批共用一个来源索引。
|
||||
- `GET /export/range`:逐任务调 `getReviewTaskStats`(每任务 4 条聚合)→ 一批任务一次取齐;`sumTaskDuration` 的整表 `GROUP BY`
|
||||
加上 `task_num IN (...)` 过滤(T 个任务时约 `4 + 5T` 条降为固定 9 条)。
|
||||
- `PUT /tasks/priority-weights`:重算全部任务时逐条 `updateById` → 每 200 条一条 `CASE id WHEN ... THEN ...` 更新,
|
||||
审计列显式写入(身份取不到时不动 `last_modified_by` / `device_info`,避免把旧值清空)。
|
||||
- `PUT /markers/rename`:逐条标记一次 `exists` + 一次 `update` → 一次预取目标标签命中的对象、一条批量 `UPDATE`、一条批量 `DELETE`。
|
||||
- 隐私清算按范围类型过滤:标签改名/标签变化只解析 `scope_type='TAG'` 的回忆,删任务只解析该任务的 `TASK` 范围回忆,
|
||||
不再整表解析 `document_json`。
|
||||
- `GET /review/report/{id}/structured`:按归属分组时逐个查任务名 → 先收集 ownerTaskNum,一次 `IN` 取齐。
|
||||
|
||||
## Alternatives considered
|
||||
|
||||
@@ -66,9 +72,14 @@ Status: implemented
|
||||
|
||||
`RecallsSummary` 不再经过 `detail()` 装饰,`reference` 也只保留文档里的汇总;如果以后摘要需要展示 AI 请求状态,必须显式调用 `detail()`,不能假设它已经在。
|
||||
|
||||
优先级批量重算不再经过 `updateById`,因此自动填充不再参与:`last_modified_time` 由调用方传入,`last_modified_by` / `device_info` 只在取到身份时写入。
|
||||
新增「任务优先级」批量 SQL 时必须保留 `deleted = 0`(自定义 SQL 不会自动追加逻辑删除条件),租户条件仍由拦截器追加。
|
||||
|
||||
## Verification
|
||||
|
||||
- 真实 MySQL 并发复现:脚本对同一用户并发发两个列表请求,旧锁形态 6/6 轮死锁,新锁形态 0/12 次失败;`performance_schema.data_locks` 中不再出现同事务的 S+X 并存。
|
||||
- SQL 条数:用 `logging.level...mapper=DEBUG` 抓单次调用语句数,列表 7 条、详情 8 条(改造前 47 / 44)。
|
||||
- `./mvnw -o validate`(Checkstyle)与 `./mvnw -o test` 全量 608 个用例通过,含 `ReviewRecallServiceIntegrationTest` 的列表删除不可读文档、
|
||||
资料清除、AI 结果装饰等断言。
|
||||
- `./mvnw -o validate`(Checkstyle)与 `./mvnw -o test` 全量 609 个用例通过,含 `ReviewRecallServiceIntegrationTest` 的列表删除不可读文档、
|
||||
资料清除、AI 结果装饰等断言,以及新增的 `PriorityWeightsIntegrationTest`(真实事务里校验批量 CASE 更新写回的值与审计时间)。
|
||||
- 批量 `CASE id` 更新语句在真实 MySQL 上单独执行验证(同一事务内 affected=2 后回滚),确认语法与索引条件可用;H2 集成测试同时覆盖了
|
||||
租户拦截器对这条 `UPDATE` 的改写。
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.guo.learningprogresstracker.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 批量重算优先级的一行输入(权重保存时用)。
|
||||
*
|
||||
* <p>优先级仍由 {@code CalculatedPriorityTool} 在 Java 侧算出,这里只负责把「主键 + 算好的值」交给一条
|
||||
* CASE 更新,避免逐任务 {@code updateById} 变成 N 次往返。</p>
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class TaskPriorityUpdate {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private Double calculatedPriority;
|
||||
}
|
||||
@@ -2,7 +2,10 @@ package com.guo.learningprogresstracker.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.guo.learningprogresstracker.dto.TaskPriorityUpdate;
|
||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
@@ -27,4 +30,16 @@ public interface TasksMapper extends BaseMapper<TaskEntity> {
|
||||
* @param markerName 用户标签名({@code kind=USER});空白时调用方不应走这个分支
|
||||
*/
|
||||
IPage<TaskEntity> selectPageByMarker(IPage<TaskEntity> page, @Param("markerName") String markerName);
|
||||
|
||||
/**
|
||||
* 一批任务一条 CASE 更新写入重算后的优先级(权重保存时用)。
|
||||
*
|
||||
* <p>逐任务 {@code updateById} 会把一次保存放大成任务数条往返;这里由调用方按批切分。
|
||||
* 自动填充不会作用于自定义 SQL,故审计列由调用方显式传入。</p>
|
||||
*/
|
||||
int updateCalculatedPriorities(
|
||||
@Param("rows") List<TaskPriorityUpdate> rows,
|
||||
@Param("lastModifiedTime") LocalDateTime lastModifiedTime,
|
||||
@Param("lastModifiedBy") String lastModifiedBy,
|
||||
@Param("deviceInfo") String deviceInfo);
|
||||
}
|
||||
|
||||
@@ -18,11 +18,14 @@ import com.guo.learningprogresstracker.service.MarkerService;
|
||||
import com.guo.learningprogresstracker.service.ReviewTagMindMapService;
|
||||
import com.guo.learningprogresstracker.service.recall.RecallPrivacyService;
|
||||
import com.guo.learningprogresstracker.service.review.TagDictionaryService;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -146,25 +149,34 @@ public class MarkerServiceImpl implements MarkerService {
|
||||
}
|
||||
|
||||
recallPrivacyService.tagRenamed(from);
|
||||
int renamed = 0;
|
||||
// 目标标签命中的对象一次取齐:逐条 exists 会把改名放大成「标记数 × 1」次往返
|
||||
Set<String> taken = new HashSet<>();
|
||||
for (MarkerEntity existing : markerMapper.selectList(Wrappers.<MarkerEntity>lambdaQuery()
|
||||
.eq(MarkerEntity::getKind, MarkerKindEnum.USER.name())
|
||||
.eq(MarkerEntity::getMarkerName, to))) {
|
||||
taken.add(objectKey(existing));
|
||||
}
|
||||
List<Integer> duplicates = new ArrayList<>();
|
||||
List<Integer> renames = new ArrayList<>();
|
||||
for (MarkerEntity marker : targets) {
|
||||
// 目标标签已存在于同一对象时,这条改名后的记录会与既有记录重复,直接清掉
|
||||
boolean duplicateExists = markerMapper.exists(Wrappers.<MarkerEntity>lambdaQuery()
|
||||
.eq(MarkerEntity::getObjectType, marker.getObjectType())
|
||||
.eq(MarkerEntity::getObjectNum, marker.getObjectNum())
|
||||
.eq(MarkerEntity::getKind, MarkerKindEnum.USER.name())
|
||||
.eq(MarkerEntity::getMarkerName, to));
|
||||
if (duplicateExists) {
|
||||
markerMapper.deleteById(marker.getId());
|
||||
continue;
|
||||
if (taken.contains(objectKey(marker))) {
|
||||
duplicates.add(marker.getId());
|
||||
} else {
|
||||
renames.add(marker.getId());
|
||||
}
|
||||
}
|
||||
if (!duplicates.isEmpty()) {
|
||||
markerMapper.delete(Wrappers.<MarkerEntity>lambdaQuery().in(MarkerEntity::getId, duplicates));
|
||||
}
|
||||
if (!renames.isEmpty()) {
|
||||
markerMapper.update(
|
||||
new MarkerEntity(),
|
||||
Wrappers.<MarkerEntity>lambdaUpdate()
|
||||
.eq(MarkerEntity::getId, marker.getId())
|
||||
.in(MarkerEntity::getId, renames)
|
||||
.set(MarkerEntity::getMarkerName, to));
|
||||
renamed++;
|
||||
}
|
||||
int renamed = renames.size();
|
||||
log.info("标签[{}]重命名为[{}],影响 {} 条标记", from, to, renamed);
|
||||
// 两个名字的取材范围都变了 → 相关标签导图置「待更新」(闭环-04,不自动重建)
|
||||
reviewTagMindMapService.markStaleForTags(List.of(from, to));
|
||||
@@ -354,6 +366,11 @@ public class MarkerServiceImpl implements MarkerService {
|
||||
return num;
|
||||
}
|
||||
|
||||
/** 标记落在哪个对象上:对象类型与对象号联合定位,用来判断改名后是否会与既有标记重复 */
|
||||
private static String objectKey(MarkerEntity marker) {
|
||||
return marker.getObjectType() + ":" + marker.getObjectNum();
|
||||
}
|
||||
|
||||
/** 当前登录用户,用于标签使用情况的统计(该查询不走拦截器,需显式传入) */
|
||||
private String currentUser() {
|
||||
try {
|
||||
|
||||
+50
-6
@@ -1,7 +1,10 @@
|
||||
package com.guo.learningprogresstracker.service.impl;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.guo.learningprogresstracker.common.TenantContext;
|
||||
import com.guo.learningprogresstracker.dto.PriorityDto;
|
||||
import com.guo.learningprogresstracker.dto.TaskPriorityUpdate;
|
||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||
import com.guo.learningprogresstracker.entity.UserPriorityWeightsEntity;
|
||||
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
||||
@@ -9,6 +12,8 @@ import com.guo.learningprogresstracker.mapper.TasksMapper;
|
||||
import com.guo.learningprogresstracker.mapper.UserPriorityWeightsMapper;
|
||||
import com.guo.learningprogresstracker.service.PriorityWeightsService;
|
||||
import com.guo.learningprogresstracker.utils.CalculatedPriorityTool;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -24,6 +29,8 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
public class PriorityWeightsServiceImpl implements PriorityWeightsService {
|
||||
|
||||
private static final double SUM_TOLERANCE = 0.001;
|
||||
/** 每条 CASE 更新带的任务数上限:任务很多时分批,避免单条 SQL 参数过多 */
|
||||
private static final int PRIORITY_BATCH_SIZE = 200;
|
||||
|
||||
private final UserPriorityWeightsMapper weightsMapper;
|
||||
private final TasksMapper tasksMapper;
|
||||
@@ -53,20 +60,57 @@ public class PriorityWeightsServiceImpl implements PriorityWeightsService {
|
||||
return weights;
|
||||
}
|
||||
|
||||
/** 用新权重重算当前用户的全部任务优先级 */
|
||||
/** 用新权重重算当前用户的全部任务优先级:每批一条 CASE 更新,任务数不放大往返次数。 */
|
||||
private void recalculateAllTasks(UserPriorityWeightsEntity weights) {
|
||||
List<TaskEntity> tasks = tasksMapper.selectList(Wrappers.lambdaQuery(TaskEntity.class));
|
||||
for (TaskEntity task : tasks) {
|
||||
List<TaskPriorityUpdate> rows = new ArrayList<>();
|
||||
for (TaskEntity task : tasksMapper.selectList(Wrappers.lambdaQuery(TaskEntity.class))) {
|
||||
rows.add(new TaskPriorityUpdate(
|
||||
task.getId(), CalculatedPriorityTool.calculatedPriority(priorityOf(task), weights)));
|
||||
}
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
String user = currentUser();
|
||||
String device = currentDevice();
|
||||
for (int from = 0; from < rows.size(); from += PRIORITY_BATCH_SIZE) {
|
||||
List<TaskPriorityUpdate> batch =
|
||||
rows.subList(from, Math.min(rows.size(), from + PRIORITY_BATCH_SIZE));
|
||||
tasksMapper.updateCalculatedPriorities(batch, now, user, device);
|
||||
}
|
||||
log.info("权重更新,已重算 {} 个任务的优先级", rows.size());
|
||||
}
|
||||
|
||||
private static PriorityDto priorityOf(TaskEntity task) {
|
||||
PriorityDto dto = new PriorityDto();
|
||||
dto.setUrgency(orZero(task.getUrgency()));
|
||||
dto.setImportance(orZero(task.getImportance()));
|
||||
dto.setContentDifficulty(orZero(task.getContentDifficulty()));
|
||||
dto.setFutureValue(orZero(task.getFutureValue()));
|
||||
dto.setSubjectivePriority(orZero(task.getSubjectivePriority()));
|
||||
task.setCalculatedPriority(CalculatedPriorityTool.calculatedPriority(dto, weights));
|
||||
tasksMapper.updateById(task);
|
||||
return dto;
|
||||
}
|
||||
|
||||
/** 与自动填充同源:后台线程走 TenantContext,请求线程走 Sa-Token;取不到身份时留空不动库里的旧值。 */
|
||||
private static String currentUser() {
|
||||
TenantContext.Identity identity = TenantContext.get();
|
||||
if (identity != null) {
|
||||
return identity.loginId();
|
||||
}
|
||||
try {
|
||||
return StpUtil.getLoginIdAsString();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static String currentDevice() {
|
||||
TenantContext.Identity identity = TenantContext.get();
|
||||
if (identity != null) {
|
||||
return identity.device();
|
||||
}
|
||||
try {
|
||||
return StpUtil.getLoginDevice();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
log.info("权重更新,已重算 {} 个任务的优先级", tasks.size());
|
||||
}
|
||||
|
||||
private void validate(UserPriorityWeightsEntity weights) throws ErrorParameterException {
|
||||
|
||||
@@ -36,6 +36,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -129,6 +131,12 @@ public class ReviewServiceImpl implements ReviewService {
|
||||
.eq(StudyReportFragmentsEntity::getContextNum, report.getSessionNum())
|
||||
.orderByAsc(StudyReportFragmentsEntity::getCreatedTime));
|
||||
|
||||
// 归属任务名一次 IN 取齐:computeIfAbsent 里逐个查会把分组数变成额外的 N 次往返
|
||||
Set<String> ownerTaskNums = fragments.stream()
|
||||
.map(StudyReportFragmentsEntity::getOwnerTaskNum)
|
||||
.filter(StringUtils::hasText)
|
||||
.collect(Collectors.toSet());
|
||||
Map<String, String> ownerTaskNames = loadTaskNames(ownerTaskNums);
|
||||
Map<String, StudyReportDetail.FragmentGroup> groups = new LinkedHashMap<>();
|
||||
for (StudyReportFragmentsEntity fragment : fragments) {
|
||||
String owner = fragment.getOwnerTaskNum();
|
||||
@@ -136,7 +144,7 @@ public class ReviewServiceImpl implements ReviewService {
|
||||
StudyReportDetail.FragmentGroup group = groups.computeIfAbsent(key, k -> {
|
||||
StudyReportDetail.FragmentGroup created = new StudyReportDetail.FragmentGroup();
|
||||
created.setTaskNum(StringUtils.hasText(owner) ? owner : null);
|
||||
created.setTaskName(StringUtils.hasText(owner) ? loadTaskName(owner) : null);
|
||||
created.setTaskName(StringUtils.hasText(owner) ? ownerTaskNames.get(owner) : null);
|
||||
// 旁支 = 归属不是本会话所属任务的内容
|
||||
created.setBranch(!StringUtils.hasText(owner) || !Objects.equals(owner, detail.getSessionTaskNum()));
|
||||
return created;
|
||||
@@ -261,15 +269,21 @@ public class ReviewServiceImpl implements ReviewService {
|
||||
}
|
||||
|
||||
private String loadTaskName(String taskNum) {
|
||||
if (!StringUtils.hasText(taskNum)) {
|
||||
return null;
|
||||
return StringUtils.hasText(taskNum) ? loadTaskNames(List.of(taskNum)).get(taskNum) : null;
|
||||
}
|
||||
return tasksMapper
|
||||
.selectOne(Wrappers.<TaskEntity>lambdaQuery()
|
||||
.eq(TaskEntity::getTaskNum, taskNum)
|
||||
.select(TaskEntity::getTaskName)
|
||||
.last("LIMIT 1"))
|
||||
.getTaskName();
|
||||
|
||||
/** 任务名一次 IN 取齐;已删除的任务不在结果里,调用方按 null 处理 */
|
||||
private Map<String, String> loadTaskNames(Collection<String> taskNums) {
|
||||
if (taskNums.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
Map<String, String> names = new LinkedHashMap<>();
|
||||
for (TaskEntity task : tasksMapper.selectList(Wrappers.<TaskEntity>lambdaQuery()
|
||||
.in(TaskEntity::getTaskNum, taskNums)
|
||||
.select(TaskEntity::getTaskNum, TaskEntity::getTaskName))) {
|
||||
names.put(task.getTaskNum(), task.getTaskName());
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+3
-5
@@ -79,14 +79,12 @@ public class RecallPrivacyService {
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void taskDeleted(String taskNum) {
|
||||
store.lock();
|
||||
for (ReviewRecallEntity row : store.all()) {
|
||||
if ("TASK".equals(row.getScopeType()) && taskNum.equals(row.getTaskNum())) {
|
||||
for (ReviewRecallEntity row : store.taskScoped(taskNum)) {
|
||||
RecallDocument doc = readable(row);
|
||||
if (doc != null) {
|
||||
store.delete(doc);
|
||||
}
|
||||
}
|
||||
}
|
||||
List<String> nums = sessions
|
||||
.selectList(Wrappers.<StudySessionsEntity>lambdaQuery().eq(StudySessionsEntity::getTaskNum, taskNum))
|
||||
.stream()
|
||||
@@ -112,7 +110,7 @@ public class RecallPrivacyService {
|
||||
public void tagRenamed(String oldName) {
|
||||
store.lock();
|
||||
String normalized = TagScopeTool.normalize(oldName);
|
||||
for (ReviewRecallEntity row : store.all()) {
|
||||
for (ReviewRecallEntity row : store.tagScoped()) {
|
||||
RecallDocument doc = readable(row);
|
||||
if (doc == null) {
|
||||
continue;
|
||||
@@ -131,7 +129,7 @@ public class RecallPrivacyService {
|
||||
public void tagsChanged() {
|
||||
store.lock();
|
||||
List<RecallDocument> docs = new ArrayList<>();
|
||||
for (ReviewRecallEntity row : store.all()) {
|
||||
for (ReviewRecallEntity row : store.tagScoped()) {
|
||||
RecallDocument doc = readable(row);
|
||||
if (doc != null) {
|
||||
docs.add(doc);
|
||||
|
||||
@@ -329,4 +329,17 @@ public class ReviewRecallStore {
|
||||
public List<ReviewRecallEntity> all() {
|
||||
return recalls.selectList(Wrappers.<ReviewRecallEntity>lambdaQuery());
|
||||
}
|
||||
|
||||
/** 只看标签范围的回忆:隐私清算按范围类型过滤,避免把整张表(含文档正文)都解析一遍。 */
|
||||
public List<ReviewRecallEntity> tagScoped() {
|
||||
return recalls.selectList(
|
||||
Wrappers.<ReviewRecallEntity>lambdaQuery().eq(ReviewRecallEntity::getScopeType, "TAG"));
|
||||
}
|
||||
|
||||
/** 只看某个任务范围的回忆:删任务时不需要扫其它范围的记录。 */
|
||||
public List<ReviewRecallEntity> taskScoped(String taskNum) {
|
||||
return recalls.selectList(Wrappers.<ReviewRecallEntity>lambdaQuery()
|
||||
.eq(ReviewRecallEntity::getScopeType, "TASK")
|
||||
.eq(ReviewRecallEntity::getTaskNum, taskNum));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,4 +49,24 @@
|
||||
AND m.deleted = 0)
|
||||
ORDER BY t.calculated_priority DESC, t.id DESC
|
||||
</select>
|
||||
|
||||
<!--
|
||||
权重保存后的批量重算:一条 CASE 更新一批任务,避免逐任务 updateById 变成 N 次往返。
|
||||
自定义 SQL 不会被 @TableLogic 追加条件,故显式 deleted = 0;租户条件由拦截器追加。
|
||||
device_info 为空时跳过,避免把已有设备信息清空(自动填充同样只在非 null 时写入)。
|
||||
-->
|
||||
<update id="updateCalculatedPriorities">
|
||||
UPDATE tasks
|
||||
SET calculated_priority = CASE id
|
||||
<foreach collection="rows" item="row">
|
||||
WHEN #{row.id} THEN #{row.calculatedPriority}
|
||||
</foreach>
|
||||
END,
|
||||
last_modified_time = #{lastModifiedTime}
|
||||
<if test="lastModifiedBy != null">, last_modified_by = #{lastModifiedBy}</if>
|
||||
<if test="deviceInfo != null">, device_info = #{deviceInfo}</if>
|
||||
WHERE id IN
|
||||
<foreach collection="rows" item="row" open="(" separator="," close=")">#{row.id}</foreach>
|
||||
AND deleted = 0
|
||||
</update>
|
||||
</mapper>
|
||||
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package com.guo.learningprogresstracker.service.impl;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||
import com.guo.learningprogresstracker.entity.UserPriorityWeightsEntity;
|
||||
import com.guo.learningprogresstracker.mapper.TasksMapper;
|
||||
import com.guo.learningprogresstracker.service.PriorityWeightsService;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* 权重重算的落库集成测试:批次 CASE 更新确实把 calculated_priority 与审计时间写回。
|
||||
*
|
||||
* <p>自定义 SQL 走不到 mock 单测里,这里用真实迁移 + MyBatis 拦截器校验整条链路。</p>
|
||||
*/
|
||||
@SpringBootTest
|
||||
class PriorityWeightsIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private PriorityWeightsService weightsService;
|
||||
|
||||
@Autowired
|
||||
private TasksMapper tasksMapper;
|
||||
|
||||
@BeforeEach
|
||||
void login() {
|
||||
StpUtil.login("priority-weights-test-" + UUID.randomUUID());
|
||||
}
|
||||
|
||||
@Test
|
||||
void saveWeightsRewritesEveryTaskPriorityInOneBatch() throws Exception {
|
||||
TaskEntity first = task();
|
||||
TaskEntity second = task();
|
||||
UserPriorityWeightsEntity weights = UserPriorityWeightsEntity.defaults();
|
||||
weights.setUrgencyWeight(0.5);
|
||||
weights.setImportanceWeight(0.2);
|
||||
weights.setContentDifficultyWeight(0.1);
|
||||
weights.setFutureValueWeight(0.1);
|
||||
weights.setSubjectivePriorityWeight(0.1);
|
||||
|
||||
weightsService.saveWeights(weights);
|
||||
|
||||
List<TaskEntity> stored = tasksMapper.selectList(
|
||||
Wrappers.<TaskEntity>lambdaQuery().in(TaskEntity::getId, List.of(first.getId(), second.getId())));
|
||||
assertEquals(2, stored.size());
|
||||
for (TaskEntity task : stored) {
|
||||
// 2*0.5 + 3*0.2 + 4*0.1 + 5*0.1 + 1*0.1 = 2.6
|
||||
assertEquals(2.6, task.getCalculatedPriority(), 0.0001);
|
||||
assertNotNull(task.getLastModifiedTime());
|
||||
}
|
||||
}
|
||||
|
||||
private TaskEntity task() {
|
||||
TaskEntity task = new TaskEntity();
|
||||
task.setTaskNum("TASK-" + UUID.randomUUID());
|
||||
task.setTaskName("权重集成测试");
|
||||
task.setUrgency(2);
|
||||
task.setImportance(3);
|
||||
task.setContentDifficulty(4);
|
||||
task.setFutureValue(5);
|
||||
task.setSubjectivePriority(1);
|
||||
tasksMapper.insert(task);
|
||||
return task;
|
||||
}
|
||||
}
|
||||
+10
-4
@@ -1,5 +1,6 @@
|
||||
package com.guo.learningprogresstracker.service.impl;
|
||||
|
||||
import com.guo.learningprogresstracker.dto.TaskPriorityUpdate;
|
||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||
import com.guo.learningprogresstracker.entity.UserPriorityWeightsEntity;
|
||||
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
||||
@@ -8,6 +9,7 @@ import com.guo.learningprogresstracker.mapper.UserPriorityWeightsMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
@@ -99,12 +101,16 @@ class PriorityWeightsServiceImplTest {
|
||||
assertEquals(Integer.valueOf(7), result.getId());
|
||||
verify(weightsMapper).updateById(weights);
|
||||
verify(weightsMapper, never()).insert(any(UserPriorityWeightsEntity.class));
|
||||
// 逐任务 updateById 改成一批 CASE 更新:重算结果落在传参里,任务数不再放大 SQL 条数
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<List<TaskPriorityUpdate>> rows = ArgumentCaptor.forClass(List.class);
|
||||
verify(tasksMapper).updateCalculatedPriorities(rows.capture(), any(), any(), any());
|
||||
verify(tasksMapper, never()).updateById(any(TaskEntity.class));
|
||||
assertEquals(2, rows.getValue().size());
|
||||
// 全 null 维度按 0 计算优先级
|
||||
assertEquals(0.0, allNullDims.getCalculatedPriority(), 0.0001);
|
||||
assertEquals(0.0, rows.getValue().get(0).getCalculatedPriority(), 0.0001);
|
||||
// 2*0.35 + 3*0.25 + 4*0.20 + 5*0.10 + 1*0.10 = 2.85
|
||||
assertEquals(2.85, fullDims.getCalculatedPriority(), 0.0001);
|
||||
verify(tasksMapper).updateById(allNullDims);
|
||||
verify(tasksMapper).updateById(fullDims);
|
||||
assertEquals(2.85, rows.getValue().get(1).getCalculatedPriority(), 0.0001);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user