张世豪
昨天 ef78717c5b956a26b360de44f774fc2b804296c2
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
package xitongshezhi;
 
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
// 新增导入
import publicway.OpenDoor;
import chuankou.Sendmsg;
import chushihua.SlotManager;
 
public class kuaisuquka extends JDialog {
    // 屏幕尺寸常量 - 适配7寸竖屏
    private static final int SCREEN_WIDTH = 600;
    private static final int SCREEN_HEIGHT = 1024;
    // 新增:记录进入快速取卡页面前的轮询状态
    private boolean wasPollingRunning = false;
    private boolean wasPollingPaused = false;
    // 颜色常量
    private static final Color PRIMARY_COLOR = new Color(52, 152, 219);
    private static final Color PRIMARY_DARK_COLOR = new Color(41, 128, 185);
    private static final Color SECONDARY_COLOR = new Color(46, 204, 113);
    private static final Color DANGER_COLOR = new Color(231, 76, 60);
    private static final Color WARNING_COLOR = new Color(243, 156, 18);
    private static final Color DARK_COLOR = new Color(15, 28, 48);
    private static final Color DARK_LIGHT_COLOR = new Color(26, 43, 68);
    private static final Color TEXT_COLOR = new Color(224, 224, 224);
    private static final Color TEXT_LIGHT_COLOR = new Color(160, 200, 255);
    private static final Color CARD_BG_COLOR = new Color(30, 60, 114, 178);
    
    // 优化的颜色常量
    private static final Color BRIGHT_GREEN = new Color(46, 204, 113);  // 有卡 - 绿色
    private static final Color DARK_GRAY = new Color(93, 109, 126);     // 无卡 - 深灰色
    
    // 图标缓存
    private static final Map<String, ImageIcon> ICON_CACHE = new HashMap<>();
    
    // 卡槽状态定义
    private enum SlotStatus {
        HAS_CARD("{卡号}", BRIGHT_GREEN),  // 占位符,实际显示时会替换为具体卡号
        NO_CARD("无卡", DARK_GRAY);
        
        private final String displayName;
        private final Color color;
        
        SlotStatus(String displayName, Color color) {
            this.displayName = displayName;
            this.color = color;
        }
        
        public String getDisplayName() {
            return displayName;
        }
        
        public Color getColor() {
            return color;
        }
    }
    
    // UI组件
    private JPanel mainPanel;
    private JPanel cardSlotsPanel;
    private final List<JButton> slotButtons;
    private final List<SlotStatus> slotStatuses;
    
    // 统计信息
    private JLabel cardsCountLabel;
    
    // 优化的对话框管理
    private JDialog progressDialog;
    private JProgressBar progressBar;
    private JLabel progressLabel;
    private JDialog resultDialog;
    
    // 共享的事件监听器
    private final SlotButtonListener slotButtonListener;
    
    private SlotManager slotManager;
    private Timer refreshTimer; // 新增:刷新定时器
 
    public kuaisuquka(JFrame parent) {
        super(parent, "快速取卡", true);
        slotManager = new SlotManager();
        slotButtons = new ArrayList<>(60);
        slotStatuses = new ArrayList<>(60);
        slotButtonListener = new SlotButtonListener();
        
        // 记录进入快速取卡页面前的轮询状态
        recordPollingStateBeforeEntering();
        
        initializeUI();
        initializeSlots();
        startAutoRefresh(); // 新增:启动自动刷新
        
        // 进入快速取卡页面时暂停轮询
        pausePollingWhenEntering();
    }
    
    /**
     * 启动自动刷新 - 每3秒刷新一次卡槽状态
     */
    private void startAutoRefresh() {
        refreshTimer = new Timer(3000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                refreshSlotStatusFromManager();
            }
        });
        refreshTimer.start();
    }
    
    /**
     * 从SlotManager刷新卡槽状态
     */
    private void refreshSlotStatusFromManager() {
        boolean statusChanged = false;
        
        for (int i = 0; i < 60; i++) {
            int slotId = i + 1;
            String hasCardStatus = SlotManager.getSlotHasCardStatus(slotId);
            
            // 检查是否有卡并且有有效的卡号
            boolean reallyHasCard = false;
            if ("1".equals(hasCardStatus)) {
                Fkj slotInfo = slotManager.getSlotInfo(slotId);
                String cardNumber = slotInfo != null ? slotInfo.getCardNumber() : "-1";
                // 只有当卡号不是-1、null且不为空时,才认为真正有卡
                reallyHasCard = !("-1".equals(cardNumber) && cardNumber != null && !cardNumber.trim().isEmpty());
            }
            
            SlotStatus newStatus = reallyHasCard ? SlotStatus.HAS_CARD : SlotStatus.NO_CARD;
            SlotStatus currentStatus = slotStatuses.get(i);
            
            // 检查状态是否发生变化
            if (newStatus != currentStatus) {
                slotStatuses.set(i, newStatus);
                statusChanged = true;
            }
        }
        
        // 如果状态发生变化,更新显示
        if (statusChanged) {
            updateCardSlotsDisplay();
            updateStatistics();
        } else {
            // 即使状态没变,也要检查卡号是否更新
            updateCardSlotsDisplay();
        }
    }
    /**
     * 记录进入快速取卡页面前的轮询状态
     */
    private void recordPollingStateBeforeEntering() {
        wasPollingRunning = chushihua.lunxun.isPolling();
        wasPollingPaused = chushihua.lunxun.isPaused();
        //System.out.println("进入快速取卡页面,记录轮询状态 - 运行: " + wasPollingRunning + ", 暂停: " + wasPollingPaused);
    }
    
    /**
     * 进入快速取卡页面时暂停轮询
     */
    private void pausePollingWhenEntering() {
        if (chushihua.lunxun.isPolling() && !chushihua.lunxun.isPaused()) {
            chushihua.lunxun.pausePolling();
            //System.out.println("进入快速取卡页面,轮询已暂停");
        }
    }
    
    /**
     * 退出快速取卡页面时恢复轮询
     */
    private void resumePollingWhenExiting() {
        // 只有进入快速取卡页面时轮询是运行状态且未被暂停,才恢复轮询
        if (wasPollingRunning && !wasPollingPaused) {
            if (chushihua.lunxun.isPolling() && chushihua.lunxun.isPaused()) {
                chushihua.lunxun.resumePolling();
                //System.out.println("退出快速取卡页面,轮询已恢复");
            }
        } else {
            //System.out.println("退出快速取卡页面,保持原有轮询状态 - 运行: " + wasPollingRunning + ", 暂停: " + wasPollingPaused);
        }
    }
    
    private void initializeUI() {
        setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        
        // 设置深色主题
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        // 创建主面板
        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setBackground(DARK_COLOR);
        mainPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
        
        // 添加各个区域
        mainPanel.add(createHeaderPanel(), BorderLayout.NORTH);
        mainPanel.add(createControlPanel(), BorderLayout.CENTER);
        mainPanel.add(createFooterPanel(), BorderLayout.SOUTH);
        
        getContentPane().add(mainPanel);
    }
    
    private JPanel createHeaderPanel() {
        JPanel headerPanel = new JPanel(new BorderLayout());
        headerPanel.setOpaque(false);
        headerPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(52, 152, 219, 77)));
        
        // 标题
        JLabel titleLabel = new JLabel("快速取卡");
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 22));
        titleLabel.setForeground(TEXT_COLOR);
        titleLabel.setIcon(getCachedIcon("⚡", 24));
        
        // 返回按钮
        JButton backButton = new JButton("关闭");
        backButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        backButton.setBackground(PRIMARY_COLOR);
        backButton.setForeground(Color.WHITE);
        backButton.setFocusPainted(false);
        backButton.setBorder(BorderFactory.createEmptyBorder(10, 18, 10, 18));
        backButton.setIcon(getCachedIcon("←", 16));
        backButton.addActionListener(e -> dispose());
        
        // 添加悬停效果
        backButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                backButton.setBackground(PRIMARY_DARK_COLOR);
            }
            
            public void mouseExited(java.awt.event.MouseEvent evt) {
                backButton.setBackground(PRIMARY_COLOR);
            }
        });
        
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        titlePanel.setOpaque(false);
        titlePanel.add(titleLabel);
        
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.setOpaque(false);
        buttonPanel.add(backButton);
        
        headerPanel.add(titlePanel, BorderLayout.WEST);
        headerPanel.add(buttonPanel, BorderLayout.EAST);
        
        // 添加装饰线
        JPanel decorLine = new JPanel();
        decorLine.setBackground(PRIMARY_COLOR);
        decorLine.setPreferredSize(new Dimension(100, 3));
        
        JPanel headerWrapper = new JPanel(new BorderLayout());
        headerWrapper.setOpaque(false);
        headerWrapper.add(headerPanel, BorderLayout.CENTER);
        headerWrapper.add(decorLine, BorderLayout.SOUTH);
        
        return headerWrapper;
    }
    
    private JPanel createControlPanel() {
        JPanel controlPanel = new JPanel(new BorderLayout());
        controlPanel.setOpaque(false);
        controlPanel.setBorder(new EmptyBorder(15, 0, 15, 0));
        
        // 打开全部按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.setOpaque(false);
        
        JButton openAllButton = new JButton("打开全部卡槽");
        openAllButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
        openAllButton.setBackground(SECONDARY_COLOR);
        openAllButton.setForeground(Color.WHITE);
        openAllButton.setFocusPainted(false);
        openAllButton.setBorder(BorderFactory.createEmptyBorder(12, 24, 12, 24));
        openAllButton.setIcon(getCachedIcon("🚪", 20));
        openAllButton.addActionListener(e -> openAllSlots());
        
        // 添加悬停效果
        openAllButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                openAllButton.setBackground(brighterColor(SECONDARY_COLOR));
            }
            
            public void mouseExited(java.awt.event.MouseEvent evt) {
                openAllButton.setBackground(SECONDARY_COLOR);
            }
        });
        
        buttonPanel.add(openAllButton);
        
        // 卡槽区域
        JPanel slotsWrapper = new JPanel(new BorderLayout());
        slotsWrapper.setOpaque(false);
        
        // 统计信息
        JPanel statsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        statsPanel.setOpaque(false);
        statsPanel.setBorder(new EmptyBorder(0, 0, 10, 0));
        
        cardsCountLabel = new JLabel("有卡: 0/60");
        cardsCountLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        cardsCountLabel.setForeground(TEXT_LIGHT_COLOR);
        
        statsPanel.add(cardsCountLabel);
        
        // 卡槽网格
        cardSlotsPanel = new JPanel();
        cardSlotsPanel.setLayout(new GridLayout(12, 5, 8, 8));
        cardSlotsPanel.setOpaque(false);
        cardSlotsPanel.setBorder(new EmptyBorder(10, 0, 0, 0));
        
        // 创建滚动面板
        JScrollPane scrollPane = new JScrollPane(cardSlotsPanel);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        scrollPane.getVerticalScrollBar().setUnitIncrement(16);
        scrollPane.setOpaque(false);
        scrollPane.getViewport().setOpaque(false);
        
        slotsWrapper.add(statsPanel, BorderLayout.NORTH);
        slotsWrapper.add(scrollPane, BorderLayout.CENTER);
        
        controlPanel.add(buttonPanel, BorderLayout.NORTH);
        controlPanel.add(slotsWrapper, BorderLayout.CENTER);
        
        return controlPanel;
    }
    
    private JPanel createFooterPanel() {
        JPanel footerPanel = new JPanel();
        footerPanel.setOpaque(false);
        footerPanel.setBorder(new EmptyBorder(10, 0, 0, 0));
                 
        return footerPanel;
    }
    
    private void initializeSlots() {
        // 从 SlotManager 获取卡槽状态
        for (int i = 0; i < 60; i++) {
            int slotId = i + 1;
            String hasCardStatus = SlotManager.getSlotHasCardStatus(slotId);
            
            // 检查是否有卡并且有有效的卡号
            boolean reallyHasCard = false;
            if ("1".equals(hasCardStatus)) {
                Fkj slotInfo = slotManager.getSlotInfo(slotId);
                String cardNumber = slotInfo != null ? slotInfo.getCardNumber() : "-1";
                // 只有当卡号不是-1、null且不为空时,才认为真正有卡
                reallyHasCard = !("-1".equals(cardNumber) && cardNumber != null && !cardNumber.trim().isEmpty());
            }
            
            SlotStatus status = reallyHasCard ? SlotStatus.HAS_CARD : SlotStatus.NO_CARD;
            slotStatuses.add(status);
        }
        
        createCardSlots();
        updateStatistics();
    }
    
    private void createCardSlots() {
        cardSlotsPanel.removeAll();
        slotButtons.clear();
        
        for (int i = 0; i < 60; i++) {
            final int slotId = i + 1;
            SlotStatus status = slotStatuses.get(i);
            
            JButton slotButton = new JButton();
            slotButton.setLayout(new BorderLayout());
            slotButton.setBackground(status.getColor());
            slotButton.setForeground(Color.WHITE);
            slotButton.setFocusPainted(false);
            slotButton.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));
            
            // 添加鼠标悬停效果
            slotButton.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseEntered(java.awt.event.MouseEvent evt) {
                    if (status == SlotStatus.HAS_CARD) {
                        slotButton.setBackground(brighterColor(status.getColor()));
                    }
                }
                
                public void mouseExited(java.awt.event.MouseEvent evt) {
                    slotButton.setBackground(status.getColor());
                }
            });
            
            // 卡槽编号
            JLabel slotIdLabel = new JLabel(String.valueOf(slotId), SwingConstants.CENTER);
            slotIdLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
            slotIdLabel.setForeground(Color.WHITE);
            
            // 卡槽状态 - 修改:有卡时显示卡号,无卡时显示"无卡"
            String displayText;
            if (status == SlotStatus.HAS_CARD) {
                // 从SlotManager获取卡号
                Fkj slotInfo = slotManager.getSlotInfo(slotId);
                String cardNumber = slotInfo != null ? slotInfo.getCardNumber() : "-1";
                // 显示实际卡号
                displayText = cardNumber;
            } else {
                displayText = "无卡";
            }
            
            JLabel statusLabel = new JLabel(displayText, SwingConstants.CENTER);
            statusLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 11));
            statusLabel.setForeground(Color.WHITE);
            statusLabel.setOpaque(true);
            statusLabel.setBackground(new Color(255, 255, 255, 51)); // 半透明白色背景
            statusLabel.setBorder(BorderFactory.createEmptyBorder(2, 8, 2, 8));
            
            slotButton.add(slotIdLabel, BorderLayout.CENTER);
            slotButton.add(statusLabel, BorderLayout.SOUTH);
            
            // 使用共享的监听器
            slotButton.addActionListener(slotButtonListener);
            
            slotButtons.add(slotButton);
            cardSlotsPanel.add(slotButton);
        }
        
        cardSlotsPanel.revalidate();
        cardSlotsPanel.repaint();
    }
    
    // 共享的按钮监听器
    private class SlotButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JButton source = (JButton) e.getSource();
            // 从按钮文本获取卡槽ID
            Component[] components = source.getComponents();
            for (Component comp : components) {
                if (comp instanceof JLabel) {
                    JLabel label = (JLabel) comp;
                    try {
                        int slotId = Integer.parseInt(label.getText());
                        SlotStatus status = slotStatuses.get(slotId - 1);
                        
                        // 如果是有卡状态,执行取卡操作
                        if (status == SlotStatus.HAS_CARD) {
                            takeCard(slotId);
                        }
                        // 发送开门指令(不管是否有卡)
                        sendOpenDoorCommand(slotId);
                        break;
                    } catch (NumberFormatException ex) {
                        // 忽略非数字标签
                    }
                }
            }
        }
    }
    
    // 新增方法:发送单个卡槽开门指令
    private void sendOpenDoorCommand(int slotId) {
        try {
            // 生成开门指令
            String command = OpenDoor.openOneDoor(slotId, OpenDoor.TYPE_ADMIN);
            
            // 发送串口指令
            boolean sent = Sendmsg.sendMessage(command);
            
            if (sent) {
                //System.out.println("成功发送开门指令到卡槽 " + slotId);
            } else {
                System.err.println("发送开门指令到卡槽 " + slotId + " 失败");
            }
        } catch (Exception e) {
            System.err.println("生成开门指令失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    private void takeCard(int slotId) {
        int index = slotId - 1;
        
        // 更新卡槽状态为无卡
        slotStatuses.set(index, SlotStatus.NO_CARD);
        
        // 调用 SlotManager 的changgehaska方法更新状态
        SlotManager.changgehaska(slotId, "1"); // "1"表示管理员操作
        
        updateCardSlotsDisplay();
        updateStatistics();
    }
    
    // 优化的卡槽显示更新
 // 修改更新卡槽显示的方法
    private void updateCardSlotsDisplay() {
        for (int i = 0; i < 60; i++) {
            JButton slotButton = slotButtons.get(i);
            SlotStatus status = slotStatuses.get(i);
            
            // 只更新改变的状态
            if (!slotButton.getBackground().equals(status.getColor())) {
                slotButton.setBackground(status.getColor());
            }
            
            // 更新状态标签 - 修改:有卡时显示卡号,无卡时显示"无卡"
            Component[] components = slotButton.getComponents();
            for (Component comp : components) {
                if (comp instanceof JLabel) {
                    JLabel label = (JLabel) comp;
                    // 找到状态标签(较小的字体)
                    if (label.getFont().getSize() == 11) {
                        String displayText;
                        if (status == SlotStatus.HAS_CARD) {
                            // 从SlotManager获取卡号
                            Fkj slotInfo = slotManager.getSlotInfo(i + 1);
                            String cardNumber = slotInfo != null ? slotInfo.getCardNumber() : "-1";
                            // 显示实际卡号
                            displayText = cardNumber;
                        } else {
                            displayText = "无卡";
                        }
                        label.setText(displayText);
                        break;
                    }
                }
            }
        }
        
        cardSlotsPanel.revalidate();
        cardSlotsPanel.repaint();
    }
    
    private void openAllSlots() {
        int openedCount = 0;
        
        // 更新所有有卡卡槽为无卡
        for (int i = 0; i < slotStatuses.size(); i++) {
            if (slotStatuses.get(i) == SlotStatus.HAS_CARD) {
                slotStatuses.set(i, SlotStatus.NO_CARD);
                // 调用 SlotManager 的changgehaska方法更新状态
                SlotManager.changgehaska(i + 1, "1"); // "1"表示管理员操作
                openedCount++;
            }
        }
        
        if (openedCount > 0) {
            updateCardSlotsDisplay();
            updateStatistics();
        }
        
        // 修改:直接调用串口发送方法,不显示进度对话框
        openAllSlotsWithSerialCommands(openedCount);
    }
    
    // 新增方法:通过串口发送开门指令
    private void openAllSlotsWithSerialCommands(int openedCount) {
        // 修改:直接调用OpenDoor的异步开门方法,不显示进度对话框
        OpenDoor.openAllSlotsAsync(60, 250, OpenDoor.TYPE_ADMIN, new OpenDoor.OpenDoorCallback() {
            @Override
            public void onProgress(int currentSlot, int totalSlots, String command) {
                // 发送串口指令
                boolean sent = Sendmsg.sendMessage(command);
                if (!sent) {
                    System.err.println("发送指令失败: " + command);
                }
            }
            
            @Override
            public void onComplete(String[] commands) {
                SwingUtilities.invokeLater(() -> {
                    String message;
                    if (openedCount > 0) {
                        message = "已成功打开全部 " + openedCount + " 个有卡卡槽\n发送了 " + commands.length + " 条开门指令";
                    } else {
                        message = "已发送 " + commands.length + " 条开门指令到所有卡槽";
                    }
                    showResultDialog("success", "操作成功", message);
                });
            }
            
            @Override
            public void onError(Exception error) {
                SwingUtilities.invokeLater(() -> {
                    showResultDialog("error", "操作失败", 
                        "发送开门指令时发生错误: " + error.getMessage());
                    error.printStackTrace();
                });
            }
        });
    }
    
    // 优化的统计计算
    private void updateStatistics() {
        int hasCardCount = 0;
        for (SlotStatus status : slotStatuses) {
            if (status == SlotStatus.HAS_CARD) {
                hasCardCount++;
            }
        }
        
        cardsCountLabel.setText("有卡: " + hasCardCount + "/60");
    }
    
    // 优化的结果对话框显示
    private void showResultDialog(String type, String title, String message) {
        if (resultDialog == null) {
            createResultDialog();
        }
        updateResultDialog(type, title, message);
        resultDialog.setVisible(true);
    }
    
    private void createResultDialog() {
        resultDialog = new JDialog(this, "", true);
        resultDialog.setSize(400, 250);
        resultDialog.setLocationRelativeTo(this);
        resultDialog.setResizable(false);
        resultDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(25, 25, 25, 25));
        contentPanel.setBackground(DARK_LIGHT_COLOR);
        
        // 图标和标题
        JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        headerPanel.setOpaque(false);
        
        JLabel iconLabel = new JLabel();
        iconLabel.setFont(new Font("Segoe UI Emoji", Font.PLAIN, 32));
        
        JLabel titleLabel = new JLabel();
        titleLabel.setFont(new Font("Microsoft YaHei", Font.BOLD, 20));
        titleLabel.setForeground(TEXT_COLOR);
        
        headerPanel.add(iconLabel);
        headerPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        headerPanel.add(titleLabel);
        
        // 消息内容
        JLabel messageLabel = new JLabel();
        messageLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
        messageLabel.setForeground(TEXT_LIGHT_COLOR);
        messageLabel.setHorizontalAlignment(SwingConstants.CENTER);
        messageLabel.setBorder(new EmptyBorder(15, 0, 25, 0));
        
        // 确定按钮
        JButton confirmButton = new JButton("确定");
        confirmButton.setFont(new Font("Microsoft YaHei", Font.BOLD, 14));
        confirmButton.setBackground(SECONDARY_COLOR);
        confirmButton.setForeground(Color.WHITE);
        confirmButton.setFocusPainted(false);
        confirmButton.setBorder(BorderFactory.createEmptyBorder(10, 30, 10, 30));
        confirmButton.addActionListener(e -> resultDialog.dispose());
        
        // 添加悬停效果
        confirmButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                confirmButton.setBackground(brighterColor(SECONDARY_COLOR));
            }
            
            public void mouseExited(java.awt.event.MouseEvent evt) {
                confirmButton.setBackground(SECONDARY_COLOR);
            }
        });
        
        JPanel buttonPanel = new JPanel();
        buttonPanel.setOpaque(false);
        buttonPanel.add(confirmButton);
        
        contentPanel.add(headerPanel, BorderLayout.NORTH);
        contentPanel.add(messageLabel, BorderLayout.CENTER);
        contentPanel.add(buttonPanel, BorderLayout.SOUTH);
        
        resultDialog.add(contentPanel);
    }
    
    private void updateResultDialog(String type, String title, String message) {
        resultDialog.setTitle(title);
        
        Component[] components = ((JPanel)resultDialog.getContentPane().getComponent(0)).getComponents();
        
        // 更新图标和标题
        JPanel headerPanel = (JPanel) components[0];
        JLabel iconLabel = (JLabel) headerPanel.getComponent(0);
        JLabel titleLabel = (JLabel) headerPanel.getComponent(2);
        
        if ("success".equals(type)) {
            iconLabel.setText("✅");
        } else if ("warning".equals(type)) {
            iconLabel.setText("⚠️");
        } else if ("error".equals(type)) {
            iconLabel.setText("❌");
        } else {
            iconLabel.setText("ℹ️");
        }
        
        titleLabel.setText(title);
        
        // 更新消息
        JLabel messageLabel = (JLabel) components[1];
        messageLabel.setText("<html><div style='text-align: center;'>" + message.replace("\n", "<br>") + "</div></html>");
    }
    
    private Color brighterColor(Color color) {
        int r = Math.min(255, color.getRed() + 30);
        int g = Math.min(255, color.getGreen() + 30);
        int b = Math.min(255, color.getBlue() + 30);
        return new Color(r, g, b);
    }
    
    // 优化的图标创建方法
    private ImageIcon getCachedIcon(String emoji, int size) {
        String cacheKey = emoji + "_" + size;
        if (ICON_CACHE.containsKey(cacheKey)) {
            return ICON_CACHE.get(cacheKey);
        }
        
        JLabel label = new JLabel(emoji);
        label.setFont(new Font("Segoe UI Emoji", Font.PLAIN, size));
        label.setSize(size, size);
        
        // 创建一个图像
        java.awt.image.BufferedImage image = new java.awt.image.BufferedImage(
            size, size, java.awt.image.BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = image.createGraphics();
        
        // 启用抗锯齿
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        label.print(g2);
        g2.dispose();
        
        ImageIcon icon = new ImageIcon(image);
        ICON_CACHE.put(cacheKey, icon);
        return icon;
    }
    
    @Override
    public void dispose() {
        // 停止刷新定时器
        if (refreshTimer != null && refreshTimer.isRunning()) {
            refreshTimer.stop();
        }
        
        // 显式清理资源
        if (slotButtons != null) {
            slotButtons.clear();
        }
        if (slotStatuses != null) {
            slotStatuses.clear();
        }
        if (progressDialog != null) {
            progressDialog.dispose();
            progressDialog = null;
        }
        if (resultDialog != null) {
            resultDialog.dispose();
            resultDialog = null;
        }
        
        // 恢复轮询状态
        resumePollingWhenExiting();
        
        super.dispose();
    }
    
    // 静态方法:从系统设置页面调用
    public static void showQuickPickupDialog(JFrame parent) {
        SwingUtilities.invokeLater(() -> {
            kuaisuquka dialog = new kuaisuquka(parent);
            dialog.setVisible(true);
        });
    }   
}