gongchunyi
14 小时以前 a3976574426c68495e41b586280745c092112950
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
146
147
148
149
150
151
152
153
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>
 * BOM子集-附表Service实现类
 * </br>
 *
 * @author deslrey
 * @version 1.0
 * @since 2026/03/18 13:21
 */
@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);
        }
    }
}