package com.ruoyi.requier.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.ruoyi.basic.mapper.StructureItemParameterMapper; import com.ruoyi.basic.pojo.Product; import com.ruoyi.basic.pojo.StructureItemParameter; import com.ruoyi.basic.service.ProductService; import com.ruoyi.common.core.domain.entity.Custom; import com.ruoyi.inspect.mapper.InsOrderMapper; import com.ruoyi.inspect.mapper.InsProductMapper; import com.ruoyi.inspect.mapper.InsSampleMapper; import com.ruoyi.inspect.pojo.InsOrder; import com.ruoyi.inspect.pojo.InsProduct; import com.ruoyi.inspect.pojo.InsSample; import com.ruoyi.inspect.service.InsOrderService; import com.ruoyi.requier.mapper.DeviceMaintenanceMapper; import com.ruoyi.requier.mapper.DeviceMapper; import com.ruoyi.requier.mapper.DeviceMetricRecordMapper; import com.ruoyi.requier.pojo.Device; import com.ruoyi.requier.pojo.DeviceMaintenance; import com.ruoyi.requier.pojo.DeviceMetricRecord; import com.ruoyi.requier.service.QrShowService; import com.ruoyi.system.service.CustomService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.ui.Model; import java.math.BigDecimal; import java.math.RoundingMode; import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; @Service public class QrShowServiceImpl implements QrShowService { @Autowired private InsOrderService insOrderService; @Autowired private ProductService productService; @Autowired private CustomService customService; @Autowired private InsOrderMapper insOrderMapper; @Autowired private DeviceMapper deviceMapper; @Autowired private DeviceMetricRecordMapper deviceMetricRecordMapper; @Autowired private DeviceMaintenanceMapper deviceMaintenanceMapper; @Autowired private StructureItemParameterMapper structureItemParameterMapper; @Autowired private InsSampleMapper insSampleMapper; @Autowired private InsProductMapper insProductMapper; @Value("${spring.profiles.active}") private String active; private String getIp(){ if(StringUtils.isNotBlank(active)){ return "prod".equals(active)?"https://zttx-lims.ztt.cn:8021/lims/":"http://114.132.189.42:8001/"; } return "http://192.168.1.124:8001/lims/"; } @Override public void transformModelByType(Model model, String code, String type) { if(StringUtils.isNotBlank(type)){ switch (type){ case "word": InsOrder insOrder = insOrderService.getOne(Wrappers.lambdaQuery() .eq(InsOrder::getEntrustCode, code)); Product product = productService.getOne(Wrappers.lambdaQuery() .eq(Product::getName, insOrder.getSample())); String insOrderModel = productService.getWordQrModel(insOrder.getId()); Custom company = customService.getOne(Wrappers.lambdaQuery() .eq(Custom::getCompany, insOrder.getCompany())); String orderType = insOrderMapper.getEnumLabelByValue(insOrder.getOrderType()); String[] monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; model.addAttribute("entrustCode", code); model.addAttribute("productName",product.getName()); model.addAttribute("productNameEn",product.getNameEn()); model.addAttribute("insOrderModel", insOrderModel); model.addAttribute("company", company.getCompany()); model.addAttribute("companyEn", company.getCompanyEn()); model.addAttribute("getTime", insOrder.getExamineTime().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"))); String getTimeEn = monthNames[insOrder.getExamineTime().getMonthValue() - 1] + " " + insOrder.getExamineTime().getDayOfMonth() + ", " + insOrder.getExamineTime().getYear(); model.addAttribute("getTimeEn", getTimeEn); model.addAttribute("issuingDate", insOrder.getIssuingDate().format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"))); String issuingDateEn = monthNames[insOrder.getIssuingDate().getMonthValue() - 1] + " " + insOrder.getIssuingDate().getDayOfMonth() + ", " + insOrder.getIssuingDate().getYear(); model.addAttribute("issuingDateEn", issuingDateEn); model.addAttribute("orderType", orderType); model.addAttribute("orderTypeEn", insOrder.getOrderType()); break; case "device": //查询设备主表信息 List deviceList = deviceMapper.selectList(Wrappers.lambdaQuery().eq(Device::getManagementNumber, code)); if(deviceList.size()>1){ throw new RuntimeException("设备编号"+code+"重复"); } Device device = deviceList.isEmpty()?null:deviceList.get(0); if(!Objects.isNull(device)){ //查询设备校准信息 DeviceMetricRecord calibrate = getDeviceMetricRecord(device.getId(), "calibrate"); //查询设备核查信息 DeviceMetricRecord examine = getDeviceMetricRecord(device.getId(), "examine"); //查询设备维护记录 DeviceMaintenance deviceMaintenance = Optional.ofNullable(deviceMaintenanceMapper.selectOne(Wrappers.lambdaQuery() .eq(DeviceMaintenance::getDeviceId, device.getId()) .orderByDesc(DeviceMaintenance::getId) .last("limit 1"))).orElse(new DeviceMaintenance()); model.addAttribute("progress",calcDeviceNextCheckRatio(calibrate.getCalibrationDate(),calibrate.getNextCalibrationDate()));//距离下次校准日期的天数百分比 model.addAttribute("deviceName",device.getDeviceName());//设备名称 model.addAttribute("deviceCode",device.getManagementNumber());//设备编号 model.addAttribute("usedYears",calcUsedYears(device.getActivationDate()));//启用时长(年) model.addAttribute("runStatus",device.getDeviceStatus());//设备运行状态 model.addAttribute("lastCalibrationDate",formatDate(calibrate.getCalibrationDate(),"yyyy-MM-dd"));//最近校准日期 model.addAttribute("nextCalibrationDate",formatDate(calibrate.getNextCalibrationDate(),"yyyy-MM-dd"));//下次校准日期 String calibrateStatus = "0yes".equals(calibrate.getStatus())?"合格":"1no".equals(calibrate.getStatus())?"不合格":"其他"; model.addAttribute("calibrateStatus",Objects.isNull(calibrate.getCalibrationDate())?"":calibrateStatus);//校准总结论 model.addAttribute("lastExamineDate",formatDate(examine.getCalibrationDate(),"yyyy-MM-dd"));//最近核查日期 model.addAttribute("nextExamineDate",formatDate(examine.getNextCalibrationDate(),"yyyy-MM-dd"));//下次核查日期 String examineStatus = "0yes".equals(examine.getStatus())?"合格":"1no".equals(examine.getStatus())?"不合格":"其他"; model.addAttribute("examineStatus",Objects.isNull(examine.getCalibrationDate())?"":examineStatus);//核查总结论 model.addAttribute("maintenanceDate",formatDate(deviceMaintenance.getDate(),"yyyy-MM-dd"));//最近维护日期 model.addAttribute("nextMaintenanceDate",formatDate(deviceMaintenance.getNextDate(),"yyyy-MM-dd"));//下次维护日期 String maintenanceType = ""; if(!Objects.isNull(deviceMaintenance.getMaintenanceType())){ maintenanceType = 0==deviceMaintenance.getMaintenanceType()?"使用前维护":"使用后维护"; } model.addAttribute("maintenanceType",maintenanceType);//维护总结论 //测量项目 String insProduct = ""; if(StringUtils.isNotBlank(device.getInsProductIds())){ String[] ids = device.getInsProductIds().split(","); List parameters = structureItemParameterMapper.selectBatchIds(Arrays.asList(ids)); List itemList = parameters.stream().map(StructureItemParameter::getInspectionItem).distinct().collect(Collectors.toList()); insProduct = String.join(",",itemList); } model.addAttribute("insProduct",insProduct);//测量项目 model.addAttribute("fileName",calibrate.getFileName()); model.addAttribute("downloadUrl",getIp()+"img/"+calibrate.getSystemFileName()); } break; case "sample": // 查询样品信息 InsSample insSample = insSampleMapper.selectById(code); InsOrder insOrder1 = insOrderMapper.selectById(insSample.getInsOrderId()); List insProducts = insProductMapper.selectList(new LambdaQueryWrapper() .eq(InsProduct::getInsSampleId, insSample.getId())); String collect = ""; if(CollectionUtils.isNotEmpty(insProducts)){ // collect = insProducts.stream().map(item -> { // String inspectionItem = ""; // if (Strings.isNotEmpty(item.getInspectionItemSubclass())) { // inspectionItem = item.getInspectionItemSubclass(); // } else { // inspectionItem = item.getInspectionItem(); // } // return inspectionItem; // }).distinct().collect(Collectors.joining(",")); collect = insProducts.stream().map(InsProduct::getInspectionItem).distinct().collect(Collectors.joining(",")); } model.addAttribute("sampleCode",insSample.getSampleCode()); // 样品编号 model.addAttribute("entrustCode",insOrder1.getEntrustCode()); // 委托单号 model.addAttribute("model",insSample.getModel()); // 规格型号 model.addAttribute("sample",insSample.getSample()); // 样品名称 model.addAttribute("inspectionItem",collect); // 检测项目 model.addAttribute("insState",insSample.getInsState()); // 检测状态 model.addAttribute("isLeave",insOrder1.getIsLeave()); // 是否留样 } } } /** * 计算启用时长 * @param activationDate * @return */ public double calcUsedYears(LocalDateTime activationDate) { if(Objects.isNull(activationDate)){ return 0; } BigDecimal defDays = BigDecimal.valueOf(365); BigDecimal usedDays = BigDecimal.valueOf(Duration.between(activationDate,LocalDateTime.now()).toDays()); return usedDays.divide(defDays,2,RoundingMode.HALF_UP).setScale(2,RoundingMode.HALF_UP).doubleValue(); } /** * 计算距离下次校准日期的天数百分比 * @param startDate * @param endDate * @return */ public double calcDeviceNextCheckRatio(Date startDate, Date endDate){ if(Objects.isNull(startDate) || Objects.isNull(endDate)){ return 0; } LocalDateTime startLocalDate = startDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); LocalDateTime endLocalDate = endDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); long totalDays = Duration.between(startLocalDate, endLocalDate).toDays(); long usedDays = Duration.between(startLocalDate, LocalDateTime.now()).toDays(); BigDecimal calcVal = BigDecimal.valueOf(usedDays).divide(BigDecimal.valueOf(totalDays),2,RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP); if(calcVal.compareTo(BigDecimal.ZERO)<0){ calcVal = BigDecimal.ZERO; }else if(calcVal.compareTo(BigDecimal.valueOf(100))>0){ calcVal = BigDecimal.valueOf(100); } return calcVal.doubleValue(); } /** * 查询设备校准/核查记录 * @param deviceId * @param type * @return */ public DeviceMetricRecord getDeviceMetricRecord(int deviceId,String type){ return Optional.ofNullable( deviceMetricRecordMapper.selectOne(Wrappers.lambdaQuery() .eq(DeviceMetricRecord::getDeviceId, deviceId) .eq(DeviceMetricRecord::getType, type) .orderByDesc(DeviceMetricRecord::getCreateTime) .last("limit 1"))).orElse(new DeviceMetricRecord()); } /** * 格式化日期 * @return */ public String formatDate(Date date,String formatter){ if(Objects.isNull(date)){ return ""; } LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); return localDateTime.format(DateTimeFormatter.ofPattern(formatter)); } /** * 格式化日期 * @return */ public String formatDate(LocalDate date,String formatter){ if(Objects.isNull(date)){ return ""; } return date.format(DateTimeFormatter.ofPattern(formatter)); } }