树形ServiceImpl.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package ${package}.${moduleName}.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.lang.tree.Tree;
  4. import cn.hutool.core.lang.tree.TreeNode;
  5. import cn.hutool.core.lang.tree.TreeUtil;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  8. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  9. import ${package}.${moduleName}.entity.${ClassName}Entity;
  10. import ${package}.${moduleName}.mapper.${ClassName}Mapper;
  11. import ${package}.${moduleName}.service.${ClassName}Service;
  12. import jakarta.validation.constraints.NotNull;
  13. import lombok.RequiredArgsConstructor;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.function.Function;
  21. import java.util.stream.Collectors;
  22. /**
  23. * ${tableComment} Service实现类
  24. *
  25. * @author ${author}
  26. * @date ${datetime}
  27. */
  28. @Service
  29. @RequiredArgsConstructor
  30. public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, ${ClassName}Entity> implements ${ClassName}Service {
  31. /**
  32. * 构建树形结构数据
  33. * @param wrapper 查询条件
  34. * @return 树形结构数据
  35. */
  36. @Override
  37. public List<Tree<${pk.attrType}>> buildTree(LambdaQueryWrapper<${ClassName}Entity> wrapper) {
  38. // 查询所有数据
  39. List<${ClassName}Entity> allList = list(wrapper);
  40. if (CollUtil.isEmpty(allList)) {
  41. return new ArrayList<>();
  42. }
  43. // 转换为TreeNode
  44. List<TreeNode<${pk.attrType}>> collect = allList.stream().map(getNodeFunction()).toList();
  45. // 使用TreeUtil构建树形结构,根节点ID为0
  46. #if($pk.attrType == 'Long')
  47. return TreeUtil.build(collect, 0L);
  48. #else
  49. return TreeUtil.build(collect, 0);
  50. #end
  51. }
  52. /**
  53. * 获取TreeNode转换函数
  54. * @return TreeNode转换函数
  55. */
  56. @NotNull
  57. private Function<${ClassName}Entity, TreeNode<${pk.attrType}>> getNodeFunction() {
  58. return entity -> {
  59. TreeNode<${pk.attrType}> node = new TreeNode<>();
  60. node.setId(entity.$str.getProperty($pk.attrName)());
  61. node.setName(entity.$str.getProperty($nameField)());
  62. node.setParentId(entity.$str.getProperty($parentField)() != null ? entity.$str.getProperty($parentField)() : 0L);
  63. // 扩展属性
  64. Map<String, Object> extra = new HashMap<>();
  65. #foreach($field in $fieldList)
  66. extra.put(${ClassName}Entity.Fields.${field.attrName}, entity.$str.getProperty($field.attrName)());
  67. #end
  68. node.setExtra(extra);
  69. return node;
  70. };
  71. }
  72. /**
  73. * 递归删除节点及其子节点
  74. * @param ids 要删除的节点ID列表
  75. * @return 删除结果
  76. */
  77. @Override
  78. @Transactional(rollbackFor = Exception.class)
  79. public boolean removeBatchByIds(List<${pk.attrType}> ids) {
  80. if (CollUtil.isEmpty(ids)) {
  81. return true;
  82. }
  83. // 获取所有要删除的ID(包括子节点)
  84. List<${pk.attrType}> allDeleteIds = new ArrayList<>(ids);
  85. for (${pk.attrType} id : ids) {
  86. List<${pk.attrType}> childIds = getChildIdsRecursive(id);
  87. allDeleteIds.addAll(childIds);
  88. }
  89. // 去重
  90. allDeleteIds = allDeleteIds.stream().distinct().collect(Collectors.toList());
  91. // 批量删除
  92. return super.removeBatchByIds(allDeleteIds);
  93. }
  94. /**
  95. * 递归获取所有子节点ID
  96. * @param parentId 父节点ID
  97. * @return 子节点ID列表
  98. */
  99. private List<${pk.attrType}> getChildIdsRecursive(${pk.attrType} parentId) {
  100. List<${pk.attrType}> childIds = new ArrayList<>();
  101. LambdaQueryWrapper<${ClassName}Entity> wrapper = Wrappers.lambdaQuery();
  102. wrapper.eq(${ClassName}Entity::$str.getProperty($parentField), parentId);
  103. wrapper.select(${ClassName}Entity::$str.getProperty($pk.attrName));
  104. List<${ClassName}Entity> children = list(wrapper);
  105. for (${ClassName}Entity child : children) {
  106. ${pk.attrType} childId = child.$str.getProperty($pk.attrName)();
  107. childIds.add(childId);
  108. // 递归获取子节点的子节点
  109. childIds.addAll(getChildIdsRecursive(childId));
  110. }
  111. return childIds;
  112. }
  113. }