DataUtil.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.util.ArrayList;
  12. import java.util.List;
  13. public class DataUtil {
  14. public static final Logger log = LoggerFactory.getLogger(DataUtil.class);
  15. public static JSONObject checkQuality(String sn, String user){
  16. try{
  17. String mes_server_ip = MesClient.mes_server_ip;
  18. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  19. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  20. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pccheck";
  21. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&ucode="+user;
  22. log.info("params="+params);
  23. String result = doPost(url,params);
  24. log.info("result="+result);
  25. if(result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) {
  26. return null;
  27. }else {
  28. JdbcUtils.insertData(oprno, "100000", params, "AQDW", sn);
  29. return JSONObject.parseObject(result);
  30. }
  31. }catch (Exception e){
  32. log.info("e="+e.getMessage());
  33. return null;
  34. }
  35. }
  36. public static JSONObject bindWarehouse(String sn,String materielSn,String user,String craft ){
  37. String ret = "OK";
  38. try{
  39. String mes_server_ip = MesClient.mes_server_ip;
  40. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  41. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  42. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pcbindRecord";
  43. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&result="+ret+"&ucode="+user + "&craft="+craft+"&materielSn="+materielSn;
  44. log.info("params="+params);
  45. String result = doPost(url,params);
  46. log.info("result="+result);
  47. if(result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) {
  48. return null;
  49. }else {
  50. JdbcUtils.insertData(oprno, "100000", params, "MQDW", sn);
  51. return JSONObject.parseObject(result);
  52. }
  53. }catch (Exception e){
  54. log.info("e="+e.getMessage());
  55. return null;
  56. }
  57. }
  58. public static JSONObject sendQuality(String sn,String ret,String user){
  59. try{
  60. String mes_server_ip = MesClient.mes_server_ip;
  61. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  62. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  63. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pcresult";
  64. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&result="+ret+"&ucode="+user;
  65. log.info("params="+params);
  66. String result = doPost(url,params);
  67. log.info("result="+result);
  68. if(result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) {
  69. return null;
  70. }else {
  71. JdbcUtils.insertData(oprno, "100000", params, "MQDW", sn);
  72. return JSONObject.parseObject(result);
  73. }
  74. }catch (Exception e){
  75. log.info("e="+e.getMessage());
  76. return null;
  77. }
  78. }
  79. public static String rightPad(final String str, final int size) {
  80. if (str == null) {
  81. return null;
  82. }
  83. String strret = str;
  84. if(str.length() > size){
  85. strret = str.substring(0,size);
  86. }
  87. return String.format("%-"+size+"s", strret);
  88. }
  89. public static JSONObject getBindMaterail() {
  90. try{
  91. String mes_server_ip = MesClient.mes_server_ip;
  92. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  93. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  94. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
  95. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  96. System.out.println("params="+params);
  97. String result = doPost(url,params);
  98. System.out.println("result="+result);
  99. if(result.equalsIgnoreCase("false")) {
  100. return null;
  101. }else {
  102. return JSONObject.parseObject(result);
  103. }
  104. }catch (Exception e){
  105. return null;
  106. }
  107. }
  108. public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
  109. try{
  110. String mes_server_ip = MesClient.mes_server_ip;
  111. String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim();
  112. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  113. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
  114. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  115. System.out.println("params="+params);
  116. String result = doPost(url,params);
  117. System.out.println("result="+result);
  118. if(result.equalsIgnoreCase("false")) {
  119. return null;
  120. }else {
  121. return JSONObject.parseObject(result);
  122. }
  123. }catch (Exception e){
  124. return null;
  125. }
  126. }
  127. public static String doPost(String httpUrl, String param) {
  128. HttpURLConnection connection = null;
  129. InputStream is = null;
  130. OutputStream os = null;
  131. BufferedReader br = null;
  132. String result = null;
  133. try {
  134. URL url = new URL(httpUrl);
  135. connection = (HttpURLConnection) url.openConnection();
  136. connection.setRequestMethod("POST");
  137. connection.setConnectTimeout(15000);
  138. connection.setReadTimeout(60000000);
  139. connection.setDoOutput(true);
  140. connection.setDoInput(true);
  141. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  142. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  143. os = connection.getOutputStream();
  144. os.write(param.getBytes());
  145. int code = connection.getResponseCode();
  146. log.info("HTTP POST " + httpUrl + " code=" + code);
  147. if (code == 200) {
  148. is = connection.getInputStream();
  149. } else {
  150. is = connection.getErrorStream();
  151. }
  152. if (is != null) {
  153. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  154. StringBuffer sbf = new StringBuffer();
  155. String temp = null;
  156. while ((temp = br.readLine()) != null) {
  157. sbf.append(temp);
  158. sbf.append("\r\n");
  159. }
  160. result = sbf.toString();
  161. }
  162. } catch (MalformedURLException e) {
  163. e.printStackTrace();
  164. } catch (IOException e) {
  165. e.printStackTrace();
  166. } finally {
  167. if (null != br) {
  168. try {
  169. br.close();
  170. } catch (IOException e) {
  171. e.printStackTrace();
  172. }
  173. }
  174. if (null != os) {
  175. try {
  176. os.close();
  177. } catch (IOException e) {
  178. e.printStackTrace();
  179. }
  180. }
  181. if (null != is) {
  182. try {
  183. is.close();
  184. } catch (IOException e) {
  185. e.printStackTrace();
  186. }
  187. }
  188. if (connection != null) {
  189. connection.disconnect();
  190. }
  191. }
  192. return result;
  193. }
  194. /**
  195. * 查询工作记录
  196. * @param oprno 工位号(空则查所有工位)
  197. * @param sn 工件码(可选,用于搜索)
  198. * @param pageNo 页码
  199. * @param pageSize 每页条数
  200. * @return 工作记录响应
  201. */
  202. public static WorkRecordResp getWorkRecordList(String oprno, String sn, int pageNo, int pageSize) {
  203. WorkRecordResp resp = new WorkRecordResp();
  204. try {
  205. String mes_server_ip = MesClient.mes_server_ip;
  206. String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
  207. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/workData";
  208. StringBuilder params = new StringBuilder();
  209. params.append("__ajax=json");
  210. params.append("&lineSn=").append(lineSn);
  211. params.append("&pageNo=").append(pageNo);
  212. params.append("&pageSize=").append(pageSize);
  213. System.out.println("oprno="+oprno);
  214. //todo
  215. if (oprno != null && !oprno.isEmpty()) {
  216. params.append("&oprno=").append(oprno);
  217. }
  218. // 注意:不传auto参数,这样"所有工位"查询时会返回整条产线所有工位的数据
  219. if (sn != null && !sn.isEmpty()) {
  220. params.append("&sn=").append(sn);
  221. }
  222. log.info("查询工作记录: url=" + url + ", params=" + params.toString());
  223. String result = doPost(url, params.toString());
  224. log.info("查询工作记录结果: result=" + result);
  225. if (result == null || result.trim().isEmpty()) {
  226. resp.setResult(false);
  227. resp.setMessage("请求返回空结果");
  228. return resp;
  229. }
  230. JSONObject jsonObj = JSONObject.parseObject(result);
  231. if (jsonObj == null) {
  232. resp.setResult(false);
  233. resp.setMessage("解析响应失败");
  234. return resp;
  235. }
  236. resp.setResult(true);
  237. resp.setPageNo(jsonObj.getIntValue("pageNo"));
  238. resp.setPageSize(jsonObj.getIntValue("pageSize"));
  239. resp.setCount(jsonObj.getLongValue("count"));
  240. List<WorkRecordData> list = new ArrayList<>();
  241. JSONArray listArray = jsonObj.getJSONArray("list");
  242. if (listArray != null) {
  243. for (int i = 0; i < listArray.size(); i++) {
  244. JSONObject item = listArray.getJSONObject(i);
  245. WorkRecordData data = new WorkRecordData();
  246. data.setId(item.getString("id"));
  247. data.setSn(item.getString("sn"));
  248. data.setOprno(item.getString("oprno"));
  249. data.setUpdateBy(item.getString("updateBy"));
  250. data.setUpdateDate(item.getString("updateDate"));
  251. data.setContent(item.getString("content"));
  252. list.add(data);
  253. }
  254. }
  255. resp.setList(list);
  256. } catch (Exception e) {
  257. log.error("查询工作记录异常: " + e.getMessage());
  258. e.printStackTrace();
  259. resp.setResult(false);
  260. resp.setMessage("查询异常: " + e.getMessage());
  261. }
  262. return resp;
  263. }
  264. }