DataUtil.java 13 KB

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