|
|
@@ -0,0 +1,168 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.entity.FamilyRelationType;
|
|
|
+import com.etotem.cfc.entity.FamilyRelationship;
|
|
|
+import com.etotem.cfc.mapper.FamilyRelationTypeMapper;
|
|
|
+import com.etotem.cfc.mapper.FamilyRelationshipMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 家庭关系类型字典管理(SC-A-04 / TC-A-10,能力键 admin.familyRelation.manage)。
|
|
|
+ *
|
|
|
+ * <p>Web 管理端关系类型 CRUD(列表/新增/编辑/停用)的数据载体 {@code family_relation_types};
|
|
|
+ * 停用前按 v2 模型检查 {@code family_relationships} 引用(relation_type = type_key,
|
|
|
+ * 同辈 spouse/sibling 类存在直接引用;直系称谓由 generation+性别派生计算,不走字典行)。
|
|
|
+ * 名称唯一(TC-A-10-E 新增重名拒绝)+ 注入防护(名称含注入 400 拒绝)。</p>
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class AdminFamilyRelationService {
|
|
|
+
|
|
|
+ /** typeKey 合法格式:小写字母开头 + 小写字母/数字/下划线 */
|
|
|
+ private static final Pattern TYPE_KEY_PATTERN = Pattern.compile("^[a-z][a-z0-9_]{0,49}$");
|
|
|
+
|
|
|
+ /** 名称/备注禁止的注入字符(SQL 注入/脚本注入,TC-A-10-E) */
|
|
|
+ private static final Pattern FORBIDDEN_CHARS = Pattern.compile("[<>\"'`;\\\\]");
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private FamilyRelationTypeMapper relationTypeMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private FamilyRelationshipMapper familyRelationshipMapper;
|
|
|
+
|
|
|
+ /** 管理端关系类型字典列表(含停用,按排序 + ID 升序) */
|
|
|
+ public Result<List<FamilyRelationType>> list() {
|
|
|
+ LambdaQueryWrapper<FamilyRelationType> wrapper = new LambdaQueryWrapper<FamilyRelationType>()
|
|
|
+ .orderByAsc(FamilyRelationType::getSortOrder)
|
|
|
+ .orderByAsc(FamilyRelationType::getId);
|
|
|
+ return Result.success(relationTypeMapper.selectList(wrapper));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 新增关系类型(type_key / type_name 唯一,重名拒绝;名称含注入 400 拒绝) */
|
|
|
+ public Result<FamilyRelationType> create(FamilyRelationType type) {
|
|
|
+ if (type == null) {
|
|
|
+ return Result.badRequest("参数不能为空");
|
|
|
+ }
|
|
|
+ Result<FamilyRelationType> checked = validateAndNormalize(type, null);
|
|
|
+ if (checked != null) {
|
|
|
+ return checked;
|
|
|
+ }
|
|
|
+ Date now = new Date();
|
|
|
+ type.setId(null);
|
|
|
+ type.setCreatedAt(now);
|
|
|
+ type.setUpdatedAt(now);
|
|
|
+ relationTypeMapper.insert(type);
|
|
|
+ log.info("家庭关系类型新增: id={}, key={}, name={}", type.getId(), type.getTypeKey(), type.getTypeName());
|
|
|
+ return Result.success(type);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 编辑关系类型(重名校验排除自身;不允许改 type_key) */
|
|
|
+ public Result<FamilyRelationType> update(FamilyRelationType type) {
|
|
|
+ if (type == null || type.getId() == null) {
|
|
|
+ return Result.badRequest("缺少 id,无法编辑关系类型");
|
|
|
+ }
|
|
|
+ FamilyRelationType exists = relationTypeMapper.selectById(type.getId());
|
|
|
+ if (exists == null) {
|
|
|
+ return Result.notFound("关系类型不存在: id=" + type.getId());
|
|
|
+ }
|
|
|
+ Result<FamilyRelationType> checked = validateAndNormalize(type, type.getId());
|
|
|
+ if (checked != null) {
|
|
|
+ return checked;
|
|
|
+ }
|
|
|
+ // type_key 是引用锚点(family_relationships.relation_type 同值),不允许变更
|
|
|
+ type.setTypeKey(exists.getTypeKey());
|
|
|
+ type.setCreatedAt(null);
|
|
|
+ type.setUpdatedAt(new Date());
|
|
|
+ relationTypeMapper.updateById(type);
|
|
|
+ log.info("家庭关系类型编辑: id={}, name={}", type.getId(), type.getTypeName());
|
|
|
+ return Result.success(type);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 停用关系类型(enabled=0)。
|
|
|
+ * SC-A-04 边界 + TC-A-10-E 步骤2:被 {@code family_relationships} 引用时拒绝,
|
|
|
+ * 提示先解绑(spouse/sibling 类通过 relation_type 直接引用;直系称谓由
|
|
|
+ * generation+性别派生,不构成字典行引用)。
|
|
|
+ */
|
|
|
+ public Result<Void> disable(Long id) {
|
|
|
+ if (id == null) {
|
|
|
+ return Result.badRequest("缺少 id,无法停用关系类型");
|
|
|
+ }
|
|
|
+ FamilyRelationType exists = relationTypeMapper.selectById(id);
|
|
|
+ if (exists == null) {
|
|
|
+ return Result.notFound("关系类型不存在: id=" + id);
|
|
|
+ }
|
|
|
+ long refs = familyRelationshipMapper.selectCount(
|
|
|
+ new LambdaQueryWrapper<FamilyRelationship>()
|
|
|
+ .eq(FamilyRelationship::getRelationType, exists.getTypeKey())
|
|
|
+ .or()
|
|
|
+ .eq(FamilyRelationship::getRelationshipKey, exists.getTypeName()));
|
|
|
+ if (refs > 0) {
|
|
|
+ return Result.conflict("该关系类型已被 " + refs + " 条家庭关系引用,请先解绑后再停用");
|
|
|
+ }
|
|
|
+ exists.setEnabled(0);
|
|
|
+ exists.setUpdatedAt(new Date());
|
|
|
+ relationTypeMapper.updateById(exists);
|
|
|
+ log.info("家庭关系类型停用: id={}, key={}, name={}", id, exists.getTypeKey(), exists.getTypeName());
|
|
|
+ return Result.success(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 公共校验 + 缺省值归一化:失败返回 Result,成功返回 null(excludeId 用于编辑排除自身)。 */
|
|
|
+ private Result<FamilyRelationType> validateAndNormalize(FamilyRelationType type, Long excludeId) {
|
|
|
+ String key = type.getTypeKey();
|
|
|
+ if (!StringUtils.hasText(key)) {
|
|
|
+ return Result.badRequest("类型标识 typeKey 不能为空");
|
|
|
+ }
|
|
|
+ key = key.trim().toLowerCase();
|
|
|
+ if (!TYPE_KEY_PATTERN.matcher(key).matches()) {
|
|
|
+ return Result.badRequest("类型标识只能是小写字母开头的字母/数字/下划线");
|
|
|
+ }
|
|
|
+ type.setTypeKey(key);
|
|
|
+
|
|
|
+ String name = type.getTypeName();
|
|
|
+ if (!StringUtils.hasText(name)) {
|
|
|
+ return Result.badRequest("类型名称不能为空");
|
|
|
+ }
|
|
|
+ name = name.trim();
|
|
|
+ if (name.length() > 100) {
|
|
|
+ return Result.badRequest("类型名称过长(最多100字符)");
|
|
|
+ }
|
|
|
+ if (FORBIDDEN_CHARS.matcher(name).find() || FORBIDDEN_CHARS.matcher(key).find()) {
|
|
|
+ return Result.badRequest("类型名称/标识含非法字符,已拒绝");
|
|
|
+ }
|
|
|
+ type.setTypeName(name);
|
|
|
+
|
|
|
+ if (type.getSortOrder() == null) {
|
|
|
+ type.setSortOrder(0);
|
|
|
+ }
|
|
|
+ if (type.getEnabled() == null) {
|
|
|
+ type.setEnabled(1);
|
|
|
+ }
|
|
|
+ // type_key 唯一
|
|
|
+ FamilyRelationType keyDup = relationTypeMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<FamilyRelationType>()
|
|
|
+ .eq(FamilyRelationType::getTypeKey, key)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ if (keyDup != null && (excludeId == null || !keyDup.getId().equals(excludeId))) {
|
|
|
+ return Result.error("关系类型标识已存在: " + key);
|
|
|
+ }
|
|
|
+ // type_name 唯一(TC-A-10-E 新增重名拒绝)
|
|
|
+ FamilyRelationType nameDup = relationTypeMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<FamilyRelationType>()
|
|
|
+ .eq(FamilyRelationType::getTypeName, name)
|
|
|
+ .last("LIMIT 1"));
|
|
|
+ if (nameDup != null && (excludeId == null || !nameDup.getId().equals(excludeId))) {
|
|
|
+ return Result.error("关系类型名称已存在: " + name);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|