DataUtil.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.mes.util.Base64Utils;
  4. import com.mes.util.DateLocalUtils;
  5. import com.mes.util.ErrorMsg;
  6. import com.mes.util.JdbcUtils;
  7. import com.mes.util.TestParam;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import java.io.*;
  11. import java.net.HttpURLConnection;
  12. import java.net.MalformedURLException;
  13. import java.net.URL;
  14. import java.net.URLEncoder;
  15. import java.util.Base64;
  16. import java.util.Properties;
  17. public class DataUtil {
  18. public static final Logger log = LoggerFactory.getLogger(DataUtil.class);
  19. private static String getMesRecordApiUrl(String action) {
  20. return "http://" + MesClient.mes_server_ip + ":8980/js/a/mes/mesProductRecord/" + action;
  21. }
  22. private static String buildCommonParams() throws UnsupportedEncodingException {
  23. return "__ajax=json"
  24. + "&oprno=" + URLEncoder.encode(MesClient.getEffectiveOprno().trim(), "UTF-8")
  25. + "&lineSn=" + URLEncoder.encode(MesClient.getEffectiveLineSn().trim(), "UTF-8");
  26. }
  27. private static JSONObject parseJsonResponse(String result) {
  28. if (result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) {
  29. return null;
  30. }
  31. try {
  32. return JSONObject.parseObject(result);
  33. } catch (Exception e) {
  34. log.error("解析JSON响应失败: {}, 原始响应: {}", e.getMessage(), result);
  35. return null;
  36. }
  37. }
  38. /** 质量检查(替代 TCP AQDW) */
  39. public static Boolean checkQuality(String sn, String user) {
  40. try {
  41. String url = getMesRecordApiUrl("checkQuality");
  42. String params = buildCommonParams()
  43. + "&sn=" + URLEncoder.encode(sn, "UTF-8")
  44. + "&userCode=" + URLEncoder.encode(user != null ? user : "", "UTF-8");
  45. String result = doPost(url, params);
  46. JSONObject obj = parseJsonResponse(result);
  47. if (obj == null) {
  48. return false;
  49. }
  50. String checkret = obj.getString("data");
  51. boolean success = "true".equalsIgnoreCase(String.valueOf(obj.get("result")));
  52. if (success || "UD".equalsIgnoreCase(checkret)) {
  53. MesClient.beginTestAfterScan();
  54. } else {
  55. MesClient.check_quality_result = false;
  56. String lmsg = obj.getString("message");
  57. if (lmsg == null || lmsg.isEmpty()) {
  58. lmsg = ErrorMsg.getErrorMsg(checkret != null ? checkret : "NE", "");
  59. }
  60. MesClient.setMenuStatus(lmsg, -1);
  61. }
  62. return true;
  63. } catch (Exception e) {
  64. log.error("质量检查失败", e);
  65. return false;
  66. }
  67. }
  68. public static Boolean startWork(String sn, String user) {
  69. try {
  70. String url = getMesRecordApiUrl("createProduct");
  71. String params = buildCommonParams()
  72. + "&sn=" + URLEncoder.encode(sn, "UTF-8")
  73. + "&ucode=" + URLEncoder.encode(user != null ? user : "", "UTF-8");
  74. JSONObject obj = parseJsonResponse(doPost(url, params));
  75. return obj != null && "true".equalsIgnoreCase(String.valueOf(obj.get("result")));
  76. } catch (Exception e) {
  77. log.error("开工失败", e);
  78. return false;
  79. }
  80. }
  81. public static Boolean bindUser(String sn, String user) {
  82. return bindWarehouse(sn, user, user, "400001");
  83. }
  84. public static Boolean bindWarehouse(String sn, String wsn, String user, String craft) {
  85. try {
  86. String url = getMesRecordApiUrl("bindWarehouse");
  87. String params = buildCommonParams()
  88. + "&sn=" + URLEncoder.encode(sn, "UTF-8")
  89. + "&wsn=" + URLEncoder.encode(wsn, "UTF-8")
  90. + "&ucode=" + URLEncoder.encode(user != null ? user : "", "UTF-8")
  91. + "&craft=" + URLEncoder.encode(craft != null ? craft : "", "UTF-8");
  92. JSONObject obj = parseJsonResponse(doPost(url, params));
  93. return obj != null && "true".equalsIgnoreCase(String.valueOf(obj.get("result")));
  94. } catch (Exception e) {
  95. log.error("物料绑定失败", e);
  96. return false;
  97. }
  98. }
  99. public static Boolean unBindWarehouse(String sn, String wsn, String user, String craft) {
  100. try {
  101. String url = getMesRecordApiUrl("unBindWarehouse");
  102. String params = buildCommonParams()
  103. + "&sn=" + URLEncoder.encode(sn, "UTF-8")
  104. + "&wsn=" + URLEncoder.encode(wsn, "UTF-8")
  105. + "&ucode=" + URLEncoder.encode(user != null ? user : "", "UTF-8")
  106. + "&craft=" + URLEncoder.encode(craft != null ? craft : "", "UTF-8");
  107. JSONObject obj = parseJsonResponse(doPost(url, params));
  108. return obj != null && "true".equalsIgnoreCase(String.valueOf(obj.get("result")));
  109. } catch (Exception e) {
  110. log.error("物料解绑失败", e);
  111. return false;
  112. }
  113. }
  114. public static Boolean sendQuality(String sn, String result, String user) {
  115. try {
  116. String msgType = "MQDW";
  117. String gw = "GW" + rightPad(MesClient.getEffectiveOprno(), 6);
  118. String gy = "GY100000";
  119. JdbcUtils.insertData(gw, gy, "HTTP qmresult sn=" + sn + " result=" + result, msgType, sn);
  120. JdbcUtils.insertSubmitRecord(gw, sn, "HTTP qmresult");
  121. return true;
  122. } catch (Exception e) {
  123. return false;
  124. }
  125. }
  126. public static String rightPad(final String str, final int size) {
  127. if (str == null) {
  128. return null;
  129. }
  130. String strret = str;
  131. if (str.length() > size) {
  132. strret = str.substring(0, size);
  133. }
  134. return String.format("%-" + size + "s", strret);
  135. }
  136. public static JSONObject getBindMaterail() {
  137. try {
  138. String mes_server_ip = MesClient.mes_server_ip;
  139. String oprno = MesClient.getEffectiveOprno().trim();
  140. String lineSn = MesClient.getEffectiveLineSn().trim();
  141. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesLineProcessMaterial/materials";
  142. String params = "__ajax=json&oprno=" + oprno + "&lineSn=" + lineSn;
  143. log.info("params=" + params);
  144. String result = doPost(url, params);
  145. log.info("result=" + result);
  146. if (result.equalsIgnoreCase("false")) {
  147. return null;
  148. } else {
  149. return JSONObject.parseObject(result);
  150. }
  151. } catch (Exception e) {
  152. return null;
  153. }
  154. }
  155. public static JSONObject saveBindMaterail(String batchSn, String craft, String materialId, String type) {
  156. try {
  157. String mes_server_ip = MesClient.mes_server_ip;
  158. String oprno = MesClient.getEffectiveOprno().trim();
  159. String lineSn = MesClient.getEffectiveLineSn().trim();
  160. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesMaterialPrebind/bind";
  161. String params = "__ajax=json&oprno=" + oprno + "&lineSn=" + lineSn + "&batchSn=" + batchSn + "&craft=" + craft + "&materialId=" + materialId + "&type=" + type;
  162. log.info("params=" + params);
  163. String result = doPost(url, params);
  164. log.info("result=" + result);
  165. if (result.equalsIgnoreCase("false")) {
  166. return null;
  167. } else {
  168. return JSONObject.parseObject(result);
  169. }
  170. } catch (Exception e) {
  171. return null;
  172. }
  173. }
  174. // 上传气密结果
  175. public static JSONObject qmResultData(TestParam testParam) {
  176. try {
  177. String enconding = "UTF-8";
  178. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  179. Properties pro = new Properties();
  180. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  181. pro.load(br);
  182. String mes_server_ip = pro.getProperty("mes.server_ip");
  183. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/qmresult";
  184. String testPressureBase64 = Base64Utils.getBase64(testParam.getParam1() != null ? testParam.getParam1() : "");
  185. String leakValBase64 = Base64Utils.getBase64(testParam.getParam3() != null ? testParam.getParam3() : "");
  186. String testTimeBase64 = Base64Utils.getBase64(testParam.getCreateTime() != null ? testParam.getCreateTime() : "");
  187. String titleBase64 = Base64Utils.getBase64(testParam.getOprnoTitle() != null ? testParam.getOprnoTitle() : "");
  188. log.info("服务器端需要Base64编码的参数: testPressure='{}'->'{}', leakVal='{}'->'{}', testTime='{}'->'{}', title='{}'->'{}'"
  189. , testParam.getParam1(), testPressureBase64, testParam.getParam3(), leakValBase64
  190. , testParam.getCreateTime(), testTimeBase64, testParam.getOprnoTitle(), titleBase64);
  191. String params = "__ajax=json&oprno=" + URLEncoder.encode(testParam.getOprno() != null ? testParam.getOprno() : "", "UTF-8")
  192. + "&lineSn=" + URLEncoder.encode(testParam.getLineSn() != null ? testParam.getLineSn() : "", "UTF-8")
  193. + "&sn=" + URLEncoder.encode(testParam.getSn() != null ? testParam.getSn() : "", "UTF-8")
  194. + "&workNum=" + URLEncoder.encode(testParam.getUcode() != null ? testParam.getUcode() : "", "UTF-8")
  195. + "&cxm=" + URLEncoder.encode(testParam.getParam5() != null ? testParam.getParam5() : "", "UTF-8")
  196. + "&result=" + URLEncoder.encode(testParam.getResult() != null ? testParam.getResult() : "", "UTF-8")
  197. + "&testPressure=" + URLEncoder.encode(testPressureBase64, "UTF-8")
  198. + "&testPressureUnit=" + URLEncoder.encode(testParam.getParam2() != null ? testParam.getParam2() : "", "UTF-8")
  199. + "&leakVal=" + URLEncoder.encode(leakValBase64, "UTF-8")
  200. + "&leakValUnit=" + URLEncoder.encode(testParam.getParam4() != null ? testParam.getParam4() : "", "UTF-8")
  201. + "&testTime=" + URLEncoder.encode(testTimeBase64, "UTF-8")
  202. + "&title=" + URLEncoder.encode(titleBase64, "UTF-8")
  203. + "&remark=" + URLEncoder.encode(testParam.getRemark() != null ? testParam.getRemark() : "", "UTF-8")
  204. + "&deviceType=" + URLEncoder.encode(testParam.getDeviceType() != null ? testParam.getDeviceType() : "", "UTF-8")
  205. + "&cq=" + URLEncoder.encode("", "UTF-8")
  206. + "&by=" + URLEncoder.encode("", "UTF-8")
  207. + "&cs=" + URLEncoder.encode("", "UTF-8")
  208. + "&leakRate=" + URLEncoder.encode("", "UTF-8")
  209. + "&leakRateUnit=" + URLEncoder.encode("", "UTF-8")
  210. + "&type=" + URLEncoder.encode("", "UTF-8");
  211. log.info("params=" + params);
  212. String result = doPost(url, params);
  213. log.info("result=" + result);
  214. if (result == null || result.trim().isEmpty()) {
  215. log.error("HTTP请求返回结果为空,请检查服务器连接和参数格式");
  216. return null;
  217. }
  218. if (result.equalsIgnoreCase("false")) {
  219. return null;
  220. } else {
  221. try {
  222. return JSONObject.parseObject(result);
  223. } catch (Exception e) {
  224. log.error("解析JSON响应失败: {}, 原始响应: {}", e.getMessage(), result);
  225. return null;
  226. }
  227. }
  228. } catch (Exception e) {
  229. log.error("qmResultData方法执行异常: {}", e.getMessage());
  230. e.printStackTrace();
  231. return null;
  232. }
  233. }
  234. public static String doPost(String httpUrl, String param) {
  235. HttpURLConnection connection = null;
  236. InputStream is = null;
  237. OutputStream os = null;
  238. BufferedReader br = null;
  239. String result = null;
  240. try {
  241. URL url = new URL(httpUrl);
  242. connection = (HttpURLConnection) url.openConnection();
  243. connection.setRequestMethod("POST");
  244. connection.setConnectTimeout(15000);
  245. connection.setReadTimeout(60000);
  246. connection.setDoOutput(true);
  247. connection.setDoInput(true);
  248. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  249. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  250. os = connection.getOutputStream();
  251. os.write(param.getBytes());
  252. int responseCode = connection.getResponseCode();
  253. log.info("HTTP响应码: " + responseCode);
  254. if (responseCode == 200) {
  255. is = connection.getInputStream();
  256. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  257. StringBuffer sbf = new StringBuffer();
  258. String temp = null;
  259. while ((temp = br.readLine()) != null) {
  260. sbf.append(temp);
  261. sbf.append("\r\n");
  262. }
  263. result = sbf.toString();
  264. } else {
  265. log.error("HTTP请求失败,响应码: " + responseCode);
  266. try {
  267. is = connection.getErrorStream();
  268. if (is != null) {
  269. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  270. StringBuffer errorBuffer = new StringBuffer();
  271. String temp = null;
  272. while ((temp = br.readLine()) != null) {
  273. errorBuffer.append(temp);
  274. errorBuffer.append("\r\n");
  275. }
  276. log.error("服务器错误响应内容: " + errorBuffer.toString());
  277. }
  278. } catch (Exception errorReadException) {
  279. log.error("读取错误响应失败: " + errorReadException.getMessage());
  280. }
  281. }
  282. } catch (MalformedURLException e) {
  283. e.printStackTrace();
  284. } catch (IOException e) {
  285. e.printStackTrace();
  286. } finally {
  287. if (null != br) {
  288. try {
  289. br.close();
  290. } catch (IOException e) {
  291. e.printStackTrace();
  292. }
  293. }
  294. if (null != os) {
  295. try {
  296. os.close();
  297. } catch (IOException e) {
  298. e.printStackTrace();
  299. }
  300. }
  301. if (null != is) {
  302. try {
  303. is.close();
  304. } catch (IOException e) {
  305. e.printStackTrace();
  306. }
  307. }
  308. if (connection != null) {
  309. connection.disconnect();
  310. }
  311. }
  312. return result;
  313. }
  314. public static String getBase64Encode(String str) {
  315. if (str == null || "".equals(str)) {
  316. return "";
  317. }
  318. try {
  319. str = str.trim().replaceAll("\\s+", "");
  320. return Base64.getEncoder().encodeToString(str.getBytes("UTF-8"));
  321. } catch (UnsupportedEncodingException e) {
  322. e.printStackTrace();
  323. return "";
  324. }
  325. }
  326. }