zss
2023-09-21 2dbc49184bd74845c8da694c20d6fd03d7ac87e0
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
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.StandardDto;
import com.yuanchu.mom.service.*;
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.stream.Collectors;
 
/**
 * @author Administrator
 * @description 针对表【standard】的数据库操作Service实现
 * @createDate 2023-07-26 15:59:05
 */
@Service
public class StandardServiceImpl extends ServiceImpl<StandardMapper, Standard> implements StandardService {
 
    @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
    ProductMapper productMapper;
 
    @Resource
    MbomMapper mbomMapper;
 
    @Resource
    TechnologyMapper technologyMapper;
 
    //(3级)新增-->标准,型号
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addStandard(StandardDto standardDto,Integer spId,Integer ver) {
        //校验添加该物料下的标准是否重复
        List<String> standNameList = standardMapper.selectList(Wrappers.<Standard>query().eq("material_id", standardDto.getId())).stream().map(standard -> {
            String standardName = standard.getName();
            return standardName;
        }).collect(Collectors.toList());
        if (standNameList.contains(standardDto.getStandard())){
            return "该产品下有该标准";
        }
        /*新增标准表*/
        Standard standard = new Standard();
        standard.setMaterial_id(standardDto.getId());
        standard.setName(standardDto.getStandard());
        standardMapper.insert(standard);
        /*新增型号表*/
        Specifications specifications = new Specifications();
        specifications.setStandardId(standard.getId());
        specifications.setName(standardDto.getSpecifications());
        specificationsMapper.insert(specifications);
        /**
         * 根据选择的情况,判断是拉取基础数据的数据,还是某一个产品下某一个型号的数据
         */
        //如果spId传的参数是0,则拉取基础数据的数据
        if (spId == 0) {
            /*新增标准BOM-->工艺路线(批量添加)*/
            //查询物料的大类(根据物料id)
            Material material = materialMapper.selectById(standardDto.getId());
            List<TechnologyTemplate> technologyTemplateList = technologyTemplateMapper.selectList(Wrappers.<TechnologyTemplate>query().eq("type", material.getFather()));
            if (ObjectUtils.isEmpty(technologyTemplateList)) {
                return "添加标准【" + standardDto.getStandard() + "】成功!";
            }
            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.setQualityTraceability(mbomModel.getQualityTraceability());
                    mbom.setSpecifications(mbomModel.getSpecifications());
                    mbom.setTechnologyId(technologyIds.get(i));
                    mbomList.add(mbom);
                }
            }
            mbomService.saveBatch(mbomList);
            return "添加标准【" + standardDto.getStandard() + "】成功!";
        }
        //如果spId!=0,等于选择的具体型号,则添加该型号版本的数据
        /*新增标准BOM-->工艺路线(批量添加)*/
        List<Technology> technologyList = technologyMapper.selAllByVerSpId(spId, ver);
        List<Technology> technologys = technologyList.stream().map(technology -> {
            Technology tec = new Technology();
            tec.setSpecificationsId(specifications.getId());
            tec.setName(technology.getName());
            tec.setFather(technology.getFather());
            tec.setDeviceGroup(technology.getDeviceGroup());
            tec.setProductionQuota(technology.getProductionQuota());
            return tec;
        }).collect(Collectors.toList());
        technologyService.saveBatch(technologys);
        //两者长度一定一样
        for (int i = 0; i < technologyList.size(); i++) {
            /*新增标准BOM-->技术指标(批量添加)*/
            List<Product> productList = productMapper.selProByVerSpe(technologyList.get(i).getId());
            List<Product> products = new ArrayList<>();
            for (Product product : productList) {
                Product pro = new Product();
                pro.setTechnologyId(technologys.get(i).getId());
                pro.setName(product.getName());
                pro.setFather(product.getFather());
                pro.setUnit(product.getUnit());
                pro.setRequired(product.getRequired());
                pro.setInternal(product.getInternal());
                products.add(pro);
            }
            productService.saveBatch(products);
            /*新增标准BOM-->物料清单(批量添加)*/
            List<Mbom> mbomList = mbomMapper.seleByTechId(technologyList.get(i).getId());
            List<Mbom> mboms = new ArrayList<>();
            for (Mbom mbom : mbomList) {
                Mbom mbo = new Mbom();
                mbo.setTechnologyId(technologys.get(i).getId());
                mbo.setName(mbom.getName());
                mbo.setUnit(mbom.getUnit());
                mbo.setUnit(mbom.getUnit());
                mbo.setNum(mbom.getNum());
                mbo.setQualityTraceability(mbom.getQualityTraceability());
                mbo.setSpecifications(mbom.getSpecifications());
                mboms.add(mbo);
            }
            mbomService.saveBatch(mboms);
        }
        return "添加标准【" + standardDto.getStandard() + "】成功!";
    }
}