17491 6 дней назад
Родитель
Сommit
49cab66f77

+ 3 - 3
src/com/mes/ui/MesClient.java

@@ -1969,7 +1969,7 @@ public class MesClient extends JFrame {
                 // MES提交成功后打印标签
                 printLabelIfEnabled();
                 resetScanA();
-                setMenuStatus("测试结果上传成功,请扫下一件", 0);
+                setMenuStatus(pendingTestParam.localRecordSaved ? "测试结果上传成功,请扫下一件" : "服务器已上传,但本地记录保存失败,请联系管理员", pendingTestParam.localRecordSaved ? 0 : -1);
             } else {
                 setMenuStatus("测试结果上传失败,请重试", -1);
             }
@@ -2049,7 +2049,7 @@ public class MesClient extends JFrame {
                 // MES提交成功后打印标签
                 printLabelIfEnabled();
                 resetScanA();
-                setMenuStatus("人工改判OK已提交,请扫下一件", 0);
+                setMenuStatus(pendingTestParam.localRecordSaved ? "人工改判OK已提交,请扫下一件" : "服务器已上传,但本地记录保存失败,请联系管理员", pendingTestParam.localRecordSaved ? 0 : -1);
             } else {
                 setMenuStatus("测试结果上传失败,请重试", -1);
             }
@@ -2111,7 +2111,7 @@ public class MesClient extends JFrame {
             resetScanA();
 
             if (leakOk) {
-                setMenuStatus("结果上传成功(含漏点记录),请扫下一件", 0);
+                setMenuStatus(pendingTestParam.localRecordSaved ? "结果上传成功(含漏点记录),请扫下一件" : "服务器已上传,但本地记录保存失败,请联系管理员", pendingTestParam.localRecordSaved ? 0 : -1);
             } else {
                 log.warn("漏点记录提交失败,但测试结果已提交,标签已打印");
                 setMenuStatus("测试结果已提交,漏点记录失败", 1);

+ 110 - 28
src/com/mes/util/JdbcUtils.java

@@ -11,7 +11,9 @@ import java.sql.*;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 public class JdbcUtils {
 
@@ -25,6 +27,7 @@ public class JdbcUtils {
         try {
             Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
             conn = DriverManager.getConnection(DATABASE_URL);//连接数据库zhou.db,不存在则创建
+            configureConnection();
 //            log.info("连接到SQLite数据库成功!");
             create_bw_record();//初始化结构表
             //create_measure_data();//初始化测试数据表
@@ -40,6 +43,8 @@ public class JdbcUtils {
 		try {
 			Class.forName(Drivde);// 加载驱动,连接sqlite的jdbc
 			conn = DriverManager.getConnection(DATABASE_URL);
+			configureConnection();
+			create_bw_record();
 		} catch (ClassNotFoundException e) {
 			e.printStackTrace();
 		} catch (SQLException e) {
@@ -57,11 +62,19 @@ public class JdbcUtils {
 			}
 			// 重新建立连接
 			conn = DriverManager.getConnection(DATABASE_URL);
+			configureConnection();
+			create_bw_record();
 		} catch (SQLException e) {
 			e.printStackTrace();
 			// 如果重连失败,可以进一步处理异常,比如记录日志、通知管理员等
 		}
 	}
+
+	private static void configureConnection() throws SQLException {
+		try (Statement statement = conn.createStatement()) {
+			statement.execute("PRAGMA busy_timeout = 5000");
+		}
+	}
     
     public static void create_bw_record() throws SQLException {
     	Statement statement=conn.createStatement();   //创建连接对象,是Java的一个操作数据库的重要接口
@@ -91,6 +104,7 @@ public class JdbcUtils {
 				")";
 
 		statement.executeUpdate(testRecord);
+		ensureTestRecordColumns(statement);
 
 		String configRecord = "CREATE TABLE if not exists config(\n" +
 				"   id INTEGER PRIMARY KEY AUTOINCREMENT, -- 自增ID\n" +
@@ -100,6 +114,29 @@ public class JdbcUtils {
 
         statement.close();
     }
+
+	private static void ensureTestRecordColumns(Statement statement) throws SQLException {
+		Set<String> existingColumns = new HashSet<>();
+		try (ResultSet resultSet = statement.executeQuery("PRAGMA table_info(test_record)")) {
+			while (resultSet.next()) {
+				existingColumns.add(resultSet.getString("name").toLowerCase());
+			}
+		}
+		String[] columns = {
+				"ucode VARCHAR(48)", "line_sn VARCHAR(48)", "oprno VARCHAR(48)",
+				"oprno_title VARCHAR(48)", "create_time DATETIME", "param1 VARCHAR(48)",
+				"param2 VARCHAR(48)", "param3 VARCHAR(48)", "param4 VARCHAR(48)",
+				"param5 VARCHAR(48)", "result VARCHAR(48)", "remark VARCHAR(48)",
+				"device_type VARCHAR(48)"
+		};
+		for (String column : columns) {
+			String columnName = column.substring(0, column.indexOf(' ')).toLowerCase();
+			if (!existingColumns.contains(columnName)) {
+				statement.executeUpdate("ALTER TABLE test_record ADD COLUMN " + column);
+				log.info("已为test_record补充字段: {}", column);
+			}
+		}
+	}
     
     //插入数据
     public static boolean insertData(String gw, String gy, String bw, String message_type, String sn) {
@@ -148,27 +185,43 @@ public class JdbcUtils {
 		return ret;
 	}
 
-	public static boolean insertTestRecord(TestParam testParam){
-		boolean ret = false;
+	public static synchronized boolean insertTestRecord(TestParam testParam){
+		String insertSQL = "INSERT INTO test_record (oprno, oprno_title, ucode, sn, line_sn, create_time, "
+				+ "param1, param2, param3, param4, param5, result, remark, device_type) "
+				+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
 		try {
 			// 确保连接已经打开
 			if (JdbcUtils.conn == null || JdbcUtils.conn.isClosed()) {
 				JdbcUtils.openConnection();
 			}
-			Statement statement=conn.createStatement();
-			String insertSQL = "INSERT INTO test_record (oprno,oprno_title,ucode, sn, line_sn, create_time, param1,param2,param3,param4,param5,result,remark,device_type)" +
-					"VALUES('"+testParam.getOprno()+"','" + testParam.getOprnoTitle() + "','"+testParam.getUcode()+"', '" + testParam.getSn() + "', '" + testParam.getLineSn() + "', '" + testParam.getCreateTime() + "', '" + testParam.getParam1() + "', '"
-					+testParam.getParam2()+"','"+testParam.getParam3()+"','"+testParam.getParam4()+"','"+testParam.getParam5()+"','"+testParam.getResult()+"','"+testParam.getRemark()+"','"+testParam.getDeviceType()+"')";
-			statement.executeUpdate(insertSQL);
-			statement.close();
-			ret = true;
-			log.info("向test_record表插入数据成功: {}", insertSQL);
+			if (conn == null) {
+				throw new SQLException("SQLite连接不可用");
+			}
+			try (PreparedStatement statement = conn.prepareStatement(insertSQL)) {
+				statement.setString(1, testParam.getOprno());
+				statement.setString(2, testParam.getOprnoTitle());
+				statement.setString(3, testParam.getUcode());
+				statement.setString(4, testParam.getSn());
+				statement.setString(5, testParam.getLineSn());
+				statement.setString(6, testParam.getCreateTime());
+				statement.setString(7, testParam.getParam1());
+				statement.setString(8, testParam.getParam2());
+				statement.setString(9, testParam.getParam3());
+				statement.setString(10, testParam.getParam4());
+				statement.setString(11, testParam.getParam5());
+				statement.setString(12, testParam.getResult());
+				statement.setString(13, testParam.getRemark());
+				statement.setString(14, testParam.getDeviceType());
+				statement.executeUpdate();
+			}
+			testParam.localRecordSaved = true;
+			log.info("本地测试记录保存成功: sn={}, db={}", testParam.getSn(), DATABASE_URL);
+			return true;
 		} catch (SQLException e) {
-			ret = false;
-			log.error("向test_record表插入数据失败: {}", e.getMessage());
-			e.printStackTrace();
+			testParam.localRecordSaved = false;
+			log.error("本地测试记录保存失败: sn={}, db={}", testParam.getSn(), DATABASE_URL, e);
+			return false;
 		}
-		return ret;
 	}
 
 	public static int checkSnTimer(String sn,ConfigParam configParam){
@@ -198,12 +251,12 @@ public class JdbcUtils {
 			int ngIndex = 0;
 			String firstTime = "";
 			while (ret.next()) {
-				String result = ret.getString(9);
+				String result = ret.getString("result");
 				if(rowIndex == 0 && result.equals("OK")){
 					return 0;
 				}
 				if(rowIndex == 0){
-					firstTime = ret.getString(4);
+					firstTime = ret.getString("create_time");
 				}
 				if(!result.equals("OK")){
 					ngIndex++;
@@ -304,13 +357,11 @@ public class JdbcUtils {
 
 
 			while (ret.next()) {
-				MesClient.rowData[rowIndex][0] = ret.getString(2);  // sn (工件码)
-				MesClient.rowData[rowIndex][1] = ret.getString(7);  // create_time (测试日期)
-				MesClient.rowData[rowIndex][2] = ret.getString(8);  // param1 (压力)
-				MesClient.rowData[rowIndex][3] = ret.getString(10); // param3 (泄漏值)
-				MesClient.rowData[rowIndex][4] = ret.getString(13); // result (结果)
-//				MesClient.rowData[rowIndex][5] = ret.getString(8);
-//				MesClient.rowData[rowIndex][6] = ret.getString(9);
+				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++;
 			}
 
@@ -362,11 +413,11 @@ public class JdbcUtils {
 
 			while (ret.next()) {
 				QmData qmData = new QmData();
-				qmData.setSn(ret.getString(2));
-				qmData.setCreateTime(ret.getString(4));
-				qmData.setParam1(ret.getString(5));
-				qmData.setParam2(ret.getString(6));
-				qmData.setResult(ret.getString(9));
+				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++;
 			}
@@ -380,6 +431,37 @@ public class JdbcUtils {
 		return lists;
 	}
 
+	private static String getDisplayPressure(ResultSet resultSet) throws SQLException {
+		if (isLegacyTestRecord(resultSet)) {
+			return resultSet.getString("param1");
+		}
+		return joinValueAndUnit(resultSet.getString("param1"), resultSet.getString("param2"));
+	}
+
+	private static String getDisplayLeakValue(ResultSet resultSet) throws SQLException {
+		String leakValue = resultSet.getString("param3");
+		if (isLegacyTestRecord(resultSet)) {
+			// 历史库把泄漏值(含单位)保存在 param2。
+			return resultSet.getString("param2");
+		}
+		return joinValueAndUnit(leakValue, resultSet.getString("param4"));
+	}
+
+	private static boolean isLegacyTestRecord(ResultSet resultSet) throws SQLException {
+		String leakValue = resultSet.getString("param3");
+		return leakValue == null || leakValue.trim().isEmpty();
+	}
+
+	private static String joinValueAndUnit(String value, String unit) {
+		if (value == null) {
+			return "";
+		}
+		if (unit == null || unit.trim().isEmpty()) {
+			return value;
+		}
+		return value + unit;
+	}
+
 
 
 	public static void close(){

+ 5 - 2
src/com/mes/util/SerialPortUtils.java

@@ -164,19 +164,22 @@ public class SerialPortUtils {
 
                                                         String testDate = DateLocalUtils.getCurrentTime();
                                                         testParam.setCreateTime(testDate);
-                                                        JdbcUtils.insertTestRecord(testParam);
+                                                        boolean localRecordSaved = JdbcUtils.insertTestRecord(testParam);
 
                                                         if(MesClient.mes_enable){ // MES开启
                                                             JSONObject retObj = DataUtil.qmResultData(testParam);
                                                             if(retObj != null && retObj.get("result") != null && retObj.get("result").toString().equalsIgnoreCase("true")) {
                                                                 MesClient.resetScanA();
-                                                                MesClient.setMenuStatus("测试结果上传成功,请扫下一件",0);
+                                                                MesClient.setMenuStatus(localRecordSaved ? "测试结果上传成功,请扫下一件" : "服务器已上传,但本地记录保存失败,请联系管理员", localRecordSaved ? 0 : -1);
                                                             }else{
                                                                 MesClient.setMenuStatus("测试结果上传失败,请重试",-1);
                                                                 return;
                                                             }
                                                         }else{
                                                             MesClient.resetScanA();
+														if (!localRecordSaved) {
+															MesClient.setMenuStatus("本地记录保存失败,请联系管理员", -1);
+														}
                                                         }
 
                                                         if(MesClient.configParam.getPrintLabel().equals("是") && lastRet.equals("OK")){

+ 2 - 0
src/com/mes/util/TestParam.java

@@ -17,6 +17,8 @@ public class TestParam {
     public String result;
     public String remark;
     public String deviceType;
+    // 本地 SQLite 保存结果,供后续上传状态提示使用。
+    public boolean localRecordSaved;
 
     public Integer getId() {
         return id;

+ 4 - 1
src/com/mes/util/WorkTimer.java

@@ -114,7 +114,10 @@ public class WorkTimer {
 
                         String testDate = DateLocalUtils.getCurrentTime();
                         testParam.setCreateTime(testDate);
-                        JdbcUtils.insertTestRecord(testParam);
+                        boolean localRecordSaved = JdbcUtils.insertTestRecord(testParam);
+						if (!localRecordSaved) {
+							MesClient.setMenuStatus("本地记录保存失败,请联系管理员", -1);
+						}
 
                         // 根据设备判定结果分支处理
                         if (lastRet.equals("OK")) {