liyong
4 天以前 dee56ed8d52f1d8ee1f89b369561c49e9b3b7c2d
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
154
155
156
157
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.ProductStructureDto;
import com.ruoyi.production.mapper.ProductStructureMapper;
import com.ruoyi.production.pojo.ProductStructure;
import com.ruoyi.production.service.ProductStructureService;
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.*;
import java.util.stream.Collectors;
 
@Slf4j
@Service
public class ProductStructureServiceImpl extends ServiceImpl<ProductStructureMapper, ProductStructure> implements ProductStructureService {
 
    @Autowired
    private ProductStructureMapper productStructureMapper;
 
 
    @Override
    @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;
    }
 
    /**
     * 将前端传入的树进行扁平化
     *
     * @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;
    }
 
}