属性标识管理

This commit is contained in:
Xia Zekun
2024-03-15 13:46:39 +08:00
committed by fengchenchen
parent 9677266fad
commit 72e92122b7
5 changed files with 345 additions and 10 deletions
@@ -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<IPage<LawsLabelDatabase>> 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<IPage<LawsLabelDatabase>> 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<List<LawsLabelDatabase>> queryByCodes(@ApiParam("选中的节点名称") @RequestBody JSONObject jsonObject) {
return Result.OK(lawsLabelDatabaseService.queryByCodes(jsonObject.getString("selectNodeCodes")));
}
/**
* 标签库-新增标签
*/
@AutoLog(value = "标签库-新增标签")
@ApiOperation(value = "标签库-新增标签", notes = "标签库-新增标签")
@PostMapping(value = "/add")
public Result<T> add(@Validated @RequestBody LawsLabelDatabase lawsLabelDatabase) {
LawsLabelDatabase labelQuery = new LawsLabelDatabase();
labelQuery.setName(lawsLabelDatabase.getName());
List<LawsLabelDatabase> 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<T> edit(@RequestBody LawsLabelDatabase lawsLabelDatabase) {
lawsLabelDatabaseService.editById(lawsLabelDatabase);
return Result.OK(ResultCommon.OK);
}
@AutoLog(value = "标签库-查询标签")
@ApiOperation(value = "标签库-查询标签", notes = "标签库-查询标签")
@PostMapping(value = "/list")
public Result<List<LawsLabelDatabase>> list(@RequestBody LawsLabelDatabase lawsLabelDatabase) {
return Result.OK(lawsLabelDatabaseService.getList(lawsLabelDatabase));
}
@AutoLog(value = "标签库-通过id删除")
@ApiOperation(value = "标签库-通过id删除", notes = "标签库-通过id删除")
@PostMapping(value = "/delete")
public Result<T> 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<T> deleteBatch(@RequestBody IdsMapBody map) {
// 前端传过来的是codes
String codes = map.getIds();
if (StringUtils.isEmpty(codes)) {
throw new JeroBootException(ResultCommon.PLEASE_SELECT_DATA);
}
// 转换为ids
List<LawsLabelDatabase> labelList = lawsLabelDatabaseService.queryByCodes(codes);
List<String> ids = labelList.stream().map(LawsLabelDatabase::getId).collect(Collectors.toList());
lawsLabelDatabaseService.deleteByIds(ids);
return Result.OK(ResultCommon.OK);
}
}
@@ -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<LawsLabelDatabase> parentList = lawsLabelDatabaseService.getList(parentLabel);
if (!parentList.isEmpty()) {
parentLabel = parentList.get(0);
parentCode = parentLabel.getCode();
}
List<LawsLabelDatabase> childList;
String result;
//获取子级数量
if (StringUtil.isNullOrEmpty(parentId) || "".equals(parentId)) {
//没有父级
LambdaQueryWrapper<LawsLabelDatabase> 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;
}
}
@@ -24,10 +24,49 @@ public interface ILawsLabelDatabaseService extends IService<LawsLabelDatabase> {
*/
IPage<LawsLabelDatabase> queryTreeList(String firstNodeName, Integer pageNo, Integer pageSize);
/**
* 标签树查询
*
* @param nodeName
* @param pageNo
* @param pageSize
* @return
*/
IPage<LawsLabelDatabase> queryLabelTree(String nodeName, Integer pageNo, Integer pageSize);
/**
* 零部件树选中回显
* @param selectNodeCodes
* @return
*/
List<LawsLabelDatabase> queryByCodes(String selectNodeCodes);
/**
* 新增
*/
void add(LawsLabelDatabase lawsLabelDatabase);
/**
* 更新
*/
void editById(LawsLabelDatabase lawsLabelDatabase);
/**
* 删除
*/
void deletebyId(String id);
/**
* 批量删除
*/
void deleteByIds(List<String> ids);
/**
* 列表查询
*
*/
List<LawsLabelDatabase> getList(LawsLabelDatabase lawsLabelDatabase);
}
@@ -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<LawsLabelDatabaseMapper, LawsLabelDatabase>
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<LawsLabelDatabase> queryLabelTree(String nodeName, Integer pageNo, Integer pageSize) {
/*
* 标签库-标签树查询
* 属性标识管理 查询
* 搜索的关键字只有3级有,只筛选出来该3级;
* 如1级或2级名称有,需要带出这个1级下所有分类或2级下所有分类
*/
// 查询所有数据
List<LawsLabelDatabase> allList = list((new LambdaQueryWrapper<LawsLabelDatabase>()
.orderByDesc(LawsLabelDatabase::getCreateTime)));
// 所有根节点
List<LawsLabelDatabase> 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<LawsLabelDatabase> resultList;
// 搜索条件
if (StringUtils.isNotBlank(nodeName)) {
List<LawsLabelDatabase> searchList = allList.stream()
.filter(v -> v.getName().contains(nodeName))
.collect(Collectors.toList());
// 不需要,searchList已经自带子节点 还带着那些没关键字的
//// 搜索条件根节点
//List<LawsLabelDatabase> 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<LawsLabelDatabase> 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<LawsLabelDatabase> rootList, LawsLabelDatabase targetLabel) {
// 广度优先搜索更容易匹配到结果
for (LawsLabelDatabase label : rootList) {
if (targetLabel.getId().equals(label.getId())) {
return label;
}
}
// 搜不到就构建下一层继续搜索
List<LawsLabelDatabase> nextRootList = new ArrayList<>();
for (LawsLabelDatabase label : rootList) {
nextRootList.addAll(label.getChildrenList());
}
return getLabelFromTree(nextRootList, targetLabel);
}
@Override
public List<LawsLabelDatabase> queryByCodes(String selectNodeCodes) {
if (StringUtils.isBlank(selectNodeCodes)){
if (StringUtils.isBlank(selectNodeCodes)) {
return new ArrayList<>();
}
List<LawsLabelDatabase> 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<LawsLabelDatabase> createChildren(LawsLabelDatabase tree, List<LawsLabelDatabase> treeList, Integer level) {
List<LawsLabelDatabase> 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<LawsLabelDatabase> childList = getList(childrenNode);
for (LawsLabelDatabase child : childList) {
deletebyId(child.getId());
}
// 删除自身
removeById(id);
}
@Override
public List<LawsLabelDatabase> getList(LawsLabelDatabase lawsLabelDatabase) {
LambdaQueryWrapper<LawsLabelDatabase> lambdaQueryWrapper = new LambdaQueryWrapper<>(lawsLabelDatabase);
return list(lambdaQueryWrapper);
}
/**
* 批量删除
*/
@Override
public void deleteByIds(List<String> ids) {
removeByIds(ids);
}
}