package com.ruoyi.technology.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.technology.bean.dto.TechnologyBomStructureDto; import com.ruoyi.technology.bean.vo.TechnologyBomStructureVo; import com.ruoyi.technology.mapper.TechnologyBomStructureMapper; import com.ruoyi.technology.pojo.TechnologyBomStructure; import com.ruoyi.technology.service.TechnologyBomStructureService; import lombok.RequiredArgsConstructor; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; @Service @RequiredArgsConstructor public class TechnologyBomStructureServiceImpl extends ServiceImpl implements TechnologyBomStructureService { private final TechnologyBomStructureMapper technologyBomStructureMapper; /** * 保存BOM结构树,处理新增、更新和删除节点。 */ @Override @Transactional(rollbackFor = Exception.class) public Boolean addTechnologyBomStructure(TechnologyBomStructureDto dto) { Long bomId = dto.getBomId(); List flatDtoList = new ArrayList<>(); flattenTree(dto.getChildren(), flatDtoList); List dbList = this.list(new LambdaQueryWrapper() .eq(TechnologyBomStructure::getBomId, bomId)); Set frontendIds = new HashSet<>(); for (TechnologyBomStructureDto item : flatDtoList) { if (item.getId() != null) { frontendIds.add(item.getId()); } } Set deleteIds = new HashSet<>(); for (TechnologyBomStructure dbItem : dbList) { if (!frontendIds.contains(dbItem.getId())) { deleteIds.add(dbItem.getId()); } } if (!deleteIds.isEmpty()) { this.removeByIds(deleteIds); } List insertList = new ArrayList<>(); List updateList = new ArrayList<>(); Map tempEntityMap = new HashMap<>(); for (TechnologyBomStructureDto item : flatDtoList) { TechnologyBomStructure entity = new TechnologyBomStructure(); BeanUtils.copyProperties(item, entity); entity.setBomId(bomId); if (item.getId() == null) { entity.setParentId(null); insertList.add(entity); tempEntityMap.put(item.getTempId(), entity); } else { updateList.add(entity); } } if (!insertList.isEmpty()) { this.saveBatch(insertList); } List parentFixList = new ArrayList<>(); for (TechnologyBomStructureDto item : flatDtoList) { if (item.getId() == null && item.getParentTempId() != null) { TechnologyBomStructure child = tempEntityMap.get(item.getTempId()); if (child == null) { continue; } TechnologyBomStructure parent = tempEntityMap.get(item.getParentTempId()); Long realParentId = parent != null ? parent.getId() : Long.valueOf(item.getParentTempId()); child.setParentId(realParentId); parentFixList.add(child); } } if (!parentFixList.isEmpty()) { this.updateBatchById(parentFixList); } if (!updateList.isEmpty()) { this.updateBatchById(updateList); } return true; } /** * 根据BOM查询并组装结构树。 */ @Override public List listByBomId(Long bomId) { List list = technologyBomStructureMapper.listByBomId(bomId); Map map = new HashMap<>(); for (TechnologyBomStructureVo node : list) { node.setChildren(new ArrayList<>()); map.put(node.getId(), node); } List tree = new ArrayList<>(); for (TechnologyBomStructureVo node : list) { Long parentId = node.getParentId(); if (parentId == null || parentId == 0L) { tree.add(node); continue; } TechnologyBomStructureVo parent = map.get(parentId); if (parent != null) { parent.getChildren().add(node); } } return tree; } /** * 将树形结构拍平成列表,便于统一保存。 */ private void flattenTree(List source, List result) { if (source == null) { return; } for (TechnologyBomStructureDto node : source) { result.add(node); flattenTree(node.getChildren(), result); } } }