zss
2023-09-25 a3f47b5e065878e5f41699ac1c597fd9ac9c4a21
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
package com.yuanchu.mom.service.impl;
 
import cn.hutool.core.date.DateUtil;
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.mom.Task.BatchInfo;
import com.yuanchu.mom.Task.SyncOrder;
import com.yuanchu.mom.mapper.*;
import com.yuanchu.mom.pojo.*;
import com.yuanchu.mom.pojo.vo.RawInsProductVo;
import com.yuanchu.mom.pojo.vo.RawInspectVo;
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.*;
import java.util.stream.Collectors;
 
/**
 * @author Administrator
 * @description 针对表【raw_inspect(原材料申请表)】的数据库操作Service实现
 * @createDate 2023-07-31 14:43:15
 */
@Service
public class RawInspectServiceImpl extends ServiceImpl<RawInspectMapper, RawInspect> implements RawInspectService {
 
    @Resource
    RawInspectMapper rawInspectMapper;
 
    @Resource
    RawInsProductMapper rawInsProductMapper;
 
    @Resource
    RawInsProductService rawInsProductService;
 
    @Resource
    InspectUnacceptedMapper inspectUnacceptedMapper;
 
    @Resource
    UserMapper userMapper;
 
    //新增原材料检验单-->根据原材料编码得到ifs中的报检数据
    @Override
    public RawInspectVo chooseIFS(String code) {
        RawInspectVo rawInspectVo = new RawInspectVo();
        List<Map<String, Object>> mapList = SyncOrder.ifsInterfaces();
        for (Map<String, Object> map : mapList) {
            // todo: 后续需要将状态改成待检验
            if (map.get("STATE").equals("已接收")) {
                if (map.get("PART_NO").toString().equals(code)) {
                    rawInspectVo.setCode(map.get("PART_NO").toString());   //原材料编码
                    String[] split = map.get("PART_DESC").toString().split(",");
                    rawInspectVo.setName(split[0]);   //原材料名称
                    rawInspectVo.setSpecifications(split[1]);  //型号规格
                    rawInspectVo.setUnit(map.get("BUY_UNIT_MEAS").toString());  //单位
                    rawInspectVo.setNumber(Integer.parseInt(String.valueOf(map.get("QTY_TO_INSPECT"))));  //数量
                    rawInspectVo.setFormTime(DateUtil.parse(map.get("APPROVED_DATE").toString())); //来料日期
                    rawInspectVo.setSupplier(map.get("SUPPLIER_NAME").toString()); //供应商
                    String orderNo = map.get("ORDER_NO").toString();//订单号
                    String releaseNo = map.get("RELEASE_NO").toString();//下达号
                    String lineNo = map.get("LINE_NO").toString();//行号
                    String lotBatchNo = map.get("LOT_BATCH_NO").toString();//批号
                    String locationNo = map.get("LOCATION_NO").toString();//库位号
                    rawInspectVo.setMessage(orderNo+","+releaseNo+","+lineNo+","+lotBatchNo+","+locationNo);
                }
            }
        }
        //查询该原材料是否有检验项目记录
        RawInspect rawInspect = rawInspectMapper.selOneByCode(code);
        if (ObjectUtils.isNotEmpty(rawInspect)) {
            List<RawInsProduct> rawInsProductList = rawInsProductMapper.selectList(Wrappers.<RawInsProduct>query().eq("raw_inspect_id", rawInspect.getId()));
            List<RawInsProductVo> rawInsProductVos = rawInsProductList.stream().map(rawInsProduct -> {
                RawInsProductVo rawInsProductVo = new RawInsProductVo();
                rawInsProductVo.setName(rawInsProduct.getName());
                rawInsProductVo.setUnit(rawInsProduct.getUnit());
                rawInsProductVo.setRequired(rawInsProduct.getRequired());
                rawInsProductVo.setInternal(rawInsProduct.getInternal());
                return rawInsProductVo;
            }).collect(Collectors.toList());
            rawInspectVo.setRawInsProducts(rawInsProductVos);
        }
        return rawInspectVo;
    }
 
    //新增原材料检验单
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addRawInspects(String userId, RawInspectVo rawInspectVo) {
        //报检人(新增检验单的人)
        rawInspectVo.setUserName(userMapper.selectById(userId).getName());
        //校验标准值,内控值格式
        List<RawInsProductVo> rawInsProductVos = rawInspectVo.getRawInsProducts();
        /*新增原材料检验单*/
        RawInspect rawInspect = new RawInspect();
        BeanUtils.copyProperties(rawInspectVo, rawInspect);
        rawInspectMapper.insert(rawInspect);
        /*新增原材料检验项目单*/
        List<RawInsProduct> rawInsProductList = rawInsProductVos.stream().map(rawInsProVo -> {
            RawInsProduct rawInsProduct = new RawInsProduct();
            BeanUtils.copyProperties(rawInsProVo, rawInsProduct);
            if (ObjectUtils.isNotEmpty(rawInsProduct.getTestValue())) {
                if (rawInsProduct.getTestValue().equals("过")) {
                    rawInsProduct.setTestState(1);
                } else if (rawInsProduct.getTestValue().equals("不过")) {
                    rawInsProduct.setTestState(0);
                } else {
                    //判断检测值是否满足标准值和内控值的要求,如果不满足则检验结论为不合格0
                    String required = rawInsProduct.getRequired();//标准值
                    String internal = rawInsProduct.getInternal();//内控值
                    String testValue = rawInsProduct.getTestValue();//检测值
                    char req = required.charAt(0);
                    List<Integer> list = Arrays.stream(testValue.split(",")).map(s -> {
                        int values = 2;
                        if (req == '>' || req == '<' || req == '=') {
                            values = checkValues(required, internal, s);
                        } else {
                            values = conValues(required, internal, s);
                        }
                        return values;
                    }).collect(Collectors.toList());
                    if (list.contains(0)) {
                        //如果其中一个检验值不合格则该项目检验不合格
                        rawInsProduct.setTestState(0);
                    } else {
                        rawInsProduct.setTestState(1);
                    }
                }
                rawInsProduct.setUserId(Integer.parseInt(userId));
            }
            rawInsProduct.setRawInspectId(rawInspect.getId());
            return rawInsProduct;
        }).collect(Collectors.toList());
        //检验项目批量添加
        rawInsProductService.saveBatch(rawInsProductList);
        return rawInspect.getId().toString();
    }
 
 
    //分页查询原材料检验单列表
    @Override
    public IPage<Map<String, Object>> selectRawInspectsList(Page<Object> page, String formTime, String code, Integer insState, String name) {
        return rawInspectMapper.selectRawInspectsList(page, formTime, code, insState, name);
    }
 
    //根据检验单id查询原材料检验单详情
    @Override
    public Map<String, Object> selectRawInspectsListById(Integer id) {
        return rawInspectMapper.selectRawInspectsListById(id);
    }
 
    //更新检验状态(上报)
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateRawInspectsById(Integer id, Integer number) {
        /*更新检验单里面的检验结论*/
        //先判断检验结果
        List<Integer> results = rawInsProductMapper.getresult(id);
        int count = 0;
        for (Integer result : results) {
            if (result != null && result == 1) {
                count++;
            }
        }
        RawInspect rawInspect = new RawInspect();
        rawInspect.setId(id);
        rawInspect.setInsState(1);
        rawInspect.setInsTime(new Date());
        //如果检验项目中的结论包含不合格则检验单不合格
        if (results.contains(0)) {
            rawInspect.setJudgeState(0);
            //更新检验单
            rawInspectMapper.updateById(rawInspect);
        } else if (count == results.size()) {
            rawInspect.setJudgeState(1);
            rawInspectMapper.updateById(rawInspect);
        } else return "项目未检验完!";
        /*如果检验结论为不合格,则需要新增不合格检验单*/
        if (rawInspect.getJudgeState() == 0) {
            //查询该检验单哪些检验项目不合格
            List<RawInsProduct> rawInsProductList = rawInsProductMapper.selectList(Wrappers.<RawInsProduct>query()
                    .eq("raw_inspect_id", id)
                    .eq("test_state", 0));
            String msg = null;
            for (RawInsProduct rawInsProduct : rawInsProductList) {
                msg+=rawInsProduct.getName();
            }
            InspectUnaccepted rawUnaccepted = InspectUnaccepted.builder()
                    .reason(msg + "不合格")  //定义为不合格的项目+不合格
                    .number(number)
                    .rawInspectId(id)
                    .type(0)        //类型为原材料
                    .build();
            inspectUnacceptedMapper.insert(rawUnaccepted);
        }
        /*检验完成后,无论合格与否,将IFS报检状态更改为已接收*/
        //根据检验单id查询原材料检验单
        RawInspect rawIns = rawInspectMapper.selectById(id);
        String[] split = rawIns.getMessage().split(",");
        BatchInfo batchInfo = new BatchInfo();
        batchInfo.setOrderNo(split[0]);
        batchInfo.setReleaseNo(split[1]);
        batchInfo.setLineNo(split[2]);
        batchInfo.setPartNo(rawIns.getCode());
        batchInfo.setLotBatchNo(split[3]);
        batchInfo.setLocationNo(split[4]);
        if (ObjectUtils.isEmpty(number)) {
            //如果不合格的数量为空null
            number=0;
        }
        batchInfo.setQtyToReceive(rawIns.getNumber()-number); //要接收的数量
        batchInfo.setQtyToInspect(rawIns.getNumber()); //要检验的数量
        batchInfo.setReceiveCase("接收到来料区");
        List<BatchInfo> batchInfos = Arrays.asList(batchInfo);
        //发送IFS请求
        String s = SyncOrder.importPolReceive(batchInfos);
        System.out.println(s);
        System.out.println("'''''''''''''''''''''''");
        return rawInspect.getJudgeState().toString();
    }
 
 
    /*判断检测值是否满足标准值和内控值的要求,如果不满足则检验结论为不合格*/
    //如果是±的操作
    private int conValues(String standardValueStr, String controlValueStr, String detectionValueStr) {
        double standVar = Double.parseDouble(standardValueStr);
        double controlVar = Double.parseDouble(controlValueStr);
        double detecVar = Double.parseDouble(detectionValueStr);
        double a = standVar + controlVar;
        double b = standVar - controlVar;
        if (detecVar >= b && detecVar <= a) {
            return 1;
        }
        return 0;
    }
 
    //如果是> , < ,=的操作
    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;
            }
        }
    }
}