feat: 零部件树同步接口 同步完成后自动设置firstNode字段

This commit is contained in:
2024-02-26 15:55:46 +08:00
parent f7159831fe
commit efe8cd99b9
4 changed files with 38 additions and 24 deletions
@@ -12,6 +12,7 @@ import com.jero.modules.docking.srms.vo.ByFileRows;
import com.jero.modules.docking.srms.vo.StandardPageVO;
import com.jero.modules.docking.srms.vo.StandardTree;
import com.jero.modules.docking.utils.ResponsesUtil;
import com.jero.modules.laws.standard.service.PartNameService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
@@ -40,6 +41,9 @@ public class SrmsApiController {
private static final String MESSAGE_HEADER = "MessageHeader";
private static final String RESULT = "Result";
@Resource
private PartNameService partNameService;
@ApiOperation("获取token")
@AutoLog(value = "获取token")
@PostMapping("/getByToken")
@@ -147,6 +151,7 @@ public class SrmsApiController {
public JSONObject syncPartData(@RequestBody JSONObject json, HttpServletRequest req){
try {
Result<String> re = srmsApiService.syncPartData(json,req);
partNameService.fixFirstNodeCode();
if(!re.isSuccess()){
log.error("零部件分类树同步接口同步失败",re.getMessage());
return ResponsesUtil.getJsonResult(json.get(MESSAGE_HEADER),null);
@@ -60,7 +60,7 @@ public class LawsPartNameController {
@ApiOperation(value = "企业标准-零部件树历史数据处理", notes = "企业标准-零部件树历史数据处理")
@PostMapping("/hisDataFix")
public Result<List<PartName>> hisDataFix() {
partNameService.hisDataFix();
partNameService.fixFirstNodeCode();
return Result.OK();
}
@@ -34,5 +34,5 @@ public interface PartNameService extends IService<PartName> {
/**
* 零部件树历史数据处理
*/
void hisDataFix();
void fixFirstNodeCode();
}
@@ -140,36 +140,45 @@ public class PartNameServiceImpl extends ServiceImpl<PartNameMapper, PartName>
}
@Override
public void hisDataFix() {
List<PartName> allDataList = list();
// 遍历所有数据,填充firstNode字段
// 找到树结构中每个节点的一级节点
Map<String, PartName> nodeMap = allDataList.stream()
.collect(Collectors.toMap(PartName::getCode, Function.identity()));
public void fixFirstNodeCode() {
try {
List<PartName> allDataList = list();
// 遍历所有数据,填充firstNode字段
// 找到树结构中每个节点的一级节点
Map<String, PartName> nodeMap = allDataList.stream()
.collect(Collectors.toMap(PartName::getCode, Function.identity()));
// 构建树形结构
allDataList.forEach(node -> {
PartName parentNode = nodeMap.get(node.getParentCode());
if (parentNode != null) {
if (parentNode.getChildPartNameList() == null) {
parentNode.setChildPartNameList(new ArrayList<>());
List<PartName> needUpdateList = new ArrayList<>();
// 构建树形结构
allDataList.forEach(node -> {
PartName parentNode = nodeMap.get(node.getParentCode());
if (parentNode != null) {
if (parentNode.getChildPartNameList() == null) {
parentNode.setChildPartNameList(new ArrayList<>());
}
parentNode.getChildPartNameList().add(node);
}
parentNode.getChildPartNameList().add(node);
}
});
if (StrUtil.isBlank(node.getFirstNode()) &&
!node.getParentCode().equals("Part") &&
!node.getParentCode().equals("0")) {
needUpdateList.add(node);
}
});
// 数据整理
allDataList.forEach(node -> {
if (!node.getParentCode().equals("Part") && node.getFirstNode() == null) {
// 数据整理
needUpdateList.forEach(node -> {
PartName firstNode = this.getFirstLevelNode(node, nodeMap);
if (firstNode != null) {
node.setFirstNode(firstNode.getCode());
}
}
});
});
// 更新数据库
updateBatchById(allDataList);
// 更新数据库
updateBatchById(needUpdateList);
} catch (Exception e) {
log.error("零部件树设置firstNode字段数据异常", e);
}
}
private PartName getFirstLevelNode(PartName node, Map<String, PartName> nodeMap) {