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;
|
}
|
}
|