package com.yuanchu.mom.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.yuanchu.mom.common.GetLook; import com.yuanchu.mom.exception.ErrorException; import com.yuanchu.mom.mapper.*; import com.yuanchu.mom.pojo.*; import com.yuanchu.mom.service.InsOrderPlanService; import com.yuanchu.mom.service.PkMasterService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.yuanchu.mom.vo.ProductVo; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; import java.time.LocalDateTime; import java.util.*; /** *

* 服务实现类 *

* * @author 江苏鵷雏网络科技有限公司 * @since 2024-07-29 01:16:26 */ @Service public class PkMasterServiceImpl extends ServiceImpl implements PkMasterService { @Resource private PkMasterMapper pkMasterMapper; @Resource private PkSlaveMapper pkSlaveMapper; @Resource private InsOrderMapper insOrderMapper; @Resource private InsSampleMapper insSampleMapper; @Resource private InsProductMapper insProductMapper; @Resource private InsProductResultMapper insProductResultMapper; @Resource private InsProductUserMapper insProductUserMapper; @Resource private UserMapper userMapper; @Resource private InsOrderPlanService insOrderPlanService; @Resource GetLook getLook; //温度循环数采 @Override @Transactional(rollbackFor = Exception.class) public int temDataAcquisition(PkMaster pkMaster) { Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId"); //查询数采关联的子表信息 PkMaster master = pkMasterMapper.selectOne(Wrappers.lambdaQuery() .eq(PkMaster::getEntrustCode, pkMaster.getEntrustCode()) .eq(PkMaster::getSampleCode, pkMaster.getSampleCode()) .eq(PkMaster::getModel, pkMaster.getModel()) .eq(PkMaster::getTemperature, pkMaster.getTemperature())); if (ObjectUtils.isEmpty(master)) { throw new ErrorException("没有找到对应委托单" + pkMaster.getEntrustCode() + "的数采数据"); } List pkSlaves = pkSlaveMapper.selectList1(master.getIsid()); if (CollectionUtils.isEmpty(pkSlaves)) { throw new ErrorException("没有找到对应委托单" + pkMaster.getEntrustCode() + "的数采子数据"); } //查询对应的检验单表 InsOrder insOrder = insOrderMapper.selectOne(Wrappers.lambdaQuery().eq(InsOrder::getState, 1).eq(InsOrder::getEntrustCode, pkMaster.getEntrustCode())); if (ObjectUtils.isEmpty(insOrder)) { throw new ErrorException("没有找到对应委托单" + pkMaster.getEntrustCode()); } //查询对应检验样品id InsSample insSample = insSampleMapper.selectOne(Wrappers.lambdaQuery() .eq(InsSample::getInsOrderId, insOrder.getId()) .eq(InsSample::getSampleCode, pkMaster.getSampleCode()) .eq(InsSample::getModel, pkMaster.getModel())); if (ObjectUtils.isEmpty(insSample)) { throw new ErrorException("没有找到对应委托单下的该样品" + pkMaster.getSampleCode()); } //查询对应检验项目 Map map = insOrderPlanService.temCycle(insSample.getId(), pkMaster.getCycles(), pkMaster.getTemperature()); List productVos = (List) map.get("productVos"); if (CollectionUtils.isEmpty(map)) { throw new ErrorException("没有找到对应委托单下该样品的相关温度循环检验项目的循环" + pkMaster.getCycles() + "次+温度" + pkMaster.getTemperature()); } //todo 如果温度循环的项目nm有改动这里需要改动 @zss for (PkSlave pkSlave : pkSlaves) { for (ProductVo productVo : productVos) { if (pkSlave.getInsBushingId().equals(productVo.getBushColor()) && pkSlave.getInsFibersId().equals(productVo.getCode()) && pkSlave.getInsFiberId().equals(productVo.getColor())) { //找到对应的数据(对应套管光纤的数据) List results = insProductResultMapper.selectList(Wrappers.lambdaQuery().eq(InsProductResult::getInsProductId, productVo.getInsProduct().getId())); InsProductResult result; if (CollectionUtils.isEmpty(results)) { result = new InsProductResult(); } else { result = results.get(0); } result.setInsProductId(productVo.getInsProduct().getId());//检验项目id List> iv = new ArrayList<>(); List> cv = new ArrayList<>(); if (productVo.getInsProduct().getInspectionItemClass().equals("1310nm")) { //1310nm外端 Map map1 = new HashMap<>(); map1.put("u", userId + ""); map1.put("v", pkSlave.getWeaken1310A().toString()); iv.add(map1); //1310nm内端 Map map2 = new HashMap<>(); map2.put("u", userId + ""); map2.put("v", pkSlave.getWeaken1310B().toString()); iv.add(map2); //1310nm衰减系数 Map map3 = new HashMap<>(); double v = (pkSlave.getWeaken1310A() + pkSlave.getWeaken1310B()) / 2; BigDecimal decimal = BigDecimal.valueOf(v).setScale(3, RoundingMode.HALF_UP); map3.put("v", String.valueOf(decimal)); cv.add(map3); } else if (productVo.getInsProduct().getInspectionItemClass().equals("1550nm")) { //1550nm外端 Map map1 = new HashMap<>(); map1.put("u", userId + ""); map1.put("v", pkSlave.getWeaken1550A().toString()); iv.add(map1); //1550nm内端 Map map2 = new HashMap<>(); map2.put("u", userId + ""); map2.put("v", pkSlave.getWeaken1550B().toString()); iv.add(map2); //1550nm衰减系数 Map map3 = new HashMap<>(); double v = (pkSlave.getWeaken1550A() + pkSlave.getWeaken1550B()) / 2; BigDecimal decimal = BigDecimal.valueOf(v).setScale(3, RoundingMode.HALF_UP); map3.put("v", String.valueOf(decimal)); cv.add(map3); } else if (productVo.getInsProduct().getInspectionItemClass().equals("1625nm")) { //1625nm外端 Map map1 = new HashMap<>(); map1.put("u", userId + ""); map1.put("v", pkSlave.getWeaken1625A().toString()); iv.add(map1); //1625nm内端 Map map2 = new HashMap<>(); map2.put("u", userId + ""); map2.put("v", pkSlave.getWeaken1625B().toString()); iv.add(map2); //1625nm衰减系数 Map map3 = new HashMap<>(); double v = (pkSlave.getWeaken1625A() + pkSlave.getWeaken1625B()) / 2; BigDecimal decimal = BigDecimal.valueOf(v).setScale(3, RoundingMode.HALF_UP); map3.put("v", String.valueOf(decimal)); cv.add(map3); } else if (productVo.getInsProduct().getInspectionItemClass().equals("1383nm")) { //1383nm外端 Map map1 = new HashMap<>(); map1.put("u", userId + ""); map1.put("v", pkSlave.getWeaken1383A().toString()); iv.add(map1); //1383nm内端 Map map2 = new HashMap<>(); map2.put("u", userId + ""); map2.put("v", pkSlave.getWeaken1383B().toString()); iv.add(map2); //1383nm衰减系数 Map map3 = new HashMap<>(); double v = (pkSlave.getWeaken1383A() + pkSlave.getWeaken1383B()) / 2; BigDecimal decimal = BigDecimal.valueOf(v).setScale(3, RoundingMode.HALF_UP); map3.put("v", String.valueOf(decimal)); cv.add(map3); } else if (productVo.getInsProduct().getInspectionItemClass().equals("1490nm")) { //1490nm外端 Map map1 = new HashMap<>(); map1.put("u", userId + ""); map1.put("v", pkSlave.getWeaken1490A().toString()); iv.add(map1); //1490nm内端 Map map2 = new HashMap<>(); map2.put("u", userId + ""); map2.put("v", pkSlave.getWeaken1490B().toString()); iv.add(map2); //1490nm衰减系数 Map map3 = new HashMap<>(); double v = (pkSlave.getWeaken1490A() + pkSlave.getWeaken1490B()) / 2; BigDecimal decimal = BigDecimal.valueOf(v).setScale(3, RoundingMode.HALF_UP); map3.put("v", String.valueOf(decimal)); cv.add(map3); } else { throw new ErrorException("没有找到该温度的检验项目" + productVo.getInsProduct().getInspectionItemClass()); } result.setInsValue(JSON.toJSONString(iv));//检验值 result.setComValue(JSON.toJSONString(cv));//计算值 if (BeanUtil.isEmpty(result.getId())) { result.setCreateUser(userId); result.setUpdateUser(userId); insProductResultMapper.insert(result); } else { result.setUpdateUser(userId); result.setUpdateTime(LocalDateTime.now()); insProductResultMapper.updateById(result); } InsProduct insProduct = productVo.getInsProduct(); InsProductResult insProductResult = insProductResultMapper.selectOne(Wrappers.lambdaQuery().eq(InsProductResult::getInsProductId, insProduct.getId())); String comple = insProductResult.getComValue().split(":")[1].split("\"")[1]; //首先查看温度是否是20度常温 if (!pkMaster.getTemperature().equals("20℃(常温)")) { //如果不是先查询是否有值 int a = insProductMapper.selectList(Wrappers.lambdaQuery() .eq(InsProduct::getInsSampleId, insSample.getId()) .eq(InsProduct::getInspectionItem, "1") .eq(InsProduct::getInspectionItemSubclass, "20℃(常温)")).stream().map(InsProduct::getInsResult).anyMatch(ObjectUtils::isEmpty) ? 1 : 0; if (a == 1) { throw new ErrorException("先获取20℃(常温)的检验值,再获取其他温度" + pkMaster.getSampleCode()); } else { //计算衰减差 InsProduct insPro = null; switch (insProduct.getInspectionItemClass()) { case "1310nm": //查询20℃(常温)1310nm的衰减系数 try { insPro = insProductMapper.selectOne(Wrappers.lambdaQuery() .eq(InsProduct::getInsSampleId, insSample.getId()) .eq(InsProduct::getInspectionItem, "1") .eq(InsProduct::getInspectionItemSubclass, "20℃(常温)") .eq(InsProduct::getInspectionItemClass, "1310nm") .eq(InsProduct::getInsFiberId, productVo.getInsProduct().getInsFiberId())); } catch (Exception e) { throw new ErrorException("没有查到20℃(常温),1310nm项目" + productVo.getInsProduct().getInsFiberId() + "光纤的检验项目"); } break; case "1550nm": //查询20℃(常温)1550nm的衰减系数 try { insPro = insProductMapper.selectOne(Wrappers.lambdaQuery() .eq(InsProduct::getInsSampleId, insSample.getId()) .eq(InsProduct::getInspectionItem, "1") .eq(InsProduct::getInspectionItemSubclass, "20℃(常温)") .eq(InsProduct::getInspectionItemClass, "1550nm") .eq(InsProduct::getInsFiberId, productVo.getInsProduct().getInsFiberId())); } catch (Exception e) { throw new ErrorException("没有查到20℃(常温),1625nm项目" + productVo.getInsProduct().getInsFiberId() + "光纤的检验项目"); } break; case "1625nm": //查询20℃(常温)1625nm的衰减系数 try { insPro = insProductMapper.selectOne(Wrappers.lambdaQuery() .eq(InsProduct::getInsSampleId, insSample.getId()) .eq(InsProduct::getInspectionItem, "1") .eq(InsProduct::getInspectionItemSubclass, "20℃(常温)") .eq(InsProduct::getInspectionItemClass, "1625nm") .eq(InsProduct::getInsFiberId, productVo.getInsProduct().getInsFiberId())); } catch (Exception e) { throw new ErrorException("没有查到20℃(常温),1625nm项目" + productVo.getInsProduct().getInsFiberId() + "光纤的检验项目"); } break; case "1383nm": //查询20℃(常温)1383nm的衰减系数 try { insPro = insProductMapper.selectOne(Wrappers.lambdaQuery() .eq(InsProduct::getInsSampleId, insSample.getId()) .eq(InsProduct::getInspectionItem, "1") .eq(InsProduct::getInspectionItemSubclass, "20℃(常温)") .eq(InsProduct::getInspectionItemClass, "1383nm") .eq(InsProduct::getInsFiberId, productVo.getInsProduct().getInsFiberId())); } catch (Exception e) { throw new ErrorException("没有查到20℃(常温),1383nm项目" + productVo.getInsProduct().getInsFiberId() + "光纤的检验项目"); } break; case "1490nm": //查询20℃(常温)1490nm的衰减系数 try { insPro = insProductMapper.selectOne(Wrappers.lambdaQuery() .eq(InsProduct::getInsSampleId, insSample.getId()) .eq(InsProduct::getInspectionItem, "1") .eq(InsProduct::getInspectionItemSubclass, "20℃(常温)") .eq(InsProduct::getInspectionItemClass, "1490nm") .eq(InsProduct::getInsFiberId, productVo.getInsProduct().getInsFiberId())); } catch (Exception e) { throw new ErrorException("没有查到20℃(常温),1490nm项目" + productVo.getInsProduct().getInsFiberId() + "光纤的检验项目"); } break; } InsProductResult insProductRes = insProductResultMapper.selectOne(Wrappers.lambdaQuery().eq(InsProductResult::getInsProductId, insPro.getId())); double abs = Math.abs(Double.parseDouble(insProductRes.getComValue().split(":")[1].split("\"")[1]) - Double.parseDouble(comple)); BigDecimal scale = BigDecimal.valueOf(abs).setScale(3, RoundingMode.HALF_UP); insProduct.setLastValue(String.valueOf(scale)); String ask = insProduct.getAsk(); int insResult = 1; switch (ask.charAt(0)) { case '<': insResult = Double.parseDouble(insProduct.getLastValue()) < Double.parseDouble(ask.substring(1)) ? 1 : 0; break; case '>': insResult = Double.parseDouble(insProduct.getLastValue()) > Double.parseDouble(ask.substring(1)) ? 1 : 0; break; case '=': insResult = Double.parseDouble(insProduct.getLastValue()) == Double.parseDouble(ask.substring(1)) ? 1 : 0; break; case '≤': insResult = Double.parseDouble(insProduct.getLastValue()) <= Double.parseDouble(ask.substring(1)) ? 1 : 0; break; case '≥': insResult = Double.parseDouble(insProduct.getLastValue()) >= Double.parseDouble(ask.substring(1)) ? 1 : 0; break; } insProduct.setInsResult(insResult); } } else { //如果是20度常温,需要给出合格的结论 insProduct.setInsResult(1);//20度常温的检验项默认为合格 } insProduct.setUpdateUser(userId); insProductMapper.updateById(insProduct); User user = userMapper.selectOne(Wrappers.lambdaQuery().eq(User::getAccount, master.getDetectionPeople())); if (ObjectUtils.isEmpty(user)) { throw new ErrorException("lims系统没有找到该用户" + master.getDetectionPeople()); } insProductUserMapper.insert(new InsProductUser(null, user.getId(), master.getDetectionDate(), insProduct.getId())); } } } //给温度循环的检验项目绑定设备 InsProduct product = insProductMapper.selectOne(Wrappers.lambdaQuery() .eq(InsProduct::getState, 1) .eq(InsProduct::getInsSampleId, insSample.getId()) .eq(InsProduct::getInspectionItem, "温度循环")); if (ObjectUtils.isEmpty(map)) { throw new ErrorException("没有找到对应委托单下该样品的相关温度循环检验项目"); } InsProductResult insProductResult = insProductResultMapper.selectOne(Wrappers.lambdaQuery().eq(InsProductResult::getInsProductId, product.getId())); if(ObjectUtils.isEmpty(insProductResult.getEquipValue())) { List> dv = new ArrayList<>(); Map map4 = new HashMap<>(); map4.put("v", "JCZX-TX-TT02006"); dv.add(map4); Map map5 = new HashMap<>(); map5.put("v", "JCZX-TX-TT02001"); dv.add(map5); insProductResult.setEquipValue(JSON.toJSONString(dv)); List> dn = new ArrayList<>(); Map map6 = new HashMap<>(); map6.put("v", "步入式高低温湿热试验箱"); dn.add(map6); Map map7 = new HashMap<>(); map7.put("v", "步入式高低温试验箱"); dn.add(map7); insProductResult.setEquipName(JSON.toJSONString(dn)); insProductResultMapper.updateById(insProductResult); } return 0; } //温度循环数采总体 @Override @Transactional(rollbackFor = Exception.class) public int temDataAcquisition2(PkMaster pkMaster) { return 0; } }