Fixiaobai
2023-09-06 8abe275e36823f1065300af45e1f7a9a68f549a7
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
package com.yuanchu.limslaboratory.service.impl;
 
 
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuanchu.limslaboratory.mapper.*;
import com.yuanchu.limslaboratory.pojo.*;
import com.yuanchu.limslaboratory.service.PlanService;
import com.yuanchu.limslaboratory.utils.MyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 江苏鵷雏网络科技有限公司
 * @since 2023-08-09
 */
@Service
public class PlanServiceImpl implements PlanService {
 
    @Resource
    private PlanMapper planMapper;
 
    @Resource
    InspectionProductMapper inspectionProductMapper;
 
    @Resource
    UserMapper userMapper;
 
    @Resource
    InstrumentMapper instrumentMapper;
 
    @Resource
    ReportMapper reportMapper;
 
    @Resource
    InspectionMapper inspectionMapper;
 
    @Resource
    InspectionMaterialMapper inspectionMaterialMapper;
 
    @Resource
    LinkDetectionMapper linkDetectionMapper;
 
    @Resource
    private NonConformanceReviewMapper nonConformanceReviewMapper;
 
    //查询检验计划
    @Override
    public List<Map<String, Object>> selectAllPlan(String code, String beginTime, String endTime, Integer status) {
        return planMapper.selectAllPlan(code, beginTime, endTime, status);
    }
 
    //分配-->选择检验人
    @Override
    public List<Map<String, Object>> choosecheck() {
        return userMapper.selectUser();
    }
 
    //分配-->选择设备
    @Override
    public List<Map<String, Object>> chooseinstum() {
        return instrumentMapper.chooseinstum();
    }
 
    //分配人员与设备
    @Override
    public String distribution(Integer id, Integer userId, Integer instrumentId) {
        InspectionProduct inspectionProduct = new InspectionProduct();
        inspectionProduct.setId(id);
        //执行人
        inspectionProduct.setUserId(userId);
        inspectionProduct.setInstrumentId(instrumentId);
        inspectionProductMapper.updateById(inspectionProduct);
        return "分配完成!";
    }
 
    //检验
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer check(Integer id, String value) {
        InspectionProduct inspectionProduct = inspectionProductMapper.selectById(id);
        //判断检测值是否满足标准值和内控值的要求,如果不满足则检验结论为不合格0
        String required = inspectionProduct.getRequired();//标准值
        String internal = inspectionProduct.getInternal();//内控值
        inspectionProduct.setTestValue(value);
        List<Integer> list = Arrays.stream(value.split(",")).map(s -> {
            int values = checkValues(required, internal, s);
            return values;
        }).collect(Collectors.toList());
        if (list.contains(0)) {
            //如果其中一个检验值不合格则该项目检验不合格
            inspectionProduct.setTestState(0);
        }else {
            inspectionProduct.setTestState(1);
        }
        inspectionProductMapper.updateById(inspectionProduct);
        return inspectionProduct.getTestState();
    }
 
    //上报
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String reported(Integer id) {
        /*更新检验单里面的检验结论*/
        //先判断检验结果
        List<Integer> results = inspectionProductMapper.getresult(id);
        int count = 0;
        for (Integer result : results) {
            if (result != null && result == 1) {
                count++;
            }
        }
        //如果检验项目中的结论包含不合格则检验单不合格
        if (results.contains(0)) {
            Inspection inspection = new Inspection();
            inspection.setId(id);
            inspection.setInspectionStatus(0);
            //更新检验单
            inspectionMapper.updateById(inspection);
            //添加不合格信息到评审
            inspectionMapper
            NonConformanceReview nonConformanceReview = new NonConformanceReview();
            LocalDateTime localDateTime = DateUtil.toLocalDateTime(DateUtil.date());
            nonConformanceReview.setCreatedTime(localDateTime);
            nonConformanceReview.setUpdatedTime(localDateTime);
            nonConformanceReview.set
        } else if (count == results.size()) {
            Inspection inspection = new Inspection();
            inspection.setId(id);
            inspection.setInspectionStatus(1);
            inspectionMapper.updateById(inspection);
        } else return "项目未检验完!";
        //生成报告单
        Report report = new Report();
        //生成报告单号
        String recode = MyUtil.getTimeSixNumberCode("BG", "BG");
        //获取检验结论
        String conclusion = "";
        Inspection inspection = inspectionMapper.selectById(id);
        if (inspection.getInspectionStatus().equals(1)) {
            conclusion = "合格";
        } else {
            conclusion = "不合格";
        }
        report.setCode(recode);
        report.setStatus(0);
        report.setConclusion(conclusion);
        report.setInspectionId(id);
        //新增检验报告
        reportMapper.insert(report);
        return "上报成功!";
    }
 
 
    /*判断检测值是否满足标准值和内控值的要求,如果不满足则检验结论为不合格*/
    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;
            }
        }
    }
}