package com.ruoyi.appendix.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.appendix.dto.ProductStructureInstanceDto; import com.ruoyi.appendix.mapper.ProductStructureInstanceMapper; import com.ruoyi.appendix.pojo.ProductStructureInstance; import com.ruoyi.appendix.service.ProductStructureInstanceService; import com.ruoyi.common.utils.bean.BeanUtils; 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子集-附表Service实现类 *
* * @author deslrey * @version 1.0 * @since 2026/03/18 13:21 */ @Slf4j @Service public class ProductStructureInstanceServiceImpl extends ServiceImpl implements ProductStructureInstanceService { @Override public List listByOrderId(Long orderId) { List list = list(new LambdaQueryWrapper().eq(ProductStructureInstance::getOrderId, orderId)); List dtoList = list.stream().map(item -> { ProductStructureInstanceDto dto = new ProductStructureInstanceDto(); BeanUtils.copyProperties(item, dto); return dto; }).collect(java.util.stream.Collectors.toList()); Map map = new HashMap<>(); for (ProductStructureInstanceDto node : dtoList) { map.put(node.getId(), node); } List tree = new ArrayList<>(); for (ProductStructureInstanceDto 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(ProductStructureInstanceDto instanceDto) { Long orderId = instanceDto.getOrderId(); // 扁平化前端传入的树 List flatList = new ArrayList<>(); flattenTree(instanceDto.getChildren(), flatList); // 查询数据库已有数据 List dbList = list(new LambdaQueryWrapper() .eq(ProductStructureInstance::getOrderId, orderId)); // 前端已有id集合 Set frontendIds = flatList.stream() .map(ProductStructureInstanceDto::getId) .filter(Objects::nonNull) .collect(Collectors.toSet()); // 需要删除的节点 Set deleteIds = dbList.stream() .map(ProductStructureInstance::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 (ProductStructureInstanceDto dto : flatList) { ProductStructureInstance entity = new ProductStructureInstance(); 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 (ProductStructureInstanceDto dto : flatList) { if (dto.getId() != null) continue; ProductStructureInstance 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 (ProductStructureInstanceDto node : source) { result.add(node); flattenTree(node.getChildren(), result); } } }