feat: 新增标签字典与打标签候选接口
This commit is contained in:
@@ -3,6 +3,7 @@ package com.guo.learningprogresstracker.controller;
|
||||
import com.guo.learningprogresstracker.dto.MarkerUsage;
|
||||
import com.guo.learningprogresstracker.entity.CommonResult;
|
||||
import com.guo.learningprogresstracker.entity.MarkerEntity;
|
||||
import com.guo.learningprogresstracker.entity.UserTagEntity;
|
||||
import com.guo.learningprogresstracker.enums.MarkerObjectTypeEnum;
|
||||
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
||||
import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
|
||||
@@ -104,4 +105,50 @@ public class MarkerController {
|
||||
public CommonResult<List<MarkerUsage>> listUserTagUsage() {
|
||||
return CommonResult.success(markerService.listUserTagUsage());
|
||||
}
|
||||
|
||||
/**
|
||||
* 标签字典:打标签的候选标签(尚未挂在任何对象上的也算)
|
||||
*/
|
||||
@GetMapping("/tags/dictionary")
|
||||
public CommonResult<List<UserTagEntity>> listDictionaryTags() {
|
||||
return CommonResult.success(markerService.listDictionaryTags());
|
||||
}
|
||||
|
||||
/**
|
||||
* 往字典里新建一个(空)标签;同名(按去空格、大小写不敏感)已存在时幂等返回既有项
|
||||
*/
|
||||
@PostMapping("/tags/dictionary")
|
||||
public CommonResult<UserTagEntity> createDictionaryTag(@RequestBody Map<String, String> body)
|
||||
throws ErrorParameterException {
|
||||
String tagName = body == null ? null : body.get("tagName");
|
||||
return CommonResult.success(markerService.createDictionaryTag(tagName));
|
||||
}
|
||||
|
||||
/**
|
||||
* 字典标签改名:只影响候选,不改动任何对象上已经打好的标记
|
||||
*/
|
||||
@PutMapping("/tags/dictionary/{id}")
|
||||
public CommonResult<UserTagEntity> renameDictionaryTag(@PathVariable Integer id,
|
||||
@RequestBody Map<String, String> body)
|
||||
throws ErrorParameterException, NotFindEntitiesException {
|
||||
String newName = body == null ? null : body.get("newName");
|
||||
return CommonResult.success(markerService.renameDictionaryTag(id, newName));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除字典标签:只影响候选,不改动任何对象上已经打好的标记
|
||||
*/
|
||||
@DeleteMapping("/tags/dictionary/{id}")
|
||||
public CommonResult<Void> deleteDictionaryTag(@PathVariable Integer id) throws NotFindEntitiesException {
|
||||
markerService.deleteDictionaryTag(id);
|
||||
return CommonResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 打标签的候选/建议:字典标签 ∪ 已使用过的用户标签(含自动任务名标签),支持关键字过滤
|
||||
*/
|
||||
@GetMapping("/tags/candidates")
|
||||
public CommonResult<List<String>> listTagCandidates(@RequestParam(required = false) String keyword) {
|
||||
return CommonResult.success(markerService.listTagCandidates(keyword));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.guo.learningprogresstracker.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 标签字典项(闭环-03)。
|
||||
*
|
||||
* <p>字典只是打标签的候选项来源,与「对象上的标记」是两份数据:缺了对象的标签不是标记,
|
||||
* 所以不在 markers 里为「空标签」造没有对象的行。字典的改名与删除只影响候选,
|
||||
* 不改动任何对象上已经打好的标记。</p>
|
||||
*/
|
||||
@TableName(value = "user_tags")
|
||||
@Data
|
||||
public class UserTagEntity extends BaseEntity implements Serializable {
|
||||
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
/** 标签名:展示保留用户输入的原样 */
|
||||
@TableField(value = "tag_name")
|
||||
private String tagName;
|
||||
|
||||
/** 归一化标签名:去首尾空白 + 小写;同一用户下唯一 */
|
||||
@TableField(value = "normalized_name")
|
||||
private String normalizedName;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.guo.learningprogresstracker.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.guo.learningprogresstracker.entity.UserTagEntity;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
|
||||
/**
|
||||
* 标签字典 Mapper(闭环-03)。
|
||||
*/
|
||||
@Mapper
|
||||
public interface UserTagMapper extends BaseMapper<UserTagEntity> {
|
||||
|
||||
/**
|
||||
* 按归一化名取字典项,<b>含已逻辑删除的行</b>——用于「删除后再次用到同名标签」时复用旧行。
|
||||
* <p>自定义 SQL 不会被 {@code @TableLogic} 追加 {@code deleted = 0};
|
||||
* 租户条件 {@code created_by} 仍由 {@code TenantLineInnerInterceptor} 自动追加。</p>
|
||||
*/
|
||||
@Select("SELECT * FROM user_tags WHERE normalized_name = #{normalizedName} LIMIT 1")
|
||||
UserTagEntity selectAnyByNormalizedName(@Param("normalizedName") String normalizedName);
|
||||
|
||||
/**
|
||||
* 复用(复活)一条已逻辑删除的字典项:把 {@code deleted} 归零并更新展示名。
|
||||
* <p>避免重新插入时与被逻辑删除的同名行撞唯一键。租户条件由拦截器自动追加。</p>
|
||||
*/
|
||||
@Update("UPDATE user_tags SET deleted = 0, tag_name = #{tagName}, last_modified_time = now() WHERE id = #{id}")
|
||||
int revive(@Param("id") Integer id, @Param("tagName") String tagName);
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.guo.learningprogresstracker.service;
|
||||
|
||||
import com.guo.learningprogresstracker.dto.MarkerUsage;
|
||||
import com.guo.learningprogresstracker.entity.MarkerEntity;
|
||||
import com.guo.learningprogresstracker.entity.UserTagEntity;
|
||||
import com.guo.learningprogresstracker.enums.MarkerObjectTypeEnum;
|
||||
import com.guo.learningprogresstracker.enums.SystemMarkerEnum;
|
||||
import com.guo.learningprogresstracker.exception.ErrorParameterException;
|
||||
@@ -81,6 +82,35 @@ public interface MarkerService {
|
||||
*/
|
||||
void addOwnerTaskNameTag(MarkerObjectTypeEnum objectType, String objectNum, String taskNum);
|
||||
|
||||
/**
|
||||
* 标签字典项列表(闭环-03)。
|
||||
*/
|
||||
List<UserTagEntity> listDictionaryTags();
|
||||
|
||||
/**
|
||||
* 往字典里建一个(空)标签;同名(按去空格、大小写不敏感归一)已存在时直接返回既有项。
|
||||
*/
|
||||
UserTagEntity createDictionaryTag(String tagName) throws ErrorParameterException;
|
||||
|
||||
/**
|
||||
* 字典项改名:只改候选项,不动任何对象上已经打好的标记。
|
||||
* <p>改名后与另一项归一化名相同则合并(保留目标项、删除当前项)。</p>
|
||||
*/
|
||||
UserTagEntity renameDictionaryTag(Integer id, String newName)
|
||||
throws ErrorParameterException, NotFindEntitiesException;
|
||||
|
||||
/**
|
||||
* 删除一个字典项(逻辑删除):只影响候选,不动任何对象上已经打好的标记。
|
||||
*/
|
||||
void deleteDictionaryTag(Integer id) throws NotFindEntitiesException;
|
||||
|
||||
/**
|
||||
* 打标签的候选/建议:字典标签 ∪ 已使用过的用户标签(含自动任务名标签)。
|
||||
*
|
||||
* @param keyword 关键字,按大小写不敏感的子串过滤;为空表示不过滤
|
||||
*/
|
||||
List<String> listTagCandidates(String keyword);
|
||||
|
||||
/**
|
||||
* 对象被删除时清理其标记(例如碎片被删除)。
|
||||
*/
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.guo.learningprogresstracker.dto.MarkerUsage;
|
||||
import com.guo.learningprogresstracker.entity.MarkerEntity;
|
||||
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
|
||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||
import com.guo.learningprogresstracker.entity.UserTagEntity;
|
||||
import com.guo.learningprogresstracker.enums.MarkerKindEnum;
|
||||
import com.guo.learningprogresstracker.enums.MarkerObjectTypeEnum;
|
||||
import com.guo.learningprogresstracker.enums.SystemMarkerEnum;
|
||||
@@ -13,6 +14,7 @@ import com.guo.learningprogresstracker.exception.NotFindEntitiesException;
|
||||
import com.guo.learningprogresstracker.mapper.MarkerMapper;
|
||||
import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper;
|
||||
import com.guo.learningprogresstracker.mapper.TasksMapper;
|
||||
import com.guo.learningprogresstracker.mapper.UserTagMapper;
|
||||
import com.guo.learningprogresstracker.service.MarkerService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -24,6 +26,7 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -42,6 +45,7 @@ public class MarkerServiceImpl implements MarkerService {
|
||||
private final MarkerMapper markerMapper;
|
||||
private final StudyReportFragmentsMapper studyReportFragmentsMapper;
|
||||
private final TasksMapper tasksMapper;
|
||||
private final UserTagMapper userTagMapper;
|
||||
|
||||
@Override
|
||||
public List<MarkerEntity> listByObject(MarkerObjectTypeEnum objectType, String objectNum) {
|
||||
@@ -86,6 +90,7 @@ public class MarkerServiceImpl implements MarkerService {
|
||||
.last("LIMIT 1"))
|
||||
.stream().findFirst();
|
||||
if (existing.isPresent()) {
|
||||
ensureDictionaryEntry(name);
|
||||
return existing.get();
|
||||
}
|
||||
|
||||
@@ -97,6 +102,8 @@ public class MarkerServiceImpl implements MarkerService {
|
||||
// 用户标签只有名,没有值:标签名本身就是全部信息
|
||||
marker.setMarkerValue(null);
|
||||
markerMapper.insert(marker);
|
||||
// 打标签时输入的新名字自动补进字典(闭环-03)
|
||||
ensureDictionaryEntry(name);
|
||||
return marker;
|
||||
}
|
||||
|
||||
@@ -220,6 +227,104 @@ public class MarkerServiceImpl implements MarkerService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<UserTagEntity> listDictionaryTags() {
|
||||
return userTagMapper.selectList(Wrappers.<UserTagEntity>lambdaQuery()
|
||||
.orderByAsc(UserTagEntity::getTagName)
|
||||
.orderByAsc(UserTagEntity::getId));
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public UserTagEntity createDictionaryTag(String tagName) throws ErrorParameterException {
|
||||
String display = normalizeTagName(tagName);
|
||||
String normalized = normalizeTagKey(display);
|
||||
UserTagEntity existing = userTagMapper.selectAnyByNormalizedName(normalized);
|
||||
if (existing != null) {
|
||||
if (existing.getDeleted() == null || existing.getDeleted() == 0) {
|
||||
// 已在字典里,幂等返回
|
||||
return existing;
|
||||
}
|
||||
// 曾有同名项被删除:复用(复活)旧行,避免重新插入撞唯一键
|
||||
userTagMapper.revive(existing.getId(), display);
|
||||
return userTagMapper.selectById(existing.getId());
|
||||
}
|
||||
UserTagEntity entity = new UserTagEntity();
|
||||
entity.setTagName(display);
|
||||
entity.setNormalizedName(normalized);
|
||||
userTagMapper.insert(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public UserTagEntity renameDictionaryTag(Integer id, String newName)
|
||||
throws ErrorParameterException, NotFindEntitiesException {
|
||||
UserTagEntity current = requireDictionaryTag(id);
|
||||
String display = normalizeTagName(newName);
|
||||
String normalized = normalizeTagKey(display);
|
||||
|
||||
if (normalized.equals(current.getNormalizedName())) {
|
||||
// 只是大小写/空白不同:更新展示名即可
|
||||
if (!display.equals(current.getTagName())) {
|
||||
UserTagEntity patch = new UserTagEntity();
|
||||
patch.setId(current.getId());
|
||||
patch.setTagName(display);
|
||||
userTagMapper.updateById(patch);
|
||||
}
|
||||
return userTagMapper.selectById(current.getId());
|
||||
}
|
||||
|
||||
UserTagEntity target = userTagMapper.selectAnyByNormalizedName(normalized);
|
||||
if (target != null) {
|
||||
// 目标名已在字典里(可能是一条被删除的行):合并——保留目标项、删掉当前项
|
||||
if (target.getDeleted() != null && target.getDeleted() != 0) {
|
||||
userTagMapper.revive(target.getId(), display);
|
||||
}
|
||||
userTagMapper.deleteById(current.getId());
|
||||
log.info("字典标签[{}]改名为[{}],与既有项合并", current.getTagName(), display);
|
||||
return userTagMapper.selectById(target.getId());
|
||||
}
|
||||
|
||||
UserTagEntity patch = new UserTagEntity();
|
||||
patch.setId(current.getId());
|
||||
patch.setTagName(display);
|
||||
patch.setNormalizedName(normalized);
|
||||
userTagMapper.updateById(patch);
|
||||
log.info("字典标签[{}]改名为[{}]", current.getTagName(), display);
|
||||
return userTagMapper.selectById(current.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteDictionaryTag(Integer id) throws NotFindEntitiesException {
|
||||
UserTagEntity existing = requireDictionaryTag(id);
|
||||
// 逻辑删除:只影响候选,对象上已经打好的标记不在此表里,不受影响
|
||||
userTagMapper.deleteById(existing.getId());
|
||||
log.info("字典标签[{}]已删除", existing.getTagName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> listTagCandidates(String keyword) {
|
||||
String kw = keyword == null ? "" : keyword.trim().toLowerCase(Locale.ROOT);
|
||||
// 归一化名 → 展示名;按归一化去重(大小写不敏感),字典展示名优先
|
||||
Map<String, String> candidates = new LinkedHashMap<>();
|
||||
for (UserTagEntity tag : listDictionaryTags()) {
|
||||
candidates.putIfAbsent(tag.getNormalizedName(), tag.getTagName());
|
||||
}
|
||||
// 已使用过的用户标签(含自动任务名标签)
|
||||
for (MarkerUsage usage : listUserTagUsage()) {
|
||||
if (!StringUtils.hasText(usage.getMarkerName())) {
|
||||
continue;
|
||||
}
|
||||
String name = usage.getMarkerName().trim();
|
||||
candidates.putIfAbsent(normalizeTagKey(name), name);
|
||||
}
|
||||
return candidates.values().stream()
|
||||
.filter(name -> kw.isEmpty() || name.toLowerCase(Locale.ROOT).contains(kw))
|
||||
.sorted()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAllMarkers(MarkerObjectTypeEnum objectType, String objectNum) {
|
||||
if (objectType == null || !StringUtils.hasText(objectNum)) {
|
||||
@@ -242,6 +347,29 @@ public class MarkerServiceImpl implements MarkerService {
|
||||
return existing;
|
||||
}
|
||||
|
||||
private UserTagEntity requireDictionaryTag(Integer id) throws NotFindEntitiesException {
|
||||
UserTagEntity existing = id == null ? null : userTagMapper.selectById(id);
|
||||
if (existing == null) {
|
||||
log.warn("未能找到标签字典项id[{}]", id);
|
||||
throw new NotFindEntitiesException("这个标签不存在或已被删除");
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
|
||||
/** 归一化标签名:去首尾空白 + 小写;用于字典去重与候选过滤(中文不受影响,英文大小写不敏感) */
|
||||
private String normalizeTagKey(String tagName) {
|
||||
return tagName.trim().toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
/** 打标签时输入的新名字自动补进字典(闭环-03);失败只记日志,不影响打标本身 */
|
||||
private void ensureDictionaryEntry(String tagName) {
|
||||
try {
|
||||
createDictionaryTag(tagName);
|
||||
} catch (ErrorParameterException e) {
|
||||
log.warn("标签[{}]加入字典失败:{}", tagName, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** 标签名规范化:去首尾空白并限制长度 */
|
||||
private String normalizeTagName(String markerName) throws ErrorParameterException {
|
||||
if (!StringUtils.hasText(markerName)) {
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
-- 闭环-03:标签字典。
|
||||
--
|
||||
-- 口径来源(结论见 lpt-docs/docs/concepts/marker.md「标签字典」):
|
||||
-- 字典只是打标签的候选项来源,与「对象上的标记」是两份数据:缺了对象的标签不是标记,
|
||||
-- 因此不在 markers 里为「空标签」造没有对象的行。
|
||||
-- 同一用户下按归一化名称(去首尾空白 + 小写)唯一,展示保留用户输入的原样。
|
||||
-- 字典的改名与删除只影响候选,不改动任何对象上已打好的标记。
|
||||
--
|
||||
-- 唯一键与逻辑删除:唯一键不含 deleted,因此「同一用户 + 同一归一化名」全局只有一行。
|
||||
-- 删除是逻辑删除(deleted=1)。打标签时输入不在字典里的名字会自动补进字典,
|
||||
-- 删除后若再次用到同名标签,服务层复用(复活)这条被逻辑删除的行,而不是再插一行——
|
||||
-- 否则被逻辑删除的行会与重新插入的同名行撞唯一键。
|
||||
CREATE TABLE user_tags
|
||||
(
|
||||
id INT AUTO_INCREMENT COMMENT 'id无业务含义'
|
||||
PRIMARY KEY,
|
||||
tag_name VARCHAR(100) NOT NULL COMMENT '标签名:展示保留用户输入的原样',
|
||||
normalized_name VARCHAR(100) NOT NULL COMMENT '归一化标签名:去首尾空白 + 小写,用于去重与关键字过滤',
|
||||
created_time DATETIME NOT NULL COMMENT '创建时间',
|
||||
created_by VARCHAR(255) NULL,
|
||||
last_modified_time DATETIME NULL,
|
||||
last_modified_by VARCHAR(255) NULL,
|
||||
device_info VARCHAR(50) NULL COMMENT '操作者设备类型',
|
||||
deleted INT DEFAULT 0 NOT NULL COMMENT '逻辑删除符',
|
||||
-- 同一用户下归一化名唯一(多租户,标签只需在租户内唯一)。
|
||||
-- 键长:created_by(255×4) + normalized_name(100×4) = 1420 字节,未越过 InnoDB 3072 字节上限。
|
||||
-- H2 不校验该上限,改动索引时务必用 MigrationIndexKeyLengthTest 复算。
|
||||
UNIQUE KEY uk_user_tags_created_by_normalized (created_by, normalized_name)
|
||||
)
|
||||
COMMENT '标签字典:打标签的候选项,与对象上的标记是两份数据';
|
||||
+209
@@ -0,0 +1,209 @@
|
||||
package com.guo.learningprogresstracker.service.impl;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.guo.learningprogresstracker.dto.request.CreateFragmentsRequest;
|
||||
import com.guo.learningprogresstracker.entity.MarkerEntity;
|
||||
import com.guo.learningprogresstracker.entity.StudyReportFragmentsEntity;
|
||||
import com.guo.learningprogresstracker.entity.StudySessionsEntity;
|
||||
import com.guo.learningprogresstracker.entity.TaskEntity;
|
||||
import com.guo.learningprogresstracker.entity.UserTagEntity;
|
||||
import com.guo.learningprogresstracker.enums.MarkerKindEnum;
|
||||
import com.guo.learningprogresstracker.enums.MarkerObjectTypeEnum;
|
||||
import com.guo.learningprogresstracker.mapper.StudyReportFragmentsMapper;
|
||||
import com.guo.learningprogresstracker.mapper.StudySessionsMapper;
|
||||
import com.guo.learningprogresstracker.mapper.TasksMapper;
|
||||
import com.guo.learningprogresstracker.service.MarkerService;
|
||||
import com.guo.learningprogresstracker.service.StudyReportFragmentsService;
|
||||
import com.guo.learningprogresstracker.utils.GenerateNumTool;
|
||||
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 java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 标签字典与打标签候选接口的落库集成测试(闭环-03)。
|
||||
*
|
||||
* <p>覆盖口径:新建/改名/删除/列出、归一化去重、打标签自动入字典、自动任务名标签进入候选、
|
||||
* 关键字过滤、改名与删除不影响对象上已经打好的标记、删除后同名标签可复用。</p>
|
||||
*/
|
||||
@SpringBootTest
|
||||
class UserTagDictionaryIntegrationTest {
|
||||
|
||||
private static final String USER = "标签字典集成测试用户";
|
||||
|
||||
private static final AtomicInteger SEQ = new AtomicInteger();
|
||||
|
||||
@Autowired
|
||||
private MarkerService markerService;
|
||||
|
||||
@Autowired
|
||||
private StudyReportFragmentsService fragmentsService;
|
||||
|
||||
@Autowired
|
||||
private StudyReportFragmentsMapper fragmentsMapper;
|
||||
|
||||
@Autowired
|
||||
private StudySessionsMapper studySessionsMapper;
|
||||
|
||||
@Autowired
|
||||
private TasksMapper tasksMapper;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
StpUtil.login(USER, "test_device");
|
||||
}
|
||||
|
||||
/** 标签名按用例唯一:候选项是「按名」列出的,共用固定名字会让用例之间互相污染 */
|
||||
private static String tag(String base) {
|
||||
return base + "-" + SEQ.incrementAndGet();
|
||||
}
|
||||
|
||||
private String saveTask(String taskName) {
|
||||
String taskNum = "TAGD-IT-" + System.nanoTime() + "-" + SEQ.incrementAndGet();
|
||||
TaskEntity task = new TaskEntity();
|
||||
task.setTaskNum(taskNum);
|
||||
task.setTaskName(taskName);
|
||||
tasksMapper.insert(task);
|
||||
return taskNum;
|
||||
}
|
||||
|
||||
private StudyReportFragmentsEntity createFragment(String taskNum, String content) throws Exception {
|
||||
StudySessionsEntity session = new StudySessionsEntity();
|
||||
session.setSessionNum(GenerateNumTool.generateNum("SESSTD"));
|
||||
session.setTaskNum(taskNum);
|
||||
session.setStartTime(LocalDateTime.now());
|
||||
session.setLastStartTime(LocalDateTime.now());
|
||||
session.setSessionState("ENDED");
|
||||
studySessionsMapper.insert(session);
|
||||
|
||||
CreateFragmentsRequest request = new CreateFragmentsRequest();
|
||||
request.setContextNum(session.getSessionNum());
|
||||
request.setContent(content);
|
||||
fragmentsService.createFragments(request);
|
||||
return fragmentsMapper.selectOne(Wrappers.<StudyReportFragmentsEntity>lambdaQuery()
|
||||
.eq(StudyReportFragmentsEntity::getContextNum, session.getSessionNum())
|
||||
.orderByDesc(StudyReportFragmentsEntity::getId)
|
||||
.last("LIMIT 1"));
|
||||
}
|
||||
|
||||
private List<String> userTagNames(Integer fragmentId) {
|
||||
return markerService.listByObject(MarkerObjectTypeEnum.FRAGMENT, String.valueOf(fragmentId)).stream()
|
||||
.filter(m -> MarkerKindEnum.USER.name().equals(m.getKind()))
|
||||
.map(MarkerEntity::getMarkerName)
|
||||
.sorted()
|
||||
.toList();
|
||||
}
|
||||
|
||||
private UserTagEntity dictionaryOf(String tagName) {
|
||||
return markerService.listDictionaryTags().stream()
|
||||
.filter(t -> tagName.equals(t.getTagName()))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new AssertionError("字典里应有标签:" + tagName));
|
||||
}
|
||||
|
||||
@Test
|
||||
void autoTaskNameTag_entersDictionary() throws Exception {
|
||||
String taskName = tag("线性代数");
|
||||
String taskNum = saveTask(taskName);
|
||||
|
||||
createFragment(taskNum, "自动入字典");
|
||||
|
||||
assertTrue(markerService.listDictionaryTags().stream()
|
||||
.anyMatch(t -> taskName.equals(t.getTagName())),
|
||||
"自动任务名标签应自动进入标签字典");
|
||||
}
|
||||
|
||||
@Test
|
||||
void createEmptyTag_listsIt() throws Exception {
|
||||
String tagName = tag("英语");
|
||||
|
||||
UserTagEntity created = markerService.createDictionaryTag(tagName);
|
||||
|
||||
assertNotNull(created.getId());
|
||||
assertEquals(tagName, dictionaryOf(tagName).getTagName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createSameTag_isIdempotentIgnoringCaseAndBlank() throws Exception {
|
||||
String base = tag("English");
|
||||
|
||||
UserTagEntity first = markerService.createDictionaryTag(base);
|
||||
UserTagEntity second = markerService.createDictionaryTag(" " + base.toLowerCase() + " ");
|
||||
|
||||
assertEquals(first.getId(), second.getId(), "归一化同名应复用同一条字典项");
|
||||
assertEquals(base, second.getTagName(), "展示保留首次输入的原样");
|
||||
assertEquals(1, markerService.listDictionaryTags().stream()
|
||||
.filter(t -> base.toLowerCase().equals(t.getNormalizedName()))
|
||||
.count());
|
||||
}
|
||||
|
||||
@Test
|
||||
void renameDictionary_doesNotTouchMarkers() throws Exception {
|
||||
String taskNum = saveTask(tag("字典改名任务"));
|
||||
StudyReportFragmentsEntity fragment = createFragment(taskNum, "改名不影响标记");
|
||||
String markerTag = tag("英语");
|
||||
markerService.addUserTag(MarkerObjectTypeEnum.FRAGMENT, String.valueOf(fragment.getId()), markerTag);
|
||||
|
||||
String renamed = tag("英文");
|
||||
markerService.renameDictionaryTag(dictionaryOf(markerTag).getId(), renamed);
|
||||
|
||||
// 对象上已经打好的标记不受字典改名影响
|
||||
assertTrue(userTagNames(fragment.getId()).contains(markerTag));
|
||||
assertTrue(markerService.listDictionaryTags().stream().anyMatch(t -> renamed.equals(t.getTagName())));
|
||||
assertTrue(markerService.listDictionaryTags().stream().noneMatch(t -> markerTag.equals(t.getTagName())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteDictionary_doesNotTouchMarkers() throws Exception {
|
||||
String taskNum = saveTask(tag("字典删除任务"));
|
||||
StudyReportFragmentsEntity fragment = createFragment(taskNum, "删除不影响标记");
|
||||
String markerTag = tag("数学");
|
||||
markerService.addUserTag(MarkerObjectTypeEnum.FRAGMENT, String.valueOf(fragment.getId()), markerTag);
|
||||
|
||||
markerService.deleteDictionaryTag(dictionaryOf(markerTag).getId());
|
||||
|
||||
assertTrue(userTagNames(fragment.getId()).contains(markerTag), "字典删除不触碰对象上的标记");
|
||||
assertTrue(markerService.listDictionaryTags().stream().noneMatch(t -> markerTag.equals(t.getTagName())));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteThenReuseTag_revivesDictionaryRow() throws Exception {
|
||||
String tagName = tag("复用标签");
|
||||
markerService.createDictionaryTag(tagName);
|
||||
markerService.deleteDictionaryTag(dictionaryOf(tagName).getId());
|
||||
assertTrue(markerService.listDictionaryTags().stream().noneMatch(t -> tagName.equals(t.getTagName())));
|
||||
|
||||
UserTagEntity revived = markerService.createDictionaryTag(tagName);
|
||||
|
||||
assertEquals(tagName, revived.getTagName());
|
||||
assertEquals(1, markerService.listDictionaryTags().stream()
|
||||
.filter(t -> tagName.equals(t.getTagName()))
|
||||
.count(), "删除后重建不应产生重复字典项");
|
||||
}
|
||||
|
||||
@Test
|
||||
void candidates_includeAutoTaskTagAndFilterByKeyword() throws Exception {
|
||||
String taskName = "候选过滤-" + tag("线性代数");
|
||||
String taskNum = saveTask(taskName);
|
||||
createFragment(taskNum, "候选");
|
||||
|
||||
// 自动任务名标签(已使用过的用户标签)进入候选
|
||||
assertTrue(markerService.listTagCandidates("候选过滤").contains(taskName));
|
||||
// 空关键字不过滤:仍包含该标签
|
||||
assertTrue(markerService.listTagCandidates(null).contains(taskName));
|
||||
// 关键字不命中时为空
|
||||
assertTrue(markerService.listTagCandidates("绝不可能匹配的关键字XYZ").isEmpty());
|
||||
|
||||
// 只建了、还没挂过任何对象的字典标签也在候选里
|
||||
String dictOnly = tag("字典独有");
|
||||
markerService.createDictionaryTag(dictOnly);
|
||||
assertTrue(markerService.listTagCandidates("字典独有").contains(dictOnly));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user