zss
2023-09-07 9fe4b53620f1ea0a4278240a7acee7bc4ef739c6
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package com.yuanchu.mom.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.yuanchu.mom.mapper.*;
import com.yuanchu.mom.pojo.*;
import com.yuanchu.mom.pojo.dto.MaterialDto;
import com.yuanchu.mom.service.*;
import com.yuanchu.mom.utils.MyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @author Administrator
 * @description 针对表【material】的数据库操作Service实现
 * @createDate 2023-07-26 15:52:50
 */
@Service
public class MaterialServiceImpl extends ServiceImpl<MaterialMapper, Material> implements MaterialService {
 
    @Resource
    MaterialMapper materialMapper;
 
    @Resource
    StandardMapper standardMapper;
 
    @Resource
    SpecificationsMapper specificationsMapper;
 
    @Resource
    TechnologyService technologyService;
 
    @Resource
    TechnologyTemplateMapper technologyTemplateMapper;
 
    @Resource
    ProductService productService;
 
    @Resource
    TechnicalModelMapper technicalModelMapper;
 
    @Resource
    MbomService mbomService;
 
    @Resource
    MbomModelMapper mbomModelMapper;
 
    @Resource
    TechniqueService techniqueService;
 
    @Resource
    TechniqueModelMapper techniqueModelMapper;
 
    @Resource
    DeviceMapper deviceMapper;
 
    @Resource
    TechnologyMapper technologyMapper;
 
    @Resource
    ProductMapper productMapper;
 
    @Resource
    MbomMapper mbomMapper;
 
    @Resource
    TechniqueMapper techniqueMapper;
 
    //标准MOM-->左侧五级树展示
    @Override
    public List<Map<String, Object>> selectTreeByMaterial() {
        return materialMapper.selectTreeByMaterial();
    }
 
    //(1,2级)新增-->物料,标准,型号
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addMaterial(MaterialDto materialDto) {
        //校验添加物料是否重复
        List<Material> materialList = materialMapper.selectList(Wrappers.<Material>query()
                .eq("type", materialDto.getType())
                .eq("father", materialDto.getFather()));
        for (Material material : materialList) {
            if (material.getName().equals(materialDto.getName())) {
                return "该类型产品大类下有该产品名称";
            }
        }
        /*新增物料表*/
        Material material = new Material();
        material.setCode(MyUtil.getTimeSixNumberCode("ML", "ML"));
        material.setName(materialDto.getName());
        material.setType(materialDto.getType());
        material.setFather(materialDto.getFather());
        materialMapper.insert(material);
        /*新增标准表*/
        Standard standard = new Standard();
        standard.setName(materialDto.getStandard());
        standard.setMaterial_id(material.getId());
        standardMapper.insert(standard);
        /*新增型号表*/
        Specifications specifications = new Specifications();
        specifications.setName(materialDto.getSpecifications());
        specifications.setStandardId(standard.getId());
        specificationsMapper.insert(specifications);
        /*新增标准BOM-->工艺路线(批量添加)*/
        List<TechnologyTemplate> technologyTemplateList = technologyTemplateMapper.selectList(Wrappers.<TechnologyTemplate>query().eq("type", materialDto.getFather()));
        List<Technology> technologyList = technologyTemplateList.stream().map(technologyTemplate -> {
            Technology technology = new Technology();
            technology.setSpecificationsId(specifications.getId());
            technology.setFather(technologyTemplate.getFather());
            technology.setName(technologyTemplate.getName());
            technology.setDeviceGroup(technologyTemplate.getDeviceGroup());
            return technology;
        }).collect(Collectors.toList());
        technologyService.saveBatch(technologyList);
        /*新增标准BOM-->技术指标(批量添加)*/
        //新增的工艺路线id集合
        List<Integer> technologyIds = technologyList.stream().map(Technology::getId).collect(Collectors.toList());
        //基础数据中工艺路线id集合
        List<Integer> techTemIds = technologyTemplateList.stream().map(TechnologyTemplate::getId).collect(Collectors.toList());
        //两者长度一定一样
        List<Product> productList = new ArrayList<>();
        for (int i = 0; i < technologyIds.size(); i++) {
            List<TechnicalModel> technicalModelList = technicalModelMapper.selectList(Wrappers.<TechnicalModel>query().eq("tech_tem_id", techTemIds.get(i)));
            for (TechnicalModel technicalModel : technicalModelList) {
                Product product = new Product();
                product.setFather(technicalModel.getFather());
                product.setName(technicalModel.getName());
                product.setUnit(technicalModel.getUnit());
                product.setTechnologyId(technologyIds.get(i));
                productList.add(product);
            }
        }
        productService.saveBatch(productList);
        /*新增标准BOM-->物料清单(批量添加)*/
        List<Mbom> mbomList = new ArrayList<>();
        for (int i = 0; i < technologyIds.size(); i++) {
            List<MbomModel> mbomModelList = mbomModelMapper.selectList(Wrappers.<MbomModel>query().eq("tech_tem_id", techTemIds.get(i)));
            for (MbomModel mbomModel : mbomModelList) {
                Mbom mbom = new Mbom();
                mbom.setUnit(mbomModel.getUnit());
                mbom.setName(mbomModel.getName());
                mbom.setSupplier(mbomModel.getSupplier());
                mbom.setQualityTraceability(mbomModel.getQualityTraceability());
                mbom.setSpecifications(mbomModel.getSpecifications());
                mbom.setTechnologyId(technologyIds.get(i));
                mbomList.add(mbom);
            }
        }
        mbomService.saveBatch(mbomList);
        /*新增标准BOM-->生产工艺(批量添加)*/
        List<Technique> techniqueList = new ArrayList<>();
        for (int i = 0; i < technologyIds.size(); i++) {
            List<TechniqueModel> techniqueModelList = techniqueModelMapper.selectList(Wrappers.<TechniqueModel>query().eq("tech_tem_id", techTemIds.get(i)));
            for (TechniqueModel techniqueModel : techniqueModelList) {
                //查询设备名称
                Device device = deviceMapper.selectById(techniqueModel.getDeviceId());
                //查询基础生产工艺中每个设备的具体项目
                TechnicalModel technicalModel = technicalModelMapper.selectById(techniqueModel.getTechnicalModelId());
                Technique technique = new Technique();
                technique.setTechnologyId(technologyIds.get(i));
                technique.setDevice(device.getName());
                technique.setProductFather(technicalModel.getFather());
                technique.setProduct(technicalModel.getName());
                technique.setUnit(technicalModel.getUnit());
                techniqueList.add(technique);
            }
        }
        techniqueService.saveBatch(techniqueList);
        return "添加物料【" + materialDto.getName() + "】成功";
    }
 
 
    //添加同一个型号工艺路线,技术指标,物料清单,生产工艺的版本
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addVersion(Integer specificationsId, Integer version) {
        List<Technology> technologyList = technologyMapper.selectList(Wrappers.<Technology>query()
                .eq("specifications_id", specificationsId)
                .eq("version", version));
        /*上一个版本的工艺路线id集合*/
        List<Integer> techTemIds = technologyList.stream().map(Technology::getId).collect(Collectors.toList());
        for (Technology technology : technologyList) {
            technology.setId(null);
            //最新版本+1
            technology.setVersion(technologyMapper.selectVerByTec(specificationsId).get(0) + 1);
        }
        technologyService.saveBatch(technologyList);
        /*工艺路线的版本新增的同时该工艺路线下下的技术指标,生产工艺,物料清单都要新增*/
        //新增版本的工艺路线id集合
        List<Integer> technologyIds = technologyList.stream().map(Technology::getId).collect(Collectors.toList());
        //两者长度一定一样
        List<Product> productList = new ArrayList<>();
        for (int i = 0; i < technologyIds.size(); i++) {
            List<Product> products = productMapper.selectList(Wrappers.<Product>query().eq("technology_id", techTemIds.get(i)));
            for (Product pro : products) {
                Product product = new Product();
                product.setFather(pro.getFather());
                product.setName(pro.getName());
                product.setUnit(pro.getUnit());
                product.setRequired(pro.getRequired());
                product.setInternal(pro.getInternal());
                product.setVersion(pro.getVersion()+1);
                product.setTechnologyId(technologyIds.get(i));
                productList.add(product);
            }
        }
        productService.saveBatch(productList);
        /*新增标准BOM-->物料清单(批量添加)*/
        List<Mbom> mbomList = new ArrayList<>();
        for (int i = 0; i < technologyIds.size(); i++) {
            List<Mbom> mboms = mbomMapper.selectList(Wrappers.<Mbom>query().eq("technology_id", techTemIds.get(i)));
            for (Mbom mb : mboms) {
                Mbom mbom = new Mbom();
                mbom.setUnit(mb.getUnit());
                mbom.setName(mb.getName());
                mbom.setSupplier(mb.getSupplier());
                mbom.setQualityTraceability(mb.getQualityTraceability());
                mbom.setSpecifications(mb.getSpecifications());
                mbom.setVersion(mb.getVersion()+1);
                mbom.setNum(mb.getNum());
                mbom.setTechnologyId(technologyIds.get(i));
                mbomList.add(mbom);
            }
        }
        mbomService.saveBatch(mbomList);
        /*新增标准BOM-->生产工艺(批量添加)*/
        List<Technique> techniqueList = new ArrayList<>();
        for (int i = 0; i < technologyIds.size(); i++) {
            List<Technique> techniques = techniqueMapper.selectList(Wrappers.<Technique>query().eq("technology_id", techTemIds.get(i)));
            for (Technique teque : techniques) {
                Technique technique = new Technique();
                technique.setTechnologyId(technologyIds.get(i));
                technique.setDevice(teque.getDevice());
                technique.setProductFather(teque.getProductFather());
                technique.setProduct(teque.getProduct());
                technique.setUnit(teque.getUnit());
                technique.setVersion(teque.getVersion()+1);
                techniqueList.add(technique);
            }
        }
        techniqueService.saveBatch(techniqueList);
        return technologyList.get(0).getVersion();
    }
 
}