jinlin
2025-03-18 d30e385951ce03335a5023f0775fd144da3c0b88
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
package com.example.client.service;
 
 
import com.example.client.dto.ColumnDto;
import com.example.client.utils.CommonTable;
import com.example.client.utils.GBC;
import com.example.client.utils.WaitUtil;
import com.example.server.DataSync.service.DataSyncService;
import com.example.server.progressTrack.dao.ImportRecordDao;
import com.example.server.progressTrack.model.ExportRecord;
import com.example.server.progressTrack.model.ImportRecord;
import com.example.server.utils.CacheUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.swing.*;
 
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
 
@Service
public class DataImportManageService {
    @Value("${data.imgDir}")
    private String imgPath;
    @Autowired
    DataSyncService dataSyncService;
    @Autowired
    ImportRecordDao importRecordDao;
    @Autowired
    ImportDataService importDataService;
    private List<ColumnDto> columnDto;
    private JTable table;
 
    public JPanel createTable(Integer width, Integer height, JFrame jFrame) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setPreferredSize(new Dimension(width-10,height));
 
        JPanel left = new JPanel(new BorderLayout());
        left.setPreferredSize(new Dimension((width-10)/2,height));
 
        JPanel right = new JPanel(new BorderLayout());
        right.setPreferredSize(new Dimension((width-10)/2,height));
 
        JPanel top = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 为 left 设置布局
        top.setPreferredSize(new Dimension((width - 10) / 2, 50));
 
        JPanel center = new JPanel(); // 为 left 设置布局
        GridBagLayout layout = new GridBagLayout();
        center.setLayout(layout);
        center.setPreferredSize(new Dimension((width - 10) / 2, (height-50)/3));
 
        JPanel south = new JPanel(new BorderLayout()); // 为 left 设置布局
        south.setPreferredSize(new Dimension((width - 10) / 2, (height-50)/3*2));
 
 
        String site = (String) CacheUtils.get("site", "site");
        JLabel label = new JLabel(site + "机器");
        top.add(label);
 
        JPanel jPanel = importDataService.FileUpload(jFrame,700,100);
        JButton btnImport = new JButton("导入");
        center.add(jPanel, new GBC(0, 0, 1, 1).setInsets(5));
        center.add(btnImport, new GBC(0, 1, 1, 1).setInsets(5));
 
 
        JLabel label1 = new JLabel("提示信息");
        JTextArea tips = new JTextArea(7, 30);
        tips.setEnabled(false);
        tips.setForeground(Color.BLACK); // 设置为黑色
        tips.setDisabledTextColor(Color.BLACK);
        tips.setLineWrap(true);
        south.add(label1,BorderLayout.NORTH);
        south.add(tips,BorderLayout.CENTER);
 
 
        btnImport.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 创建旋转等待框的实例(假设WaitUtil是一个自定义的Swing组件)
                final WaitUtil waitUtil = new WaitUtil(imgPath, "数据正在导入,请稍候");
 
                // 开始上传文件的异步任务
                SwingWorker<String, Void> sw = new SwingWorker<String, Void>() {
                    @Override
                    protected String doInBackground() throws Exception {
                        String flag =  importDataService.UnzipFile(tips);
                        return flag;
                    }
 
                    @Override
                    protected void done() {
                        try {
                            // 获取上传结果(在这个例子中,我们假设上传方法返回Boolean类型)
                            String uploadSucceeded = get();
 
                            // 刷新表格数据(如果上传成功)
                            if (uploadSucceeded.equals("true")) {
                                waitUtil.dispose();
                                tips.setText("导入成功"+new Date());
                            } else {
                                waitUtil.dispose();
                                tips.setText("导入失败"+new Date());
                                JOptionPane.showMessageDialog(null, uploadSucceeded, "提示", JOptionPane.ERROR_MESSAGE);
                            }
 
                        } catch (Exception ex) {
                            ex.printStackTrace();
                            waitUtil.dispose();
                        }
                    }
                };
                // 执行异步任务
                sw.execute();
                waitUtil.setVisible(true);
            }
        });
 
        left.add(top,BorderLayout.NORTH);
        left.add(center,BorderLayout.CENTER);
        left.add(south,BorderLayout.SOUTH);
 
        JLabel label2 = new JLabel("导入记录");
        List<ImportRecord> list  = importRecordDao.getList();
 
        columnDto = new ArrayList<>();
        //columnDto.add(new ColumnDto("ID", "id", -1, null,false));
        columnDto.add(new ColumnDto("序号", "", 200, "autoCreate", false, null,null));
        columnDto.add(new ColumnDto("数据源", "importSite", 200, null, false, null,null));
        columnDto.add(new ColumnDto("导入时间", "createDate", 200, null, false, null,null));
        columnDto.add(new ColumnDto("操作人", "userName", 200, null, false, null,null));
        table = CommonTable.createCommonTable(list, columnDto);
        table.setRowHeight(25);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
 
        right.add(label2, BorderLayout.NORTH);
        right.add(new JScrollPane(table), BorderLayout.CENTER);
 
        panel.add(left,BorderLayout.WEST);
        panel.add(right, BorderLayout.CENTER);
 
        return panel;
    }
 
    public void refresh(List<ImportRecord> list){
        CommonTable.refreshTable(list, columnDto,table);
    }
}