diff --git a/laws-base/laws-base-core/src/main/java/com/jero/common/constant/FillRuleConstant.java b/laws-base/laws-base-core/src/main/java/com/jero/common/constant/FillRuleConstant.java index 2b1e4b25..44fb1a13 100644 --- a/laws-base/laws-base-core/src/main/java/com/jero/common/constant/FillRuleConstant.java +++ b/laws-base/laws-base-core/src/main/java/com/jero/common/constant/FillRuleConstant.java @@ -31,4 +31,9 @@ public class FillRuleConstant { */ public static final String SYSTEM = "system_tree_node"; + /** + * 标签树编码 + */ + public static final String LABEL = "label_tree_node"; + } diff --git a/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/controller/LawsLabelDatabaseController.java b/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/controller/LawsLabelDatabaseController.java index b30058a5..bc8d5469 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/controller/LawsLabelDatabaseController.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/controller/LawsLabelDatabaseController.java @@ -3,16 +3,26 @@ package com.jero.modules.laws.labeldatabase.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.metadata.IPage; import com.jero.common.api.vo.Result; +import com.jero.common.api.vo.ResultCommon; import com.jero.common.aspect.annotation.AutoLog; +import com.jero.common.constant.FillRuleConstant; +import com.jero.common.exception.JeroBootException; +import com.jero.common.util.FillRuleUtil; import com.jero.modules.laws.labeldatabase.entity.LawsLabelDatabase; import com.jero.modules.laws.labeldatabase.service.ILawsLabelDatabaseService; +import com.jero.modules.system.vo.IdMapBody; +import com.jero.modules.system.vo.IdsMapBody; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.ss.formula.functions.T; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.stream.Collectors; /** * @ClassName: LawsLabelDatabaseController @@ -34,30 +44,122 @@ public class LawsLabelDatabaseController { } /** - * 标签库-树结构查询 + * 标签库-树结构查询(根节点查询) + * 只查询一级节点 * @return */ @AutoLog(value = "标签库-树结构查询") - @ApiOperation(value="标签库-树结构查询", notes="标签库-树结构查询") + @ApiOperation(value = "标签库-树结构查询", notes = "标签库-树结构查询") @GetMapping(value = "/queryTreeList") public Result> queryTreeList(@ApiParam("一级节点名称") String firstNodeName, @ApiParam("当前页") - @RequestParam(name="pageNo", defaultValue="1") - Integer pageNo, + @RequestParam(name = "pageNo", defaultValue = "1") + Integer pageNo, @ApiParam("每页条数") - @RequestParam(name="pageSize", defaultValue="10") - Integer pageSize) { + @RequestParam(name = "pageSize", defaultValue = "10") + Integer pageSize) { return Result.OK(lawsLabelDatabaseService.queryTreeList(firstNodeName, pageNo, pageSize)); } + /** + * 标签库-标签树查询 + * 属性标识管理 + * 搜索的关键字只有3级有,只筛选出来该3级; + * 如1级或2级名称有,需要带出这个1级下所有分类或2级下所有分类 + * + * @return + */ + @AutoLog(value = "标签库-标签树查询") + @ApiOperation(value = "标签库-标签树查询", notes = "标签库-标签树查询") + @GetMapping(value = "/queryLabelTree") + public Result> queryLabelTree(@ApiParam("节点名称") String nodeName, + @ApiParam("当前页") + @RequestParam(name = "pageNo", defaultValue = "1") + Integer pageNo, + @ApiParam("每页条数") + @RequestParam(name = "pageSize", defaultValue = "10") + Integer pageSize) { + return Result.OK(lawsLabelDatabaseService.queryLabelTree(nodeName, pageNo, pageSize)); + } + + /** * 标签库-树结构查询 + * * @return */ @AutoLog(value = "标签库-零部件树选中回显") - @ApiOperation(value="标签库-零部件树选中回显", notes="标签库-零部件树选中回显") + @ApiOperation(value = "标签库-零部件树选中回显", notes = "标签库-零部件树选中回显") @PostMapping(value = "/queryByCodes") public Result> queryByCodes(@ApiParam("选中的节点名称") @RequestBody JSONObject jsonObject) { return Result.OK(lawsLabelDatabaseService.queryByCodes(jsonObject.getString("selectNodeCodes"))); } + + /** + * 标签库-新增标签 + */ + @AutoLog(value = "标签库-新增标签") + @ApiOperation(value = "标签库-新增标签", notes = "标签库-新增标签") + @PostMapping(value = "/add") + public Result add(@Validated @RequestBody LawsLabelDatabase lawsLabelDatabase) { + LawsLabelDatabase labelQuery = new LawsLabelDatabase(); + labelQuery.setName(lawsLabelDatabase.getName()); + List list = lawsLabelDatabaseService.getList(labelQuery); + if (!list.isEmpty()) { + return Result.error("标签名称重复!"); + } + // 生成code + String parentId = lawsLabelDatabase.getParentId(); + JSONObject formData = new JSONObject(); + formData.put("parentId",parentId); + String code = (String) FillRuleUtil.executeRule(FillRuleConstant.LABEL,formData); + lawsLabelDatabase.setCode(code); + lawsLabelDatabaseService.add(lawsLabelDatabase); + return Result.OK(ResultCommon.OK); + } + + @AutoLog(value = "标签库-编辑标签") + @ApiOperation(value = "标签库-编辑标签", notes = "标签库-编辑标签") + @PostMapping(value = "/edit") + public Result edit(@RequestBody LawsLabelDatabase lawsLabelDatabase) { + lawsLabelDatabaseService.editById(lawsLabelDatabase); + return Result.OK(ResultCommon.OK); + } + + @AutoLog(value = "标签库-查询标签") + @ApiOperation(value = "标签库-查询标签", notes = "标签库-查询标签") + @PostMapping(value = "/list") + public Result> list(@RequestBody LawsLabelDatabase lawsLabelDatabase) { + return Result.OK(lawsLabelDatabaseService.getList(lawsLabelDatabase)); + } + + @AutoLog(value = "标签库-通过id删除") + @ApiOperation(value = "标签库-通过id删除", notes = "标签库-通过id删除") + @PostMapping(value = "/delete") + public Result delete(@RequestBody IdMapBody map) { + String id = map.getId(); + if (StringUtils.isEmpty(id)) { + throw new JeroBootException(ResultCommon.PLEASE_SELECT_DATA); + } + lawsLabelDatabaseService.deletebyId(id); + return Result.OK(ResultCommon.OK); + } + + @AutoLog(value = "标签库-批量删除") + @ApiOperation(value = "标签库-批量删除", notes = "标签库-批量删除") + @PostMapping(value = "/deleteBatch") + public Result deleteBatch(@RequestBody IdsMapBody map) { + // 前端传过来的是codes + String codes = map.getIds(); + if (StringUtils.isEmpty(codes)) { + throw new JeroBootException(ResultCommon.PLEASE_SELECT_DATA); + } + // 转换为ids + List labelList = lawsLabelDatabaseService.queryByCodes(codes); + List ids = labelList.stream().map(LawsLabelDatabase::getId).collect(Collectors.toList()); + lawsLabelDatabaseService.deleteByIds(ids); + return Result.OK(ResultCommon.OK); + } + + } diff --git a/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/rule/LabelTreeRule.java b/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/rule/LabelTreeRule.java new file mode 100644 index 00000000..5b98ff9d --- /dev/null +++ b/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/rule/LabelTreeRule.java @@ -0,0 +1,60 @@ +package com.jero.modules.laws.labeldatabase.rule; + +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.jero.common.handler.IFillRuleHandler; +import com.jero.common.util.SpringContextUtils; +import com.jero.common.util.YouBianCodeUtil; +import com.jero.modules.laws.labeldatabase.entity.LawsLabelDatabase; +import com.jero.modules.laws.labeldatabase.service.ILawsLabelDatabaseService; +import com.jero.modules.laws.standard.entity.LawsTreeNode; +import com.jero.modules.laws.standard.service.ILawsTreeNodeService; +import io.netty.util.internal.StringUtil; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author: Mzaxd + * @Date: 2023/9/12 10:38 + */ +public class LabelTreeRule implements IFillRuleHandler { + + @Override + public Object execute(JSONObject params, JSONObject formData) { + // 基本思路: 根据 parentId 获取 父级code, 解析父级code, + // 然后获取所有子级code, 获取最大值,+1作为当前code + String parentId = (String) formData.get("parentId"); + ILawsLabelDatabaseService lawsLabelDatabaseService = (ILawsLabelDatabaseService) SpringContextUtils.getBean("lawsLabelDatabaseServiceImpl"); + + // 获取父级编码 + String parentCode = ""; + LawsLabelDatabase parentLabel = new LawsLabelDatabase(); + parentLabel.setId(parentId); + List parentList = lawsLabelDatabaseService.getList(parentLabel); + if (!parentList.isEmpty()) { + parentLabel = parentList.get(0); + parentCode = parentLabel.getCode(); + } + + List childList; + String result; + //获取子级数量 + if (StringUtil.isNullOrEmpty(parentId) || "".equals(parentId)) { + //没有父级 + LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); + lambdaQueryWrapper.isNull(LawsLabelDatabase::getParentId); + childList = lawsLabelDatabaseService.list(lambdaQueryWrapper); + result = "tag" + (childList.size() + 1); + } else { + //有父级 + LawsLabelDatabase childLabel = new LawsLabelDatabase(); + childLabel.setParentId(parentId); + childList = lawsLabelDatabaseService.getList(childLabel); + result = parentCode+"-"+(childList.size() + 1); + } + return result; + } + +} diff --git a/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/service/ILawsLabelDatabaseService.java b/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/service/ILawsLabelDatabaseService.java index 056b9839..c30ae009 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/service/ILawsLabelDatabaseService.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/service/ILawsLabelDatabaseService.java @@ -24,10 +24,49 @@ public interface ILawsLabelDatabaseService extends IService { */ IPage queryTreeList(String firstNodeName, Integer pageNo, Integer pageSize); + /** + * 标签树查询 + * + * @param nodeName + * @param pageNo + * @param pageSize + * @return + */ + IPage queryLabelTree(String nodeName, Integer pageNo, Integer pageSize); + + + /** * 零部件树选中回显 * @param selectNodeCodes * @return */ List queryByCodes(String selectNodeCodes); + + /** + * 新增 + */ + void add(LawsLabelDatabase lawsLabelDatabase); + + /** + * 更新 + */ + void editById(LawsLabelDatabase lawsLabelDatabase); + + /** + * 删除 + */ + void deletebyId(String id); + + /** + * 批量删除 + */ + void deleteByIds(List ids); + + /** + * 列表查询 + * + */ + List getList(LawsLabelDatabase lawsLabelDatabase); + } diff --git a/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/service/impl/LawsLabelDatabaseServiceImpl.java b/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/service/impl/LawsLabelDatabaseServiceImpl.java index 9b6c7643..6880c69e 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/service/impl/LawsLabelDatabaseServiceImpl.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/labeldatabase/service/impl/LawsLabelDatabaseServiceImpl.java @@ -5,11 +5,13 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.jero.common.exception.JeroBootException; import com.jero.modules.laws.labeldatabase.entity.LawsLabelDatabase; import com.jero.modules.laws.labeldatabase.mapper.LawsLabelDatabaseMapper; import com.jero.modules.laws.labeldatabase.service.ILawsLabelDatabaseService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.*; import java.util.stream.Collectors; @@ -23,6 +25,7 @@ import java.util.stream.Collectors; **/ @Service @Slf4j +@Transactional(rollbackFor = JeroBootException.class) public class LawsLabelDatabaseServiceImpl extends ServiceImpl implements ILawsLabelDatabaseService { @@ -38,7 +41,7 @@ public class LawsLabelDatabaseServiceImpl .map(v -> v.setLevel(1)) .collect(Collectors.toList()); // 搜索条件 - if (StringUtils.isNotBlank(firstNodeName)){ + if (StringUtils.isNotBlank(firstNodeName)) { rootNodeList = rootNodeList.stream() .filter(v -> v.getName().contains(firstNodeName)) .collect(Collectors.toList()); @@ -60,9 +63,84 @@ public class LawsLabelDatabaseServiceImpl return page; } + @Override + public IPage queryLabelTree(String nodeName, Integer pageNo, Integer pageSize) { + /* + * 标签库-标签树查询 + * 属性标识管理 查询 + * 搜索的关键字只有3级有,只筛选出来该3级; + * 如1级或2级名称有,需要带出这个1级下所有分类或2级下所有分类 + */ + // 查询所有数据 + List allList = list((new LambdaQueryWrapper() + .orderByDesc(LawsLabelDatabase::getCreateTime))); + // 所有根节点 + List rootAllList = allList.stream() + .filter(v -> StringUtils.isBlank(v.getParentId())) + .map(v -> v.setLevel(1)) + .collect(Collectors.toList()); + // 构建所有节点树结构 + rootAllList.stream().map(v -> { + v.setChildrenList(createChildren(v, allList, 1)); + return v; + }).forEach(v -> log.info(v.getId())); + + List resultList; + + // 搜索条件 + if (StringUtils.isNotBlank(nodeName)) { + List searchList = allList.stream() + .filter(v -> v.getName().contains(nodeName)) + .collect(Collectors.toList()); + // 不需要,searchList已经自带子节点 还带着那些没关键字的 + //// 搜索条件根节点 + //List rootSearchList = searchList.stream() + // .filter(v -> StringUtils.isBlank(v.getParentId())) + // .map(v -> v.setLevel(1)) + // .collect(Collectors.toList()); + //// 构建搜索条件树结构 + //rootSearchList.stream().map(v -> { + // v.setChildrenList(createChildren(v, searchList, 1)); + // return v; + //}).forEach(v -> log.info(v.getId())); + // 因为搜索到的节点的子节点也要带出来,子节点不一定带关键字 + // 所以与完整树进行比对,匹配到就将完整树的此节点加入结果树 + //for (LawsLabelDatabase label : searchList) { + // resultList.add(getLabelFromTree(rootAllList, label)); + //} + resultList = searchList; + } else { + resultList = rootAllList; + } + + // 手动分页 + IPage page = new Page<>(pageNo, pageSize); + page.setTotal(resultList.size()); + page.setRecords(resultList.stream() + .skip((long) (Math.max(1, pageNo) - 1) * pageSize) + .limit(pageSize) + .collect(Collectors.toList())); + return page; + } + + LawsLabelDatabase getLabelFromTree(List rootList, LawsLabelDatabase targetLabel) { + // 广度优先搜索更容易匹配到结果 + for (LawsLabelDatabase label : rootList) { + if (targetLabel.getId().equals(label.getId())) { + return label; + } + } + // 搜不到就构建下一层继续搜索 + List nextRootList = new ArrayList<>(); + for (LawsLabelDatabase label : rootList) { + nextRootList.addAll(label.getChildrenList()); + } + return getLabelFromTree(nextRootList, targetLabel); + } + @Override public List queryByCodes(String selectNodeCodes) { - if (StringUtils.isBlank(selectNodeCodes)){ + if (StringUtils.isBlank(selectNodeCodes)) { return new ArrayList<>(); } List lawsLabelDatabaseList = new ArrayList<>(); @@ -78,8 +156,10 @@ public class LawsLabelDatabaseServiceImpl return lawsLabelDatabaseList; } + /** * 递归构建子节点 + * * @param tree * @param treeList * @return @@ -87,7 +167,7 @@ public class LawsLabelDatabaseServiceImpl private List createChildren(LawsLabelDatabase tree, List treeList, Integer level) { List collect = treeList.stream().filter(v -> Objects.equals(tree.getId(), v.getParentId())).collect(Collectors.toList()); level += 1; - if (collect.isEmpty()){ + if (collect.isEmpty()) { return null; } Integer finalLevel = level; @@ -99,4 +179,53 @@ public class LawsLabelDatabaseServiceImpl return v; }).collect(Collectors.toList()); } + + /** + * 保存 + */ + @Override + public void add(LawsLabelDatabase lawsLabelDatabase) { + save(lawsLabelDatabase); + } + + /** + * 更新 + */ + @Override + public void editById(LawsLabelDatabase lawsLabelDatabase) { + saveOrUpdate(lawsLabelDatabase); + + } + + /** + * 通过id删除 + */ + @Override + public void deletebyId(String id) { + // 递归删除子节点 + // 查询其所有子节点并进行删除 + LawsLabelDatabase childrenNode = new LawsLabelDatabase(); + childrenNode.setParentId(id); + List childList = getList(childrenNode); + for (LawsLabelDatabase child : childList) { + deletebyId(child.getId()); + } + // 删除自身 + removeById(id); + } + + @Override + public List getList(LawsLabelDatabase lawsLabelDatabase) { + LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(lawsLabelDatabase); + return list(lambdaQueryWrapper); + } + + /** + * 批量删除 + */ + @Override + public void deleteByIds(List ids) { + removeByIds(ids); + } + }