maven
5 天以前 2cf52f66f3e7ffd415dbf49eb74aed441871f3a4
src/main/java/com/ruoyi/production/service/impl/ProductStructureServiceImpl.java
@@ -1,34 +1,157 @@
package com.ruoyi.production.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.production.dto.ProductStructureDto;
import com.ruoyi.production.mapper.ProductStructureMapper;
import com.ruoyi.production.pojo.ProductStructure;
import com.ruoyi.production.service.ProductStructureService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
@Service
@RequiredArgsConstructor
@Slf4j
@Service
public class ProductStructureServiceImpl extends ServiceImpl<ProductStructureMapper, ProductStructure> implements ProductStructureService {
    @Autowired
    private  ProductStructureMapper productStructureMapper;
    private ProductStructureMapper productStructureMapper;
    @Override
    public List<ProductStructureDto> listByproductModelId(Long productModelId) {
        return productStructureMapper.listByproductModelId( productModelId);
    @Transactional
    public Boolean addProductStructureDto(ProductStructureDto dto) {
        Long bomId = dto.getBomId();
        //  将树扁平化
        List<ProductStructureDto> flatDtoList = new ArrayList<>();
        flattenTree(dto.getChildren(), flatDtoList);
        //  查询数据库中已有的 BOM 数据
        List<ProductStructure> dbList = this.list(new LambdaQueryWrapper<ProductStructure>().eq(ProductStructure::getBomId, bomId));
        //  查找已存在的节点 - ID
        Set<Long> frontendIds = flatDtoList.stream()
                .map(ProductStructureDto::getId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        //  需要删除的节点 - ID
        Set<Long> deleteIds = dbList.stream()
                .map(ProductStructure::getId)
                .filter(id -> !frontendIds.contains(id))
                .collect(Collectors.toSet());
        if (!deleteIds.isEmpty()) {
            this.removeByIds(deleteIds);
        }
        //  新增 / 更新
        List<ProductStructure> insertList = new ArrayList<>();
        List<ProductStructure> updateList = new ArrayList<>();
        // 用于回写 parentId
        Map<String, ProductStructure> tempEntityMap = new HashMap<>();
        for (ProductStructureDto psDto : flatDtoList) {
            ProductStructure entity = new ProductStructure();
            BeanUtils.copyProperties(psDto, entity);
            entity.setBomId(bomId);
            if (psDto.getId() == null) {
                // 新增
                entity.setId(null);
                entity.setParentId(null);
                insertList.add(entity);
                tempEntityMap.put(psDto.getTempId(), entity);
            } else {
                // 更新
                updateList.add(entity);
            }
        }
        //  插入新节点
        if (!insertList.isEmpty()) {
            this.saveBatch(insertList);
        }
        //  回写新增节点 parentId
        List<ProductStructure> parentFixList = new ArrayList<>();
        //  真实的父节点 ID
        Long realParentId;
        for (ProductStructureDto psDto : flatDtoList) {
            if (psDto.getId() == null && psDto.getParentTempId() != null) {
                ProductStructure child = tempEntityMap.get(psDto.getTempId());
                if (tempEntityMap.containsKey(psDto.getParentTempId())) {
                    // 父节点是新节点
                    realParentId = tempEntityMap.get(psDto.getParentTempId()).getId();
                } else {
                    // 父节点是老节点
                    realParentId = Long.valueOf(psDto.getParentTempId());
                }
                child.setParentId(realParentId);
                parentFixList.add(child);
            }
        }
        if (!parentFixList.isEmpty()) {
            this.updateBatchById(parentFixList);
        }
        if (!updateList.isEmpty()) {
            this.updateBatchById(updateList);
        }
        return true;
    }
    @Override
    public Boolean addProductStructureDto(ProductStructureDto productStructureDto) {
        this.remove(new QueryWrapper<ProductStructure>().lambda().eq(ProductStructure::getProductModelId, productStructureDto.getProductModelId()));
        return this.saveBatch(productStructureDto.getProductStructureList());
    /**
     * 将前端传入的树进行扁平化
     *
     * @param source 数据树
     * @param result 扁平化数据
     */
    private void flattenTree(List<ProductStructureDto> source, List<ProductStructureDto> result) {
        if (source == null) {
            return;
        }
        for (ProductStructureDto node : source) {
            result.add(node);
            flattenTree(node.getChildren(), result);
        }
    }
    @Override
    public List<ProductStructureDto> listBybomId(Long bomId) {
        List<ProductStructureDto> list = productStructureMapper.listBybomId(bomId);
        Map<Long, ProductStructureDto> map = new HashMap<>();
        for (ProductStructureDto node : list) {
            node.setChildren(new ArrayList<>());
            map.put(node.getId(), node);
        }
        List<ProductStructureDto> tree = new ArrayList<>();
        for (ProductStructureDto node : list) {
            Long parentId = node.getParentId();
            if (parentId == null || parentId == 0) {
                tree.add(node);
            } else {
                ProductStructureDto parent = map.get(parentId);
                if (parent != null) {
                    parent.getChildren().add(node);
                }
            }
        }
        return tree;
    }
}