zss
2023-09-09 f9e06198cfbccf337b53c0fa6cb3404455ceb0ba
inspect-server/src/main/java/com/yuanchu/mom/service/impl/RawInspectServiceImpl.java
@@ -2,8 +2,10 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.mapper.*;
import com.yuanchu.mom.pojo.*;
@@ -14,6 +16,7 @@
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;
@@ -25,8 +28,7 @@
 * @createDate 2023-07-31 14:43:15
 */
@Service
public class RawInspectServiceImpl extends ServiceImpl<RawInspectMapper, RawInspect>
        implements RawInspectService {
public class RawInspectServiceImpl extends ServiceImpl<RawInspectMapper, RawInspect> implements RawInspectService {
    @Resource
    RawInspectMapper rawInspectMapper;
@@ -41,127 +43,156 @@
    InspectUnacceptedMapper inspectUnacceptedMapper;
    @Resource
    DeviceService deviceService;
    UserMapper userMapper;
    @Resource
    MaterialMapper materialMapper;
    @Resource
    UserService userService;
    @Resource
    SpecificationsService specificationsService;
    @Resource
    StandardService standardService;
    @Resource
    ProductMapper productMapper;
    //新增原材料检验单
    @Override
    public Map<String, Object> selectRawInspectsList(int pageSize, int countSize, String formTime, String createTime, int insState, int judgeState) {
        Map<String, Object> map = new HashMap<>();
        map.put("count", rawInspectMapper.selectCount(new QueryWrapper<RawInspect>().eq("state", 1)));
        map.put("data", rawInspectMapper.selectRawInspectsByLimit((pageSize - 1) * countSize, pageSize * countSize, formTime, createTime, insState, judgeState));
        return map;
    @Transactional(rollbackFor = Exception.class)
    public String addRawInspects(String userId, RawInspectVo rawInspectVo) {
        //报检人(新增检验单的人)
        rawInspectVo.setUserName(userMapper.selectById(userId).getName());
        //校验标准值,内控值格式
        List<RawInsProductVo> rawInsProductVos = rawInspectVo.getRawInsProducts();
        for (RawInsProductVo rawInsProductVo : rawInsProductVos) {
            char internal = rawInsProductVo.getInternal().charAt(0);
            char required = rawInsProductVo.getRequired().charAt(0);
            if (internal != '>' && internal != '<' && internal != '=') {
                return "内控值输入格式有问题!";
            }
            if (required != '>' && required != '<' && required != '=') {
                return "标准值输入格式有问题!";
            }
        }
        /*新增原材料检验单*/
        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())) {
                //判断检测值是否满足标准值和内控值的要求,如果不满足则检验结论为不合格0
                String required = rawInsProduct.getRequired();//标准值
                String internal = rawInsProduct.getInternal();//内控值
                String testValue = rawInsProduct.getTestValue();//检测值
                List<Integer> list = Arrays.stream(testValue.split(",")).map(s -> {
                    int values = checkValues(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 "新增检验单成功!";
    }
    //分页查询原材料检验单列表
    @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 RawInspectVo selectRawInspectsListById(Integer id) {
        //将检验单基本信息查询出来并封装到RawInspectVo对象中
        RawInspect rawInspect = rawInspectMapper.selectById(id);
        RawInspectVo rawInspectVo = new RawInspectVo();
        BeanUtils.copyProperties(rawInspect, rawInspectVo);
        //查询检验单里面的检验项目,并封装到RawInspectVo对象中
        LambdaQueryWrapper<RawInsProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RawInsProduct::getRawInspectId, id);
        List<RawInsProduct> rawInsProducts = rawInsProductMapper.selectList(queryWrapper);
        //这里查到的设备id和检验员id要查询名称
        List<RawInsProductVo> rawInsProductVos = rawInsProducts.stream().map(rawInsProduct -> {
            //将一个对象的值赋值给另一个对象
            RawInsProductVo rawInsProductVo = new RawInsProductVo();
            BeanUtils.copyProperties(rawInsProduct, rawInsProductVo);
            //获取设备名(前提是如果存在)
            if (rawInsProduct.getDeviceId() != null) {
                String deviceName = deviceService.getDeviceNameById(rawInsProduct.getDeviceId());
                rawInsProductVo.setDeviceName(deviceName);
            }
            //获取用户名(前提是如果存在)
            if (rawInsProduct.getUserId() != null) {
                String userName = userService.selectNameById(rawInsProduct.getUserId());
                rawInsProductVo.setUserName(userName);
            }
            return rawInsProductVo;
        }).collect(Collectors.toList());
        rawInspectVo.setRawInsProducts(rawInsProductVos);
        return rawInspectVo;
    public List<Map<String, Object>> selectRawInspectsListById(Integer id) {
        return rawInspectMapper.selectRawInspectsListById(id);
    }
    //更新检验状态(上报)
    @Override
    public boolean updateRawInspectsById(Integer id) {
        //更新检验单里面的检验状态和检验结论
        RawInspectVo rawInspectVo = selectRawInspectsListById(id);
        RawInspect rawInspect = RawInspect.builder()
                .id(id)
                .insState(1)
                .insTime(new Date())
                .judgeState(rawInspectVo.getJudgeState())
                .build();
        rawInspectMapper.updateById(rawInspect);
        //不管检验合格与不合格,都要新增到原材料库存
       /* //如果检验结论为不合格,则需要新增不合格检验单
        if (rawInspectVo.getJudgeState() == 0) {
    @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(rawInspectVo.getName() + "不合格")  //暂且定义为原材料不合格
                    .reason(rawInspectMapper.selectById(id).getName() + "不合格")  //暂且定义为原材料不合格
                    .rawInspectId(id)
                    .type(2)        //类型为原材料
                    .type(0)        //类型为原材料
                    .build();
            inspectUnacceptedMapper.insert(rawUnaccepted);
        }*/
        return true;
        }
        return "上报成功!";
    }
    //新增检验单
    @Override
    public Integer addRawInspects(String userName, RawInspect rawInspect) {
        rawInspect.setUserName(userName);
        //新增检验单
        rawInspectMapper.insert(rawInspect);
        //获取物料id
        Material material = materialMapper.selectOne(Wrappers.<Material>query()
                .eq("name", rawInspect.getName())
                .eq("code", rawInspect.getCode()));
        //获取规格名称和型号名称
        String specification = rawInspect.getSpecifications();
        String[] strings = specification.split("-");
        String stName = strings[0];//规格名称
        String spName = strings[1];//型号名称
        //获取规格id
        Standard standard = standardService.getOne(Wrappers.<Standard>query()
                .eq("name", stName)
                .eq("material_id", material.getId()));
        //获取型号id
        Specifications specifications = specificationsService.selectSpIdByname(standard.getId(), spName);
        //根据型号id查询项目信息
        List<Product> productList = productMapper.selectList(Wrappers.<Product>query().eq("specifications_id", specifications.getId()));
        ArrayList<RawInsProduct> list = new ArrayList<>();
        for (Product product : productList) {
            RawInsProduct rawInsProduct = RawInsProduct.builder()
                    .name(product.getName())
                    .unit(product.getUnit())
                    .required(product.getRequired())
                    .internal(product.getInternal())
                    .rawInspectId(rawInspect.getId())
                    .build();
            list.add(rawInsProduct);
    /*判断检测值是否满足标准值和内控值的要求,如果不满足则检验结论为不合格*/
    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;
        }
        //检验项目批量添加
        rawInsProductService.saveBatch(list);
        return rawInspect.getId();
    }
    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;
            }
        }
    }
}