WorkRecordPanel.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. package com.mes.ui;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import javax.swing.*;
  5. import javax.swing.table.DefaultTableCellRenderer;
  6. import javax.swing.table.DefaultTableModel;
  7. import javax.swing.table.TableColumn;
  8. import java.awt.*;
  9. import java.awt.event.ComponentAdapter;
  10. import java.awt.event.ComponentEvent;
  11. import java.util.List;
  12. /**
  13. * 工作记录面板组件
  14. * 支持分页查询和展示工作记录数据
  15. */
  16. public class WorkRecordPanel extends JPanel {
  17. private static final Logger log = LoggerFactory.getLogger(WorkRecordPanel.class);
  18. // 表格列名
  19. private static final String[] COLUMN_NAMES = {"工件码", "工位号", "工位结果", "作业人员", "日期"};
  20. // 分页配置
  21. private static final int PAGE_SIZE = 20;
  22. // 查询条件
  23. private String oprno; // 工位号(null或空则查全部)
  24. private String lineSn; // 产线编号
  25. private boolean isAllStation; // 是否是"所有工位"模式
  26. // 分页状态
  27. private int pageNo = 1;
  28. private long totalCount = 0;
  29. private int totalPages = 0;
  30. // UI组件
  31. private JTable table;
  32. private DefaultTableModel tableModel;
  33. private JLabel pageLabel;
  34. private JButton btnPrevious;
  35. private JButton btnNext;
  36. private JButton btnRefresh;
  37. private JButton btnSearch;
  38. private JLabel statusLabel;
  39. private JTextField snSearchField; // 工件码搜索框
  40. private JTextField oprnoSearchField; // 工位号搜索框(仅"所有工位"模式)
  41. private JLabel snLabel;
  42. private JLabel oprnoLabel;
  43. /**
  44. * 构造函数
  45. * @param oprno 工位号(如OP150A查询该工位,null或空则查所有工位)
  46. * @param lineSn 产线编号
  47. */
  48. public WorkRecordPanel(String oprno, String lineSn) {
  49. this.oprno = oprno;
  50. this.lineSn = lineSn;
  51. this.isAllStation = (oprno == null || oprno.isEmpty());
  52. initUI();
  53. }
  54. /**
  55. * 初始化UI
  56. */
  57. private void initUI() {
  58. setLayout(null);
  59. setBackground(Color.WHITE);
  60. // 顶部控制区
  61. JPanel topPanel = new JPanel();
  62. topPanel.setLayout(null);
  63. topPanel.setBounds(0, 0, 1200, 50);
  64. topPanel.setBackground(new Color(248, 248, 248));
  65. add(topPanel);
  66. int xPos = 10;
  67. // 状态标签
  68. String displayOprno = isAllStation ? "所有工位" : oprno;
  69. statusLabel = new JLabel("工位:" + displayOprno);
  70. statusLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  71. statusLabel.setBounds(xPos, 10, 160, 30);
  72. topPanel.add(statusLabel);
  73. xPos += 165;
  74. // 工件码搜索
  75. snLabel = new JLabel("工件码:");
  76. snLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  77. snLabel.setBounds(xPos, 10, 50, 30);
  78. topPanel.add(snLabel);
  79. xPos += 52;
  80. snSearchField = new JTextField();
  81. snSearchField.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  82. snSearchField.setBounds(xPos, 10, 200, 30);
  83. snSearchField.addActionListener(e -> doSearch()); // 回车搜索
  84. topPanel.add(snSearchField);
  85. xPos += 210;
  86. // 工位号搜索(仅"所有工位"模式显示)
  87. if (isAllStation) {
  88. oprnoLabel = new JLabel("工位:");
  89. oprnoLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  90. oprnoLabel.setBounds(xPos, 10, 40, 30);
  91. topPanel.add(oprnoLabel);
  92. xPos += 42;
  93. oprnoSearchField = new JTextField();
  94. oprnoSearchField.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  95. oprnoSearchField.setBounds(xPos, 10, 100, 30);
  96. oprnoSearchField.addActionListener(e -> doSearch()); // 回车搜索
  97. topPanel.add(oprnoSearchField);
  98. xPos += 110;
  99. }
  100. // 查询按钮
  101. btnSearch = new JButton("查询");
  102. btnSearch.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  103. btnSearch.setBounds(xPos, 10, 70, 30);
  104. btnSearch.addActionListener(e -> doSearch());
  105. topPanel.add(btnSearch);
  106. xPos += 80;
  107. // 刷新按钮
  108. btnRefresh = new JButton("刷新");
  109. btnRefresh.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  110. btnRefresh.setBounds(xPos, 10, 70, 30);
  111. btnRefresh.addActionListener(e -> {
  112. clearSearchFields();
  113. refreshData();
  114. });
  115. topPanel.add(btnRefresh);
  116. xPos += 80;
  117. // 上一页按钮
  118. btnPrevious = new JButton("上一页");
  119. btnPrevious.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  120. btnPrevious.setBounds(xPos, 10, 80, 30);
  121. btnPrevious.addActionListener(e -> {
  122. if (pageNo > 1) {
  123. pageNo--;
  124. loadData();
  125. }
  126. });
  127. topPanel.add(btnPrevious);
  128. xPos += 85;
  129. // 页码显示
  130. pageLabel = new JLabel("0/0");
  131. pageLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  132. pageLabel.setHorizontalAlignment(SwingConstants.CENTER);
  133. pageLabel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
  134. pageLabel.setBounds(xPos, 10, 70, 30);
  135. topPanel.add(pageLabel);
  136. xPos += 75;
  137. // 下一页按钮
  138. btnNext = new JButton("下一页");
  139. btnNext.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  140. btnNext.setBounds(xPos, 10, 80, 30);
  141. btnNext.addActionListener(e -> {
  142. if (pageNo < totalPages) {
  143. pageNo++;
  144. loadData();
  145. }
  146. });
  147. topPanel.add(btnNext);
  148. // 表格面板
  149. JPanel tablePanel = new JPanel();
  150. tablePanel.setBounds(0, 50, 1200, 518);
  151. tablePanel.setLayout(new GridLayout(1, 1, 0, 0));
  152. add(tablePanel);
  153. // 创建表格模型
  154. tableModel = new DefaultTableModel(COLUMN_NAMES, 0) {
  155. @Override
  156. public boolean isCellEditable(int row, int column) {
  157. return false;
  158. }
  159. };
  160. // 创建表格
  161. table = new JTable(tableModel);
  162. table.setRowHeight(32);
  163. table.setFont(new Font("微软雅黑", Font.PLAIN, 13));
  164. table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
  165. table.getTableHeader().setReorderingAllowed(false);
  166. // 设置列宽
  167. setColumnWidths();
  168. // 设置居中渲染器
  169. DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
  170. centerRenderer.setHorizontalAlignment(JLabel.CENTER);
  171. for (int i = 0; i < table.getColumnCount(); i++) {
  172. table.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
  173. }
  174. // 添加滚动面板
  175. JScrollPane scrollPane = new JScrollPane(table);
  176. tablePanel.add(scrollPane);
  177. // 添加组件大小变化监听器
  178. addComponentListener(new ComponentAdapter() {
  179. @Override
  180. public void componentResized(ComponentEvent e) {
  181. int w = getWidth();
  182. int h = getHeight();
  183. // 调整顶部面板宽度
  184. topPanel.setBounds(0, 0, w, 50);
  185. // 调整表格面板
  186. if (h > 50) {
  187. tablePanel.setBounds(0, 50, w, h - 50);
  188. }
  189. topPanel.revalidate();
  190. tablePanel.revalidate();
  191. }
  192. });
  193. }
  194. /**
  195. * 设置列宽
  196. */
  197. private void setColumnWidths() {
  198. TableColumn column;
  199. // 工件码
  200. column = table.getColumnModel().getColumn(0);
  201. column.setPreferredWidth(260);
  202. // 工位号
  203. column = table.getColumnModel().getColumn(1);
  204. column.setPreferredWidth(90);
  205. // 工位结果
  206. column = table.getColumnModel().getColumn(2);
  207. column.setPreferredWidth(70);
  208. // 作业人员
  209. column = table.getColumnModel().getColumn(3);
  210. column.setPreferredWidth(100);
  211. // 日期
  212. column = table.getColumnModel().getColumn(4);
  213. column.setPreferredWidth(150);
  214. }
  215. /**
  216. * 清空搜索框
  217. */
  218. private void clearSearchFields() {
  219. snSearchField.setText("");
  220. if (oprnoSearchField != null) {
  221. oprnoSearchField.setText("");
  222. }
  223. }
  224. /**
  225. * 执行搜索
  226. */
  227. private void doSearch() {
  228. pageNo = 1;
  229. loadData();
  230. }
  231. /**
  232. * 刷新数据(重置到第一页)
  233. */
  234. public void refreshData() {
  235. pageNo = 1;
  236. loadData();
  237. }
  238. /**
  239. * 加载数据
  240. */
  241. public void loadData() {
  242. // 获取搜索条件
  243. String searchSn = snSearchField.getText().trim();
  244. String searchOprno = oprno; // 默认使用构造函数传入的工位号
  245. System.out.println("search="+searchOprno);
  246. // 如果是"所有工位"模式,检查是否有工位号搜索条件
  247. if (isAllStation && oprnoSearchField != null) {
  248. String inputOprno = oprnoSearchField.getText().trim();
  249. if (!inputOprno.isEmpty()) {
  250. searchOprno = inputOprno;
  251. }
  252. }
  253. final String finalSearchOprno = searchOprno;
  254. final String finalSearchSn = searchSn;
  255. // 在后台线程中执行数据加载
  256. SwingWorker<WorkRecordResp, Void> worker = new SwingWorker<WorkRecordResp, Void>() {
  257. @Override
  258. protected WorkRecordResp doInBackground() throws Exception {
  259. // 更新状态
  260. SwingUtilities.invokeLater(() -> {
  261. statusLabel.setText("正在加载...");
  262. setButtonsEnabled(false);
  263. });
  264. // 调用接口查询数据
  265. return DataUtil.getWorkRecordList(finalSearchOprno, finalSearchSn, pageNo, PAGE_SIZE);
  266. }
  267. @Override
  268. protected void done() {
  269. try {
  270. WorkRecordResp resp = get();
  271. updateUI(resp, finalSearchOprno);
  272. } catch (Exception e) {
  273. log.error("加载工作记录失败: {}", e.getMessage());
  274. updateUIError("加载失败: " + e.getMessage());
  275. }
  276. }
  277. };
  278. worker.execute();
  279. }
  280. /**
  281. * 设置按钮启用状态
  282. */
  283. private void setButtonsEnabled(boolean enabled) {
  284. btnSearch.setEnabled(enabled);
  285. btnRefresh.setEnabled(enabled);
  286. btnPrevious.setEnabled(enabled && pageNo > 1);
  287. btnNext.setEnabled(enabled && pageNo < totalPages);
  288. }
  289. /**
  290. * 更新UI显示数据
  291. */
  292. private void updateUI(WorkRecordResp resp, String queryOprno) {
  293. // 清空表格
  294. tableModel.setRowCount(0);
  295. if (resp == null || !resp.isResult()) {
  296. String msg = resp != null ? resp.getMessage() : "请求失败";
  297. updateUIError(msg);
  298. return;
  299. }
  300. // 更新分页信息
  301. totalCount = resp.getCount();
  302. totalPages = (int) Math.ceil((double) totalCount / PAGE_SIZE);
  303. if (totalPages == 0) totalPages = 1;
  304. // 更新页码显示
  305. pageLabel.setText(pageNo + "/" + totalPages);
  306. // 更新按钮状态
  307. setButtonsEnabled(true);
  308. // 更新状态标签
  309. String oprnoDisplay;
  310. if (isAllStation) {
  311. oprnoDisplay = (queryOprno == null || queryOprno.isEmpty()) ? "所有工位" : queryOprno;
  312. } else {
  313. oprnoDisplay = oprno;
  314. }
  315. statusLabel.setText("工位:" + oprnoDisplay + " 共" + totalCount + "条");
  316. // 填充表格数据
  317. List<WorkRecordData> list = resp.getList();
  318. if (list != null && !list.isEmpty()) {
  319. for (WorkRecordData data : list) {
  320. Object[] row = {
  321. data.getSn(),
  322. data.getOprno(),
  323. data.getContent(),
  324. data.getUpdateBy(),
  325. data.getUpdateDate()
  326. };
  327. tableModel.addRow(row);
  328. }
  329. }
  330. // 刷新表格
  331. table.repaint();
  332. }
  333. /**
  334. * 更新UI显示错误信息
  335. */
  336. private void updateUIError(String message) {
  337. statusLabel.setText("错误: " + message);
  338. pageLabel.setText("0/0");
  339. setButtonsEnabled(true);
  340. btnPrevious.setEnabled(false);
  341. btnNext.setEnabled(false);
  342. tableModel.setRowCount(0);
  343. }
  344. /**
  345. * 获取当前工位号
  346. */
  347. public String getOprno() {
  348. return oprno;
  349. }
  350. }