Fixiaobai
2023-09-06 8abe275e36823f1065300af45e1f7a9a68f549a7
inspection-server/src/main/java/com/yuanchu/limslaboratory/service/impl/PlanServiceImpl.java
@@ -1,13 +1,24 @@
package com.yuanchu.limslaboratory.service.impl;
import com.yuanchu.limslaboratory.mapper.PlanMapper;
import com.yuanchu.limslaboratory.pojo.vo.PlanVo;
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>
@@ -23,39 +34,182 @@
    @Resource
    private PlanMapper planMapper;
    /**
     * 查询检验计划
     *
     * @return
     */
    @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<PlanVo> selectAllPlan(Integer deviceId, Date beginTime, Date endTime, Integer userId) {
        //获取数据库数据
        List<PlanVo> planVos = planMapper.selectAllPlan(deviceId, beginTime, endTime, userId);
        //添加计划工期和检验进度
        planVos.forEach(planVo -> {
            //添加检验进度
            //判断是否是已完成
            if (planVo.getState() != null) {
                planVo.setProgress(100);
    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 (planVo.getCheckproject() == null) {
                planVo.setProgress(0);
        }
        //如果检验项目中的结论包含不合格则检验单不合格
        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;
            }
            //判断是否是进行中
            if (planVo.getState() == null && planVo.getCheckproject() != null) {
                planVo.setProgress(50);
        } 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;
            }
            //添加计划工期
            if (planVo.getFinishtime() != null && planVo.getStarttime() != null) {
                long startTimeInMillis = planVo.getStarttime().getTime();
                long endTimeInMillis = planVo.getFinishtime().getTime();
                long durationInMillis = endTimeInMillis - startTimeInMillis;
                long duration = durationInMillis / (1000 * 60 * 60);
                planVo.setDuration(Integer.valueOf((int) duration));
            }
        });
        return planVos;
        }
    }
}