package com.yuanchu.mom.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.yuanchu.mom.mapper.*; import com.yuanchu.mom.pojo.*; import com.yuanchu.mom.pojo.vo.FinishedInspectVo; import com.yuanchu.mom.service.*; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; /** *

* 服务实现类 *

* * @author 江苏鵷雏网络科技有限公司 * @since 2023-08-01 */ @Service public class FinishedInspectServiceImpl extends ServiceImpl implements FinishedInspectService { @Resource FinishedInspectMapper finishedInspectMapper; @Resource MaterialMapper materialMapper; @Resource StandardService standardService; @Resource SpecificationsService specificationsService; @Resource ManualTechnologyMapper manualTechnologyMapper; @Resource ProductService productService; @Resource InspectionItemService inspectionItemService; @Resource InspectionItemMapper inspectionItemMapper; @Resource InspectUnacceptedMapper inspectUnacceptedMapper; @Resource RepertoryMapper repertoryMapper; //新增检验单-->根据订单号选择产品信息 @Override public List> chooseMater(String orderNumber) { return finishedInspectMapper.chooseMater(orderNumber); } //新增成品检验单 @Override @Transactional(rollbackFor = Exception.class) public Integer addProcessInspectionSheet(String userId, FinishedInspectVo finishedInspectVo) { /*新增成品检验单*/ FinishedInspect finishedInspect = new FinishedInspect(); finishedInspect.setUserId(Integer.parseInt(userId)); finishedInspect.setOrderNumber(finishedInspectVo.getOrderNumber()); finishedInspect.setCustomerName(finishedInspectVo.getPrname()); finishedInspect.setProjectName(finishedInspectVo.getSname()); finishedInspect.setQualityTraceability(finishedInspectVo.getQualityTraceability()); finishedInspect.setUnit(finishedInspectVo.getUnit()); finishedInspect.setQuantity(finishedInspectVo.getQuantity()); finishedInspect.setSpecificationsModel(finishedInspectVo.getSpecificationsModel()); finishedInspect.setMaterial(finishedInspectVo.getMaterial()); finishedInspect.setMaterialCode(finishedInspectVo.getMcode()); finishedInspectMapper.insert(finishedInspect); /*批量新增成品检验项目单*/ //获取型号id Integer specificationId = getSpecificationId(finishedInspectVo.getMaterial(), finishedInspectVo.getMcode(), finishedInspectVo.getSpecificationsModel()); //根据生产订单id查询编制工序的最后一道工艺 List manualTechnologyList = manualTechnologyMapper.selAllByMoId(finishedInspectVo.getId()); //获取最后一道工艺关联的工艺路线id Integer technologyId = manualTechnologyList.get(0).getTechnologyId(); //查询标准BOM技术指标中该型号工艺下最新版本的检验项目 Integer ver = productService.selectVerByPro(specificationId).get(0);//该型号下技术指标最新版本 List productList = productService.selProByVerSpe(technologyId, ver); List inspectionItemList = productList.stream().map(product -> { InspectionItem inspectionItem = new InspectionItem(); BeanUtils.copyProperties(product, inspectionItem); inspectionItem.setInspectId(finishedInspect.getId()); inspectionItem.setType(2);//成品 return inspectionItem; }).collect(Collectors.toList()); inspectionItemService.saveBatch(inspectionItemList); return finishedInspect.getId(); } //上报(更新检验状态) @Override @Transactional(rollbackFor = Exception.class) public String updateFinishInspectsById(String username,Integer id) { /*更新检验单里面的检验结论*/ //先判断检验结果 List results = inspectionItemMapper.getResult(id,1); int count = 0; for (Integer result : results) { if (result != null && result == 1) { count++; } } FinishedInspect finishedInspect = finishedInspectMapper.selectById(id); //如果检验项目中的结论包含不合格则检验单不合格 if (results.contains(0)) { finishedInspect.setResult(0);//不合格 //更新检验单 finishedInspectMapper.updateById(finishedInspect); } else if (count == results.size()) { finishedInspect.setResult(1);//合格 finishedInspectMapper.updateById(finishedInspect); } else return "项目未检验完!"; /*如果检验结论为不合格,则需要新增不合格检验单*/ if (finishedInspect.getResult() == 0) { InspectUnaccepted finishUnaccepted = InspectUnaccepted.builder() .reason(finishedInspect.getMaterial() + "不合格") //暂且定义为产品名称不合格 .rawInspectId(id) .type(1) //类型为成品检验 .build(); inspectUnacceptedMapper.insert(finishUnaccepted); } /*如果检验结论为合格,则需要新增半成品库存*/ if (finishedInspect.getResult() == 1) { //如果入库的信息一样只有库存不一样,则在原来的库存数量上加上相应的数量 LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(Repertory::getOrderCode, finishedInspect.getOrderNumber()) .eq(Repertory::getQualityTraceability, finishedInspect.getQualityTraceability()) .eq(Repertory::getName, finishedInspect.getMaterial()) .eq(Repertory::getSpecifications, finishedInspect.getSpecificationsModel()) .eq(Repertory::getUnit, finishedInspect.getUnit()) .eq(Repertory::getType, 1); Repertory rep = repertoryMapper.selectOne(queryWrapper); if (rep != null ) { rep.setNumber(rep.getNumber() + finishedInspect.getQuantity()); rep.setUserName(username); repertoryMapper.updateById(rep); } else { //如果除了库存别的信息有任何一个不一样,则新增一条成品库存 Repertory repertory = new Repertory(); repertory.setOrderCode(finishedInspect.getOrderNumber()); repertory.setQualityTraceability(finishedInspect.getQualityTraceability()); repertory.setName(finishedInspect.getMaterial()); repertory.setSpecifications(finishedInspect.getSpecificationsModel()); repertory.setUnit(finishedInspect.getUnit()); repertory.setNumber(finishedInspect.getQuantity()); repertory.setUserName(username); repertory.setType(1);//半成品 repertory.setCheckResult(1);//暂定不删 repertoryMapper.insert(repertory); } } return "上报成功!"; } //分页查询成品检验单列表 @Override public IPage> selectFinishedInspectPage(Page page, Integer result, String material) { return finishedInspectMapper.selectFinishedInspectPage(page, result,material); } //根据检验单id查询成品检验单详情 @Override public List> selectFinishInspectsListById(Integer id) { return finishedInspectMapper.selectFinishInspectsListById(id); } /*根据样品名称,样品编号,型号规格获取型号id*/ private Integer getSpecificationId(String name, String mcode, String specification) { //获取物料id Material material = materialMapper.selectOne(Wrappers.query() .eq("name", name) .eq("code", mcode)); if (Objects.isNull(material)) { return null; } //获取规格名称和型号名称 String[] split = specification.split("-"); String stName = split[0]; String spName = split[1]; //获取规格id Standard standard = standardService.getOne(Wrappers.query() .eq("name", stName) .eq("material_id", material.getId())); //获取型号id Specifications specifications = specificationsService.getOne(Wrappers.query() .eq("name", spName) .eq("standard_id", standard.getId())); return specifications.getId(); } /*判断检测值是否满足标准值和内控值的要求,如果不满足则检验结论为不合格*/ 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; } } } }