package com.mes.ui; import com.alibaba.fastjson2.JSONObject; import com.mes.util.Base64Utils; import com.mes.util.DateLocalUtils; import com.mes.util.ErrorMsg; import com.mes.util.JdbcUtils; import com.mes.util.TestParam; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.Base64; import java.util.Properties; public class DataUtil { public static final Logger log = LoggerFactory.getLogger(DataUtil.class); private static String getMesRecordApiUrl(String action) { return "http://" + MesClient.mes_server_ip + ":8980/js/a/mes/mesProductRecord/" + action; } private static String buildCommonParams() throws UnsupportedEncodingException { return "__ajax=json" + "&oprno=" + URLEncoder.encode(MesClient.getEffectiveOprno().trim(), "UTF-8") + "&lineSn=" + URLEncoder.encode(MesClient.getEffectiveLineSn().trim(), "UTF-8"); } private static JSONObject parseJsonResponse(String result) { if (result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) { return null; } try { return JSONObject.parseObject(result); } catch (Exception e) { log.error("解析JSON响应失败: {}, 原始响应: {}", e.getMessage(), result); return null; } } /** 质量检查(替代 TCP AQDW) */ public static Boolean checkQuality(String sn, String user) { try { String url = getMesRecordApiUrl("checkQuality"); String params = buildCommonParams() + "&sn=" + URLEncoder.encode(sn, "UTF-8") + "&userCode=" + URLEncoder.encode(user != null ? user : "", "UTF-8"); String result = doPost(url, params); JSONObject obj = parseJsonResponse(result); if (obj == null) { return false; } String checkret = obj.getString("data"); boolean success = "true".equalsIgnoreCase(String.valueOf(obj.get("result"))); if (success || "UD".equalsIgnoreCase(checkret)) { MesClient.beginTestAfterScan(); } else { MesClient.check_quality_result = false; String lmsg = obj.getString("message"); if (lmsg == null || lmsg.isEmpty()) { lmsg = ErrorMsg.getErrorMsg(checkret != null ? checkret : "NE", ""); } MesClient.setMenuStatus(lmsg, -1); } return true; } catch (Exception e) { log.error("质量检查失败", e); return false; } } public static Boolean startWork(String sn, String user) { try { String url = getMesRecordApiUrl("createProduct"); String params = buildCommonParams() + "&sn=" + URLEncoder.encode(sn, "UTF-8") + "&ucode=" + URLEncoder.encode(user != null ? user : "", "UTF-8"); JSONObject obj = parseJsonResponse(doPost(url, params)); return obj != null && "true".equalsIgnoreCase(String.valueOf(obj.get("result"))); } catch (Exception e) { log.error("开工失败", e); return false; } } public static Boolean bindUser(String sn, String user) { return bindWarehouse(sn, user, user, "400001"); } public static Boolean bindWarehouse(String sn, String wsn, String user, String craft) { try { String url = getMesRecordApiUrl("bindWarehouse"); String params = buildCommonParams() + "&sn=" + URLEncoder.encode(sn, "UTF-8") + "&wsn=" + URLEncoder.encode(wsn, "UTF-8") + "&ucode=" + URLEncoder.encode(user != null ? user : "", "UTF-8") + "&craft=" + URLEncoder.encode(craft != null ? craft : "", "UTF-8"); JSONObject obj = parseJsonResponse(doPost(url, params)); return obj != null && "true".equalsIgnoreCase(String.valueOf(obj.get("result"))); } catch (Exception e) { log.error("物料绑定失败", e); return false; } } public static Boolean unBindWarehouse(String sn, String wsn, String user, String craft) { try { String url = getMesRecordApiUrl("unBindWarehouse"); String params = buildCommonParams() + "&sn=" + URLEncoder.encode(sn, "UTF-8") + "&wsn=" + URLEncoder.encode(wsn, "UTF-8") + "&ucode=" + URLEncoder.encode(user != null ? user : "", "UTF-8") + "&craft=" + URLEncoder.encode(craft != null ? craft : "", "UTF-8"); JSONObject obj = parseJsonResponse(doPost(url, params)); return obj != null && "true".equalsIgnoreCase(String.valueOf(obj.get("result"))); } catch (Exception e) { log.error("物料解绑失败", e); return false; } } public static Boolean sendQuality(String sn, String result, String user) { try { String msgType = "MQDW"; String gw = "GW" + rightPad(MesClient.getEffectiveOprno(), 6); String gy = "GY100000"; JdbcUtils.insertData(gw, gy, "HTTP qmresult sn=" + sn + " result=" + result, msgType, sn); JdbcUtils.insertSubmitRecord(gw, sn, "HTTP qmresult"); return true; } catch (Exception e) { return false; } } public static String rightPad(final String str, final int size) { if (str == null) { return null; } String strret = str; if (str.length() > size) { strret = str.substring(0, size); } return String.format("%-" + size + "s", strret); } public static JSONObject getBindMaterail() { try { String mes_server_ip = MesClient.mes_server_ip; String oprno = MesClient.getEffectiveOprno().trim(); String lineSn = MesClient.getEffectiveLineSn().trim(); String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesLineProcessMaterial/materials"; String params = "__ajax=json&oprno=" + oprno + "&lineSn=" + lineSn; log.info("params=" + params); String result = doPost(url, params); log.info("result=" + result); if (result.equalsIgnoreCase("false")) { return null; } else { return JSONObject.parseObject(result); } } catch (Exception e) { return null; } } public static JSONObject saveBindMaterail(String batchSn, String craft, String materialId, String type) { try { String mes_server_ip = MesClient.mes_server_ip; String oprno = MesClient.getEffectiveOprno().trim(); String lineSn = MesClient.getEffectiveLineSn().trim(); String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesMaterialPrebind/bind"; String params = "__ajax=json&oprno=" + oprno + "&lineSn=" + lineSn + "&batchSn=" + batchSn + "&craft=" + craft + "&materialId=" + materialId + "&type=" + type; log.info("params=" + params); String result = doPost(url, params); log.info("result=" + result); if (result.equalsIgnoreCase("false")) { return null; } else { return JSONObject.parseObject(result); } } catch (Exception e) { return null; } } // 上传气密结果 public static JSONObject qmResultData(TestParam testParam) { try { String enconding = "UTF-8"; InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties"); Properties pro = new Properties(); BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding)); pro.load(br); String mes_server_ip = pro.getProperty("mes.server_ip"); String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/qmresult"; String testPressureBase64 = Base64Utils.getBase64(testParam.getParam1() != null ? testParam.getParam1() : ""); String leakValBase64 = Base64Utils.getBase64(testParam.getParam3() != null ? testParam.getParam3() : ""); String testTimeBase64 = Base64Utils.getBase64(testParam.getCreateTime() != null ? testParam.getCreateTime() : ""); String titleBase64 = Base64Utils.getBase64(testParam.getOprnoTitle() != null ? testParam.getOprnoTitle() : ""); log.info("服务器端需要Base64编码的参数: testPressure='{}'->'{}', leakVal='{}'->'{}', testTime='{}'->'{}', title='{}'->'{}'" , testParam.getParam1(), testPressureBase64, testParam.getParam3(), leakValBase64 , testParam.getCreateTime(), testTimeBase64, testParam.getOprnoTitle(), titleBase64); String params = "__ajax=json&oprno=" + URLEncoder.encode(testParam.getOprno() != null ? testParam.getOprno() : "", "UTF-8") + "&lineSn=" + URLEncoder.encode(testParam.getLineSn() != null ? testParam.getLineSn() : "", "UTF-8") + "&sn=" + URLEncoder.encode(testParam.getSn() != null ? testParam.getSn() : "", "UTF-8") + "&workNum=" + URLEncoder.encode(testParam.getUcode() != null ? testParam.getUcode() : "", "UTF-8") + "&cxm=" + URLEncoder.encode(testParam.getParam5() != null ? testParam.getParam5() : "", "UTF-8") + "&result=" + URLEncoder.encode(testParam.getResult() != null ? testParam.getResult() : "", "UTF-8") + "&testPressure=" + URLEncoder.encode(testPressureBase64, "UTF-8") + "&testPressureUnit=" + URLEncoder.encode(testParam.getParam2() != null ? testParam.getParam2() : "", "UTF-8") + "&leakVal=" + URLEncoder.encode(leakValBase64, "UTF-8") + "&leakValUnit=" + URLEncoder.encode(testParam.getParam4() != null ? testParam.getParam4() : "", "UTF-8") + "&testTime=" + URLEncoder.encode(testTimeBase64, "UTF-8") + "&title=" + URLEncoder.encode(titleBase64, "UTF-8") + "&remark=" + URLEncoder.encode(testParam.getRemark() != null ? testParam.getRemark() : "", "UTF-8") + "&deviceType=" + URLEncoder.encode(testParam.getDeviceType() != null ? testParam.getDeviceType() : "", "UTF-8") + "&cq=" + URLEncoder.encode("", "UTF-8") + "&by=" + URLEncoder.encode("", "UTF-8") + "&cs=" + URLEncoder.encode("", "UTF-8") + "&leakRate=" + URLEncoder.encode("", "UTF-8") + "&leakRateUnit=" + URLEncoder.encode("", "UTF-8") + "&type=" + URLEncoder.encode("", "UTF-8"); log.info("params=" + params); String result = doPost(url, params); log.info("result=" + result); if (result == null || result.trim().isEmpty()) { log.error("HTTP请求返回结果为空,请检查服务器连接和参数格式"); return null; } if (result.equalsIgnoreCase("false")) { return null; } else { try { return JSONObject.parseObject(result); } catch (Exception e) { log.error("解析JSON响应失败: {}, 原始响应: {}", e.getMessage(), result); return null; } } } catch (Exception e) { log.error("qmResultData方法执行异常: {}", e.getMessage()); e.printStackTrace(); return null; } } public static String doPost(String httpUrl, String param) { HttpURLConnection connection = null; InputStream is = null; OutputStream os = null; BufferedReader br = null; String result = null; try { URL url = new URL(httpUrl); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setConnectTimeout(15000); connection.setReadTimeout(60000); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0"); os = connection.getOutputStream(); os.write(param.getBytes()); int responseCode = connection.getResponseCode(); log.info("HTTP响应码: " + responseCode); if (responseCode == 200) { is = connection.getInputStream(); br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer sbf = new StringBuffer(); String temp = null; while ((temp = br.readLine()) != null) { sbf.append(temp); sbf.append("\r\n"); } result = sbf.toString(); } else { log.error("HTTP请求失败,响应码: " + responseCode); try { is = connection.getErrorStream(); if (is != null) { br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer errorBuffer = new StringBuffer(); String temp = null; while ((temp = br.readLine()) != null) { errorBuffer.append(temp); errorBuffer.append("\r\n"); } log.error("服务器错误响应内容: " + errorBuffer.toString()); } } catch (Exception errorReadException) { log.error("读取错误响应失败: " + errorReadException.getMessage()); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != os) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null) { connection.disconnect(); } } return result; } public static String getBase64Encode(String str) { if (str == null || "".equals(str)) { return ""; } try { str = str.trim().replaceAll("\\s+", ""); return Base64.getEncoder().encodeToString(str.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return ""; } } }