XiaoRuby
2023-09-07 c28d3bb363dde2afb44c168b93379b2bf6b1f67f
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
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
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.mom.mapper.*;
import com.yuanchu.mom.pojo.*;
import com.yuanchu.mom.pojo.vo.ProcessInspectVo;
import com.yuanchu.mom.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * 过程检验(ProcessInspect)表服务实现类
 *
 * @author zss
 * @since 2023-09-06 13:36:03
 */
@Service
public class ProcessInspectServiceImpl extends ServiceImpl<ProcessInspectMapper, ProcessInspect> implements ProcessInspectService {
 
    @Resource
    ProcessInspectMapper processInspectMapper;
 
    @Resource
    MaterialMapper materialMapper;
 
    @Resource
    StandardService standardService;
 
    @Resource
    SpecificationsService specificationsService;
 
    @Resource
    ProductService productService;
 
    @Resource
    InspectionItemMapper inspectionItemMapper;
 
    @Resource
    InspectionItemService inspectionItemService;
 
    @Resource
    InspectUnacceptedMapper inspectUnacceptedMapper;
 
    //新增过程检验单-->根据订单号选择产品信息和工艺
    @Override
    public List<Map<String, Object>> chooseMater(String orderNumber) {
        return processInspectMapper.chooseMater(orderNumber);
    }
 
    //新增过程检验单
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addProcess(String userId, ProcessInspectVo processInspectVo) {
        /*新增过程检验单*/
        ProcessInspect processInspect = new ProcessInspect();
        processInspect.setUserId(Integer.parseInt(userId));
        BeanUtils.copyProperties(processInspectVo, processInspect);
        processInspectMapper.insert(processInspect);
        /*批量新增过程检验项目表*/
        //获取型号id
        Integer specificationId = getSpecificationId(processInspectVo.getMaterial(), processInspectVo.getMaterialCode(), processInspectVo.getSpecificationsModel());
        //查询标准BOM技术指标中该型号工艺下最新版本的检验项目
        Integer ver = productService.selectVerByPro(specificationId).get(0);//该型号下技术指标最新版本
        List<Product> productList = productService.selProByVerSpe(processInspectVo.getTechnologyId(), ver);
        List<InspectionItem> inspectionItemList = productList.stream().map(product -> {
            InspectionItem inspectionItem = new InspectionItem();
            BeanUtils.copyProperties(product, inspectionItem);
            inspectionItem.setInspectId(processInspect.getId());
            inspectionItem.setType(1);
            return inspectionItem;
        }).collect(Collectors.toList());
        inspectionItemService.saveBatch(inspectionItemList);
        return processInspect.getId();
    }
 
    //上报(更新检验状态)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateProcessInspectsById(Integer id) {
        /*更新检验单里面的检验结论*/
        //先判断检验结果
        List<Integer> results = inspectionItemMapper.getResult(id,1);
        int count = 0;
        for (Integer result : results) {
            if (result != null && result == 1) {
                count++;
            }
        }
        ProcessInspect processInspect = processInspectMapper.selectById(id);
        //如果检验项目中的结论包含不合格则检验单不合格
        if (results.contains(0)) {
            processInspect.setResult(0);//不合格
            //更新检验单
            processInspectMapper.updateById(processInspect);
        } else if (count == results.size()) {
            processInspect.setResult(1);//合格
            processInspectMapper.updateById(processInspect);
        } else return "项目未检验完!";
        /*如果检验结论为不合格,则需要新增不合格检验单*/
        if (processInspect.getResult() == 0) {
            InspectUnaccepted processUnaccepted = InspectUnaccepted.builder()
                    .reason(processInspect.getMaterial() +processInspect.getTechname()+ "不合格")  //暂且定义为产品名称+工艺不合格
                    .rawInspectId(id)
                    .type(2)        //类型为过程检验
                    .build();
            inspectUnacceptedMapper.insert(processUnaccepted);
        }
        return "上报成功!";
    }
 
    //根据检验单id查询过程检验单详情
    @Override
    public List<Map<String, Object>> selectProcessInspectsListById(Integer id) {
        return processInspectMapper.selectProcessInspectsListById(id);
    }
 
    //分页查询过程检验单列表
    @Override
    public IPage<Map<String, Object>> selectProcessInspectsList(Page<Object> page, String techfather, Integer result, String name) {
        return processInspectMapper.selectProcessInspectsList(page,techfather,result,name);
    }
 
 
    /*根据样品名称,样品编号,型号规格获取型号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();
    }
 
    /*判断检测值是否满足标准值和内控值的要求,如果不满足则检验结论为不合格*/
    private int checkValues(String standardValueStr, String controlValueStr, String detectionValueStr) {
        boolean isStandardValueSatisfied = isValueSatisfied(standardValueStr, detectionValueStr);
        boolean isControlValueSatisfied = isValueSatisfied(controlValueStr, detectionValueStr);
 
        if (isStandardValueSatisfied && isControlValueSatisfied) {
            return 1;
        } else {
            return 0;
        }
    }
 
    private boolean isValueSatisfied(String valueStr, String detectionValueStr) {
        String substring = valueStr.substring(1, 2);
        if (substring.equals("=")) {
            String operator = valueStr.substring(0, 2);
            Double standardValue = Double.parseDouble(valueStr.substring(2));
            Double detectionValue = Double.parseDouble(detectionValueStr);
            switch (operator) {
                case ">=":
                    return detectionValue >= standardValue;
                case "<=":
                    return detectionValue <= standardValue;
                default:
                    return false;
            }
        } else {
            String operator = valueStr.substring(0, 1);
            Double standardValue = Double.parseDouble(valueStr.substring(1));
            Double detectionValue = Double.parseDouble(detectionValueStr);
            switch (operator) {
                case ">":
                    return detectionValue > standardValue;
                case "≥":
                    return detectionValue >= standardValue;
                case "≤":
                    return detectionValue <= standardValue;
                case "<":
                    return detectionValue < standardValue;
                case "=":
                    return detectionValue.equals(standardValue);
                default:
                    return false;
            }
        }
    }
}