gongchunyi
6 小时以前 eaf6548902e9472fc9b876d531db8508e16c4f2f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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.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;
 
/**
 * <p>
 * 生产订单绑定的BOM子表 服务实现类
 * </p>
 *
 * @author deslrey
 * @since 2026-03-20
 */
@Slf4j
@Service
public class ProductionOrderStructureServiceImpl extends ServiceImpl<ProductionOrderStructureMapper, ProductionOrderStructure> implements IProductionOrderStructureService {
 
    @Override
    public List<ProductionOrderStructureDto> listByOrderId(Long orderId) {
        List<ProductionOrderStructureDto> dtoList = baseMapper.listByOrderId(orderId);
 
        Map<Long, ProductionOrderStructureDto> map = new HashMap<>();
        for (ProductionOrderStructureDto node : dtoList) {
            map.put(node.getId(), node);
        }
 
        List<ProductionOrderStructureDto> 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(Long orderId, List<ProductionOrderStructureDto> list) {
        // 扁平化前端传入的树
        List<ProductionOrderStructureDto> flatList = new ArrayList<>();
        for (ProductionOrderStructureDto root : list) {
            flatList.add(root);
            flattenTree(root.getChildren(), flatList);
        }
 
        // 查询数据库已有数据
        List<ProductionOrderStructure> dbList = list(new LambdaQueryWrapper<ProductionOrderStructure>()
                .eq(ProductionOrderStructure::getOrderId, orderId));
 
        // 前端已有id集合
        Set<Long> frontendIds = flatList.stream()
                .map(ProductionOrderStructureDto::getId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
 
        // 需要删除的节点
        Set<Long> deleteIds = dbList.stream()
                .map(ProductionOrderStructure::getId)
                .filter(id -> !frontendIds.contains(id))
                .collect(Collectors.toSet());
        if (!deleteIds.isEmpty()) {
            removeByIds(deleteIds);
        }
 
        List<ProductionOrderStructure> insertList = new ArrayList<>();
        List<ProductionOrderStructure> updateList = new ArrayList<>();
        Map<String, ProductionOrderStructure> tempEntityMap = new HashMap<>();
 
        for (ProductionOrderStructureDto dto : flatList) {
            ProductionOrderStructure entity = new ProductionOrderStructure();
            org.springframework.beans.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<ProductionOrderStructure> parentFixList = new ArrayList<>();
        Long realParentId;
        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()) {
                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<ProductionOrderStructureDto> source, List<ProductionOrderStructureDto> result) {
        if (source == null) return;
        for (ProductionOrderStructureDto node : source) {
            result.add(node);
            flattenTree(node.getChildren(), result);
        }
    }
}