DataUtil.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSONArray;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.mes.util.JdbcUtils;
  5. import com.mes.util.Ut5310TestResult;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import java.io.*;
  9. import java.net.HttpURLConnection;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12. import java.net.URLEncoder;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. public class DataUtil {
  16. public static final Logger log = LoggerFactory.getLogger(DataUtil.class);
  17. public static JSONObject checkQuality(String sn, String user){
  18. try{
  19. String mes_server_ip = MesClient.mes_server_ip;
  20. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  21. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  22. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pccheck";
  23. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&ucode="+user;
  24. log.info("params="+params);
  25. String result = doPost(url,params);
  26. log.info("result="+result);
  27. if(result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) {
  28. return null;
  29. }else {
  30. JdbcUtils.insertData(oprno, "100000", params, "AQDW", sn);
  31. return JSONObject.parseObject(result);
  32. }
  33. }catch (Exception e){
  34. log.info("e="+e.getMessage());
  35. return null;
  36. }
  37. }
  38. public static JSONObject bindWarehouse(String sn,String materielSn,String user,String craft ){
  39. String ret = "OK";
  40. try{
  41. String mes_server_ip = MesClient.mes_server_ip;
  42. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  43. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  44. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pcbindRecord";
  45. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&result="+ret+"&ucode="+user + "&craft="+craft+"&materielSn="+materielSn;
  46. log.info("params="+params);
  47. String result = doPost(url,params);
  48. log.info("result="+result);
  49. if(result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) {
  50. return null;
  51. }else {
  52. JdbcUtils.insertData(oprno, "100000", params, "MQDW", sn);
  53. return JSONObject.parseObject(result);
  54. }
  55. }catch (Exception e){
  56. log.info("e="+e.getMessage());
  57. return null;
  58. }
  59. }
  60. public static JSONObject sendQuality(String sn,String ret,String user){
  61. try{
  62. String mes_server_ip = MesClient.mes_server_ip;
  63. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  64. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  65. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pcresult";
  66. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&result="+ret+"&ucode="+user;
  67. log.info("params="+params);
  68. String result = doPost(url,params);
  69. log.info("result="+result);
  70. if(result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) {
  71. return null;
  72. }else {
  73. JdbcUtils.insertData(oprno, "100000", params, "MQDW", sn);
  74. return JSONObject.parseObject(result);
  75. }
  76. }catch (Exception e){
  77. log.info("e="+e.getMessage());
  78. return null;
  79. }
  80. }
  81. public static JSONObject sendQuality(String sn, String ret, String user, String deviceResult, String deviceData) {
  82. try {
  83. String mes_server_ip = MesClient.mes_server_ip;
  84. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  85. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  86. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/pcresult";
  87. String params = "__ajax=json&oprno=" + encode(oprno)
  88. + "&lineSn=" + encode(lineSn)
  89. + "&sn=" + encode(sn)
  90. + "&result=" + encode(ret)
  91. + "&ucode=" + encode(user)
  92. + "&deviceType=UT5310"
  93. + "&deviceResult=" + encode(deviceResult)
  94. + "&deviceData=" + encode(deviceData);
  95. System.out.println("========== MES 上传(UT5310) ==========");
  96. System.out.println("URL: " + url);
  97. System.out.println("oprno=" + oprno + ", lineSn=" + lineSn + ", sn=" + sn
  98. + ", result=" + ret + ", ucode=" + user);
  99. System.out.println("deviceType=UT5310, deviceResult=" + deviceResult);
  100. System.out.println("deviceData(原始报文)=" + deviceData);
  101. System.out.println("POST body=" + params);
  102. log.info("params=" + params);
  103. String result = doPost(url, params);
  104. System.out.println("MES 响应: " + result);
  105. System.out.println("====================================");
  106. log.info("result=" + result);
  107. if (result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) {
  108. return null;
  109. } else {
  110. JdbcUtils.insertData(oprno, "100000", params, "MQDW", sn);
  111. return JSONObject.parseObject(result);
  112. }
  113. } catch (Exception e) {
  114. log.info("e=" + e.getMessage());
  115. return null;
  116. }
  117. }
  118. /** 上传 UT5310 绝缘耐压分步明细到 mes_product_insulation2 */
  119. public static JSONObject uploadInsulation2(String sn, String user, Ut5310TestResult testResult) {
  120. try {
  121. if (testResult == null || testResult.getSteps().isEmpty()) {
  122. log.info("uploadInsulation2 skipped: no step data");
  123. return null;
  124. }
  125. String mes_server_ip = MesClient.mes_server_ip;
  126. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductInsulation2/batchUpload";
  127. String stepsJson = testResult.toUploadStepsArray().toJSONString();
  128. String params = "__ajax=json&sn=" + encode(sn)
  129. + "&ucode=" + encode(user)
  130. + "&steps=" + encode(stepsJson);
  131. System.out.println("========== MES 上传(绝缘耐压明细) ==========");
  132. System.out.println("URL: " + url);
  133. System.out.println("sn=" + sn + ", ucode=" + user);
  134. System.out.println("steps=" + stepsJson);
  135. log.info("uploadInsulation2 params=" + params);
  136. String result = doPost(url, params);
  137. System.out.println("MES 响应: " + result);
  138. System.out.println("==========================================");
  139. log.info("uploadInsulation2 result=" + result);
  140. if (result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) {
  141. return null;
  142. }
  143. return JSONObject.parseObject(result);
  144. } catch (Exception e) {
  145. log.info("uploadInsulation2 e=" + e.getMessage());
  146. return null;
  147. }
  148. }
  149. private static String encode(String value) throws UnsupportedEncodingException {
  150. return URLEncoder.encode(value == null ? "" : value, "UTF-8");
  151. }
  152. public static String rightPad(final String str, final int size) {
  153. if (str == null) {
  154. return null;
  155. }
  156. String strret = str;
  157. if(str.length() > size){
  158. strret = str.substring(0,size);
  159. }
  160. return String.format("%-"+size+"s", strret);
  161. }
  162. public static JSONObject getBindMaterail() {
  163. try{
  164. String mes_server_ip = MesClient.mes_server_ip;
  165. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  166. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  167. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
  168. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  169. System.out.println("params="+params);
  170. String result = doPost(url,params);
  171. System.out.println("result="+result);
  172. if(result.equalsIgnoreCase("false")) {
  173. return null;
  174. }else {
  175. return JSONObject.parseObject(result);
  176. }
  177. }catch (Exception e){
  178. return null;
  179. }
  180. }
  181. public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
  182. try{
  183. String mes_server_ip = MesClient.mes_server_ip;
  184. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  185. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  186. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
  187. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  188. System.out.println("params="+params);
  189. String result = doPost(url,params);
  190. System.out.println("result="+result);
  191. if(result.equalsIgnoreCase("false")) {
  192. return null;
  193. }else {
  194. return JSONObject.parseObject(result);
  195. }
  196. }catch (Exception e){
  197. return null;
  198. }
  199. }
  200. public static String doPost(String httpUrl, String param) {
  201. HttpURLConnection connection = null;
  202. InputStream is = null;
  203. OutputStream os = null;
  204. BufferedReader br = null;
  205. String result = null;
  206. try {
  207. URL url = new URL(httpUrl);
  208. connection = (HttpURLConnection) url.openConnection();
  209. connection.setRequestMethod("POST");
  210. connection.setConnectTimeout(15000);
  211. connection.setReadTimeout(60000000);
  212. connection.setDoOutput(true);
  213. connection.setDoInput(true);
  214. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  215. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  216. os = connection.getOutputStream();
  217. os.write(param.getBytes());
  218. int code = connection.getResponseCode();
  219. log.info("HTTP POST " + httpUrl + " code=" + code);
  220. if (code == 200) {
  221. is = connection.getInputStream();
  222. } else {
  223. is = connection.getErrorStream();
  224. }
  225. if (is != null) {
  226. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  227. StringBuffer sbf = new StringBuffer();
  228. String temp = null;
  229. while ((temp = br.readLine()) != null) {
  230. sbf.append(temp);
  231. sbf.append("\r\n");
  232. }
  233. result = sbf.toString();
  234. }
  235. } catch (MalformedURLException e) {
  236. e.printStackTrace();
  237. } catch (IOException e) {
  238. e.printStackTrace();
  239. } finally {
  240. if (null != br) {
  241. try {
  242. br.close();
  243. } catch (IOException e) {
  244. e.printStackTrace();
  245. }
  246. }
  247. if (null != os) {
  248. try {
  249. os.close();
  250. } catch (IOException e) {
  251. e.printStackTrace();
  252. }
  253. }
  254. if (null != is) {
  255. try {
  256. is.close();
  257. } catch (IOException e) {
  258. e.printStackTrace();
  259. }
  260. }
  261. if (connection != null) {
  262. connection.disconnect();
  263. }
  264. }
  265. return result;
  266. }
  267. /**
  268. * 查询工作记录
  269. * @param oprno 工位号(空则查所有工位)
  270. * @param sn 工件码(可选,用于搜索)
  271. * @param pageNo 页码
  272. * @param pageSize 每页条数
  273. * @return 工作记录响应
  274. */
  275. public static WorkRecordResp getWorkRecordList(String oprno, String sn, int pageNo, int pageSize) {
  276. WorkRecordResp resp = new WorkRecordResp();
  277. try {
  278. String mes_server_ip = MesClient.mes_server_ip;
  279. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  280. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/workData";
  281. StringBuilder params = new StringBuilder();
  282. params.append("__ajax=json");
  283. params.append("&lineSn=").append(lineSn);
  284. params.append("&pageNo=").append(pageNo);
  285. params.append("&pageSize=").append(pageSize);
  286. System.out.println("oprno="+oprno);
  287. //todo
  288. if (oprno != null && !oprno.isEmpty()) {
  289. params.append("&oprno=").append(oprno);
  290. }
  291. // 注意:不传auto参数,这样"所有工位"查询时会返回整条产线所有工位的数据
  292. if (sn != null && !sn.isEmpty()) {
  293. params.append("&sn=").append(sn);
  294. }
  295. log.info("查询工作记录: url=" + url + ", params=" + params.toString());
  296. String result = doPost(url, params.toString());
  297. log.info("查询工作记录结果: result=" + result);
  298. if (result == null || result.trim().isEmpty()) {
  299. resp.setResult(false);
  300. resp.setMessage("请求返回空结果");
  301. return resp;
  302. }
  303. JSONObject jsonObj = JSONObject.parseObject(result);
  304. if (jsonObj == null) {
  305. resp.setResult(false);
  306. resp.setMessage("解析响应失败");
  307. return resp;
  308. }
  309. resp.setResult(true);
  310. resp.setPageNo(jsonObj.getIntValue("pageNo"));
  311. resp.setPageSize(jsonObj.getIntValue("pageSize"));
  312. resp.setCount(jsonObj.getLongValue("count"));
  313. List<WorkRecordData> list = new ArrayList<>();
  314. JSONArray listArray = jsonObj.getJSONArray("list");
  315. if (listArray != null) {
  316. for (int i = 0; i < listArray.size(); i++) {
  317. JSONObject item = listArray.getJSONObject(i);
  318. WorkRecordData data = new WorkRecordData();
  319. data.setId(item.getString("id"));
  320. data.setSn(item.getString("sn"));
  321. data.setOprno(item.getString("oprno"));
  322. data.setUpdateBy(item.getString("updateBy"));
  323. data.setUpdateDate(item.getString("updateDate"));
  324. data.setContent(item.getString("content"));
  325. list.add(data);
  326. }
  327. }
  328. resp.setList(list);
  329. } catch (Exception e) {
  330. log.error("查询工作记录异常: " + e.getMessage());
  331. e.printStackTrace();
  332. resp.setResult(false);
  333. resp.setMessage("查询异常: " + e.getMessage());
  334. }
  335. return resp;
  336. }
  337. }