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.StandardPageVO;
import com.jero.modules.docking.srms.vo.StandardTree; import com.jero.modules.docking.srms.vo.StandardTree;
import com.jero.modules.docking.utils.ResponsesUtil; import com.jero.modules.docking.utils.ResponsesUtil;
import com.jero.modules.laws.standard.service.PartNameService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -40,6 +41,9 @@ public class SrmsApiController {
private static final String MESSAGE_HEADER = "MessageHeader"; private static final String MESSAGE_HEADER = "MessageHeader";
private static final String RESULT = "Result"; private static final String RESULT = "Result";
@Resource
private PartNameService partNameService;
@ApiOperation("获取token") @ApiOperation("获取token")
@AutoLog(value = "获取token") @AutoLog(value = "获取token")
@PostMapping("/getByToken") @PostMapping("/getByToken")
@@ -147,6 +151,7 @@ public class SrmsApiController {
public JSONObject syncPartData(@RequestBody JSONObject json, HttpServletRequest req){ public JSONObject syncPartData(@RequestBody JSONObject json, HttpServletRequest req){
try { try {
Result<String> re = srmsApiService.syncPartData(json,req); Result<String> re = srmsApiService.syncPartData(json,req);
partNameService.fixFirstNodeCode();
if(!re.isSuccess()){ if(!re.isSuccess()){
log.error("零部件分类树同步接口同步失败",re.getMessage()); log.error("零部件分类树同步接口同步失败",re.getMessage());
return ResponsesUtil.getJsonResult(json.get(MESSAGE_HEADER),null); return ResponsesUtil.getJsonResult(json.get(MESSAGE_HEADER),null);
@@ -60,7 +60,7 @@ public class LawsPartNameController {
@ApiOperation(value = "企业标准-零部件树历史数据处理", notes = "企业标准-零部件树历史数据处理") @ApiOperation(value = "企业标准-零部件树历史数据处理", notes = "企业标准-零部件树历史数据处理")
@PostMapping("/hisDataFix") @PostMapping("/hisDataFix")
public Result<List<PartName>> hisDataFix() { public Result<List<PartName>> hisDataFix() {
partNameService.hisDataFix(); partNameService.fixFirstNodeCode();
return Result.OK(); 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 @Override
public void hisDataFix() { public void fixFirstNodeCode() {
List<PartName> allDataList = list(); try {
// 遍历所有数据,填充firstNode字段 List<PartName> allDataList = list();
// 找到树结构中每个节点的一级节点 // 遍历所有数据,填充firstNode字段
Map<String, PartName> nodeMap = allDataList.stream() // 找到树结构中每个节点的一级节点
.collect(Collectors.toMap(PartName::getCode, Function.identity())); Map<String, PartName> nodeMap = allDataList.stream()
.collect(Collectors.toMap(PartName::getCode, Function.identity()));
// 构建树形结构 List<PartName> needUpdateList = new ArrayList<>();
allDataList.forEach(node -> {
PartName parentNode = nodeMap.get(node.getParentCode()); // 构建树形结构
if (parentNode != null) { allDataList.forEach(node -> {
if (parentNode.getChildPartNameList() == null) { PartName parentNode = nodeMap.get(node.getParentCode());
parentNode.setChildPartNameList(new ArrayList<>()); 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 -> { needUpdateList.forEach(node -> {
if (!node.getParentCode().equals("Part") && node.getFirstNode() == null) {
PartName firstNode = this.getFirstLevelNode(node, nodeMap); PartName firstNode = this.getFirstLevelNode(node, nodeMap);
if (firstNode != null) { if (firstNode != null) {
node.setFirstNode(firstNode.getCode()); node.setFirstNode(firstNode.getCode());
} }
} });
});
// 更新数据库 // 更新数据库
updateBatchById(allDataList); updateBatchById(needUpdateList);
} catch (Exception e) {
log.error("零部件树设置firstNode字段数据异常", e);
}
} }
private PartName getFirstLevelNode(PartName node, Map<String, PartName> nodeMap) { private PartName getFirstLevelNode(PartName node, Map<String, PartName> nodeMap) {