package com.ruoyi.require.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.ruoyi.basic.vo.StandardProductVO; import com.ruoyi.common.core.domain.entity.User; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.framework.exception.ErrorException; import com.ruoyi.inspect.mapper.*; import com.ruoyi.inspect.pojo.*; import com.ruoyi.inspect.service.InsOrderPlanService; import com.ruoyi.inspect.service.impl.InsOrderPlanServiceImpl; import com.ruoyi.inspect.vo.ProductVo; import com.ruoyi.performance.mapper.AuxiliaryOutputWorkingHoursMapper; import com.ruoyi.performance.mapper.PerformanceShiftMapper; import com.ruoyi.performance.mapper.ShiftTimeMapper; import com.ruoyi.performance.pojo.AuxiliaryOutputWorkingHours; import com.ruoyi.performance.pojo.PerformanceShift; import com.ruoyi.performance.pojo.ShiftTime; import com.ruoyi.require.mapper.PkMasterMapper; import com.ruoyi.require.mapper.PkSlaveMapper; import com.ruoyi.require.pojo.PkMaster; import com.ruoyi.require.pojo.PkSlave; import com.ruoyi.require.service.PkMasterService; import com.ruoyi.system.mapper.UserMapper; import lombok.extern.slf4j.Slf4j; import org.apache.logging.log4j.util.Strings; 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.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; /** *

* 服务实现类 *

* * @author 江苏鵷雏网络科技有限公司 * @since 2024-07-29 01:16:26 */ @Service @Slf4j 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 private InsOrderPlanServiceImpl insOrderPlanServiceImpl; @Resource private ShiftTimeMapper shiftTimeMapper; @Resource private PerformanceShiftMapper performanceShiftMapper; @Resource private AuxiliaryOutputWorkingHoursMapper auxiliaryOutputWorkingHoursMapper; //温度循环数采 @Override @Transactional(rollbackFor = Exception.class) public int temDataAcquisition(PkMaster pkMaster) { Integer userId = SecurityUtils.getUserId().intValue(); //查询数采关联的子表信息 PkMaster master = new PkMaster(); List pkMasters = pkMasterMapper.selectList(Wrappers.lambdaQuery() .eq(PkMaster::getEntrustCode, pkMaster.getEntrustCode()) .eq(PkMaster::getSampleCode, pkMaster.getSampleCode()) .eq(PkMaster::getModel, pkMaster.getModel()) .eq(PkMaster::getCycles, pkMaster.getCycles()) .eq(PkMaster::getTemperature, pkMaster.getTemperature()).orderByDesc(PkMaster::getId)); if(!pkMasters.isEmpty()){ master = pkMasters.get(0); } //查询对应的检验单表 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()); } if (ObjectUtils.isNotEmpty(master)) { //throw new ErrorException("没有找到对应委托单" + pkMaster.getEntrustCode() + "的数采数据"); List pkSlaves = pkSlaveMapper.selectList1(master.getIsid()); if (CollectionUtils.isEmpty(pkSlaves)) { throw new ErrorException("没有找到对应委托单" + pkMaster.getEntrustCode() + "的数采子数据"); } //查询对应检验项目 Map map = insOrderPlanService.temCycle(insSample.getId(), pkMaster.getCycles(), pkMaster.getTemperature()); List productVos = (List) map.get("productVos"); if (CollectionUtils.isNotEmpty(productVos)) { //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", ObjectUtils.isNotEmpty(pkSlave.getWeaken1310A()) ? pkSlave.getWeaken1310A().toString() : null); iv.add(map1); //1310nm内端 Map map2 = new HashMap<>(); map2.put("u", userId + ""); map2.put("v", ObjectUtils.isNotEmpty(pkSlave.getWeaken1310B()) ? pkSlave.getWeaken1310B().toString() : null); iv.add(map2); //1310nm衰减系数 Map map3 = new HashMap<>(); double v = 0.0; if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1310A()) && ObjectUtils.isNotEmpty(pkSlave.getWeaken1310B())) { v = (pkSlave.getWeaken1310A() + pkSlave.getWeaken1310B()) / 2; } else if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1310A()) && ObjectUtils.isEmpty(pkSlave.getWeaken1310B())) { v = pkSlave.getWeaken1310A(); } else if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1310B()) && ObjectUtils.isEmpty(pkSlave.getWeaken1310A())) { v = pkSlave.getWeaken1310B(); } else { log.info("数采id:{}", pkSlave.getId()); throw new ErrorException("1310的两端都没有数据!!!"); } 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", ObjectUtils.isNotEmpty(pkSlave.getWeaken1550A()) ? pkSlave.getWeaken1550A().toString() : null); iv.add(map1); //1550nm内端 Map map2 = new HashMap<>(); map2.put("u", userId + ""); map2.put("v", ObjectUtils.isNotEmpty(pkSlave.getWeaken1550B()) ? pkSlave.getWeaken1550B().toString() : null); iv.add(map2); //1550nm衰减系数 Map map3 = new HashMap<>(); double v = 0.0; if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1550A()) && ObjectUtils.isNotEmpty(pkSlave.getWeaken1550B())) { v = (pkSlave.getWeaken1550A() + pkSlave.getWeaken1550B()) / 2; } else if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1550A()) && ObjectUtils.isEmpty(pkSlave.getWeaken1550B())) { v = pkSlave.getWeaken1550A(); } else if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1550B()) && ObjectUtils.isEmpty(pkSlave.getWeaken1550A())) { v = pkSlave.getWeaken1550B(); } else { throw new ErrorException("1550的两端都没有数据!!!"); } 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", ObjectUtils.isNotEmpty(pkSlave.getWeaken1625A()) ? pkSlave.getWeaken1625A().toString() : null); iv.add(map1); //1625nm内端 Map map2 = new HashMap<>(); map2.put("u", userId + ""); map2.put("v", ObjectUtils.isNotEmpty(pkSlave.getWeaken1625B()) ? pkSlave.getWeaken1625B().toString() : null); iv.add(map2); //1625nm衰减系数 Map map3 = new HashMap<>(); double v = 0.0; if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1625A()) && ObjectUtils.isNotEmpty(pkSlave.getWeaken1625B())) { v = (pkSlave.getWeaken1625A() + pkSlave.getWeaken1625B()) / 2; } else if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1625A()) && ObjectUtils.isEmpty(pkSlave.getWeaken1625B())) { v = pkSlave.getWeaken1625A(); } else if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1625B()) && ObjectUtils.isEmpty(pkSlave.getWeaken1625A())) { v = pkSlave.getWeaken1625B(); } else { throw new ErrorException("1625的两端都没有数据!!!"); } 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", ObjectUtils.isNotEmpty(pkSlave.getWeaken1383A()) ? pkSlave.getWeaken1383A().toString() : null); iv.add(map1); //1383nm内端 Map map2 = new HashMap<>(); map2.put("u", userId + ""); map2.put("v", ObjectUtils.isNotEmpty(pkSlave.getWeaken1383B()) ? pkSlave.getWeaken1383B().toString() : null); iv.add(map2); //1383nm衰减系数 Map map3 = new HashMap<>(); double v = 0.0; if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1383A()) && ObjectUtils.isNotEmpty(pkSlave.getWeaken1383B())) { v = (pkSlave.getWeaken1383A() + pkSlave.getWeaken1310B()) / 2; } else if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1383A()) && ObjectUtils.isEmpty(pkSlave.getWeaken1383B())) { v = pkSlave.getWeaken1383A(); } else if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1383B()) && ObjectUtils.isEmpty(pkSlave.getWeaken1383A())) { v = pkSlave.getWeaken1383B(); } else { throw new ErrorException("1383的两端都没有数据!!!"); } 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", ObjectUtils.isNotEmpty(pkSlave.getWeaken1490A()) ? pkSlave.getWeaken1490A().toString() : null); iv.add(map1); //1490nm内端 Map map2 = new HashMap<>(); map2.put("u", userId + ""); map2.put("v", ObjectUtils.isNotEmpty(pkSlave.getWeaken1490B()) ? pkSlave.getWeaken1490B().toString() : null); iv.add(map2); //1490nm衰减系数 Map map3 = new HashMap<>(); double v = 0.0; if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1490A()) && ObjectUtils.isNotEmpty(pkSlave.getWeaken1490B())) { v = (pkSlave.getWeaken1490A() + pkSlave.getWeaken1310B()) / 2; } else if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1490A()) && ObjectUtils.isEmpty(pkSlave.getWeaken1490B())) { v = pkSlave.getWeaken1490A(); } else if (ObjectUtils.isNotEmpty(pkSlave.getWeaken1490B()) && ObjectUtils.isEmpty(pkSlave.getWeaken1490A())) { v = pkSlave.getWeaken1490B(); } else { throw new ErrorException("1490的两端都没有数据!!!"); } 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[] comValue = insProductResult.getComValue().split(":"); String comple = comValue[comValue.length-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())); String[] split = insProductRes.getComValue().split(":"); double abs = Math.abs(Double.parseDouble(split[split.length-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(product)) { throw new ErrorException("没有找到对应委托单下该样品的相关温度循环检验项目"); } InsProductResult insProductResult = insProductResultMapper.selectOne(Wrappers.lambdaQuery().eq(InsProductResult::getInsProductId, product.getId())); if (ObjectUtils.isEmpty(insProductResult)) { insProductResult = new InsProductResult(); insProductResult.setInsProductId(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)); if (BeanUtil.isEmpty(insProductResult.getId())) { insProductResult.setCreateUser(userId); insProductResult.setUpdateUser(userId); insProductResultMapper.insert(insProductResult); } else { insProductResult.setUpdateUser(userId); insProductResult.setUpdateTime(LocalDateTime.now()); insProductResultMapper.updateById(insProductResult); } } } //查找样品下该次数的温度的项目 List insProducts = insProductMapper.selectList(Wrappers.lambdaQuery() .eq(InsProduct::getInsSampleId, insSample.getId()) .eq(InsProduct::getState, 1) .isNotNull(InsProduct::getSpecialItemParentId) .isNotNull(InsProduct::getInspectionItemClass)); // 过滤出当前循环次数 和 温度的检验项目 List productList = insProducts.stream() .filter(item -> item.getInspectionItem().equals(pkMaster.getCycles()) && item.getInspectionItemSubclass().equals(pkMaster.getTemperature())) .collect(Collectors.toList()); // 工时添加 int count = 0; for(InsProduct insProduct1 : productList) { // 查询insProductResult拿到insValue的有值的个数 InsProductResult insProductResult = insProductResultMapper.selectOne(new LambdaQueryWrapper() .eq(InsProductResult::getInsProductId, insProduct1.getId())); if(!Objects.isNull(insProductResult)) { // insProductResult不为空,获取有值的个数 insProductResult.getInsValue(); List maps = JSONArray.parseArray(insProductResult.getInsValue(), Map.class); for (Map map : maps) { if (Strings.isNotEmpty(map.get("v").toString())) { count++; break; } } } } // 该单子下除了当前次数以及温度有值的记录 List productList1 = insProducts.stream() .filter(item -> !(item.getInspectionItem().equals(pkMaster.getCycles()) && item.getInspectionItemSubclass().equals(pkMaster.getTemperature()))) .collect(Collectors.toList()); int allValue = 0; for(InsProduct insProduct1 : productList1) { // 查询insProductResult拿到insValue的有值的个数 InsProductResult insProductResult = insProductResultMapper.selectOne(new LambdaQueryWrapper() .eq(InsProductResult::getInsProductId, insProduct1.getId())); if(!Objects.isNull(insProductResult)) { // insProductResult不为空,获取有值的个数 insProductResult.getInsValue(); List maps = JSONArray.parseArray(insProductResult.getInsValue(), Map.class); for (Map map : maps) { if (Strings.isNotEmpty(map.get("v").toString())) { allValue++; break; } } } } Map map = isOvertimeAndCrossDay(userId); Boolean isWithinRange = map.get("isWithinRange"); // 是否正常上班 Boolean isCross = map.get("isCross"); // 是否跨天 BigDecimal manHour = BigDecimal.ZERO; // 工时 InsProduct insProduct = insProductMapper.selectOne(new LambdaQueryWrapper().eq(InsProduct::getInsSampleId, insSample.getId()) .eq(InsProduct::getInspectionItem, "温度循环")); StandardProductVO workHourMap = insOrderPlanServiceImpl.getInspectWorkHourAndGroup(insProduct); if(ObjectUtil.isNotNull(workHourMap)){ manHour = new BigDecimal(workHourMap.getManHour()); } DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); DateTime parse = DateUtil.parse(LocalDateTime.now().format(formatter)); AuxiliaryOutputWorkingHours auxiliaryOutputWorkingHours = new AuxiliaryOutputWorkingHours(); auxiliaryOutputWorkingHours.setInspectionItem("温度循环"); // 检验项目 auxiliaryOutputWorkingHours.setInspectionItemSubclass(""); // 检验项目子类 if(isWithinRange) { // 正常上班 auxiliaryOutputWorkingHours.setOrderNo(insOrder.getEntrustCode()); // 非加班委托单号 auxiliaryOutputWorkingHours.setWorkTime(accuracy(manHour,count)); // 工时 auxiliaryOutputWorkingHours.setAmount(count); // 非加班数量 }else{ // 加班 auxiliaryOutputWorkingHours.setOvertimeOrderNo(insOrder.getEntrustCode()); // 非加班委托单号 auxiliaryOutputWorkingHours.setOvertimeWorkTime(accuracy(manHour,count)); // 工时 auxiliaryOutputWorkingHours.setOvertimeAmount(count); // 非加班数量 } if(isCross) { String date = LocalDateTime.now().minusDays(1).toLocalDate().atStartOfDay().format(formatters);// 前一天 auxiliaryOutputWorkingHours.setDateTime(date); // 日期 }else { String date = LocalDateTime.now().toLocalDate().atStartOfDay().format(formatters);// 当天 auxiliaryOutputWorkingHours.setDateTime(date); // 日期 } auxiliaryOutputWorkingHours.setOutputWorkTime(accuracy(manHour,count)); // 产出工时 auxiliaryOutputWorkingHours.setWeekDay(insOrderPlanServiceImpl.getWeek(LocalDateTime.now().format(formatters)));//星期 auxiliaryOutputWorkingHours.setWeek(String.valueOf(DateUtil.weekOfYear(DateUtil.offsetDay(parse, 1))));//周次 auxiliaryOutputWorkingHours.setCheckId(userId);//检测人 auxiliaryOutputWorkingHours.setSample(insSample.getSampleCode());//样品编号 auxiliaryOutputWorkingHours.setManHourGroup(workHourMap.getManHourGroup());//工时分组 // 查询需要操作的日期是否有记录 List oldRecords = auxiliaryOutputWorkingHoursMapper.selectList(Wrappers.lambdaQuery() .eq(AuxiliaryOutputWorkingHours::getCheckId,userId) .eq(AuxiliaryOutputWorkingHours::getInspectionItem, "温度循环") .eq(AuxiliaryOutputWorkingHours::getInspectionItemSubclass, "") .eq(AuxiliaryOutputWorkingHours::getSample, insSample.getSampleCode()) .and(i->i.eq(AuxiliaryOutputWorkingHours::getOrderNo, insOrder.getEntrustCode()) .or() .eq(AuxiliaryOutputWorkingHours::getOvertimeOrderNo, insOrder.getEntrustCode())) .eq(AuxiliaryOutputWorkingHours::getDateTime, auxiliaryOutputWorkingHours.getDateTime()) ); // 所有的记录(其他天 + 当前天) List addOldRecords = auxiliaryOutputWorkingHoursMapper.selectList(Wrappers.lambdaQuery() .eq(AuxiliaryOutputWorkingHours::getCheckId,userId) .eq(AuxiliaryOutputWorkingHours::getInspectionItem, "温度循环") .eq(AuxiliaryOutputWorkingHours::getInspectionItemSubclass, "") .eq(AuxiliaryOutputWorkingHours::getSample, insSample.getSampleCode()) .and(i->i.eq(AuxiliaryOutputWorkingHours::getOrderNo, insOrder.getEntrustCode()) .or() .eq(AuxiliaryOutputWorkingHours::getOvertimeOrderNo, insOrder.getEntrustCode())) ); if(CollectionUtils.isEmpty(oldRecords)){ // 新增也要查看是否有所有的记录 if(Objects.isNull(addOldRecords)) { // 其他天也没有记录 auxiliaryOutputWorkingHoursMapper.insert(auxiliaryOutputWorkingHours); }else{ // 所有的记录 int otherDayAmount = 0; // 所有的数量 for(AuxiliaryOutputWorkingHours a : addOldRecords) { otherDayAmount += (Objects.isNull(a.getAmount()) ? 0 : a.getAmount()) + (Objects.isNull(a.getOvertimeAmount()) ? 0 : a.getOvertimeAmount()); } if(isWithinRange) { // 正常上班 auxiliaryOutputWorkingHours.setWorkTime(accuracy(manHour,(count - otherDayAmount + allValue))); // 工时 auxiliaryOutputWorkingHours.setAmount((count - otherDayAmount + allValue)); // 非加班数量 }else { // 加班 auxiliaryOutputWorkingHours.setOvertimeWorkTime(accuracy(manHour,(count - otherDayAmount + allValue))); // 工时 auxiliaryOutputWorkingHours.setOvertimeAmount((count - otherDayAmount + allValue)); // 非加班数量 } auxiliaryOutputWorkingHours.setOutputWorkTime(accuracy(manHour,(count - otherDayAmount + allValue))); // 产出工时 auxiliaryOutputWorkingHoursMapper.insert(auxiliaryOutputWorkingHours); } }else { // 不为空 Integer oldAmount = 0; // 工时添加的个数 for(AuxiliaryOutputWorkingHours a : addOldRecords) { oldAmount += (Objects.isNull(a.getAmount()) ? 0 : a.getAmount()) + (Objects.isNull(a.getOvertimeAmount()) ? 0 : a.getOvertimeAmount()); } if(count > (oldAmount - allValue)){ // 查找出日期为得到日期的记录 List collect = oldRecords.stream().filter(item -> item.getDateTime().equals(auxiliaryOutputWorkingHours.getDateTime())).collect(Collectors.toList()); if(CollectionUtils.isNotEmpty(collect)){ // 有的话就是更新操作 AuxiliaryOutputWorkingHours auxiliaryOutputWorkingHours1 = collect.get(0); if(isWithinRange) { // 正常上班 // 拿到原本的数量 与 新的数量相加 int num = Objects.isNull(auxiliaryOutputWorkingHours1.getAmount()) ? 0 :auxiliaryOutputWorkingHours1.getAmount(); // 原本的数量 auxiliaryOutputWorkingHours1.setAmount(count - oldAmount + num + allValue); // 非加班数量 auxiliaryOutputWorkingHours1.setOrderNo(insOrder.getEntrustCode()); // 非加班委托单号 auxiliaryOutputWorkingHours1.setWorkTime(accuracy(manHour,(count - oldAmount + num + allValue))); // 工时 auxiliaryOutputWorkingHours1.setOutputWorkTime(accuracy(manHour,auxiliaryOutputWorkingHours1.getAmount()) + (Objects.isNull(auxiliaryOutputWorkingHours1.getOvertimeWorkTime()) ? 0 : auxiliaryOutputWorkingHours1.getOvertimeWorkTime())); // 产出工时 采集肯定是会采集完 }else{ // 加班 // 拿到原本的数量 与 新的数量相加 int num = Objects.isNull(auxiliaryOutputWorkingHours1.getOvertimeAmount()) ? 0 :auxiliaryOutputWorkingHours1.getOvertimeAmount(); // 原本的数量 auxiliaryOutputWorkingHours1.setOvertimeAmount(count - oldAmount + num + allValue);// 加班数量 auxiliaryOutputWorkingHours1.setOvertimeOrderNo(insOrder.getEntrustCode()); // 加班委托单号 auxiliaryOutputWorkingHours1.setOvertimeWorkTime(accuracy(manHour,(count - oldAmount + num + allValue))); // 工时 auxiliaryOutputWorkingHours1.setOutputWorkTime(accuracy(manHour,auxiliaryOutputWorkingHours1.getOvertimeAmount()) + (Objects.isNull(auxiliaryOutputWorkingHours1.getWorkTime()) ? 0 : auxiliaryOutputWorkingHours1.getWorkTime())); // 产出工时 采集肯定是会采集完 } auxiliaryOutputWorkingHoursMapper.updateById(auxiliaryOutputWorkingHours1); } } } return 0; } //温度循环数采总体 @Override @Transactional(rollbackFor = Exception.class) public int temDataAcquisition2(PkMaster pkMaster) { //查询出所有循环次数和温度,然后循环调用temDataAcquisition接口 //查询对应的检验单表 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()); } //查找样品下所有温度循环的项目 List insProducts = insProductMapper.selectList(Wrappers.lambdaQuery() .eq(InsProduct::getInsSampleId, insSample.getId()) .eq(InsProduct::getState, 1) .isNotNull(InsProduct::getSpecialItemParentId) .isNotNull(InsProduct::getInspectionItemClass)); //过滤出所有循环次数和温度 List collect1 = insProducts.stream().map(InsProduct::getInspectionItem).distinct().collect(Collectors.toList()); List collect2 = insProducts.stream().map(InsProduct::getInspectionItemSubclass).distinct().collect(Collectors.toList()); for (String s : collect1) { for (String s1 : collect2) { PkMaster master = new PkMaster(); master.setEntrustCode(pkMaster.getEntrustCode()); master.setSampleCode(pkMaster.getSampleCode()); master.setModel(pkMaster.getModel()); master.setCycles(s); master.setTemperature(s1); temDataAcquisition(master); } } return 0; } /** * 是否加班、跨天 */ public Map isOvertimeAndCrossDay(Integer userId){ Map map = new HashMap<>(); Boolean isWithinRange = true; Boolean isCross = false; // 是否将当前检验单子的工时计算到班次开始天 LocalDateTime today = LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT); PerformanceShift performanceShift = performanceShiftMapper.selectOne(Wrappers.lambdaQuery() .eq(PerformanceShift::getUserId, userId) .eq(PerformanceShift::getWorkTime, today)); if (ObjectUtils.isNotEmpty(performanceShift)) { ShiftTime shiftTime = shiftTimeMapper.selectOne(Wrappers.lambdaQuery().eq(ShiftTime::getShift, performanceShift.getShift())); if (ObjectUtils.isNotEmpty(shiftTime)) { DateTimeFormatter forma = DateTimeFormatter.ofPattern("HH:mm"); LocalTime now = LocalTime.now(); LocalTime startTime = LocalTime.parse(shiftTime.getStartTime(), forma); LocalTime endTime = LocalTime.parse(shiftTime.getEndTime(), forma); // 检查当前时间是否在范围内(包括边界) // 如果当前时间不在今天的班次内 17:00~5.00 15.00 首先判断是否跨天了 // 跨天的情况 if (startTime.isAfter(endTime)) { // 查看当前时间是否在班次时间内 if (now.isAfter(startTime)) { // 如果是在结束时间之后那么就是正常上班 isWithinRange = true; isCross = false; } else { // 当前时间不在班次内,查看昨天的班次 LocalDateTime yesterday = LocalDateTime.of(LocalDate.now().minusDays(1), LocalTime.MIDNIGHT); PerformanceShift yesterdayShift = performanceShiftMapper.selectOne(Wrappers.lambdaQuery() .eq(PerformanceShift::getUserId, userId) .eq(PerformanceShift::getWorkTime, yesterday)); ShiftTime yesterdayShiftTime = shiftTimeMapper.selectOne(Wrappers.lambdaQuery().eq(ShiftTime::getShift, yesterdayShift.getShift())); // 如果昨天的班次不为空,那么就要看昨天的班次时间 if (!Objects.isNull(yesterdayShiftTime)) { LocalTime yesterdayStartTime = LocalTime.parse(yesterdayShiftTime.getStartTime(), forma); LocalTime yesterdayEndTime = LocalTime.parse(yesterdayShiftTime.getEndTime(), forma); // 昨天的班次是否跨天 if (yesterdayStartTime.isAfter(yesterdayEndTime)) { // 如果昨天的班次跨天了,那么就要看今天是否在昨天的班次内,并且只能是在结束时间之前,已经实现了跨天 if (now.isBefore(yesterdayEndTime)) { // 在昨天班次时间内正常上班,工时计算到昨天 isWithinRange = true; isCross = true; } else { // 不在昨天班次时间内就是加班,工时计算到昨天 isWithinRange = false; isCross = true; } } else { // 昨天的班次没有跨天 如果当前时间在今天班次开始时间之前就是昨天的加班,否则就是今天的加班 if (now.isBefore(startTime)) { isWithinRange = false; isCross = true; } else { isWithinRange = false; isCross = false; } } } else { isWithinRange = false; // 如果是在开始时间之前那么就要算到昨天 if (now.isBefore(startTime)) { isCross = true; } else { isCross = false; } } } } else { // 当前班次不跨天的情况下 // 如果当前时间在开始时间之前,查昨天的班次看当前时间是否在昨天的班次内 if (now.isBefore(startTime)) { // 查看昨天是否跨天 LocalDateTime yesterday = LocalDateTime.of(LocalDate.now().minusDays(1), LocalTime.MIDNIGHT); PerformanceShift yesterdayShift = performanceShiftMapper.selectOne(Wrappers.lambdaQuery() .eq(PerformanceShift::getUserId, userId) .eq(PerformanceShift::getWorkTime, yesterday)); ShiftTime yesterdayShiftTime = shiftTimeMapper.selectOne(Wrappers.lambdaQuery().eq(ShiftTime::getShift, yesterdayShift.getShift())); LocalTime yesterdayStartTime = LocalTime.parse(yesterdayShiftTime.getStartTime(), forma); LocalTime yesterdayEndTime = LocalTime.parse(yesterdayShiftTime.getEndTime(), forma); if (yesterdayStartTime.isAfter(yesterdayEndTime)) { // 如果昨天跨天 if (now.isBefore(yesterdayEndTime)) { // 在昨天的班次时间内正常上班,工时计算到昨天 isWithinRange = true; isCross = true; } else { // 不在昨天班次时间内就是加班,工时计算到昨天 isWithinRange = false; isCross = true; } } else { // 昨天不跨天 isWithinRange = false; isCross = true; } } else if (now.isAfter(endTime)) { // 如果当前时间在结束时间之后,那么就是今天的加班 isWithinRange = false; isCross = false; } else { // 正常上班 isWithinRange = true; isCross = false; } } } } map.put("isWithinRange",isWithinRange); map.put("isCross",isCross); return map; } /** * 精度问题 */ public double accuracy(BigDecimal manHour, int count){ return manHour.multiply(BigDecimal.valueOf(count)).setScale(4, RoundingMode.HALF_UP).doubleValue(); } }