DataUtil.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import java.io.*;
  8. import java.net.HttpURLConnection;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11. import java.net.URLEncoder;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import java.util.Properties;
  15. public class DataUtil {
  16. public static final Logger log = LoggerFactory.getLogger(DataUtil.class);
  17. //删除当前工位工件码
  18. public static JSONObject delCurSn(String oprno,String serverIp,String lineSn){
  19. try {
  20. String url = "http://"+serverIp+":8980/js/a/mes/mesProductRecord/cancelCurSn";
  21. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&__sid="+MesClient.sessionid;
  22. JSONObject result = JSONObject.parseObject(doPost(url, params));
  23. // log.info("请求参数:"+params);
  24. return result;
  25. } catch (Exception e) {
  26. log.info("e="+e.getMessage());
  27. return null;
  28. }
  29. }
  30. //获取当前工位工件码
  31. public static JSONObject getCurSn(String oprno,String serverIp,String lineSn){
  32. try {
  33. String url = "http://"+serverIp+":8980/js/a/mes/mesProductRecord/getCurSn";
  34. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&__sid="+MesClient.sessionid;
  35. JSONObject result = JSONObject.parseObject(doPost(url, params));
  36. // log.info("请求参数:"+params);
  37. return result;
  38. } catch (Exception e) {
  39. log.info("e="+e.getMessage());
  40. return null;
  41. }
  42. }
  43. public static JSONObject checkQuality(String sn, String user){
  44. try{
  45. String enconding = "UTF-8";
  46. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  47. Properties pro = new Properties();
  48. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  49. pro.load(br);
  50. String mes_server_ip = pro.getProperty("mes.server_ip");
  51. String oprno = pro.getProperty("mes.gw").trim();
  52. String lineSn = pro.getProperty("mes.line_sn").trim();
  53. return checkQuality(sn, user, oprno, lineSn, mes_server_ip);
  54. }catch (Exception e){
  55. log.info("e="+e.getMessage());
  56. return null;
  57. }
  58. }
  59. public static JSONObject checkQuality(String sn, String user, String oprno, String lineSn, String serverIp){
  60. try{
  61. String url = "http://"+serverIp+":8980/js/a/mes/mesProductRecord/pccheck";
  62. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&ucode="+user+"&__sid="+MesClient.sessionid;
  63. log.info("params="+params);
  64. String result = doPost(url,params);
  65. log.info("result="+result);
  66. if(result.equalsIgnoreCase("false")) {
  67. return null;
  68. }else {
  69. JdbcUtils.insertData(oprno, "100000", params, "AQDW", sn);
  70. return JSONObject.parseObject(result);
  71. }
  72. }catch (Exception e){
  73. log.info("e="+e.getMessage());
  74. return null;
  75. }
  76. }
  77. public static JSONObject sendQuality(String sn,String ret,String user){
  78. try{
  79. String enconding = "UTF-8";
  80. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  81. Properties pro = new Properties();
  82. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  83. pro.load(br);
  84. String mes_server_ip = pro.getProperty("mes.server_ip");
  85. String oprno = pro.getProperty("mes.gw").trim();
  86. String lineSn = pro.getProperty("mes.line_sn").trim();
  87. return sendQuality(sn, ret, user, oprno, lineSn, mes_server_ip);
  88. }catch (Exception e){
  89. log.info("e="+e.getMessage());
  90. return null;
  91. }
  92. }
  93. public static JSONObject sendQuality(String sn, String ret, String user, String oprno, String lineSn, String serverIp){
  94. try{
  95. String url = "http://"+serverIp+":8980/js/a/mes/mesProductRecord/pcresult";
  96. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&result="+ret+"&ucode="+user+"&__sid="+MesClient.sessionid;
  97. log.info("params="+params);
  98. String result = doPost(url,params);
  99. log.info("result="+result);
  100. System.out.println("请求参数"+params);
  101. if(result.equalsIgnoreCase("false")) {
  102. return null;
  103. }else {
  104. JdbcUtils.insertData(oprno, "100000", params, "MQDW", sn);
  105. return JSONObject.parseObject(result);
  106. }
  107. }catch (Exception e){
  108. log.info("e="+e.getMessage());
  109. return null;
  110. }
  111. }
  112. // 保存 PLC 读取到的点焊/焊接参数,复用后端 mesProductArcWelding/batchsave 接口。
  113. public static JSONObject saveWeldingParams(String sn, String oprno, String lineSn, String serverIp, String content) {
  114. try {
  115. if (content == null || content.trim().isEmpty()) {
  116. log.info("焊接参数为空,不上传,工位=" + oprno + ",工件码=" + sn);
  117. return null;
  118. }
  119. String url = "http://" + serverIp + ":8980/js/a/mes/mesProductArcWelding/batchsave";
  120. JSONArray paramsArray = new JSONArray();
  121. JSONObject item = new JSONObject();
  122. item.put("sn", sn);
  123. item.put("oprno", oprno);
  124. item.put("lineSn", lineSn);
  125. item.put("content", content);
  126. paramsArray.add(item);
  127. String params = "__ajax=json&__sid=" + urlEncode(MesClient.sessionid)
  128. + "&params=" + urlEncode(paramsArray.toJSONString());
  129. log.info("上传焊接参数:" + paramsArray.toJSONString());
  130. String result = doPost(url, params);
  131. log.info("上传焊接参数结果:" + result);
  132. if (result == null || result.equalsIgnoreCase("false")) {
  133. return null;
  134. }
  135. return JSONObject.parseObject(result);
  136. } catch (Exception e) {
  137. log.info("上传焊接参数异常:" + e.getMessage());
  138. return null;
  139. }
  140. }
  141. private static String urlEncode(String value) throws UnsupportedEncodingException {
  142. return URLEncoder.encode(value == null ? "" : value, "UTF-8");
  143. }
  144. public static String rightPad(final String str, final int size) {
  145. if (str == null) {
  146. return null;
  147. }
  148. String strret = str;
  149. if(str.length() > size){
  150. strret = str.substring(0,size);
  151. }
  152. return String.format("%-"+size+"s", strret);
  153. }
  154. public static JSONObject getBindMaterail() {
  155. try{
  156. String enconding = "UTF-8";
  157. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  158. Properties pro = new Properties();
  159. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  160. pro.load(br);
  161. String mes_server_ip = pro.getProperty("mes.server_ip");
  162. String oprno = pro.getProperty("mes.gw").trim();
  163. String lineSn = pro.getProperty("mes.line_sn").trim();
  164. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
  165. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  166. System.out.println("params="+params);
  167. String result = doPost(url,params);
  168. System.out.println("result="+result);
  169. if(result.equalsIgnoreCase("false")) {
  170. return null;
  171. }else {
  172. return JSONObject.parseObject(result);
  173. }
  174. }catch (Exception e){
  175. return null;
  176. }
  177. }
  178. public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
  179. try{
  180. String enconding = "UTF-8";
  181. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  182. Properties pro = new Properties();
  183. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  184. pro.load(br);
  185. String mes_server_ip = pro.getProperty("mes.server_ip");
  186. String oprno = pro.getProperty("mes.gw").trim();
  187. String lineSn = pro.getProperty("mes.line_sn").trim();
  188. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
  189. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  190. System.out.println("params="+params);
  191. String result = doPost(url,params);
  192. System.out.println("result="+result);
  193. if(result.equalsIgnoreCase("false")) {
  194. return null;
  195. }else {
  196. return JSONObject.parseObject(result);
  197. }
  198. }catch (Exception e){
  199. return null;
  200. }
  201. }
  202. public static String doPost(String httpUrl, String param) {
  203. HttpURLConnection connection = null;
  204. InputStream is = null;
  205. OutputStream os = null;
  206. BufferedReader br = null;
  207. String result = null;
  208. try {
  209. URL url = new URL(httpUrl);
  210. connection = (HttpURLConnection) url.openConnection();
  211. connection.setRequestMethod("POST");
  212. connection.setConnectTimeout(15000);
  213. connection.setReadTimeout(60000);
  214. connection.setDoOutput(true);
  215. connection.setDoInput(true);
  216. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  217. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  218. os = connection.getOutputStream();
  219. os.write(param.getBytes());
  220. if (connection.getResponseCode() == 200) {
  221. is = connection.getInputStream();
  222. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  223. StringBuffer sbf = new StringBuffer();
  224. String temp = null;
  225. while ((temp = br.readLine()) != null) {
  226. sbf.append(temp);
  227. sbf.append("\r\n");
  228. }
  229. result = sbf.toString();
  230. }
  231. } catch (MalformedURLException e) {
  232. e.printStackTrace();
  233. } catch (IOException e) {
  234. e.printStackTrace();
  235. } finally {
  236. if (null != br) {
  237. try {
  238. br.close();
  239. } catch (IOException e) {
  240. e.printStackTrace();
  241. }
  242. }
  243. if (null != os) {
  244. try {
  245. os.close();
  246. } catch (IOException e) {
  247. e.printStackTrace();
  248. }
  249. }
  250. if (null != is) {
  251. try {
  252. is.close();
  253. } catch (IOException e) {
  254. e.printStackTrace();
  255. }
  256. }
  257. connection.disconnect();
  258. }
  259. return result;
  260. }
  261. /**
  262. * 查询工作记录
  263. * @param oprno 工位号(空则查所有工位)
  264. * @param sn 工件码(可选,用于搜索)
  265. * @param pageNo 页码
  266. * @param pageSize 每页条数
  267. * @return 工作记录响应
  268. */
  269. public static WorkRecordResp getWorkRecordList(String oprno, String sn, int pageNo, int pageSize) {
  270. WorkRecordResp resp = new WorkRecordResp();
  271. try {
  272. String enconding = "UTF-8";
  273. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  274. Properties pro = new Properties();
  275. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  276. pro.load(br);
  277. String mes_server_ip = pro.getProperty("mes.server_ip");
  278. String lineSn = pro.getProperty("mes.line_sn").trim();
  279. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/ ";
  280. StringBuilder params = new StringBuilder();
  281. params.append("__ajax=json");
  282. params.append("&lineSn=").append(lineSn);
  283. params.append("&pageNo=").append(pageNo);
  284. params.append("&pageSize=").append(pageSize);
  285. if (oprno != null && !oprno.isEmpty()) {
  286. params.append("&oprno=").append(oprno);
  287. }
  288. // 注意:不传auto参数,这样"所有工位"查询时会返回整条产线所有工位的数据
  289. if (sn != null && !sn.isEmpty()) {
  290. params.append("&sn=").append(sn);
  291. }
  292. log.info("查询工作记录: url=" + url + ", params=" + params.toString());
  293. String result = doPost(url, params.toString());
  294. log.info("查询工作记录结果: result=" + result);
  295. if (result == null || result.trim().isEmpty()) {
  296. resp.setResult(false);
  297. resp.setMessage("请求返回空结果");
  298. return resp;
  299. }
  300. JSONObject jsonObj = JSONObject.parseObject(result);
  301. if (jsonObj == null) {
  302. resp.setResult(false);
  303. resp.setMessage("解析响应失败");
  304. return resp;
  305. }
  306. resp.setResult(true);
  307. resp.setPageNo(jsonObj.getIntValue("pageNo"));
  308. resp.setPageSize(jsonObj.getIntValue("pageSize"));
  309. resp.setCount(jsonObj.getLongValue("count"));
  310. List<WorkRecordData> list = new ArrayList<>();
  311. JSONArray listArray = jsonObj.getJSONArray("list");
  312. if (listArray != null) {
  313. for (int i = 0; i < listArray.size(); i++) {
  314. JSONObject item = listArray.getJSONObject(i);
  315. WorkRecordData data = new WorkRecordData();
  316. data.setId(item.getString("id"));
  317. data.setSn(item.getString("sn"));
  318. data.setOprno(item.getString("oprno"));
  319. data.setUpdateBy(item.getString("updateBy"));
  320. data.setUpdateDate(item.getString("updateDate"));
  321. data.setContent(item.getString("content"));
  322. list.add(data);
  323. }
  324. }
  325. resp.setList(list);
  326. } catch (Exception e) {
  327. log.error("查询工作记录异常: " + e.getMessage());
  328. e.printStackTrace();
  329. resp.setResult(false);
  330. resp.setMessage("查询异常: " + e.getMessage());
  331. }
  332. return resp;
  333. }
  334. /**
  335. * 查询点检记录
  336. * @param oprno 工位号
  337. * @param pageNo 页码
  338. * @param pageSize 每页条数
  339. * @return 点检记录响应
  340. */
  341. public static CheckRecordResp getCheckRecordList(String oprno, int pageNo, int pageSize) {
  342. CheckRecordResp resp = new CheckRecordResp();
  343. try {
  344. String enconding = "UTF-8";
  345. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  346. Properties pro = new Properties();
  347. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  348. pro.load(br);
  349. String mes_server_ip = pro.getProperty("mes.server_ip");
  350. String lineSn = pro.getProperty("mes.line_sn").trim();
  351. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesCheckRecord/list";
  352. StringBuilder params = new StringBuilder();
  353. params.append("__ajax=json");
  354. params.append("&lineSn=").append(lineSn);
  355. params.append("&oprno=").append(oprno);
  356. params.append("&pageNo=").append(pageNo);
  357. params.append("&pageSize=").append(pageSize);
  358. log.info("查询点检记录: url=" + url + ", params=" + params.toString());
  359. String result = doPost(url, params.toString());
  360. log.info("查询点检记录结果: result=" + result);
  361. if (result == null || result.trim().isEmpty()) {
  362. resp.setResult(false);
  363. resp.setMessage("请求返回空结果");
  364. return resp;
  365. }
  366. JSONObject jsonObj = JSONObject.parseObject(result);
  367. if (jsonObj == null) {
  368. resp.setResult(false);
  369. resp.setMessage("解析响应失败");
  370. return resp;
  371. }
  372. resp.setResult(true);
  373. resp.setPageNo(jsonObj.getIntValue("pageNo"));
  374. resp.setPageSize(jsonObj.getIntValue("pageSize"));
  375. resp.setCount(jsonObj.getLongValue("count"));
  376. List<CheckRecordData> list = new ArrayList<>();
  377. JSONArray listArray = jsonObj.getJSONArray("list");
  378. if (listArray != null) {
  379. for (int i = 0; i < listArray.size(); i++) {
  380. JSONObject item = listArray.getJSONObject(i);
  381. CheckRecordData data = new CheckRecordData();
  382. data.setId(item.getString("id"));
  383. data.setCheckItem(item.getString("checkItem"));
  384. data.setCheckResult(item.getString("checkResult"));
  385. data.setUpdateBy(item.getString("updateBy"));
  386. data.setUpdateDate(item.getString("updateDate"));
  387. list.add(data);
  388. }
  389. }
  390. resp.setList(list);
  391. } catch (Exception e) {
  392. log.error("查询点检记录异常: " + e.getMessage());
  393. e.printStackTrace();
  394. resp.setResult(false);
  395. resp.setMessage("查询异常: " + e.getMessage());
  396. }
  397. return resp;
  398. }
  399. }