tjx
2 天以前 a53e6872c70ba7c6c870627007ece21df9bcedbc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
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<DeviceMetricsMapper, DeviceMetrics> implements DeviceMetricsService {
 
 
    // 需要乘以 0.1 的字段列表
    private static final List<String> 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<VOrder> query = new LambdaQueryWrapper<>();
        query.eq(VOrder::getId, barcode.getOrderId());
        VOrder womdaa = vOrderService.getOne(query, false);
 
        if (womdaa == null) {
            return false;
        }
 
        LambdaQueryWrapper<DevMachine> 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<Boolean> list = new ArrayList<Boolean>();
 
        //根据工单和机台查是否有工艺参数的记录
        LambdaUpdateWrapper<DeviceMetrics> 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<Device> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Device::getDevNo, dev);
        List<Device> list1 = deviceService.list(wrapper);
 
 
        int machineNo = Integer.parseInt(barcode.getMachineNo());
 
        list1.forEach(s -> {
            ApiDataResult<DeviceRealTimeData> 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<DeviceRealTimeData> result = numericalService.getDeviceRealTimeDataApiDataResult(one.getDevNo());
//
//        DeviceRealTimeData data = result.getData();
//        return saveDeviceMetrics(data, womdaa.getEngineeringNo());
    }
 
    @Override
    public boolean manualSynchronizationBycl(NumbericalDto barcode) throws Exception {
        LambdaQueryWrapper<VOrderDetail> query = new LambdaQueryWrapper<>();
        query.eq(VOrderDetail::getId, barcode.getOrderId());
        VOrderDetail womdaa = vOrderDetailService.getOne(query, false);
 
        if (womdaa == null) {
            return false;
        }
 
        LambdaQueryWrapper<DevMacBycl> 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<Boolean> list = new ArrayList<Boolean>();
 
        //根据工单和机台查是否有工艺参数的记录
        LambdaUpdateWrapper<DeviceMetrics> 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<DeviceBycl> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(DeviceBycl::getDevNo, dev);
        List<DeviceBycl> list1 = deviceByclService.list(wrapper);
 
        list1.forEach(s -> {
            ApiDataResult<DeviceRealTimeData> 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<DeviceMetrics> list = new ArrayList<>();
 
        // 根据 machineNo 选择字段映射
        Map<String, DevMetricsSeq> fieldMap = createFieldMappingForBycl(machineNo);
 
        String formattedDate = DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss");
 
        for (Map.Entry<String, DevMetricsSeq> 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<DeviceMetrics> list = new ArrayList<>();
 
        String seq = devNo.substring(16);
 
        LambdaQueryWrapper<Devicedatastandardmoditem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Devicedatastandardmoditem::getModname, "UA注塑机")
                .eq(Devicedatastandardmoditem::getIsShow, 1)
                .eq(Devicedatastandardmoditem::getDatainfo, seq);
 
        List<Devicedatastandardmoditem> deviceList = devicedatastandardmoditemService.list(wrapper);
 
        Map<String, DevMetricsSeq> 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<String, DevMetricsSeq> 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<String, DevMetricsSeq> createFieldMapping() {
        Map<String, DevMetricsSeq> 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<String, DevMetricsSeq> createFieldMappingForBycl(String machineNo) {
        // 定义特殊机台号列表
        List<String> 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<String, DevMetricsSeq> createCncFieldMapping() {
        Map<String, DevMetricsSeq> 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<String, DevMetricsSeq> createGeneralFieldMapping() {
        Map<String, DevMetricsSeq> 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<DeviceMetrics> list = new ArrayList<>();
 
        Map<String, DevMetricsSeq> fieldMap = createFieldMapping();
 
        //String formattedDate = data.getLastEditDate();
        String formattedDate = DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss");
 
        //根据工单和机台查是否有工艺参数的记录
        LambdaUpdateWrapper<DeviceMetrics> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(DeviceMetrics::getDevNo, data.getDevNo()).eq(DeviceMetrics::getItemNo, engineeringNo).eq(DeviceMetrics::getType, 1);
 
        remove(queryWrapper);
 
        for (Map.Entry<String, DevMetricsSeq> 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<DeviceMetrics> 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<String, String> 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);
    }
}