17491 5 天之前
父節點
當前提交
76b4acea3d
共有 2 個文件被更改,包括 165 次插入100 次删除
  1. 80 24
      src/com/mes/ui/MesClient.java
  2. 85 76
      src/com/mes/util/JdbcUtils.java

+ 80 - 24
src/com/mes/ui/MesClient.java

@@ -99,9 +99,14 @@ public class MesClient extends JFrame {
 //    };
     public static Object[][] rowData = new Object[12][5];
     public static JLabel pageLabel;
+    public static JLabel totalCountLabel;
     public static int pageNo=1 ; // 当前页
     public static int totalPages; // 总页数
     public static JTextField textField;//搜索框
+    public static JButton dayShiftButton;
+    public static JButton nightShiftButton;
+    public static Date startTime;
+    public static Date endTime;
 
     public static Integer tjFlag = 0;
 
@@ -1187,7 +1192,7 @@ public class MesClient extends JFrame {
 
         JPanel panel2 = new JPanel();
         // 改用自适应宽度,避免工控机出现横向滚动条
-        panel2.setBounds(0, 0, 1000, 50);
+        panel2.setBounds(0, 0, 1000, 100);
 //        panel2.setLayout(new GridLayout(0, 1, 0, 0));
         panel2.setLayout(null);
         //记录查询搜索框
@@ -1195,47 +1200,59 @@ public class MesClient extends JFrame {
         textField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
 //        textField.setEnabled(false);
         textField.setForeground(Color.BLACK);
-        textField.setBounds(10, 5, 371, 40);
+        textField.setToolTipText("输入工件码");
+        textField.setBounds(70, 5, 190, 40);
         panel2.add(textField);
 //        textField.setColumns(20);
         indexPanelC.add(panel2);
 
+        JLabel snLabel = new JLabel("工件码");
+        snLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
+        snLabel.setBounds(10, 5, 60, 40);
+        panel2.add(snLabel);
+
+        dayShiftButton = new JButton("白班");
+        dayShiftButton.setFont(new Font("微软雅黑", Font.PLAIN, 18));
+        dayShiftButton.setBounds(270, 5, 100, 40);
+        dayShiftButton.addActionListener(e -> selectShift(true));
+        panel2.add(dayShiftButton);
+
+        nightShiftButton = new JButton("夜班");
+        nightShiftButton.setFont(new Font("微软雅黑", Font.PLAIN, 18));
+        nightShiftButton.setBounds(380, 5, 100, 40);
+        nightShiftButton.addActionListener(e -> selectShift(false));
+        panel2.add(nightShiftButton);
+        selectShift(true, false);
+
         JButton btnNewButton = new JButton("搜索");
         btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 20));
-        btnNewButton.setBounds(385, 5, 90, 40);
+        btnNewButton.setBounds(500, 5, 90, 40);
         panel2.add(btnNewButton);
         // 上一页按钮添加按钮事件监听
         btnNewButton.addActionListener(e -> {
+            pageNo = 1;
             JdbcUtils.getTestData();
         });
         JButton resetBtn = new JButton("重置");
         resetBtn.setFont(new Font("微软雅黑", Font.PLAIN, 20));
-        resetBtn.setBounds(475, 5, 90, 40);
+        resetBtn.setBounds(600, 5, 90, 40);
         panel2.add(resetBtn);
         // 上一页按钮添加按钮事件监听
         resetBtn.addActionListener(e -> {
             textField.setText("");
+            selectShift(true, false);
+            pageNo = 1;
             JdbcUtils.getTestData();
         });
 
-        JButton resetBtn2 = new JButton("导出");
-        resetBtn2.setFont(new Font("微软雅黑", Font.PLAIN, 20));
-        resetBtn2.setBounds(565, 5, 90, 40);
-        panel2.add(resetBtn2);
-        // 上一页按钮添加按钮事件监听
-        resetBtn2.addActionListener(e -> {
-            List<QmData> lists = JdbcUtils.getTestDataAll();
-            Boolean ret = ExportUtil.export(lists);
-            if(ret){
-                MesClient.setMenuStatus("导出成功,请在D盘中查看",0);
-            }else{
-                MesClient.setMenuStatus("导出失败,请重试",-1);
-            }
-        });
+        totalCountLabel = new JLabel("数量:0");
+        totalCountLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
+        totalCountLabel.setBounds(10, 55, 160, 40);
+        panel2.add(totalCountLabel);
 
         JButton btnPrevious = new JButton("上一页");
         btnPrevious.setFont(new Font("微软雅黑", Font.PLAIN, 18));
-        btnPrevious.setBounds(680, 5, 90, 40);
+        btnPrevious.setBounds(680, 55, 90, 40);
         panel2.add(btnPrevious);
 
         // 页码显示
@@ -1243,13 +1260,13 @@ public class MesClient extends JFrame {
         pageLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
         pageLabel.setHorizontalAlignment(SwingConstants.CENTER); // 设置文本居中对齐
         pageLabel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1)); // 添加灰色边框
-        pageLabel.setBounds(775, 5, 90, 40); // 调整位置适配1024屏幕
+        pageLabel.setBounds(775, 55, 90, 40); // 调整位置适配1024屏幕
         panel2.add(pageLabel);
 
         // “下一页”按钮
         JButton btnNext = new JButton("下一页");
         btnNext.setFont(new Font("微软雅黑", Font.PLAIN, 18));
-        btnNext.setBounds(870, 5, 90, 40); // 调整位置,总宽度控制在960以内
+        btnNext.setBounds(870, 55, 90, 40); // 调整位置,总宽度控制在960以内
         panel2.add(btnNext);
 
         // 分页逻辑变量
@@ -1271,7 +1288,7 @@ public class MesClient extends JFrame {
 
         JPanel panel = new JPanel();
         // 改用自适应宽度
-        panel.setBounds(0, 49, 1000, 518);
+        panel.setBounds(0, 99, 1000, 468);
         panel.setLayout(new GridLayout(0, 1, 0, 0));
         indexPanelC.add(panel);
 
@@ -1353,8 +1370,8 @@ public class MesClient extends JFrame {
             @Override
             public void componentResized(ComponentEvent e) {
                 // 同时调整顶部按钮区和表格区的宽度
-                panel2.setBounds(0, 0, indexPanelC.getWidth(), 50);
-                panel.setBounds(0, 49, indexPanelC.getWidth(), Math.max(0, indexPanelC.getHeight() - 49));
+                panel2.setBounds(0, 0, indexPanelC.getWidth(), 100);
+                panel.setBounds(0, 99, indexPanelC.getWidth(), Math.max(0, indexPanelC.getHeight() - 99));
                 panel.revalidate();
                 panel2.revalidate();
             }
@@ -2102,6 +2119,45 @@ public class MesClient extends JFrame {
         submitNgWithLeakPoints(leakPoints, isManualOverride);
     }
 
+    private static void selectShift(boolean isDayShift) {
+        selectShift(isDayShift, true);
+    }
+
+    private static void selectShift(boolean isDayShift, boolean queryImmediately) {
+        Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.MINUTE, 0);
+        calendar.set(Calendar.SECOND, 0);
+        calendar.set(Calendar.MILLISECOND, 0);
+        if (isDayShift) {
+            calendar.set(Calendar.HOUR_OF_DAY, 8);
+            startTime = calendar.getTime();
+            calendar.set(Calendar.HOUR_OF_DAY, 20);
+            endTime = calendar.getTime();
+        } else {
+            calendar.set(Calendar.HOUR_OF_DAY, 20);
+            calendar.add(Calendar.DATE, -1);
+            startTime = calendar.getTime();
+            calendar.add(Calendar.DATE, 1);
+            calendar.set(Calendar.HOUR_OF_DAY, 8);
+            endTime = calendar.getTime();
+        }
+        updateShiftButtonState(isDayShift);
+        if (queryImmediately) {
+            pageNo = 1;
+            JdbcUtils.getTestData();
+        }
+    }
+
+    private static void updateShiftButtonState(boolean isDayShift) {
+        Color selectedColor = new Color(184, 207, 229);
+        if (dayShiftButton != null) {
+            dayShiftButton.setBackground(isDayShift ? selectedColor : null);
+        }
+        if (nightShiftButton != null) {
+            nightShiftButton.setBackground(isDayShift ? null : selectedColor);
+        }
+    }
+
     /**
      * 提交NG结果,不提交泄露点位。
      */

+ 85 - 76
src/com/mes/util/JdbcUtils.java

@@ -11,6 +11,7 @@ import java.sql.*;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.Date;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -305,41 +306,29 @@ public class JdbcUtils {
 		}
 		//每页固定12条
 		int pageSize = 12;
-		// 根据当前页数计算偏移量
 		int offset = (MesClient.pageNo - 1) * pageSize;
-		//搜索框工件码
-		String sn = MesClient.textField.getText();
-
-		String sql = "SELECT * FROM test_record";  // 初始查询条件
-		// SQL 查询总条数
-		String countSql = "select count(*) as total from test_record";
-
-		// 如果 sn 不为 null,则添加 sn 的查询条件
-		if (!sn.equals("")&&sn != null) {
-			sql += " WHERE sn like '%" + sn + "%'";
-			countSql += " WHERE sn like '%" + sn + "%'";
+		TestRecordQuery query = createTestRecordQuery();
+		if (query == null) {
+			return;
 		}
-
-		// 添加分页条件
-		sql += " ORDER BY id DESC LIMIT " + pageSize + " OFFSET " + offset;
-		Statement stmt = null;
-		ResultSet ret = null;
+		String sql = "SELECT * FROM test_record" + query.whereSql + " ORDER BY id DESC LIMIT ? OFFSET ?";
+		String countSql = "SELECT count(*) AS total FROM test_record" + query.whereSql;
 		try {
 			// 确保连接已经打开
 			if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
 				JdbcUtils.openConnection();
 			}
 
-			stmt = conn.createStatement();
-
-			// 执行查询总条数
-			ResultSet countRet = stmt.executeQuery(countSql);
-			int totalItems = 0;
-			if (countRet.next()) {
-				totalItems = countRet.getInt("total");
+			int totalItems;
+			try (PreparedStatement countStmt = conn.prepareStatement(countSql)) {
+				bindQueryParameters(countStmt, query.parameters);
+				try (ResultSet countRet = countStmt.executeQuery()) {
+					totalItems = countRet.next() ? countRet.getInt("total") : 0;
+				}
 			}
 			if (totalItems == 0){
 				MesClient.pageLabel.setText( "0/0" );
+				MesClient.totalCountLabel.setText("数量:0");
 				MesClient.table.repaint();
 //				ret.close();
 				return;
@@ -350,23 +339,25 @@ public class JdbcUtils {
 			MesClient.totalPages = (int) Math.ceil(Double.valueOf(Double.valueOf(totalItems)/Double.valueOf(pageSize)));
 //			MesClient.totalPages = (totalItems + pageSize - 1) / 12;
 			MesClient.pageLabel.setText(MesClient.pageNo + "/" + MesClient.totalPages);
-			//执行语句,得到结果集
-			ret = stmt.executeQuery(sql);
-			System.out.println("sql:" + sql);
-			int rowIndex = 0;
-
-
-			while (ret.next()) {
-				MesClient.rowData[rowIndex][0] = ret.getString("sn");
-				MesClient.rowData[rowIndex][1] = ret.getString("create_time");
-				MesClient.rowData[rowIndex][2] = getDisplayPressure(ret);
-				MesClient.rowData[rowIndex][3] = getDisplayLeakValue(ret);
-				MesClient.rowData[rowIndex][4] = ret.getString("result");
-				rowIndex++;
+			MesClient.totalCountLabel.setText("数量:" + totalItems);
+			try (PreparedStatement dataStmt = conn.prepareStatement(sql)) {
+				int parameterIndex = bindQueryParameters(dataStmt, query.parameters);
+				dataStmt.setInt(parameterIndex++, pageSize);
+				dataStmt.setInt(parameterIndex, offset);
+				try (ResultSet ret = dataStmt.executeQuery()) {
+					int rowIndex = 0;
+					while (ret.next()) {
+						MesClient.rowData[rowIndex][0] = ret.getString("sn");
+						MesClient.rowData[rowIndex][1] = ret.getString("create_time");
+						MesClient.rowData[rowIndex][2] = getDisplayPressure(ret);
+						MesClient.rowData[rowIndex][3] = getDisplayLeakValue(ret);
+						MesClient.rowData[rowIndex][4] = ret.getString("result");
+						rowIndex++;
+					}
+				}
 			}
 
 			MesClient.table.repaint();
-			ret.close();
 //			conn.close();//关闭连接
 		} catch (SQLException e1) {
 			e1.printStackTrace();
@@ -376,27 +367,11 @@ public class JdbcUtils {
 
 	//面板查询(分页)
 	public static List<QmData> getTestDataAll() {
-		//每页固定12条
-		int pageSize = 1000000;
-		// 根据当前页数计算偏移量
-		int offset = (MesClient.pageNo - 1) * pageSize;
-		//搜索框工件码
-		String sn = MesClient.textField.getText();
-
-		String sql = "SELECT * FROM test_record";  // 初始查询条件
-		// SQL 查询总条数
-		String countSql = "select count(*) as total from test_record";
-
-		// 如果 sn 不为 null,则添加 sn 的查询条件
-		if (!sn.equals("")&&sn != null) {
-			sql += " WHERE sn like '%" + sn + "%'";
-			countSql += " WHERE sn like '%" + sn + "%'";
+		TestRecordQuery query = createTestRecordQuery();
+		if (query == null) {
+			return null;
 		}
-
-		// 添加分页条件
-		sql += " ORDER BY id DESC LIMIT " + pageSize + " OFFSET " + offset;
-		Statement stmt = null;
-		ResultSet ret = null;
+		String sql = "SELECT * FROM test_record" + query.whereSql + " ORDER BY id DESC";
 		List<QmData> lists = new ArrayList<>();
 		try {
 			// 确保连接已经打开
@@ -404,25 +379,20 @@ public class JdbcUtils {
 				JdbcUtils.openConnection();
 			}
 
-			stmt = conn.createStatement();
-			//执行语句,得到结果集
-			ret = stmt.executeQuery(sql);
-			System.out.println("sql:" + sql);
-			int rowIndex = 0;
-
-
-			while (ret.next()) {
-				QmData qmData = new QmData();
-				qmData.setSn(ret.getString("sn"));
-				qmData.setCreateTime(ret.getString("create_time"));
-				qmData.setParam1(getDisplayPressure(ret));
-				qmData.setParam2(getDisplayLeakValue(ret));
-				qmData.setResult(ret.getString("result"));
-				lists.add(qmData);
-				rowIndex++;
+			try (PreparedStatement stmt = conn.prepareStatement(sql)) {
+				bindQueryParameters(stmt, query.parameters);
+				try (ResultSet ret = stmt.executeQuery()) {
+					while (ret.next()) {
+						QmData qmData = new QmData();
+						qmData.setSn(ret.getString("sn"));
+						qmData.setCreateTime(ret.getString("create_time"));
+						qmData.setParam1(getDisplayPressure(ret));
+						qmData.setParam2(getDisplayLeakValue(ret));
+						qmData.setResult(ret.getString("result"));
+						lists.add(qmData);
+					}
+				}
 			}
-
-			ret.close();
 //			conn.close();//关闭连接
 		} catch (SQLException e1) {
 			e1.printStackTrace();
@@ -431,6 +401,45 @@ public class JdbcUtils {
 		return lists;
 	}
 
+	private static TestRecordQuery createTestRecordQuery() {
+		List<String> conditions = new ArrayList<>();
+		List<String> parameters = new ArrayList<>();
+		String sn = MesClient.textField.getText();
+		if (sn != null && !sn.trim().isEmpty()) {
+			conditions.add("sn LIKE ?");
+			parameters.add("%" + sn.trim() + "%");
+		}
+		if (MesClient.startTime != null && MesClient.endTime != null) {
+			Date startTime = MesClient.startTime;
+			Date endTime = MesClient.endTime;
+			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+			conditions.add("create_time >= ?");
+			parameters.add(formatter.format(startTime));
+			conditions.add("create_time < ?");
+			parameters.add(formatter.format(endTime));
+		}
+		String whereSql = conditions.isEmpty() ? "" : " WHERE " + String.join(" AND ", conditions);
+		return new TestRecordQuery(whereSql, parameters);
+	}
+
+	private static int bindQueryParameters(PreparedStatement statement, List<String> parameters) throws SQLException {
+		int index = 1;
+		for (String parameter : parameters) {
+			statement.setString(index++, parameter);
+		}
+		return index;
+	}
+
+	private static class TestRecordQuery {
+		private final String whereSql;
+		private final List<String> parameters;
+
+		private TestRecordQuery(String whereSql, List<String> parameters) {
+			this.whereSql = whereSql;
+			this.parameters = parameters;
+		}
+	}
+
 	private static String getDisplayPressure(ResultSet resultSet) throws SQLException {
 		if (isLegacyTestRecord(resultSet)) {
 			return resultSet.getString("param1");