| | |
| | | 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; |
| | | |
| | | /** |
| | | * <br> |
| | |
| | | @Slf4j |
| | | @Service |
| | | public class ProductStructureInstanceServiceImpl extends ServiceImpl<ProductStructureInstanceMapper, ProductStructureInstance> implements ProductStructureInstanceService { |
| | | |
| | | @Override |
| | | public List<ProductStructureInstanceDto> listByOrderId(Long orderId) { |
| | | List<ProductStructureInstance> list = list(new LambdaQueryWrapper<ProductStructureInstance>().eq(ProductStructureInstance::getOrderId, orderId)); |
| | | |
| | | List<ProductStructureInstanceDto> dtoList = list.stream().map(item -> { |
| | | ProductStructureInstanceDto dto = new ProductStructureInstanceDto(); |
| | | BeanUtils.copyProperties(item, dto); |
| | | return dto; |
| | | }).collect(java.util.stream.Collectors.toList()); |
| | | |
| | | Map<Long, ProductStructureInstanceDto> map = new HashMap<>(); |
| | | for (ProductStructureInstanceDto node : dtoList) { |
| | | map.put(node.getId(), node); |
| | | } |
| | | |
| | | List<ProductStructureInstanceDto> 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<ProductStructureInstanceDto> flatList = new ArrayList<>(); |
| | | flattenTree(instanceDto.getChildren(), flatList); |
| | | |
| | | // 查询数据库已有数据 |
| | | List<ProductStructureInstance> dbList = list(new LambdaQueryWrapper<ProductStructureInstance>() |
| | | .eq(ProductStructureInstance::getOrderId, orderId)); |
| | | |
| | | // 前端已有id集合 |
| | | Set<Long> frontendIds = flatList.stream() |
| | | .map(ProductStructureInstanceDto::getId) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | // 需要删除的节点 |
| | | Set<Long> deleteIds = dbList.stream() |
| | | .map(ProductStructureInstance::getId) |
| | | .filter(id -> !frontendIds.contains(id)) |
| | | .collect(Collectors.toSet()); |
| | | if (!deleteIds.isEmpty()) { |
| | | removeByIds(deleteIds); |
| | | } |
| | | |
| | | List<ProductStructureInstance> insertList = new ArrayList<>(); |
| | | List<ProductStructureInstance> updateList = new ArrayList<>(); |
| | | Map<String, ProductStructureInstance> 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<ProductStructureInstance> 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<ProductStructureInstanceDto> source, List<ProductStructureInstanceDto> result) { |
| | | if (source == null) return; |
| | | for (ProductStructureInstanceDto node : source) { |
| | | result.add(node); |
| | | flattenTree(node.getChildren(), result); |
| | | } |
| | | } |
| | | } |