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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.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;
 
    //新增原材料检验单
    @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) {
        /*更新检验单里面的检验结论*/
        //先判断检验结果
        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) {
            InspectUnaccepted rawUnaccepted = InspectUnaccepted.builder()
                    .reason(rawInspectMapper.selectById(id).getName() + "不合格")  //暂且定义为原材料不合格
                    .rawInspectId(id)
                    .type(0)        //类型为原材料
                    .build();
            inspectUnacceptedMapper.insert(rawUnaccepted);
        }
        return "上报成功!";
    }
 
 
    /*判断检测值是否满足标准值和内控值的要求,如果不满足则检验结论为不合格*/
    //如果是±的操作
    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;
            }
        }
    }
}