Commit fe2dc7b9 by 杨卫

feat:标签分类自测

1 parent 60f1131e
Showing 22 changed files with 277 additions and 98 deletions
...@@ -36,4 +36,9 @@ public class TagCategoryCreateReqDTO implements Serializable { ...@@ -36,4 +36,9 @@ public class TagCategoryCreateReqDTO implements Serializable {
* 备注 * 备注
*/ */
private String remark; private String remark;
/**
* 创建人
*/
private String createdBy;
} }
package cn.com.glsx.dj.strategy.dto; package cn.com.glsx.dj.strategy.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
/** /**
* 标签类别叶子节点响应DTO * 可新增子级的分类响应DTO
* @author yangwei_by_claude * @author yangwei_by_claude
* @version 1.0 * @version 1.0
* @since 2026/06/15 10:00 * @since 2026/06/15 10:00
*/ */
@Data @Data
@ApiModel("可新增子级的分类响应")
public class TagCategoryLeafRespDTO implements Serializable { public class TagCategoryLeafRespDTO implements Serializable {
private static final long serialVersionUID = 5832815750190952238L; private static final long serialVersionUID = 5832815750190952238L;
@ApiModelProperty("类别ID")
private Integer id; private Integer id;
@ApiModelProperty("类别代码")
private String categoryCode; private String categoryCode;
@ApiModelProperty("类别名称")
private String categoryName; private String categoryName;
@ApiModelProperty("层级")
private Integer level; private Integer level;
/** 父类别名称 */ @ApiModelProperty("父类别名称")
private String parentName; private String parentName;
/** 完整路径,如"行为标签 > 需求挖掘能力" */ @ApiModelProperty("完整路径,如 行为标签/需求挖掘能力")
private String fullPath; private String fullPath;
} }
package cn.com.glsx.dj.strategy.dto; package cn.com.glsx.dj.strategy.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
...@@ -13,34 +15,44 @@ import java.util.List; ...@@ -13,34 +15,44 @@ import java.util.List;
* @since 2026/06/15 10:00 * @since 2026/06/15 10:00
*/ */
@Data @Data
@ApiModel("标签类别响应")
public class TagCategoryRespDTO implements Serializable { public class TagCategoryRespDTO implements Serializable {
private static final long serialVersionUID = -7038956294494131823L; private static final long serialVersionUID = -7038956294494131823L;
@ApiModelProperty("类别ID")
private Integer id; private Integer id;
@ApiModelProperty("类别代码")
private String categoryCode; private String categoryCode;
@ApiModelProperty("类别名称")
private String categoryName; private String categoryName;
@ApiModelProperty("父类别ID")
private Integer parentId; private Integer parentId;
@ApiModelProperty("层级")
private Integer level; private Integer level;
@ApiModelProperty("排序号")
private Integer sortOrder; private Integer sortOrder;
@ApiModelProperty("是否可编辑")
private Integer canEdit; private Integer canEdit;
@ApiModelProperty("是否可删除")
private Integer canDelete; private Integer canDelete;
@ApiModelProperty("是否可添加子级")
private Integer canAddChild; private Integer canAddChild;
private String remark; @ApiModelProperty("创建时间")
private Date createdDate; private Date createdDate;
@ApiModelProperty("更新时间")
private Date updatedDate; private Date updatedDate;
/** 子类别列表 */ @ApiModelProperty("子类别列表")
private List<TagCategoryRespDTO> children; private List<TagCategoryRespDTO> children;
} }
package cn.com.glsx.dj.strategy.dto; package cn.com.glsx.dj.strategy.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
...@@ -12,26 +14,35 @@ import java.util.List; ...@@ -12,26 +14,35 @@ import java.util.List;
* @since 2026/06/15 10:00 * @since 2026/06/15 10:00
*/ */
@Data @Data
@ApiModel("标签类别树形响应")
public class TagCategoryTreeRespDTO implements Serializable { public class TagCategoryTreeRespDTO implements Serializable {
private static final long serialVersionUID = -1535919739247302368L; private static final long serialVersionUID = -1535919739247302368L;
@ApiModelProperty("类别ID")
private Integer id; private Integer id;
@ApiModelProperty("类别代码")
private String categoryCode; private String categoryCode;
@ApiModelProperty("类别名称")
private String categoryName; private String categoryName;
@ApiModelProperty("父类别ID")
private Integer parentId; private Integer parentId;
@ApiModelProperty("层级")
private Integer level; private Integer level;
@ApiModelProperty("排序号")
private Integer sortOrder; private Integer sortOrder;
@ApiModelProperty("是否可编辑")
private Integer canEdit; private Integer canEdit;
@ApiModelProperty("是否可添加子级")
private Integer canAddChild; private Integer canAddChild;
/** 子类别列表 */ @ApiModelProperty("子类别列表")
private List<TagCategoryTreeRespDTO> children; private List<TagCategoryTreeRespDTO> children;
} }
...@@ -36,4 +36,9 @@ public class TagCategoryUpdateReqDTO implements Serializable { ...@@ -36,4 +36,9 @@ public class TagCategoryUpdateReqDTO implements Serializable {
* 备注 * 备注
*/ */
private String remark; private String remark;
/**
* 更新人
*/
private String updatedBy;
} }
package cn.com.glsx.dj.strategy.dto; package cn.com.glsx.dj.strategy.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
...@@ -11,27 +13,20 @@ import java.io.Serializable; ...@@ -11,27 +13,20 @@ import java.io.Serializable;
* @since 2026/06/15 10:00 * @since 2026/06/15 10:00
*/ */
@Data @Data
@ApiModel("新增维度时可选的标签分类响应")
public class TagDimensionCategoryOptionsRespDTO implements Serializable { public class TagDimensionCategoryOptionsRespDTO implements Serializable {
private static final long serialVersionUID = 4648560907741582796L; private static final long serialVersionUID = 4648560907741582796L;
/** @ApiModelProperty("分类ID")
* 分类ID
*/
private Integer value; private Integer value;
/** @ApiModelProperty("分类名称")
* 分类名称
*/
private String label; private String label;
/** @ApiModelProperty("完整路径")
* 完整路径
*/
private String fullPath; private String fullPath;
/** @ApiModelProperty("该分类下是否已存在维度")
* 该分类下是否已存在维度
*/
private Boolean hasDimension; private Boolean hasDimension;
} }
...@@ -38,4 +38,9 @@ public class TagDimensionCreateReqDTO implements Serializable { ...@@ -38,4 +38,9 @@ public class TagDimensionCreateReqDTO implements Serializable {
* 备注 * 备注
*/ */
private String remark; private String remark;
/**
* 创建人
*/
private String createdBy;
} }
package cn.com.glsx.dj.strategy.dto; package cn.com.glsx.dj.strategy.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
...@@ -11,27 +13,35 @@ import java.io.Serializable; ...@@ -11,27 +13,35 @@ import java.io.Serializable;
* @since 2026/06/15 10:00 * @since 2026/06/15 10:00
*/ */
@Data @Data
@ApiModel("标签维度详情响应")
public class TagDimensionDetailRespDTO implements Serializable { public class TagDimensionDetailRespDTO implements Serializable {
private static final long serialVersionUID = -8186494400128319019L; private static final long serialVersionUID = -8186494400128319019L;
@ApiModelProperty("维度ID")
private Integer id; private Integer id;
@ApiModelProperty("维度代码")
private String dimensionCode; private String dimensionCode;
@ApiModelProperty("维度名称")
private String dimensionName; private String dimensionName;
@ApiModelProperty("所属分类ID")
private Integer categoryId; private Integer categoryId;
/** 所属分类名称 */ @ApiModelProperty("所属分类名称")
private String categoryName; private String categoryName;
/** 分类完整路径 */ @ApiModelProperty("分类完整路径")
private String categoryFullPath; private String categoryFullPath;
@ApiModelProperty("排序号")
private Integer sortOrder; private Integer sortOrder;
@ApiModelProperty("是否可编辑")
private Integer canEdit; private Integer canEdit;
@ApiModelProperty("备注")
private String remark; private String remark;
} }
package cn.com.glsx.dj.strategy.dto; package cn.com.glsx.dj.strategy.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* 标签维度响应DTO * 标签维度响应DTO
...@@ -12,32 +15,47 @@ import java.util.Date; ...@@ -12,32 +15,47 @@ import java.util.Date;
* @since 2026/06/15 10:00 * @since 2026/06/15 10:00
*/ */
@Data @Data
@ApiModel("标签维度响应")
public class TagDimensionRespDTO implements Serializable { public class TagDimensionRespDTO implements Serializable {
private static final long serialVersionUID = -5649686805086989885L; private static final long serialVersionUID = -5649686805086989885L;
@ApiModelProperty("维度ID")
private Integer id; private Integer id;
@ApiModelProperty("维度代码")
private String dimensionCode; private String dimensionCode;
@ApiModelProperty("维度名称")
private String dimensionName; private String dimensionName;
@ApiModelProperty("所属分类ID")
private Integer categoryId; private Integer categoryId;
/** 所属分类名称 */ @ApiModelProperty("所属分类名称")
private String categoryName; private String categoryName;
/** 分类完整路径 */ @ApiModelProperty("分类完整路径")
private String categoryFullPath; private String categoryFullPath;
/** 枚举数量 */ @ApiModelProperty("枚举数量")
private Integer enumCount; private Integer enumCount;
@ApiModelProperty("排序号")
private Integer sortOrder; private Integer sortOrder;
@ApiModelProperty("是否可编辑")
private Integer canEdit; private Integer canEdit;
@ApiModelProperty("是否可新增枚举(综合DB配置+业务规则)")
private Integer canAddEnum;
@ApiModelProperty("维度下的枚举值列表")
private List<TagEnumRespDTO> enums;
@ApiModelProperty("创建时间")
private Date createdDate; private Date createdDate;
@ApiModelProperty("更新时间")
private Date updatedDate; private Date updatedDate;
} }
...@@ -36,4 +36,9 @@ public class TagDimensionUpdateReqDTO implements Serializable { ...@@ -36,4 +36,9 @@ public class TagDimensionUpdateReqDTO implements Serializable {
* 备注 * 备注
*/ */
private String remark; private String remark;
/**
* 更新人
*/
private String updatedBy;
} }
...@@ -44,4 +44,9 @@ public class TagEnumCreateReqDTO implements Serializable { ...@@ -44,4 +44,9 @@ public class TagEnumCreateReqDTO implements Serializable {
* 备注 * 备注
*/ */
private String remark; private String remark;
/**
* 创建人
*/
private String createdBy;
} }
package cn.com.glsx.dj.strategy.dto; package cn.com.glsx.dj.strategy.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
...@@ -12,31 +14,41 @@ import java.util.Date; ...@@ -12,31 +14,41 @@ import java.util.Date;
* @since 2026/06/15 10:00 * @since 2026/06/15 10:00
*/ */
@Data @Data
@ApiModel("标签枚举值响应")
public class TagEnumRespDTO implements Serializable { public class TagEnumRespDTO implements Serializable {
private static final long serialVersionUID = -6921209396593157562L; private static final long serialVersionUID = -6921209396593157562L;
@ApiModelProperty("枚举ID")
private Integer id; private Integer id;
@ApiModelProperty("枚举代码")
private String enumCode; private String enumCode;
@ApiModelProperty("枚举名称")
private String enumName; private String enumName;
@ApiModelProperty("所属维度ID")
private Integer dimensionId; private Integer dimensionId;
@ApiModelProperty("命中客户分数")
private Integer score; private Integer score;
@ApiModelProperty("排序号")
private Integer sortOrder; private Integer sortOrder;
/** 枚举名称是否可编辑 */ @ApiModelProperty("枚举名称是否可编辑")
private Integer enumNameCanEdit; private Integer enumNameCanEdit;
/** 分数是否可修改 */ @ApiModelProperty("分数是否可修改")
private Integer scoreCanEdit; private Integer scoreCanEdit;
@ApiModelProperty("备注")
private String remark; private String remark;
@ApiModelProperty("创建时间")
private Date createdDate; private Date createdDate;
@ApiModelProperty("更新时间")
private Date updatedDate; private Date updatedDate;
} }
...@@ -41,4 +41,9 @@ public class TagEnumUpdateReqDTO implements Serializable { ...@@ -41,4 +41,9 @@ public class TagEnumUpdateReqDTO implements Serializable {
* 备注 * 备注
*/ */
private String remark; private String remark;
/**
* 更新人
*/
private String updatedBy;
} }
...@@ -42,7 +42,7 @@ public enum TagErrorEnum implements ErrorEnum { ...@@ -42,7 +42,7 @@ public enum TagErrorEnum implements ErrorEnum {
DIMENSION_CANNOT_EDIT("11021002", "标签维度不可编辑"), DIMENSION_CANNOT_EDIT("11021002", "标签维度不可编辑"),
/** 分类不可新增维度 */ /** 分类不可新增维度 */
CATEGORY_CANNOT_ADD_DIMENSION("11021003", "该分类下不可新增维度,仅支持初始化数据"), CATEGORY_CANNOT_ADD_DIMENSION("11021003", "该分类下不可新增维度"),
/** 分类不是叶子节点 */ /** 分类不是叶子节点 */
CATEGORY_NOT_LEAF("11021004", "分类不是叶子节点,不允许创建维度"), CATEGORY_NOT_LEAF("11021004", "分类不是叶子节点,不允许创建维度"),
......
...@@ -32,6 +32,11 @@ public interface TagCategoryMapper extends OreMapper<TagCategory> { ...@@ -32,6 +32,11 @@ public interface TagCategoryMapper extends OreMapper<TagCategory> {
List<TagCategory> selectAddChildEligibleCategories(@Param("maxLevel") Integer maxLevel); List<TagCategory> selectAddChildEligibleCategories(@Param("maxLevel") Integer maxLevel);
/** /**
* 查询叶子节点分类(无子分类的类别,用于维度挂载选项)
*/
List<TagCategory> selectLeafCategories();
/**
* 统计分类下是否存在标签维度 * 统计分类下是否存在标签维度
*/ */
int countDimensionsByCategoryId(Integer categoryId); int countDimensionsByCategoryId(Integer categoryId);
......
...@@ -21,4 +21,9 @@ public interface TagEnumMapper extends OreMapper<TagEnum> { ...@@ -21,4 +21,9 @@ public interface TagEnumMapper extends OreMapper<TagEnum> {
* 分页查询枚举值列表 * 分页查询枚举值列表
*/ */
List<TagEnum> pageEnum(@Param("query") TagEnumPageReqDTO query); List<TagEnum> pageEnum(@Param("query") TagEnumPageReqDTO query);
/**
* 根据维度ID列表批量查询枚举值
*/
List<TagEnum> selectByDimensionIds(@Param("dimensionIds") List<Integer> dimensionIds);
} }
...@@ -8,6 +8,7 @@ import cn.com.glsx.dj.strategy.model.TagCategory; ...@@ -8,6 +8,7 @@ import cn.com.glsx.dj.strategy.model.TagCategory;
import cn.com.glsx.dj.strategy.util.validation.BeanValidationUtils; import cn.com.glsx.dj.strategy.util.validation.BeanValidationUtils;
import cn.com.glsx.framework.core.exception.RpcServiceException; import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.scrm.equity.enums.DeleteFlagEnum; import cn.com.glsx.scrm.equity.enums.DeleteFlagEnum;
import cn.hutool.core.lang.UUID;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -49,22 +50,11 @@ public class TagCategoryService { ...@@ -49,22 +50,11 @@ public class TagCategoryService {
.filter(c -> DeleteFlagEnum.N.getValue().equals(c.getDeletedFlag())) .filter(c -> DeleteFlagEnum.N.getValue().equals(c.getDeletedFlag()))
.orElseThrow(() -> new RpcServiceException(TagErrorEnum.PARENT_CATEGORY_NOT_EXISTS)); .orElseThrow(() -> new RpcServiceException(TagErrorEnum.PARENT_CATEGORY_NOT_EXISTS));
// 校验父类别是否可添加子级 // 综合校验父类别是否实际可新增子级
if (parentCategory.getCanAddChild() == TagConstants.CAN_FLAG_NO) { if (!canActuallyAddChild(parentCategory)) {
throw new RpcServiceException(TagErrorEnum.CATEGORY_CANNOT_ADD_CHILD); throw new RpcServiceException(TagErrorEnum.CATEGORY_CANNOT_ADD_CHILD);
} }
// 校验父类别下是否存在标签维度(有维度则不能再添加子分类)
int dimensionCount = tagCategoryMapper.countDimensionsByCategoryId(request.getParentId());
if (dimensionCount > TagConstants.CAN_FLAG_NO) {
throw new RpcServiceException(TagErrorEnum.CATEGORY_CANNOT_ADD_CHILD);
}
// 校验层级是否超过最大配置
if (parentCategory.getLevel() >= maxCategoryLevel) {
throw new RpcServiceException(TagErrorEnum.CATEGORY_LEVEL_MAX);
}
level = parentCategory.getLevel() + 1; level = parentCategory.getLevel() + 1;
entity.setParentId(request.getParentId()); entity.setParentId(request.getParentId());
} else { } else {
...@@ -78,10 +68,14 @@ public class TagCategoryService { ...@@ -78,10 +68,14 @@ public class TagCategoryService {
entity.setDeletedFlag(DeleteFlagEnum.N.getValue()); entity.setDeletedFlag(DeleteFlagEnum.N.getValue());
entity.setCanEdit(TagConstants.CAN_FLAG_YES); entity.setCanEdit(TagConstants.CAN_FLAG_YES);
entity.setCanDelete(TagConstants.CAN_FLAG_NO); entity.setCanDelete(TagConstants.CAN_FLAG_NO);
entity.setCanAddChild(level < maxCategoryLevel ? TagConstants.CAN_FLAG_YES : TagConstants.CAN_FLAG_NO); entity.setCanAddChild(TagConstants.CAN_FLAG_YES);
entity.setCanAddDimension(TagConstants.CAN_FLAG_YES); entity.setCanAddDimension(TagConstants.CAN_FLAG_YES);
entity.setCreatedBy(request.getCreatedBy());
entity.setCreatedDate(new Date()); entity.setCreatedDate(new Date());
entity.setUpdatedBy(request.getCreatedBy());
entity.setUpdatedDate(new Date()); entity.setUpdatedDate(new Date());
// 先用UUID占位,满足NOT NULL和唯一约束,后续回写真实code
entity.setCategoryCode("TEMP_" + UUID.randomUUID().toString(true));
tagCategoryMapper.insert(entity); tagCategoryMapper.insert(entity);
...@@ -115,6 +109,7 @@ public class TagCategoryService { ...@@ -115,6 +109,7 @@ public class TagCategoryService {
if (request.getRemark() != null) { if (request.getRemark() != null) {
category.setRemark(request.getRemark()); category.setRemark(request.getRemark());
} }
category.setUpdatedBy(request.getUpdatedBy());
category.setUpdatedDate(new Date()); category.setUpdatedDate(new Date());
tagCategoryMapper.updateByPrimaryKeySelective(category); tagCategoryMapper.updateByPrimaryKeySelective(category);
} }
...@@ -147,7 +142,7 @@ public class TagCategoryService { ...@@ -147,7 +142,7 @@ public class TagCategoryService {
dto.setLevel(category.getLevel()); dto.setLevel(category.getLevel());
dto.setSortOrder(category.getSortOrder()); dto.setSortOrder(category.getSortOrder());
dto.setCanEdit(category.getCanEdit()); dto.setCanEdit(category.getCanEdit());
dto.setCanAddChild(category.getCanAddChild()); dto.setCanAddChild(canActuallyAddChild(category) ? TagConstants.CAN_FLAG_YES : TagConstants.CAN_FLAG_NO);
List<TagCategory> children = parentIdMap.getOrDefault(category.getId(), Collections.emptyList()); List<TagCategory> children = parentIdMap.getOrDefault(category.getId(), Collections.emptyList());
if (!children.isEmpty()) { if (!children.isEmpty()) {
...@@ -168,21 +163,9 @@ public class TagCategoryService { ...@@ -168,21 +163,9 @@ public class TagCategoryService {
return Collections.emptyList(); return Collections.emptyList();
} }
// 收集所有父ID用于查询父类别名称 // 一次性加载全部分类用于 buildFullPath 和父名称查找
Set<Integer> parentIds = categories.stream() Map<Integer, TagCategory> allCategoryMap = tagCategoryMapper.selectAllCategories().stream()
.map(c -> c.getParentId() == null ? TagConstants.CAN_FLAG_NO : c.getParentId()) .collect(Collectors.toMap(TagCategory::getId, c -> c));
.filter(id -> id > TagConstants.CAN_FLAG_NO)
.collect(Collectors.toSet());
Map<Integer, String> parentNameMap = new HashMap<>();
if (!parentIds.isEmpty()) {
for (Integer parentId : parentIds) {
TagCategory parent = tagCategoryMapper.selectByPrimaryKey(parentId);
if (parent != null) {
parentNameMap.put(parentId, parent.getCategoryName());
}
}
}
return categories.stream().map(c -> { return categories.stream().map(c -> {
TagCategoryLeafRespDTO dto = new TagCategoryLeafRespDTO(); TagCategoryLeafRespDTO dto = new TagCategoryLeafRespDTO();
...@@ -190,13 +173,50 @@ public class TagCategoryService { ...@@ -190,13 +173,50 @@ public class TagCategoryService {
dto.setCategoryCode(c.getCategoryCode()); dto.setCategoryCode(c.getCategoryCode());
dto.setCategoryName(c.getCategoryName()); dto.setCategoryName(c.getCategoryName());
dto.setLevel(c.getLevel()); dto.setLevel(c.getLevel());
String parentName = parentNameMap.get(c.getParentId()); TagCategory parent = allCategoryMap.get(c.getParentId());
dto.setParentName(parentName); dto.setParentName(parent != null ? parent.getCategoryName() : null);
dto.setFullPath(parentName != null ? parentName + " > " + c.getCategoryName() : c.getCategoryName()); dto.setFullPath(buildFullPath(c, allCategoryMap));
return dto; return dto;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }
/**
* 判断分类是否实际可新增子级(综合DB配置+层级+维度)
*/
boolean canActuallyAddChild(TagCategory category) {
if (category.getCanAddChild() == TagConstants.CAN_FLAG_NO) {
return false;
}
if (category.getLevel() >= maxCategoryLevel) {
return false;
}
return tagCategoryMapper.countDimensionsByCategoryId(category.getId()) == TagConstants.CAN_FLAG_NO;
}
/**
* 构建分类完整路径,一次性加载所有分类后在内存中递归,如 "行为标签/需求挖掘能力"
*/
String buildFullPath(TagCategory category) {
List<TagCategory> allCategories = tagCategoryMapper.selectAllCategories();
Map<Integer, TagCategory> idMap = allCategories.stream()
.collect(Collectors.toMap(TagCategory::getId, c -> c));
return buildFullPath(category, idMap);
}
/**
* 使用预加载的分类Map构建完整路径(避免重复DB查询)
*/
String buildFullPath(TagCategory category, Map<Integer, TagCategory> idMap) {
if (category.getParentId() == null || category.getParentId() == TagConstants.CAN_FLAG_NO) {
return category.getCategoryName();
}
TagCategory parent = idMap.get(category.getParentId());
if (parent == null) {
return category.getCategoryName();
}
return buildFullPath(parent, idMap) + "/" + category.getCategoryName();
}
private TagCategoryRespDTO convertToRespDTO(TagCategory entity) { private TagCategoryRespDTO convertToRespDTO(TagCategory entity) {
TagCategoryRespDTO dto = new TagCategoryRespDTO(); TagCategoryRespDTO dto = new TagCategoryRespDTO();
dto.setId(entity.getId()); dto.setId(entity.getId());
......
...@@ -5,11 +5,14 @@ import cn.com.glsx.dj.strategy.enums.TagConstants; ...@@ -5,11 +5,14 @@ import cn.com.glsx.dj.strategy.enums.TagConstants;
import cn.com.glsx.dj.strategy.enums.TagErrorEnum; import cn.com.glsx.dj.strategy.enums.TagErrorEnum;
import cn.com.glsx.dj.strategy.mapper.TagCategoryMapper; import cn.com.glsx.dj.strategy.mapper.TagCategoryMapper;
import cn.com.glsx.dj.strategy.mapper.TagDimensionMapper; import cn.com.glsx.dj.strategy.mapper.TagDimensionMapper;
import cn.com.glsx.dj.strategy.mapper.TagEnumMapper;
import cn.com.glsx.dj.strategy.model.TagCategory; import cn.com.glsx.dj.strategy.model.TagCategory;
import cn.com.glsx.dj.strategy.model.TagDimension; import cn.com.glsx.dj.strategy.model.TagDimension;
import cn.com.glsx.dj.strategy.model.TagEnum;
import cn.com.glsx.dj.strategy.util.validation.BeanValidationUtils; import cn.com.glsx.dj.strategy.util.validation.BeanValidationUtils;
import cn.com.glsx.framework.core.exception.RpcServiceException; import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.scrm.equity.enums.DeleteFlagEnum; import cn.com.glsx.scrm.equity.enums.DeleteFlagEnum;
import cn.hutool.core.lang.UUID;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
...@@ -33,6 +36,8 @@ public class TagDimensionService { ...@@ -33,6 +36,8 @@ public class TagDimensionService {
private final TagDimensionMapper tagDimensionMapper; private final TagDimensionMapper tagDimensionMapper;
private final TagCategoryMapper tagCategoryMapper; private final TagCategoryMapper tagCategoryMapper;
private final TagCategoryService tagCategoryService;
private final TagEnumMapper tagEnumMapper;
/** /**
* 创建标签维度 * 创建标签维度
...@@ -42,21 +47,16 @@ public class TagDimensionService { ...@@ -42,21 +47,16 @@ public class TagDimensionService {
BeanValidationUtils.validateAndThrow(request); BeanValidationUtils.validateAndThrow(request);
// 校验分类是否存在 // 校验分类是否存在
TagCategory category = Optional.ofNullable(tagCategoryMapper.selectByPrimaryKey(request.getCategoryId())) Optional.ofNullable(tagCategoryMapper.selectByPrimaryKey(request.getCategoryId()))
.filter(c -> DeleteFlagEnum.N.getValue().equals(c.getDeletedFlag())) .filter(c -> DeleteFlagEnum.N.getValue().equals(c.getDeletedFlag()))
.orElseThrow(() -> new RpcServiceException(TagErrorEnum.CATEGORY_NOT_EXISTS)); .orElseThrow(() -> new RpcServiceException(TagErrorEnum.CATEGORY_NOT_EXISTS));
// 校验分类是否允许新增维度(仅初始化数据的分类不可通过API新增) // 校验分类是否可作为维度挂载目标(与 queryCategoryOptions 口径一致:叶子节点 + can_add_dimension=1)
if (category.getCanAddDimension() == TagConstants.CAN_FLAG_NO) { List<TagCategory> leafCategories = tagCategoryMapper.selectLeafCategories();
if (leafCategories.stream().noneMatch(c -> c.getId().equals(request.getCategoryId()))) {
throw new RpcServiceException(TagErrorEnum.CATEGORY_CANNOT_ADD_DIMENSION); throw new RpcServiceException(TagErrorEnum.CATEGORY_CANNOT_ADD_DIMENSION);
} }
// 校验分类是否为叶子节点
List<TagCategory> children = tagCategoryMapper.selectByParentId(request.getCategoryId());
if (!children.isEmpty()) {
throw new RpcServiceException(TagErrorEnum.CATEGORY_NOT_LEAF);
}
TagDimension entity = new TagDimension(); TagDimension entity = new TagDimension();
entity.setDimensionName(request.getDimensionName()); entity.setDimensionName(request.getDimensionName());
entity.setCategoryId(request.getCategoryId()); entity.setCategoryId(request.getCategoryId());
...@@ -65,8 +65,12 @@ public class TagDimensionService { ...@@ -65,8 +65,12 @@ public class TagDimensionService {
entity.setCanEdit(TagConstants.CAN_FLAG_YES); entity.setCanEdit(TagConstants.CAN_FLAG_YES);
entity.setCanAddEnum(TagConstants.CAN_FLAG_YES); entity.setCanAddEnum(TagConstants.CAN_FLAG_YES);
entity.setDeletedFlag(DeleteFlagEnum.N.getValue()); entity.setDeletedFlag(DeleteFlagEnum.N.getValue());
entity.setCreatedBy(request.getCreatedBy());
entity.setCreatedDate(new Date()); entity.setCreatedDate(new Date());
entity.setUpdatedBy(request.getCreatedBy());
entity.setUpdatedDate(new Date()); entity.setUpdatedDate(new Date());
// 先用UUID占位,满足NOT NULL和唯一约束,后续回写真实code
entity.setDimensionCode("TEMP_" + UUID.randomUUID().toString(true));
tagDimensionMapper.insert(entity); tagDimensionMapper.insert(entity);
...@@ -89,17 +93,35 @@ public class TagDimensionService { ...@@ -89,17 +93,35 @@ public class TagDimensionService {
List<TagDimension> list = tagDimensionMapper.queryDimensionList(request); List<TagDimension> list = tagDimensionMapper.queryDimensionList(request);
PageInfo<TagDimension> pageInfo = new PageInfo<>(list); PageInfo<TagDimension> pageInfo = new PageInfo<>(list);
// 一次性加载全部分类,用于 buildFullPath 和 categoryName 查找
Map<Integer, TagCategory> allCategoryMap = tagCategoryMapper.selectAllCategories().stream()
.collect(Collectors.toMap(TagCategory::getId, c -> c));
// 批量查询所有维度的枚举值,按 dimensionId 分组
List<Integer> dimensionIds = list.stream().map(TagDimension::getId).collect(Collectors.toList());
Map<Integer, List<TagEnum>> enumMap = Collections.emptyMap();
if (!dimensionIds.isEmpty()) {
enumMap = tagEnumMapper.selectByDimensionIds(dimensionIds).stream()
.collect(Collectors.groupingBy(TagEnum::getDimensionId));
}
final Map<Integer, List<TagEnum>> finalEnumMap = enumMap;
List<TagDimensionRespDTO> dtoList = list.stream().map(dim -> { List<TagDimensionRespDTO> dtoList = list.stream().map(dim -> {
TagDimensionRespDTO dto = new TagDimensionRespDTO(); TagDimensionRespDTO dto = new TagDimensionRespDTO();
dto.setId(dim.getId()); dto.setId(dim.getId());
dto.setDimensionCode(dim.getDimensionCode()); dto.setDimensionCode(dim.getDimensionCode());
dto.setDimensionName(dim.getDimensionName()); dto.setDimensionName(dim.getDimensionName());
dto.setCategoryId(dim.getCategoryId()); dto.setCategoryId(dim.getCategoryId());
dto.setCategoryName(dim.getCategoryName()); TagCategory cat = allCategoryMap.get(dim.getCategoryId());
dto.setCategoryFullPath(dim.getCategoryFullPath()); dto.setCategoryName(cat != null ? cat.getCategoryName() : null);
dto.setCategoryFullPath(cat != null ? tagCategoryService.buildFullPath(cat, allCategoryMap) : null);
dto.setEnumCount(dim.getEnumCount()); dto.setEnumCount(dim.getEnumCount());
dto.setSortOrder(dim.getSortOrder()); dto.setSortOrder(dim.getSortOrder());
dto.setCanEdit(dim.getCanEdit()); dto.setCanEdit(dim.getCanEdit());
dto.setCanAddEnum(dim.getCanAddEnum());
// 组装枚举值列表
List<TagEnum> dimEnums = finalEnumMap.getOrDefault(dim.getId(), Collections.emptyList());
dto.setEnums(dimEnums.stream().map(this::convertToEnumRespDTO).collect(Collectors.toList()));
dto.setCreatedDate(dim.getCreatedDate()); dto.setCreatedDate(dim.getCreatedDate());
dto.setUpdatedDate(dim.getUpdatedDate()); dto.setUpdatedDate(dim.getUpdatedDate());
return dto; return dto;
...@@ -121,22 +143,15 @@ public class TagDimensionService { ...@@ -121,22 +143,15 @@ public class TagDimensionService {
public List<TagDimensionCategoryOptionsRespDTO> queryCategoryOptions() { public List<TagDimensionCategoryOptionsRespDTO> queryCategoryOptions() {
List<TagCategory> leafCategories = tagCategoryMapper.selectLeafCategories(); List<TagCategory> leafCategories = tagCategoryMapper.selectLeafCategories();
// 一次性加载全部分类用于 buildFullPath
Map<Integer, TagCategory> allCategoryMap = tagCategoryMapper.selectAllCategories().stream()
.collect(Collectors.toMap(TagCategory::getId, c -> c));
return leafCategories.stream().map(c -> { return leafCategories.stream().map(c -> {
TagDimensionCategoryOptionsRespDTO dto = new TagDimensionCategoryOptionsRespDTO(); TagDimensionCategoryOptionsRespDTO dto = new TagDimensionCategoryOptionsRespDTO();
dto.setValue(c.getId()); dto.setValue(c.getId());
dto.setLabel(c.getCategoryName()); dto.setLabel(c.getCategoryName());
dto.setFullPath(tagCategoryService.buildFullPath(c, allCategoryMap));
// 构建完整路径
if (c.getParentId() != null && c.getParentId() != TagConstants.CAN_FLAG_NO) {
TagCategory parent = tagCategoryMapper.selectByPrimaryKey(c.getParentId());
if (parent != null) {
dto.setFullPath(parent.getCategoryName() + " > " + c.getCategoryName());
} else {
dto.setFullPath(c.getCategoryName());
}
} else {
dto.setFullPath(c.getCategoryName());
}
// 判断该分类下是否已存在维度 // 判断该分类下是否已存在维度
int dimensionCount = tagCategoryMapper.countDimensionsByCategoryId(c.getId()); int dimensionCount = tagCategoryMapper.countDimensionsByCategoryId(c.getId());
...@@ -167,16 +182,7 @@ public class TagDimensionService { ...@@ -167,16 +182,7 @@ public class TagDimensionService {
TagCategory category = tagCategoryMapper.selectByPrimaryKey(dimension.getCategoryId()); TagCategory category = tagCategoryMapper.selectByPrimaryKey(dimension.getCategoryId());
if (category != null) { if (category != null) {
dto.setCategoryName(category.getCategoryName()); dto.setCategoryName(category.getCategoryName());
if (category.getParentId() != null && category.getParentId() != TagConstants.CAN_FLAG_NO) { dto.setCategoryFullPath(tagCategoryService.buildFullPath(category));
TagCategory parent = tagCategoryMapper.selectByPrimaryKey(category.getParentId());
if (parent != null) {
dto.setCategoryFullPath(parent.getCategoryName() + " > " + category.getCategoryName());
} else {
dto.setCategoryFullPath(category.getCategoryName());
}
} else {
dto.setCategoryFullPath(category.getCategoryName());
}
} }
return dto; return dto;
...@@ -205,7 +211,27 @@ public class TagDimensionService { ...@@ -205,7 +211,27 @@ public class TagDimensionService {
if (request.getRemark() != null) { if (request.getRemark() != null) {
dimension.setRemark(request.getRemark()); dimension.setRemark(request.getRemark());
} }
dimension.setUpdatedBy(request.getUpdatedBy());
dimension.setUpdatedDate(new Date()); dimension.setUpdatedDate(new Date());
tagDimensionMapper.updateByPrimaryKeySelective(dimension); tagDimensionMapper.updateByPrimaryKeySelective(dimension);
} }
/**
* TagEnum 实体转 TagEnumRespDTO
*/
private TagEnumRespDTO convertToEnumRespDTO(TagEnum entity) {
TagEnumRespDTO dto = new TagEnumRespDTO();
dto.setId(entity.getId());
dto.setEnumCode(entity.getEnumCode());
dto.setEnumName(entity.getEnumName());
dto.setDimensionId(entity.getDimensionId());
dto.setScore(entity.getScore());
dto.setSortOrder(entity.getSortOrder());
dto.setEnumNameCanEdit(entity.getEnumNameCanEdit());
dto.setScoreCanEdit(entity.getScoreCanEdit());
dto.setRemark(entity.getRemark());
dto.setCreatedDate(entity.getCreatedDate());
dto.setUpdatedDate(entity.getUpdatedDate());
return dto;
}
} }
...@@ -10,6 +10,7 @@ import cn.com.glsx.dj.strategy.model.TagEnum; ...@@ -10,6 +10,7 @@ import cn.com.glsx.dj.strategy.model.TagEnum;
import cn.com.glsx.dj.strategy.util.validation.BeanValidationUtils; import cn.com.glsx.dj.strategy.util.validation.BeanValidationUtils;
import cn.com.glsx.framework.core.exception.RpcServiceException; import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.scrm.equity.enums.DeleteFlagEnum; import cn.com.glsx.scrm.equity.enums.DeleteFlagEnum;
import cn.hutool.core.lang.UUID;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
...@@ -98,8 +99,12 @@ public class TagEnumService { ...@@ -98,8 +99,12 @@ public class TagEnumService {
entity.setEnumNameCanEdit(TagConstants.CAN_FLAG_YES); entity.setEnumNameCanEdit(TagConstants.CAN_FLAG_YES);
entity.setScoreCanEdit(TagConstants.CAN_FLAG_YES); entity.setScoreCanEdit(TagConstants.CAN_FLAG_YES);
entity.setDeletedFlag(DeleteFlagEnum.N.getValue()); entity.setDeletedFlag(DeleteFlagEnum.N.getValue());
entity.setCreatedBy(request.getCreatedBy());
entity.setCreatedDate(new Date()); entity.setCreatedDate(new Date());
entity.setUpdatedBy(request.getCreatedBy());
entity.setUpdatedDate(new Date()); entity.setUpdatedDate(new Date());
// 先用UUID占位,满足NOT NULL和唯一约束,后续回写真实code
entity.setEnumCode("TEMP_" + UUID.randomUUID().toString(true));
tagEnumMapper.insert(entity); tagEnumMapper.insert(entity);
...@@ -140,6 +145,7 @@ public class TagEnumService { ...@@ -140,6 +145,7 @@ public class TagEnumService {
if (request.getRemark() != null) { if (request.getRemark() != null) {
tagEnum.setRemark(request.getRemark()); tagEnum.setRemark(request.getRemark());
} }
tagEnum.setUpdatedBy(request.getUpdatedBy());
tagEnum.setUpdatedDate(new Date()); tagEnum.setUpdatedDate(new Date());
tagEnumMapper.updateByPrimaryKeySelective(tagEnum); tagEnumMapper.updateByPrimaryKeySelective(tagEnum);
} }
......
...@@ -59,6 +59,19 @@ ...@@ -59,6 +59,19 @@
ORDER BY c.level ASC, c.sort_order ASC ORDER BY c.level ASC, c.sort_order ASC
</select> </select>
<!-- 查询叶子节点分类(无子分类且可挂载维度,用于维度挂载选项) -->
<select id="selectLeafCategories" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM dj_car_life_tag_category
WHERE deleted_flag = 'N'
AND can_add_dimension = 1
AND NOT EXISTS (
SELECT 1 FROM dj_car_life_tag_category sub
WHERE sub.parent_id = id AND sub.deleted_flag = 'N'
)
ORDER BY level ASC, sort_order ASC
</select>
<!-- 统计分类下是否存在标签维度 --> <!-- 统计分类下是否存在标签维度 -->
<select id="countDimensionsByCategoryId" resultType="int"> <select id="countDimensionsByCategoryId" resultType="int">
SELECT COUNT(1) SELECT COUNT(1)
......
...@@ -33,15 +33,10 @@ ...@@ -33,15 +33,10 @@
SELECT d.id, d.dimension_code, d.dimension_name, d.category_id, d.sort_order, SELECT d.id, d.dimension_code, d.dimension_name, d.category_id, d.sort_order,
d.can_edit, d.can_add_enum, d.deleted_flag, d.created_by, d.created_date, d.updated_by, d.updated_date, d.can_edit, d.can_add_enum, d.deleted_flag, d.created_by, d.created_date, d.updated_by, d.updated_date,
c.category_name, c.category_name,
CASE
WHEN c.parent_id = 0 OR c.parent_id IS NULL THEN c.category_name
ELSE CONCAT(pc.category_name, ' > ', c.category_name)
END AS category_full_path,
(SELECT COUNT(1) FROM dj_car_life_tag_enum e (SELECT COUNT(1) FROM dj_car_life_tag_enum e
WHERE e.dimension_id = d.id AND e.deleted_flag = 'N') AS enum_count WHERE e.dimension_id = d.id AND e.deleted_flag = 'N') AS enum_count
FROM dj_car_life_tag_dimension d FROM dj_car_life_tag_dimension d
LEFT JOIN dj_car_life_tag_category c ON d.category_id = c.id LEFT JOIN dj_car_life_tag_category c ON d.category_id = c.id
LEFT JOIN dj_car_life_tag_category pc ON c.parent_id = pc.id
WHERE d.deleted_flag = 'N' WHERE d.deleted_flag = 'N'
<if test="query.categoryId != null"> <if test="query.categoryId != null">
AND d.category_id = #{query.categoryId} AND d.category_id = #{query.categoryId}
......
...@@ -37,4 +37,18 @@ ...@@ -37,4 +37,18 @@
ORDER BY sort_order ASC ORDER BY sort_order ASC
</select> </select>
<!-- 根据维度ID列表批量查询枚举值 -->
<select id="selectByDimensionIds" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>
FROM dj_car_life_tag_enum
WHERE deleted_flag = 'N'
<if test="dimensionIds != null and dimensionIds.size() > 0">
AND dimension_id IN
<foreach collection="dimensionIds" item="dimensionId" open="(" separator="," close=")">
#{dimensionId}
</foreach>
</if>
ORDER BY dimension_id ASC, sort_order ASC
</select>
</mapper> </mapper>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!