XiaoRuby
2023-09-24 20956b0f05f81ca47cf6c3e8f9b3b426e9cfd035
inspect-server/src/main/java/com/yuanchu/mom/service/impl/RawInspectServiceImpl.java
@@ -1,19 +1,20 @@
package com.yuanchu.mom.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.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 com.yuanchu.mom.utils.MyUtil;
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 +26,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,145 +41,211 @@
    InspectUnacceptedMapper inspectUnacceptedMapper;
    @Resource
    DeviceService deviceService;
    UserMapper userMapper;
    @Resource
    MaterialMapper materialMapper;
    @Resource
    UserService userService;
    @Resource
    SpecificationsService specificationsService;
    @Resource
    StandardService standardService;
    @Resource
    ProductMapper productMapper;
    //新增原材料检验单-->根据原材料编码得到ifs中的报检数据
    @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;
    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()); //供应商
                }
            }
        }
        //查询该原材料是否有检验项目记录
        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 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 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,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) {
            InspectUnaccepted rawUnaccepted = InspectUnaccepted.builder()
                    .reason(rawInspectVo.getName() + "不合格")  //暂且定义为原材料不合格
                    .reason(rawInspectMapper.selectById(id).getName() + "不合格")  //暂且定义为原材料不合格
                    .number(number)
                    .rawInspectId(id)
                    .type(2)        //类型为原材料
                    .dealReasult(1)
                    .type(0)        //类型为原材料
                    .build();
            inspectUnacceptedMapper.insert(rawUnaccepted);
        }
        return true;
        return rawInspect.getJudgeState().toString();
    }
    //新增检验单
    @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 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;
        }
        //检验项目批量添加
        rawInsProductService.saveBatch(list);
        return rawInspect.getId();
        return 0;
    }
    //判断数组是否包含0
    private static boolean containsZero(Object[] array) {
        for (Object num : array) {
            if (num.equals(0)) {
                return true;
    //如果是> , < ,=的操作
    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;
            }
        }
        return false;
    }
    //判断数组是否全部为1
    private static boolean allOnes(Object[] array) {
        for (Object num : array) {
            if (!num.equals(1)) {
                return false;
            }
        }
        return true;
    }
}