package com.ruoyi.production.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.common.utils.bean.BeanUtils; import com.ruoyi.production.dto.ProductionOrderStructureDto; import com.ruoyi.production.mapper.ProductionOrderStructureMapper; import com.ruoyi.production.pojo.ProductionOrderStructure; import com.ruoyi.production.service.IProductionOrderStructureService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; /** *

* 生产订单绑定的BOM子表 服务实现类 *

* * @author deslrey * @since 2026-03-20 */ @Slf4j @Service public class ProductionOrderStructureServiceImpl extends ServiceImpl implements IProductionOrderStructureService { @Override public List listByOrderId(Long orderId) { List list = list(new LambdaQueryWrapper().eq(ProductionOrderStructure::getOrderId, orderId)); List dtoList = list.stream().map(item -> { ProductionOrderStructureDto dto = new ProductionOrderStructureDto(); BeanUtils.copyProperties(item, dto); return dto; }).collect(Collectors.toList()); Map map = new HashMap<>(); for (ProductionOrderStructureDto node : dtoList) { map.put(node.getId(), node); } List tree = new ArrayList<>(); for (ProductionOrderStructureDto node : dtoList) { Long parentId = node.getParentId(); if (parentId == null || !map.containsKey(parentId)) { tree.add(node); } else { map.get(parentId).getChildren().add(node); } } return tree; } @Override public void addOrUpdateBomStructs(ProductionOrderStructureDto instanceDto) { Long orderId = instanceDto.getOrderId(); // 扁平化前端传入的树 List flatList = new ArrayList<>(); flattenTree(instanceDto.getChildren(), flatList); // 查询数据库已有数据 List dbList = list(new LambdaQueryWrapper() .eq(ProductionOrderStructure::getOrderId, orderId)); // 前端已有id集合 Set frontendIds = flatList.stream() .map(ProductionOrderStructureDto::getId) .filter(Objects::nonNull) .collect(Collectors.toSet()); // 需要删除的节点 Set deleteIds = dbList.stream() .map(ProductionOrderStructure::getId) .filter(id -> !frontendIds.contains(id)) .collect(Collectors.toSet()); if (!deleteIds.isEmpty()) { removeByIds(deleteIds); } List insertList = new ArrayList<>(); List updateList = new ArrayList<>(); Map tempEntityMap = new HashMap<>(); for (ProductionOrderStructureDto dto : flatList) { ProductionOrderStructure entity = new ProductionOrderStructure(); BeanUtils.copyProperties(dto, entity); entity.setOrderId(orderId); if (dto.getId() == null) { entity.setId(null); entity.setParentId(null); insertList.add(entity); if (dto.getTempId() != null) { tempEntityMap.put(dto.getTempId(), entity); } } else { updateList.add(entity); } } if (!insertList.isEmpty()) { saveBatch(insertList); } // 回写新增节点的 parentId List parentFixList = new ArrayList<>(); for (ProductionOrderStructureDto dto : flatList) { if (dto.getId() != null) continue; ProductionOrderStructure child = tempEntityMap.get(dto.getTempId()); if (child == null) continue; String parentTempId = dto.getParentTempId(); if (parentTempId != null && !parentTempId.isEmpty()) { Long realParentId; if (tempEntityMap.containsKey(parentTempId)) { realParentId = tempEntityMap.get(parentTempId).getId(); } else { try { realParentId = Long.valueOf(parentTempId); } catch (NumberFormatException e) { realParentId = 0L; } } child.setParentId(realParentId); } else { child.setParentId(0L); } parentFixList.add(child); } if (!parentFixList.isEmpty()) { updateBatchById(parentFixList); } if (!updateList.isEmpty()) { updateBatchById(updateList); } } private void flattenTree(List source, List result) { if (source == null) return; for (ProductionOrderStructureDto node : source) { result.add(node); flattenTree(node.getChildren(), result); } } }