72 lines
2.2 KiB
Java
72 lines
2.2 KiB
Java
package com.hivekion.supplier.controller;
|
|
|
|
import com.hivekion.common.entity.ResponseData;
|
|
import com.hivekion.common.entity.TreeNode;
|
|
import com.hivekion.supplier.entity.SuppliesDict;
|
|
import com.hivekion.supplier.service.SuppliesDictService;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
import javax.annotation.Resource;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
/**
|
|
* <p>
|
|
* 物资字典表 前端控制器
|
|
* </p>
|
|
*
|
|
* @author liDongYu
|
|
* @since 2025-09-09
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/suppliesDict")
|
|
public class SuppliesDictController {
|
|
|
|
@Resource
|
|
private SuppliesDictService suppliesDictService;
|
|
|
|
@GetMapping("/tree")
|
|
public ResponseData<List<TreeNode>> supplierTree() {
|
|
List<SuppliesDict> dictList = suppliesDictService.list();
|
|
//获取所有的顶级节点
|
|
List<SuppliesDict> topList = dictList.stream().filter(a -> a.getParentId() == null)
|
|
.collect(Collectors.toList());
|
|
Map<String, List<SuppliesDict>> parentMap = dictList.stream()
|
|
.filter(a -> a.getParentId() != null)
|
|
.collect(Collectors.groupingBy(SuppliesDict::getParentId));
|
|
List<TreeNode> treeNodeList = new ArrayList<>();
|
|
topList.forEach(a -> {
|
|
TreeNode treeNode = new TreeNode();
|
|
treeNode.setKey(a.getId());
|
|
treeNode.setData(a);
|
|
treeNode.setTitle(a.getSupplierName());
|
|
buildChildren(treeNode, parentMap);
|
|
treeNodeList.add(treeNode);
|
|
});
|
|
return ResponseData.success(treeNodeList);
|
|
}
|
|
|
|
private void buildChildren(TreeNode node, Map<String, List<SuppliesDict>> parentMap) {
|
|
if (parentMap.get(node.getKey()) != null) {
|
|
List<TreeNode> childrenList = new ArrayList<>();
|
|
node.setChildren(childrenList);
|
|
parentMap.get(node.getKey()).forEach(a -> {
|
|
TreeNode treeNode = new TreeNode();
|
|
treeNode.setKey(a.getId());
|
|
treeNode.setData(a);
|
|
treeNode.setValue(a.getId());
|
|
treeNode.setTitle(a.getSupplierName());
|
|
childrenList.add(treeNode);
|
|
buildChildren(treeNode, parentMap);
|
|
});
|
|
}else{
|
|
node.setLeaf(true);
|
|
}
|
|
}
|
|
|
|
|
|
}
|