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