package com.yuanchu.mom.service.impl; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.yuanchu.mom.mapper.*; import com.yuanchu.mom.pojo.DeviceMetricRecord; import com.yuanchu.mom.service.CustomService; import com.yuanchu.mom.service.InsOrderService; import com.yuanchu.mom.service.ProductService; import com.yuanchu.mom.service.QrShowService; 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.Date; import java.util.Objects; import java.util.Optional; @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; @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": // //查询设备主表信息 // Device device = deviceMapper.selectOne(Wrappers.lambdaQuery().eq(Device::getManagementNumber, code)); // 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.getSystemFileName()); // model.addAttribute("downloadUrl",getIp()+"img/"+calibrate.getSystemFileName()); // } // break; // } // } } /** * 计算启用时长 * @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)); } }