Fixiaobai
2023-09-05 c9da1b0da1178911e383ddcaebecd1e088fa6004
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package com.yuanchu.limslaboratory.service.impl;
 
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.limslaboratory.exception.ApplicationException;
import com.yuanchu.limslaboratory.mapper.*;
import com.yuanchu.limslaboratory.pojo.*;
import com.yuanchu.limslaboratory.pojo.vo.InsProductVo;
import com.yuanchu.limslaboratory.pojo.vo.InspectDetailVo;
import com.yuanchu.limslaboratory.pojo.vo.InspectionVo;
import com.yuanchu.limslaboratory.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
 
/**
 * 申请表(Inspection)表服务实现类
 *
 * @author zss
 * @since 2023-08-03 13:03:36
 */
@Service
public class InspectionServiceImpl extends ServiceImpl<InspectionMapper, Inspection> implements InspectionService {
 
    @Resource
    InspectionMapper inspectionMapper;
 
    @Resource
    InspectionMaterialMapper inspectionMaterialMapper;
 
    @Resource
    InspectionProductService inspectionProductService;
 
    @Resource
    InspectionProductMapper inspectionProductMapper;
 
    @Resource
    MaterialMapper materialMapper;
 
    @Resource
    StandardService standardService;
 
    @Resource
    SpecificationsService specificationsService;
 
    @Resource
    ProductMapper productMapper;
 
    @Resource
    InstrumentService instrumentService;
 
    @Resource
    UserMapper userMapper;
 
    @Resource
    RawMaterialMapper rawMaterialMapper;
 
    @Resource
    LinkDetectionMapper linkDetectionMapper;
 
 
    /**
     * 查询检验申请单列表
     *
     * @param message
     * @return
     */
    @Override
    public IPage<Map<String, Object>> selectInspectsList(Page<Object> page, String message) {
        return inspectionMapper.selectInspectsList(page, message);
    }
 
    //新增检验单-->选择检验项目版本
    @Override
    public List<Integer> chooseVer(String name, String mcode, String specifications) {
        return productMapper.chooseVersion(Integer.parseInt(specifications));
    }
 
    //新增检验单-->选择检验项目版本-->查看该版本下我们要做的项目要求
    @Override
    public List<Map<String, Object>> lookProByVer(String name, String mcode, String specifications, Integer version, String experiment) {
        Integer specificationId = Integer.parseInt(specifications);
        /*如果试验项目为空则是成品检验或者原材料检验则是展示该版本的所有项目检验要求参数*/
        if (ObjectUtils.isEmpty(experiment)) {
            return productMapper.pageProductInformation(specificationId, version);
        }
        /*如果不为空则是委托检验,只展示我们要检验的项目要求参数*/
        //获取试验项目信息(结构,导线外径)
        List<String> experiments = Arrays.stream(experiment.split(",")).collect(Collectors.toList());
        //根据型号id和项目信息查询项目信息
        List<Map<String, Object>> products = new ArrayList<>();
        for (String exper : experiments) {
            List<Map<String, Object>> list = productMapper.selFath(specificationId, exper, version);
            if (ObjectUtils.isEmpty(list)) {
                Map<String, Object> project = productMapper.selNam(specificationId, exper, version);
                products.add(project);
            }
            products.addAll(list);
        }
        return products;
    }
 
    /**
     * 新增检验申请表
     * @param id 用户id
     * @param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addInspect(Integer id, InspectionVo inspectionVo) throws ApplicationException {
        /*新增检验申请表*/
        Inspection inspection = Inspection.builder()
                .startTime(inspectionVo.getStartTime())
                .endTime(inspectionVo.getEndTime())
                .type(inspectionVo.getType())
                .code(new Snowflake(1, 1).nextIdStr())
                .userId(id)
                .build();
        inspectionMapper.insert(inspection);
        /*如果是原材料检验,新增之后要更改原材料报检的状态和检验人,检验日期*/
        if (inspectionVo.getType()==0) {
            RawMaterial rawMaterial = new RawMaterial();
            rawMaterial.setId(inspectionVo.getId());
            rawMaterial.setType(1);
            rawMaterial.setInspectionDate(DateUtil.date());
            rawMaterial.setSurveyor(userMapper.selectById(id).getName());
            rawMaterialMapper.updateById(rawMaterial);
        }
        /*如果是委托检验,新增之后要更改委托报检的状态*/
        else if (inspectionVo.getType()==2){
            LinkDetection linkDetection = new LinkDetection();
            linkDetection.setId(inspectionVo.getId());
            linkDetection.setInspectionStatus(2);
            linkDetectionMapper.updateById(linkDetection);
        }
        /*新增检验样品表*/
        InspectionMaterial inspectionMaterial = InspectionMaterial.builder()
                .code(inspectionVo.getMcode())
                .name(inspectionVo.getName())
                .num(inspectionVo.getNum())
                .unit(inspectionVo.getUnit())
                .supplier(inspectionVo.getSupplier())
                .specificationsId(inspectionVo.getSpecificationId())
                .specifications(inspectionVo.getSpecifications())
                .formTime(inspectionVo.getFormTime())
                .inspectionId(inspection.getId())
                .notes(inspectionVo.getNotes())
                .build();
        inspectionMaterialMapper.insert(inspectionMaterial);
        /*新增检验项目表*/
        //根据样品名称编号以及型号规格获取型号id
        Integer specificationId = Integer.parseInt(inspectionVo.getSpecificationId());
        //if(ObjectUtils.isNotEmpty(inspectionVo.getSpecificationId())){
        //    specificationId =Integer.parseInt(inspectionVo.getSpecificationId());
        //}else{
        //    specificationId=getSpecificationId(inspectionVo.getName(), inspectionVo.getMcode(), inspectionVo.getSpecifications());
        //}
        //如果试验项目字段不为空则按该字段的项目进行匹配
        if (ObjectUtils.isNotEmpty(inspectionVo.getExperiment())) {
            //获取试验项目信息(结构,导线外径)
            List<String> experiments = Arrays.stream(inspectionVo.getExperiment().split(",")).collect(Collectors.toList());
            //根据型号id和项目信息查询项目信息
            List<Product> products = new ArrayList<>();
            for (String experiment : experiments) {
                List<Product> productList1 = productMapper.selectList(Wrappers.<Product>query()
                        .eq("specifications_id", specificationId)
                        .eq("father", experiment)
                        .eq("version", inspectionVo.getVersion()));
                if (ObjectUtils.isEmpty(productList1)) {
                    Product product = productMapper.selectOne(Wrappers.<Product>query()
                            .eq("specifications_id", specificationId)
                            .isNull("father")
                            .eq("name", experiment)
                            .eq("version", inspectionVo.getVersion()));
                    products.add(product);
                } else products.addAll(productList1);
            }
            //将查询的项目信息构建成检验项目
            ArrayList<InspectionProduct> list = new ArrayList<>();
            for (Product product : products) {
                InspectionProduct rawInsProduct = InspectionProduct.builder()
                        .name(product.getName())
                        .unit(product.getUnit())
                        .required(product.getRequired())
                        .internal(product.getInternal())
                        .inspectionMaterialId(inspectionMaterial.getId())
                        .build();
                list.add(rawInsProduct);
            }
            //检验项目批量添加
            inspectionProductService.saveBatch(list);
            return inspection.getId();
        }
        //如果试验项目为空则按照型号id在标准库里面全部匹配
        List<Product> productList =
                productMapper.selectList(Wrappers.<Product>query()
                        .eq("specifications_id", specificationId)
                .eq("version",inspectionVo.getVersion()));
        productList.stream().forEach(p->{
            String internal = Optional.ofNullable(p)
                    .map(Product::getInternal)
                    .orElse("空");
            String required = Optional.ofNullable(p)
                    .map(Product::getRequired).orElse("空");
            if(Objects.equals("空",internal)||Objects.equals(required,"空")) {
                throw new ApplicationException("500","项目版本不可用");
            }
        });
        //将查询的项目信息构建成检验项目
        ArrayList<InspectionProduct> list = new ArrayList<>();
        for (Product product : productList) {
            InspectionProduct rawInsProduct = InspectionProduct.builder()
                    .name(product.getName())
                    .unit(product.getUnit())
                    .required(product.getRequired())
                    .internal(product.getInternal())
                    .inspectionMaterialId(inspectionMaterial.getId())
                    .build();
            list.add(rawInsProduct);
        }
        //检验项目批量添加
        inspectionProductService.saveBatch(list);
        return inspection.getId();
    }
 
    //根据检验单id查询原材料检验单详情
    @Override
    @Transactional(rollbackFor = Exception.class)
    public InspectDetailVo selectInspectsListById(Integer id) {
        /*将检验单基本信息查询出来并封装到RawInspectVo对象中*/
        Inspection inspection = inspectionMapper.selectById(id);
        InspectDetailVo inspectDetailVo = new InspectDetailVo();
        //报检人
        User user = userMapper.selectById(inspection.getUserId());
        inspectDetailVo.setUserName(user.getName());
        //报检开始时间和结束时间
        inspectDetailVo.setStartTime(inspection.getStartTime());
        inspectDetailVo.setEndTime(inspection.getEndTime());
        //检验结论
        inspectDetailVo.setInspectionStatus(inspection.getInspectionStatus());
        //查询检验物料
        InspectionMaterial inspectionMaterial = inspectionMaterialMapper.selectOne(Wrappers.<InspectionMaterial>query().eq("inspection_id", id));
        //来料日期,供应商名称,原材料编码,原材料名称,规格型号,单位,数量
        BeanUtils.copyProperties(inspectionMaterial, inspectDetailVo);
        /*查询检验单里面的检验项目,并封装到RawInspectVo对象中*/
        LambdaQueryWrapper<InspectionProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(InspectionProduct::getInspectionMaterialId, inspectionMaterial.getId());
        List<InspectionProduct> inspectionProducts = inspectionProductMapper.selectList(queryWrapper);
        //这里查到的设备id和检验员id要查询名称
        List<InsProductVo> insProductVos = inspectionProducts.stream().map(insProduct -> {
            //将一个对象的值赋值给另一个对象
            InsProductVo insProductVo = new InsProductVo();
            BeanUtils.copyProperties(insProduct, insProductVo);
            //获取设备名(前提是如果存在)
            if (insProduct.getInstrumentId() != null) {
                insProductVo.setInstrumentId(insProduct.getInstrumentId());
            }
            //获取用户名(前提是如果存在)
            if (insProduct.getUserProId() != null) {
                insProductVo.setUserId(insProduct.getUserProId());
            }
            //项目关联物料id
            insProductVo.setInspectionMaterialId(inspectionMaterial.getId());
            insProductVo.setId(insProduct.getId());
            return insProductVo;
        }).collect(Collectors.toList());
        inspectDetailVo.setInsProducts(insProductVos);
        return inspectDetailVo;
    }
 
    //作废检验单
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delInspect(Integer id) {
        /*作废检验单*/
        Inspection inspection = new Inspection();
        inspection.setId(id);
        inspection.setState(0);
        inspectionMapper.updateById(inspection);
        /*作废检验样品*/
        InspectionMaterial inspectionMaterial = inspectionMaterialMapper.selectOne(Wrappers.<InspectionMaterial>query().eq("inspection_id", id));
        inspectionMaterial.setState(0);
        inspectionMaterialMapper.updateById(inspectionMaterial);
        /*作废检验项目*/
        inspectionProductMapper.updat(inspectionMaterial.getId());
        return "作废成功!";
    }
 
    //保存检验项目责任人
    @Override
    public String chooseUseProId(Integer id, Integer userProId) {
        InspectionProduct inspectionProduct = new InspectionProduct();
        inspectionProduct.setId(id);
        inspectionProduct.setUserProId(userProId);
        inspectionProductMapper.updateById(inspectionProduct);
        return "保存成功!";
    }
 
    @Override
    public boolean chooseEquipment(Integer id, Integer equipmentId) {
        UpdateWrapper<InspectionProduct>inspectionProductUpdateWrapper=new UpdateWrapper<>();
        inspectionProductUpdateWrapper.lambda().set(InspectionProduct::getInstrumentId,equipmentId)
                .eq(InspectionProduct::getId,id);
        return inspectionProductMapper.update(null,inspectionProductUpdateWrapper)>0;
    }
 
    /*根据样品名称,样品编号,型号规格获取型号id*/
    private Integer getSpecificationId(String name, String mcode, String specification) {
        //获取物料id
        Material material = materialMapper.selectOne(Wrappers.<Material>query()
                .eq("name", name)
                .eq("code", mcode));
        if (Objects.isNull(material)){
            return null;
        }
        //获取规格名称和型号名称
        String[] split = specification.split("-");
        String stName = split[0];
        String spName = split[1];
        //获取规格id
        Standard standard = standardService.getOne(Wrappers.<Standard>query()
                .eq("name", stName)
                .eq("material_id", material.getId()));
        //获取型号id
        Specifications specifications = specificationsService.getOne(Wrappers.<Specifications>query()
                .eq("name", spName)
                .eq("standard_id", standard.getId()));
        return specifications.getId();
    }
 
}