package com.hk.NumericalCollection.service.impl; import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelWriter; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.hk.NumericalCollection.dto.ApiDataResult; import com.hk.NumericalCollection.dto.NumbericalDto; import com.hk.NumericalCollection.entity.*; import com.hk.NumericalCollection.mapper.DeviceMetricsMapper; import com.hk.NumericalCollection.service.*; import lombok.RequiredArgsConstructor; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.lang.reflect.Field; import java.net.HttpURLConnection; import java.net.URL; import java.util.*; /** * @author Administrator * @description 针对表【DEVICE_METRICS】的数据库操作Service实现 * @createDate 2024-10-14 17:04:36 */ @Service @Transactional(rollbackFor = Exception.class) @RequiredArgsConstructor public class DeviceMetricsServiceImpl extends ServiceImpl implements DeviceMetricsService { // 需要乘以 0.1 的字段列表 private static final List FIELDS_TO_SCALE = Arrays.asList("d23", "d53", "d55", "d57", "d59", "d61", "d63", "d65", "d67", "d69", "d71", "d73", "d76", "d79", "d82", "d85", "d88", "d91", "d94", "d97", "d100", "d103", "d106", "d112", "d115", "d118", "d121", "d126", "d131", "d136", "d141", "d146", "d151", "d156", "d161", "d166", "d170", "d174", "d178", "d182", "d186"); // private final WomdaaService womdaaService; private final INumericalService numericalService; private final VOrderService vOrderService; private final VOrderDetailService vOrderDetailService; private final DevMachineService devMachineService; private final DevMacByclService devMacByclService; private final DeviceService deviceService; private final DeviceByclService deviceByclService; private final DevicedatastandardmoditemService devicedatastandardmoditemService; @Override public boolean manualSynchronization(NumbericalDto barcode) throws Exception { LambdaQueryWrapper query = new LambdaQueryWrapper<>(); query.eq(VOrder::getId, barcode.getOrderId()); VOrder womdaa = vOrderService.getOne(query, false); if (womdaa == null) { return false; } LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(DevMachine::getMachineNo, barcode.getMachineNo()); DevMachine one = devMachineService.getOne(queryWrapper); if (one == null) { return false; } //if ("862858071070278_1".equals(one.getDevNo())) { List list = new ArrayList(); //根据工单和机台查是否有工艺参数的记录 LambdaUpdateWrapper queryWrapper1 = new LambdaUpdateWrapper<>(); queryWrapper1.eq(DeviceMetrics::getDevNo, one.getDevNo()) // .eq(DeviceMetrics::getItemNo, womdaa.getEngineeringNo()) .eq(DeviceMetrics::getItemNo, womdaa.getDaa002()) .eq(DeviceMetrics::getType, 1); remove(queryWrapper1); String dev = one.getDevNo().substring(0, 15); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.like(Device::getDevNo, dev); List list1 = deviceService.list(wrapper); int machineNo = Integer.parseInt(barcode.getMachineNo()); list1.forEach(s -> { ApiDataResult result = null; try { result = numericalService.getDeviceRealTimeDataApiDataResult(s.getDevNo()); } catch (Exception e) { throw new RuntimeException(e); } DeviceRealTimeData data = result.getData(); // boolean b = saveDeviceMetrics(data, womdaa.getEngineeringNo(), s.getDevNo()); boolean b = saveDeviceMetrics(data, womdaa.getDaa002(), s.getDevNo(), womdaa.getDaa001(), machineNo); list.add(b); }); return list.stream().allMatch(Boolean::booleanValue); //} // ApiDataResult result = numericalService.getDeviceRealTimeDataApiDataResult(one.getDevNo()); // // DeviceRealTimeData data = result.getData(); // return saveDeviceMetrics(data, womdaa.getEngineeringNo()); } @Override public boolean manualSynchronizationBycl(NumbericalDto barcode) throws Exception { LambdaQueryWrapper query = new LambdaQueryWrapper<>(); query.eq(VOrderDetail::getId, barcode.getOrderId()); VOrderDetail womdaa = vOrderDetailService.getOne(query, false); if (womdaa == null) { return false; } LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(DevMacBycl::getMachineNo, barcode.getMachineNo()); DevMacBycl one = devMacByclService.getOne(queryWrapper); if (one == null) { return false; } //if ("862858071070278_1".equals(one.getDevNo())) { List list = new ArrayList(); //根据工单和机台查是否有工艺参数的记录 LambdaUpdateWrapper queryWrapper1 = new LambdaUpdateWrapper<>(); queryWrapper1.eq(DeviceMetrics::getDevNo, one.getDevNo()) // .eq(DeviceMetrics::getItemNo, womdaa.getEngineeringNo()) .eq(DeviceMetrics::getItemNo, womdaa.getDaa002()) .eq(DeviceMetrics::getType, 1); remove(queryWrapper1); String dev = one.getDevNo().substring(0, 15); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.like(DeviceBycl::getDevNo, dev); List list1 = deviceByclService.list(wrapper); list1.forEach(s -> { ApiDataResult result = null; try { result = numericalService.getDeviceRealTimeDataApiDataResult(s.getDevNo()); } catch (Exception e) { throw new RuntimeException(e); } DeviceRealTimeData data = result.getData(); // boolean b = saveDeviceMetrics(data, womdaa.getEngineeringNo(), s.getDevNo()); boolean b = saveDeviceMetricsBycl(data, womdaa.getDaa002(), s.getDevNo(), womdaa.getDaa001(), barcode.getMachineNo()); list.add(b); }); return list.stream().allMatch(Boolean::booleanValue); } private boolean saveDeviceMetricsBycl(DeviceRealTimeData data, String engineeringNo, String devNo, String daa001, String machineNo) { if (StrUtil.isNullOrUndefined(devNo)) { return false; } List list = new ArrayList<>(); // 根据 machineNo 选择字段映射 Map fieldMap = createFieldMappingForBycl(machineNo); String formattedDate = DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"); for (Map.Entry entry : fieldMap.entrySet()) { String fieldKey = entry.getKey(); DevMetricsSeq value = entry.getValue(); // 使用反射从 DeviceRealTimeData 中获取对应字段的值 String fieldValue = getFieldValue1(data, fieldKey); // 如果字段值不为空,插入到数据库 if (fieldValue != null) { DeviceMetrics entity = new DeviceMetrics(); entity.setField(value.getField()); entity.setGroupSeq(value.getGroupSeq()); entity.setSeq(value.getSeq()); entity.setValue(fieldValue); entity.setDevNo(devNo); entity.setDayDate(formattedDate); entity.setItemNo(engineeringNo); entity.setType((short) 1); entity.setBillNo(daa001); entity.setMachineNo(machineNo); list.add(entity); } } //导出到excel //exportDeviceMetricsToExcel(list); return saveBatch(list); } private boolean saveDeviceMetrics(DeviceRealTimeData data, String engineeringNo, String devNo, String daa001, int machineNo) { if (StrUtil.isNullOrUndefined(devNo)) { return false; } List list = new ArrayList<>(); String seq = devNo.substring(16); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(Devicedatastandardmoditem::getModname, "UA注塑机") .eq(Devicedatastandardmoditem::getIsShow, 1) .eq(Devicedatastandardmoditem::getDatainfo, seq); List deviceList = devicedatastandardmoditemService.list(wrapper); Map fieldMap = new HashMap<>(); deviceList.forEach(s -> { DevMetricsSeq devMetricsSeq = new DevMetricsSeq(); devMetricsSeq.setField(s.getKeyname()); devMetricsSeq.setGroupSeq(s.getGroupSeq().shortValue()); devMetricsSeq.setSeq(s.getSeq().shortValue()); fieldMap.put(s.getKeyshort(), devMetricsSeq); }); //String formattedDate = data.getLastEditDate(); String formattedDate = DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"); for (Map.Entry entry : fieldMap.entrySet()) { String fieldKey = entry.getKey(); DevMetricsSeq value = entry.getValue(); // 使用反射从 DeviceRealTimeData 中获取对应字段的值 String fieldValue = getFieldValue1(data, fieldKey); // 如果字段值不为空,插入到数据库 if (fieldValue != null) { String dev = devNo.substring(0, 15); DeviceMetrics entity = new DeviceMetrics(); entity.setField(value.getField()); entity.setGroupSeq(value.getGroupSeq()); entity.setSeq(value.getSeq()); entity.setValue(fieldValue); entity.setDevNo(dev + "_1"); entity.setDayDate(formattedDate); entity.setItemNo(engineeringNo); entity.setType((short) 1); entity.setBillNo(daa001); entity.setMachineNo(String.valueOf(machineNo)); list.add(entity); } } //导出到excel //exportDeviceMetricsToExcel(list); return saveBatch(list); } private String getFieldValue1(DeviceRealTimeData data, String fieldName) { try { Field field = DeviceRealTimeData.class.getDeclaredField(fieldName); field.setAccessible(true); // 允许访问私有字段 String fieldValue = (String) field.get(data); // 如果字段需要乘以 0.1,则进行转换 // if (fieldValue != null && FIELDS_TO_SCALE.contains(fieldName)) { if (fieldValue != null) { double scaledValue = Double.parseDouble(fieldValue); // fieldValue = String.valueOf(scaledValue); // 保留一位小数 fieldValue = String.format("%.1f", scaledValue); double v = Double.parseDouble(fieldValue); if (v <= 0) { fieldValue = "/"; } else { fieldValue = String.format("%.1f", v); } } else { fieldValue = "/"; } // if (fieldValue != null) { // double v = Double.parseDouble(fieldValue); // if (v < 0) { // fieldValue = "/"; // } else { // fieldValue = String.format("%.1f", v); // } // } else { // fieldValue = "/"; // } return fieldValue; } catch (NoSuchFieldException | IllegalAccessException | NumberFormatException e) { // 忽略没有的字段或无法访问的字段 System.out.println("字段不存在或无法访问: " + fieldName); return null; } } // 创建字段与中文描述的映射 private Map createFieldMapping() { Map fieldMap = new HashMap<>(180); // fieldMap.put("d15", "开模总数"); // fieldMap.put("d3", "告警信息"); fieldMap.put("d21", new DevMetricsSeq("托模位置", (short) 1, (short) 46)); // fieldMap.put("d22", "输出压力"); // fieldMap.put("d23", "输出速度"); // fieldMap.put("d24", "输出背压"); fieldMap.put("d25", new DevMetricsSeq("射出位置", (short) 2, (short) 19)); fieldMap.put("d26", new DevMetricsSeq("温一实际", (short) 3, (short) 132)); fieldMap.put("d27", new DevMetricsSeq("温二实际", (short) 3, (short) 133)); fieldMap.put("d28", new DevMetricsSeq("温三实际", (short) 3, (short) 134)); fieldMap.put("d29", new DevMetricsSeq("温四实际", (short) 3, (short) 135)); fieldMap.put("d30", new DevMetricsSeq("温五实际", (short) 3, (short) 136)); fieldMap.put("d31", new DevMetricsSeq("温六实际", (short) 3, (short) 137)); fieldMap.put("d32", new DevMetricsSeq("温七实际", (short) 3, (short) 138)); // fieldMap.put("d33", "上模循环时间"); // fieldMap.put("d34", "上模射出时间"); // fieldMap.put("d35", "上模转保时间"); // fieldMap.put("d36", "上模储料时间"); // fieldMap.put("d37", "上模关模计时"); // fieldMap.put("d38", "上模低压计时"); // fieldMap.put("d39", "上模高压计时"); // fieldMap.put("d40", "上模推力座位置"); // fieldMap.put("d41", "上模开模计时"); // fieldMap.put("d42", "上模转保压力"); // fieldMap.put("d43", "上模射出起点"); // fieldMap.put("d44", "上模保压起点"); // fieldMap.put("d45", "上模射出终点位置"); // fieldMap.put("d46", "上模射出监控位置"); // fieldMap.put("d47", "上模射出尖压"); // fieldMap.put("d48", "上模储料尖压"); // fieldMap.put("d49", "上模最大射速"); // fieldMap.put("d50", "上模取件时间"); fieldMap.put("d51", new DevMetricsSeq("开模行程", (short) 1, (short) 1)); fieldMap.put("d52", new DevMetricsSeq("关模一段压力", (short) 1, (short) 17)); fieldMap.put("d53", new DevMetricsSeq("关模一段速度", (short) 1, (short) 18)); fieldMap.put("d54", new DevMetricsSeq("关模二段压力", (short) 1, (short) 20)); fieldMap.put("d55", new DevMetricsSeq("关模二段速度", (short) 1, (short) 21)); fieldMap.put("d56", new DevMetricsSeq("关模三段压力", (short) 1, (short) 23)); fieldMap.put("d57", new DevMetricsSeq("关模三段速度", (short) 1, (short) 24)); fieldMap.put("d58", new DevMetricsSeq("关模低压压力", (short) 1, (short) 26)); fieldMap.put("d59", new DevMetricsSeq("关模低压速度", (short) 1, (short) 27)); fieldMap.put("d60", new DevMetricsSeq("关模高压压力", (short) 1, (short) 29)); fieldMap.put("d61", new DevMetricsSeq("关模高压速度", (short) 1, (short) 30)); fieldMap.put("d62", new DevMetricsSeq("开模一段压力", (short) 1, (short) 2)); fieldMap.put("d63", new DevMetricsSeq("开模一段速度", (short) 1, (short) 3)); fieldMap.put("d64", new DevMetricsSeq("开模二段压力", (short) 1, (short) 5)); fieldMap.put("d65", new DevMetricsSeq("开模二段速度", (short) 1, (short) 6)); fieldMap.put("d66", new DevMetricsSeq("开模三段压力", (short) 1, (short) 8)); fieldMap.put("d67", new DevMetricsSeq("开模三段速度", (short) 1, (short) 9)); fieldMap.put("d68", new DevMetricsSeq("开模四段压力", (short) 1, (short) 11)); fieldMap.put("d69", new DevMetricsSeq("开模四段速度", (short) 1, (short) 12)); fieldMap.put("d70", new DevMetricsSeq("开模五段压力", (short) 1, (short) 14)); fieldMap.put("d71", new DevMetricsSeq("开模五段速度", (short) 1, (short) 15)); fieldMap.put("d72", new DevMetricsSeq("射出一段压力", (short) 2, (short) 1)); fieldMap.put("d73", new DevMetricsSeq("射出一段速度", (short) 2, (short) 2)); fieldMap.put("d74", new DevMetricsSeq("射出一段位置", (short) 2, (short) 3)); fieldMap.put("d75", new DevMetricsSeq("射出二段压力", (short) 2, (short) 4)); fieldMap.put("d76", new DevMetricsSeq("射出二段速度", (short) 2, (short) 5)); fieldMap.put("d77", new DevMetricsSeq("射出二段位置", (short) 2, (short) 6)); fieldMap.put("d78", new DevMetricsSeq("射出三段压力", (short) 2, (short) 7)); fieldMap.put("d79", new DevMetricsSeq("射出三段速度", (short) 2, (short) 8)); fieldMap.put("d80", new DevMetricsSeq("射出三段位置", (short) 2, (short) 9)); fieldMap.put("d81", new DevMetricsSeq("射出四段压力", (short) 2, (short) 10)); fieldMap.put("d82", new DevMetricsSeq("射出四段速度", (short) 2, (short) 11)); fieldMap.put("d83", new DevMetricsSeq("射出四段位置", (short) 2, (short) 12)); fieldMap.put("d84", new DevMetricsSeq("射出五段压力", (short) 2, (short) 13)); fieldMap.put("d85", new DevMetricsSeq("射出五段速度", (short) 2, (short) 14)); fieldMap.put("d86", new DevMetricsSeq("射出五段位置", (short) 2, (short) 15)); fieldMap.put("d87", new DevMetricsSeq("射出六段压力", (short) 2, (short) 16)); fieldMap.put("d88", new DevMetricsSeq("射出六段速度", (short) 2, (short) 17)); fieldMap.put("d89", new DevMetricsSeq("射出六段位置", (short) 2, (short) 18)); fieldMap.put("d90", new DevMetricsSeq("保压一段压力", (short) 2, (short) 24)); fieldMap.put("d91", new DevMetricsSeq("保压一段速度", (short) 2, (short) 25)); fieldMap.put("d92", new DevMetricsSeq("保压一段时间", (short) 2, (short) 26)); fieldMap.put("d93", new DevMetricsSeq("保压二段压力", (short) 2, (short) 27)); fieldMap.put("d94", new DevMetricsSeq("保压二段速度", (short) 2, (short) 28)); fieldMap.put("d95", new DevMetricsSeq("保压二段时间", (short) 2, (short) 29)); fieldMap.put("d96", new DevMetricsSeq("保压三段压力", (short) 2, (short) 30)); fieldMap.put("d97", new DevMetricsSeq("保压三段速度", (short) 2, (short) 31)); fieldMap.put("d98", new DevMetricsSeq("保压三段时间", (short) 2, (short) 32)); fieldMap.put("d99", new DevMetricsSeq("保压四段压力", (short) 2, (short) 33)); fieldMap.put("d100", new DevMetricsSeq("保压四段速度", (short) 2, (short) 34)); fieldMap.put("d101", new DevMetricsSeq("保压四段时间", (short) 2, (short) 35)); fieldMap.put("d102", new DevMetricsSeq("保压五段压力", (short) 2, (short) 36)); fieldMap.put("d103", new DevMetricsSeq("保压五段速度", (short) 2, (short) 37)); fieldMap.put("d104", new DevMetricsSeq("保压五段时间", (short) 2, (short) 38)); fieldMap.put("d105", new DevMetricsSeq("保压六段压力", (short) 2, (short) 39)); fieldMap.put("d106", new DevMetricsSeq("保压六段速度", (short) 2, (short) 40)); fieldMap.put("d107", new DevMetricsSeq("保压六段时间", (short) 2, (short) 41)); fieldMap.put("d108", new DevMetricsSeq("转保压位置设定", (short) 2, (short) 23)); fieldMap.put("d109", new DevMetricsSeq("转保压时间设定", (short) 2, (short) 22)); fieldMap.put("d110", new DevMetricsSeq("转保压选择", (short) 2, (short) 20)); fieldMap.put("d111", new DevMetricsSeq("托模进一段压力", (short) 1, (short) 32)); fieldMap.put("d112", new DevMetricsSeq("托模进一段速度", (short) 1, (short) 33)); fieldMap.put("d113", new DevMetricsSeq("托模进一段位置", (short) 1, (short) 34)); fieldMap.put("d114", new DevMetricsSeq("托模进二段压力", (short) 1, (short) 36)); fieldMap.put("d115", new DevMetricsSeq("托模进二段速度", (short) 1, (short) 35)); fieldMap.put("d116", new DevMetricsSeq("托模进二段位置", (short) 1, (short) 37)); fieldMap.put("d117", new DevMetricsSeq("托模退一段压力", (short) 1, (short) 39)); fieldMap.put("d118", new DevMetricsSeq("托模退一段速度", (short) 1, (short) 40)); fieldMap.put("d119", new DevMetricsSeq("托模退一段位置", (short) 1, (short) 41)); fieldMap.put("d120", new DevMetricsSeq("托模退二段压力", (short) 1, (short) 43)); fieldMap.put("d121", new DevMetricsSeq("托模退二段速度", (short) 1, (short) 42)); fieldMap.put("d122", new DevMetricsSeq("托模退二段位置", (short) 1, (short) 44)); fieldMap.put("d123", new DevMetricsSeq("托模进延时时间", (short) 1, (short) 38)); fieldMap.put("d124", new DevMetricsSeq("托模退延时时间", (short) 1, (short) 45)); fieldMap.put("d209", new DevMetricsSeq("中子 A 选择", (short) 4, (short) 1)); fieldMap.put("d210", new DevMetricsSeq("中子 B 选择", (short) 4, (short) 11)); fieldMap.put("d137", new DevMetricsSeq("中子 B 进动作位置", (short) 4, (short) 17)); fieldMap.put("d211", new DevMetricsSeq("中子 C 选择", (short) 4, (short) 23)); fieldMap.put("d212", new DevMetricsSeq("中子 D 选择", (short) 4, (short) 34)); fieldMap.put("d125", new DevMetricsSeq("中子 A 进压力", (short) 4, (short) 2)); fieldMap.put("d126", new DevMetricsSeq("中子 A 进速度", (short) 4, (short) 3)); // fieldMap.put("d127", "中子 A 进动作位置"); fieldMap.put("d128", new DevMetricsSeq("中子 A 进动作时间", (short) 4, (short) 5)); fieldMap.put("d129", new DevMetricsSeq("中子 A 进绞牙计数", (short) 4, (short) 4)); fieldMap.put("d130", new DevMetricsSeq("中子 A 退压力", (short) 4, (short) 6)); fieldMap.put("d131", new DevMetricsSeq("中子 A 退速度", (short) 4, (short) 7)); fieldMap.put("d132", new DevMetricsSeq("中子 A 退动作时间", (short) 4, (short) 9)); fieldMap.put("d133", new DevMetricsSeq("中子 A 退动作位置", (short) 4, (short) 10)); fieldMap.put("d134", new DevMetricsSeq("中子 A 退绞牙计数", (short) 4, (short) 8)); fieldMap.put("d135", new DevMetricsSeq("中子 B 进压力", (short) 4, (short) 12)); fieldMap.put("d136", new DevMetricsSeq("中子 B 进速度", (short) 4, (short) 13)); fieldMap.put("d138", new DevMetricsSeq("中子 B 进动作时间", (short) 4, (short) 14)); // fieldMap.put("d138", "中子 B 进动作时间"); fieldMap.put("d139", new DevMetricsSeq("中子 B 进绞牙计数", (short) 4, (short) 15)); fieldMap.put("d140", new DevMetricsSeq("中子 B 退压力", (short) 4, (short) 18)); fieldMap.put("d141", new DevMetricsSeq("中子 B 退速度", (short) 4, (short) 19)); fieldMap.put("d142", new DevMetricsSeq("中子 B 退动作位置", (short) 4, (short) 22)); fieldMap.put("d143", new DevMetricsSeq("中子 B 退动作时间", (short) 4, (short) 21)); fieldMap.put("d144", new DevMetricsSeq("中子 B 退绞牙计数", (short) 4, (short) 20)); fieldMap.put("d145", new DevMetricsSeq("中子 C 进压力", (short) 4, (short) 24)); fieldMap.put("d146", new DevMetricsSeq("中子 C 进速度", (short) 4, (short) 25)); fieldMap.put("d147", new DevMetricsSeq("中子 C 进动作位置", (short) 4, (short) 28)); fieldMap.put("d148", new DevMetricsSeq("中子 C 进动作时间", (short) 4, (short) 27)); // fieldMap.put("d148", "中子 C 进动作时间"); fieldMap.put("d149", new DevMetricsSeq("中子 C 进绞牙计数", (short) 4, (short) 26)); fieldMap.put("d150", new DevMetricsSeq("中子 C 退压力", (short) 4, (short) 29)); fieldMap.put("d151", new DevMetricsSeq("中子 C 退速度", (short) 4, (short) 30)); fieldMap.put("d152", new DevMetricsSeq("中子 C 退动作位置", (short) 4, (short) 33)); fieldMap.put("d153", new DevMetricsSeq("中子 C 退动作时间", (short) 4, (short) 32)); fieldMap.put("d154", new DevMetricsSeq("中子 C 退绞牙计数", (short) 4, (short) 31)); fieldMap.put("d155", new DevMetricsSeq("中子 D 进压力", (short) 4, (short) 35)); fieldMap.put("d156", new DevMetricsSeq("中子 D 进速度", (short) 4, (short) 36)); fieldMap.put("d158", new DevMetricsSeq("中子 D 进动作时间", (short) 4, (short) 38)); fieldMap.put("d157", new DevMetricsSeq("中子 D 进动作位置", (short) 4, (short) 39)); // fieldMap.put("d158", "中子 D 进动作时间"); fieldMap.put("d159", new DevMetricsSeq("中子 D 进绞牙计数", (short) 4, (short) 37)); fieldMap.put("d160", new DevMetricsSeq("中子 D 退压力", (short) 4, (short) 40)); fieldMap.put("d161", new DevMetricsSeq("中子 D 退速度", (short) 4, (short) 41)); fieldMap.put("d162", new DevMetricsSeq("中子 D 退动作位置", (short) 4, (short) 44)); fieldMap.put("d163", new DevMetricsSeq("中子 D 退动作时间", (short) 4, (short) 43)); fieldMap.put("d164", new DevMetricsSeq("中子 D 退绞牙计数", (short) 4, (short) 42)); fieldMap.put("d165", new DevMetricsSeq("储料一段压力", (short) 3, (short) 9)); fieldMap.put("d166", new DevMetricsSeq("储料一段速度", (short) 3, (short) 10)); fieldMap.put("d167", new DevMetricsSeq("储料一段背压", (short) 3, (short) 8)); fieldMap.put("d168", new DevMetricsSeq("储料一段位置", (short) 3, (short) 11)); fieldMap.put("d169", new DevMetricsSeq("储料二段压力", (short) 3, (short) 13)); fieldMap.put("d170", new DevMetricsSeq("储料二段速度", (short) 3, (short) 14)); fieldMap.put("d171", new DevMetricsSeq("储料二段背压", (short) 3, (short) 12)); fieldMap.put("d172", new DevMetricsSeq("储料二段位置", (short) 3, (short) 15)); fieldMap.put("d173", new DevMetricsSeq("储料三段压力", (short) 3, (short) 17)); fieldMap.put("d174", new DevMetricsSeq("储料三段速度", (short) 3, (short) 18)); fieldMap.put("d175", new DevMetricsSeq("储料三段背压", (short) 3, (short) 16)); fieldMap.put("d176", new DevMetricsSeq("储料三段位置", (short) 3, (short) 19)); fieldMap.put("d177", new DevMetricsSeq("储料四段压力", (short) 3, (short) 21)); fieldMap.put("d178", new DevMetricsSeq("储料四段速度", (short) 3, (short) 22)); fieldMap.put("d179", new DevMetricsSeq("储料四段背压", (short) 3, (short) 20)); fieldMap.put("d180", new DevMetricsSeq("储料四段位置", (short) 3, (short) 23)); fieldMap.put("d181", new DevMetricsSeq("储料五段压力", (short) 3, (short) 25)); fieldMap.put("d182", new DevMetricsSeq("储料五段速度", (short) 3, (short) 26)); fieldMap.put("d183", new DevMetricsSeq("储料五段背压", (short) 3, (short) 24)); fieldMap.put("d184", new DevMetricsSeq("储料五段位置", (short) 3, (short) 27)); fieldMap.put("d185", new DevMetricsSeq("射退压力", (short) 3, (short) 1)); fieldMap.put("d186", new DevMetricsSeq("射退速度", (short) 3, (short) 2)); fieldMap.put("d187", new DevMetricsSeq("射退距离", (short) 3, (short) 29)); fieldMap.put("d188", new DevMetricsSeq("储前射退距离", (short) 3, (short) 3)); fieldMap.put("d189", new DevMetricsSeq("射退模式", (short) 3, (short) 28)); fieldMap.put("d190", new DevMetricsSeq("储前冷却", (short) 3, (short) 31)); fieldMap.put("d191", new DevMetricsSeq("温一设定", (short) 3, (short) 32)); fieldMap.put("d192", new DevMetricsSeq("温二设定", (short) 3, (short) 33)); fieldMap.put("d193", new DevMetricsSeq("温三设定", (short) 3, (short) 34)); fieldMap.put("d194", new DevMetricsSeq("温四设定", (short) 3, (short) 35)); fieldMap.put("d195", new DevMetricsSeq("温五设定", (short) 3, (short) 36)); fieldMap.put("d196", new DevMetricsSeq("温六设定", (short) 3, (short) 37)); fieldMap.put("d197", new DevMetricsSeq("温七设定", (short) 3, (short) 38)); fieldMap.put("d198", new DevMetricsSeq("转保压位置设定", (short) 2, (short) 21)); // fieldMap.put("d204", new DevMetricsSeq("开模一段位置", (short) 1, (short) 4)); fieldMap.put("d205", new DevMetricsSeq("开模一段位置", (short) 1, (short) 7)); fieldMap.put("d206", new DevMetricsSeq("开模二段位置", (short) 1, (short) 10)); fieldMap.put("d207", new DevMetricsSeq("开模三段位置", (short) 1, (short) 13)); fieldMap.put("d208", new DevMetricsSeq("开模四段位置", (short) 1, (short) 16)); fieldMap.put("d199", new DevMetricsSeq("关模一段位置", (short) 1, (short) 19)); fieldMap.put("d200", new DevMetricsSeq("关模二段位置", (short) 1, (short) 22)); fieldMap.put("d201", new DevMetricsSeq("关模三段位置", (short) 1, (short) 25)); fieldMap.put("d202", new DevMetricsSeq("关模低压位置", (short) 1, (short) 28)); fieldMap.put("d203", new DevMetricsSeq("关模高压位置", (short) 1, (short) 31)); return fieldMap; } /** * 根据 machineNo 创建 BYCL 设备的字段映射 * @param machineNo 机台号 * @return 字段映射 Map */ private Map createFieldMappingForBycl(String machineNo) { // 定义特殊机台号列表 List cncMachineNos = Arrays.asList("A04", "A05", "A06", "A07", "A08", "A09", "A20", "A21"); // 根据 machineNo 判断使用哪种映射 if (cncMachineNos.contains(machineNo)) { return createCncFieldMapping(); } else { return createGeneralFieldMapping(); } } /** * 创建 CNC 机床的字段映射 (machineNo 为 A04, A05, A06, A07, A08, A09, A20, A21) */ private Map createCncFieldMapping() { Map fieldMap = new HashMap<>(); // CNC 机床专用字段映射 fieldMap.put("d26", new DevMetricsSeq("工作模式", (short) 1, (short) 1)); fieldMap.put("d27", new DevMetricsSeq("运行状态", (short) 1, (short) 2)); fieldMap.put("d28", new DevMetricsSeq("是否报警", (short) 1, (short) 3)); fieldMap.put("d40", new DevMetricsSeq("开机时间", (short) 1, (short) 4)); fieldMap.put("d41", new DevMetricsSeq("切削时间", (short) 1, (short) 5)); fieldMap.put("d42", new DevMetricsSeq("循环时间/单件计时", (short) 1, (short) 6)); fieldMap.put("d43", new DevMetricsSeq("累计加工时间", (short) 1, (short) 7)); fieldMap.put("d1", new DevMetricsSeq("工件数", (short) 1, (short) 8)); fieldMap.put("d44", new DevMetricsSeq("需求工件数", (short) 1, (short) 9)); fieldMap.put("d45", new DevMetricsSeq("总工件数", (short) 1, (short) 10)); fieldMap.put("d46", new DevMetricsSeq("工作笔数", (short) 1, (short) 11)); fieldMap.put("d3", new DevMetricsSeq("报警信息", (short) 1, (short) 12)); return fieldMap; } /** * 创建通用设备的字段映射 (其他 machineNo) */ private Map createGeneralFieldMapping() { Map fieldMap = new HashMap<>(); // 通用设备字段映射 fieldMap.put("d21", new DevMetricsSeq("当前刀具号", (short) 1, (short) 1)); fieldMap.put("d22", new DevMetricsSeq("当前刀偏号", (short) 1, (short) 2)); fieldMap.put("d29", new DevMetricsSeq("加工件数", (short) 1, (short) 3)); fieldMap.put("d23", new DevMetricsSeq("运行时间-时", (short) 2, (short) 1)); fieldMap.put("d24", new DevMetricsSeq("运行时间-秒", (short) 2, (short) 2)); fieldMap.put("d25", new DevMetricsSeq("切削时间-时", (short) 2, (short) 3)); fieldMap.put("d26", new DevMetricsSeq("切削时间-秒", (short) 2, (short) 4)); fieldMap.put("d34", new DevMetricsSeq("进给编程速度", (short) 3, (short) 1)); fieldMap.put("d35", new DevMetricsSeq("进给实际速度", (short) 3, (short) 2)); fieldMap.put("d36", new DevMetricsSeq("进给倍率", (short) 3, (short) 3)); fieldMap.put("d37", new DevMetricsSeq("主轴编程速度", (short) 3, (short) 4)); fieldMap.put("d38", new DevMetricsSeq("主轴实际速度", (short) 3, (short) 5)); fieldMap.put("d39", new DevMetricsSeq("主轴倍率", (short) 3, (short) 6)); fieldMap.put("d40", new DevMetricsSeq("快速倍率", (short) 3, (short) 7)); fieldMap.put("d41", new DevMetricsSeq("手动倍率", (short) 3, (short) 8)); fieldMap.put("d42", new DevMetricsSeq("手轮倍率", (short) 3, (short) 9)); fieldMap.put("d30", new DevMetricsSeq("CNC工作方式", (short) 4, (short) 1)); fieldMap.put("d31", new DevMetricsSeq("当前运行的程序号", (short) 4, (short) 2)); fieldMap.put("d32", new DevMetricsSeq("当前程序段号", (short) 4, (short) 3)); fieldMap.put("d43", new DevMetricsSeq("手动速度", (short) 4, (short) 4)); fieldMap.put("d27", new DevMetricsSeq("总报警数", (short) 5, (short) 1)); fieldMap.put("d28", new DevMetricsSeq("PLC报警数", (short) 5, (short) 2)); fieldMap.put("d33", new DevMetricsSeq("最后一条报警号", (short) 5, (short) 3)); return fieldMap; } // 使用反射获取实体类中d系列字段的值 // 使用反射获取实体类中 d 系列字段的值,如果字段不存在则忽略 private String getFieldValue(DeviceRealTimeData data, String fieldName) { try { Field field = DeviceRealTimeData.class.getDeclaredField(fieldName); field.setAccessible(true); // 允许访问私有字段 String fieldValue = (String) field.get(data); // 如果字段需要乘以 0.1,则进行转换 if (fieldValue != null && FIELDS_TO_SCALE.contains(fieldName)) { double scaledValue = Double.parseDouble(fieldValue) * 0.1; // fieldValue = String.valueOf(scaledValue); // 保留一位小数 fieldValue = String.format("%.1f", scaledValue); } if (fieldValue != null) { double v = Double.parseDouble(fieldValue); if (v < 0) { fieldValue = "/"; } else { fieldValue = String.format("%.1f", v); } } else { fieldValue = "/"; } return fieldValue; } catch (NoSuchFieldException | IllegalAccessException | NumberFormatException e) { // 忽略没有的字段或无法访问的字段 System.out.println("字段不存在或无法访问: " + fieldName); return null; } } // 将实体类字段插入到数据库 @Override public boolean saveDeviceMetrics(DeviceRealTimeData data, String engineeringNo) { List list = new ArrayList<>(); Map fieldMap = createFieldMapping(); //String formattedDate = data.getLastEditDate(); String formattedDate = DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss"); //根据工单和机台查是否有工艺参数的记录 LambdaUpdateWrapper queryWrapper = new LambdaUpdateWrapper<>(); queryWrapper.eq(DeviceMetrics::getDevNo, data.getDevNo()).eq(DeviceMetrics::getItemNo, engineeringNo).eq(DeviceMetrics::getType, 1); remove(queryWrapper); for (Map.Entry entry : fieldMap.entrySet()) { String fieldKey = entry.getKey(); DevMetricsSeq value = entry.getValue(); // 使用反射从 DeviceRealTimeData 中获取对应字段的值 String fieldValue = getFieldValue(data, fieldKey); // 如果字段值不为空,插入到数据库 if (fieldValue != null) { DeviceMetrics entity = new DeviceMetrics(); entity.setField(value.getField()); entity.setGroupSeq(value.getGroupSeq()); entity.setSeq(value.getSeq()); entity.setValue(fieldValue); entity.setDevNo(data.getDevNo()); entity.setDayDate(formattedDate); entity.setItemNo(engineeringNo); entity.setType((short) 1); list.add(entity); } } //导出到excel //exportDeviceMetricsToExcel(list); return saveBatch(list); } @Override public String PdfToBase64(NumbericalDto barcode) throws Exception { String PDF_URL = "http://121.37.180.207/api/IMWeb/getSOP?gcth=" + barcode.getEngineeringNo(); // 获取 PDF URL String pdfUrl = fetchPdfUrl(PDF_URL, barcode.getEngineeringNo()); if (pdfUrl != null && !pdfUrl.isEmpty()) { // 修正 PDF URL 格式 String correctedUrl = correctUrl(pdfUrl); // 打印修正后的 URL System.out.println("Corrected PDF URL: " + correctedUrl); // 下载 PDF 并转换为图片后转为 Base64 String base64Image = downloadPdfAndConvertToImageBase64(correctedUrl); // 打印 Base64 字符串 System.out.println("Base64 Encoded Image: " + base64Image); return base64Image; } else { throw new Exception(barcode.getEngineeringNo() + " 图号不存在图纸"); } } // 获取 PDF URL private String fetchPdfUrl(String urlstr, String engineeringNo) throws IOException { URL url = new URL(urlstr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } // 使用 FastJSON 解析 JSON 数据 JSONArray jsonResponse = JSONArray.parseArray(response.toString()); if (!jsonResponse.isEmpty()) { JSONObject firstItem = jsonResponse.getJSONObject(0); if (firstItem.containsKey(engineeringNo)) { return firstItem.getString(engineeringNo); } } } return null; // 没有找到 PDF URL,返回 null } // 修正 PDF URL 格式 private String correctUrl(String url) { // Step 1: 去除多余的斜杠 url = url.replaceFirst("http:////", "http://"); return url; } // 下载 PDF 并转换为图片后转为 Base64 private String downloadPdfAndConvertToImageBase64(String pdfUrl) throws IOException { URL url = new URL(pdfUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); try (InputStream inputStream = connection.getInputStream()) { // Load the PDF document using PDFBox PDDocument document = PDDocument.load(inputStream); // Create a PDFRenderer to render the PDF to images PDFRenderer pdfRenderer = new PDFRenderer(document); // Render the first page of the PDF as an image BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(0, 300); // First page at 300 DPI // Convert the image to Base64 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "PNG", byteArrayOutputStream); byte[] imageData = byteArrayOutputStream.toByteArray(); return Base64.getEncoder().encodeToString(imageData); } } /** * 导出设备指标到 Excel 并保存到指定位置 * * @param metricsList 需要导出的设备指标列表 */ public void exportDeviceMetricsToExcel(List metricsList) { // 定义目标文件夹路径 String folderPath = "C:\\out"; // 如果目标文件夹不存在,则创建 File folder = new File(folderPath); if (!folder.exists()) { FileUtil.mkdir(folderPath); } // 获取第一个工单号作为文件名的一部分 String orderNo = metricsList.isEmpty() ? "default" : metricsList.get(0).getItemNo(); // 使用 Hutool 获取当前时间,格式为 24 小时制精确到秒 String timestamp = DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss"); // 生成文件名:工单号 + 时间戳 String fileName = orderNo + "_" + timestamp + ".xlsx"; String filePath = folderPath + File.separator + fileName; // 创建 ExcelWriter,写入到指定文件 try (ExcelWriter writer = ExcelUtil.getWriter(filePath)) { // 定义表头,使用字段的注释作为表头内容 Map headerAlias = new HashMap<>(); headerAlias.put("itemNo", "物料编码"); // 物料编码 headerAlias.put("devNo", "设备编号"); // 设备编号 headerAlias.put("dayDate", "时间"); // 时间 headerAlias.put("field", "参数名"); // 参数名 headerAlias.put("value", "参数值"); // 参数值 // 设置表头别名 writer.setHeaderAlias(headerAlias); // 写入数据并自动生成表头 writer.write(metricsList, true); } // 关闭写入器,释放内存 //writer.close(); //使用了try的另外一种语法, 它会自己在finally调用close方法 System.out.println("Excel 导出成功,文件路径:" + filePath); } }