jinlin
2025-04-05 92705ac08a97ddc4904795f024723aab69e1abd8
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
package com.example.client.service;
 
 
import com.example.client.Login;
import com.example.client.dto.JComboBoxItem;
import com.example.client.utils.GBC;
import com.example.client.utils.MultiSelectComboBox;
import com.example.client.utils.WaitUtil;
import com.example.server.DataSync.service.DataSyncService;
import com.example.server.teamGroup.service.SysTeamGroupClassService;
import com.example.server.user.model.SysUser;
import com.example.server.utils.UserAndSiteUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
 
 
@Service
public class ConfigManageService {
    @Autowired
    private SysTeamGroupClassService sysTeamGroupClassService;
    @Value("${data.config-path}")
    private String configPath;
 
    public JPanel createTable(Integer width, Integer height, JFrame jFrame) {
        Properties properties = new Properties();
 
        String site = "";
        String teamGroup = "";
        String team = "";
 
        String path = Login.class.getClassLoader().getResource("config.properties").getPath();
        InputStream inStream = null;
        File dir = new File(path);
        if (dir.exists()) {
            inStream = Login.class.getClassLoader().getResourceAsStream("config.properties");
        } else {
            try {
                inStream = new FileInputStream(configPath);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        Reader reader = null;
        try {
            reader = new InputStreamReader(inStream, StandardCharsets.UTF_8);
            // 使用Properties.load(Reader)加载属性文件
            properties.load(reader);
            site = properties.get("site").toString();
            teamGroup = properties.get("teamGroup").toString();
            team = properties.get("team").toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        JPanel panel = new JPanel();
 
        GridBagLayout layout = new GridBagLayout();
        panel.setLayout(layout);
 
        panel.setPreferredSize(new Dimension(width - 20, height));
        String[] siteList = new String[]{
                "工作组", "厂家", "TD"
        };
        JComboBoxItem[] teamList = sysTeamGroupClassService.getTeamList(null);
 
        JLabel JLabel1 = new JLabel("所属机器");
        JComboBox<String> comboBox = new JComboBox<>(siteList);
        comboBox.setPreferredSize(new Dimension(185,28));
        if (StringUtils.isNotBlank(site)) {
            comboBox.setSelectedItem(site);
        }
 
        JLabel JLabel2 = new JLabel("所属TD");
        JTextField teamName = new JTextField(16);
        if (StringUtils.isNotBlank(team)) {
            teamName.setText(team.replace("TD", ""));
        }
 
        JLabel JLabel3 = new JLabel("可操作的专业");
        MultiSelectComboBox comboBox2 = new MultiSelectComboBox(teamList);
        comboBox2.setPreferredSize(new Dimension(500, 25));
        if (StringUtils.isNotBlank(teamGroup) && !teamGroup.equals("null")) {
            Set<Long> selectedIds = new HashSet<>();
            String[] idParts = teamGroup.split(",");
            for (String idPart : idParts) {
                selectedIds.add(Long.parseLong(idPart.trim())); // 将字符串转换为 Long 类型并添加到集合中
            }
 
            // 将 ID 转换为对应的 JComboBoxItem 对象
            Set<JComboBoxItem> selectedItems = new HashSet<>();
            for (JComboBoxItem item : teamList) {
                if (selectedIds.contains(item.getId())) { // 检查 Long 类型的 ID 是否匹配
                    selectedItems.add(item); // 添加到选中项集合
                }
            }
            comboBox2.setSelectedItems(selectedItems);
        }
 
 
        JButton save = new JButton("保存");
 
        save.addActionListener((e) -> {
            try {
                String path2 = Login.class.getClassLoader().getResource("config.properties").getPath();
                OutputStream outputStream = null;
                File dir2 = new File(path2);
                if (dir2.exists()) {
                    outputStream = new FileOutputStream(path2);
                } else {
                    try {
                        outputStream = new FileOutputStream(configPath);
                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    }
                }
                Set<JComboBoxItem> selectedItems = comboBox2.getSelectedItems();
                StringBuilder selectedIds = new StringBuilder();
                for (JComboBoxItem item : selectedItems) {
                    selectedIds.append(item.getId()).append(",");
                }
                properties.setProperty("site", comboBox.getSelectedItem().toString());
                properties.setProperty("team", teamName.getText() + "TD");
                properties.setProperty("teamGroup", String.valueOf(selectedIds));
                UserAndSiteUtils.remove("site", "site");
                UserAndSiteUtils.put("site", "site", comboBox.getSelectedItem().toString());
                properties.store(outputStream, "rxkj");
                outputStream.close();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ep) {
                ep.printStackTrace();
            }
        });
 
 
        panel.add(JLabel1, new GBC(0, 0, 1, 1).setAnchor(GBC.SOUTHEAST).setInsets(5));
        panel.add(comboBox, new GBC(1, 0, 1, 1).setAnchor(GBC.SOUTHWEST).setInsets(5));
 
        panel.add(JLabel2, new GBC(0, 1, 1, 1).setAnchor(GBC.SOUTHEAST).setInsets(5));
        panel.add(teamName, new GBC(1, 1, 1, 1).setAnchor(GBC.SOUTHWEST).setInsets(5));
 
        panel.add(JLabel3, new GBC(0, 2, 1, 1).setAnchor(GBC.SOUTHEAST).setInsets(5));
        panel.add(comboBox2, new GBC(1, 2, 2, 1).setAnchor(GBC.SOUTHWEST).setInsets(5));
 
        panel.add(save, new GBC(0, 3, 2, 1).setWeight(0, 0));
 
 
        return panel;
    }
}