DataUtil.java 14 KB

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