DataUtil.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. //获取当前工位工件码
  43. public static JSONObject getCurSn(){
  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. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/getCurSn";
  54. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&__sid="+MesClient.sessionid;
  55. JSONObject result = JSONObject.parseObject(doPost(url, params));
  56. // log.info("请求参数:"+params);
  57. return result;
  58. } catch (Exception e) {
  59. log.info("e="+e.getMessage());
  60. return null;
  61. }
  62. }
  63. public static JSONObject delCurSn(){
  64. try {
  65. String enconding = "UTF-8";
  66. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  67. Properties pro = new Properties();
  68. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  69. pro.load(br);
  70. String mes_server_ip = pro.getProperty("mes.server_ip");
  71. String oprno = pro.getProperty("mes.gw").trim();
  72. String lineSn = pro.getProperty("mes.line_sn").trim();
  73. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/cancelCurSn";
  74. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&__sid="+MesClient.sessionid;
  75. JSONObject result = JSONObject.parseObject(doPost(url, params));
  76. // log.info("请求参数:"+url);
  77. return result;
  78. } catch (Exception e) {
  79. log.info("e="+e.getMessage());
  80. return null;
  81. }
  82. }
  83. public static JSONObject createQuality(String sn, String user,String OKNgResult){
  84. try{
  85. String enconding = "UTF-8";
  86. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  87. Properties pro = new Properties();
  88. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  89. pro.load(br);
  90. String mes_server_ip = pro.getProperty("mes.server_ip");
  91. String oprno = pro.getProperty("mes.gw").trim();
  92. String lineSn = pro.getProperty("mes.line_sn").trim();
  93. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pccreate";
  94. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&ucode="+user+"&result="+OKNgResult;
  95. log.info("params="+params);
  96. String result = doPost(url,params);
  97. log.info("result="+result);
  98. return JSONObject.parseObject(result);
  99. }catch (Exception e){
  100. log.info("e="+e.getMessage());
  101. return null;
  102. }
  103. }
  104. public static JSONObject sendQuality(String sn,String ret,String user){
  105. try{
  106. String enconding = "UTF-8";
  107. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  108. Properties pro = new Properties();
  109. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  110. pro.load(br);
  111. String mes_server_ip = pro.getProperty("mes.server_ip");
  112. String oprno = pro.getProperty("mes.gw").trim();
  113. String lineSn = pro.getProperty("mes.line_sn").trim();
  114. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pcresult";
  115. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&result="+ret+"&ucode="+user;
  116. log.info("params="+params);
  117. String result = doPost(url,params);
  118. log.info("result="+result);
  119. if(result.equalsIgnoreCase("false")) {
  120. return null;
  121. }else {
  122. JdbcUtils.insertData(oprno, "100000", params, "MQDW", sn);
  123. return JSONObject.parseObject(result);
  124. }
  125. }catch (Exception e){
  126. log.info("e="+e.getMessage());
  127. return null;
  128. }
  129. }
  130. public static String rightPad(final String str, final int size) {
  131. if (str == null) {
  132. return null;
  133. }
  134. String strret = str;
  135. if(str.length() > size){
  136. strret = str.substring(0,size);
  137. }
  138. return String.format("%-"+size+"s", strret);
  139. }
  140. public static JSONObject getBindMaterail() {
  141. try{
  142. String enconding = "UTF-8";
  143. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  144. Properties pro = new Properties();
  145. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  146. pro.load(br);
  147. String mes_server_ip = pro.getProperty("mes.server_ip");
  148. String oprno = pro.getProperty("mes.gw").trim();
  149. String lineSn = pro.getProperty("mes.line_sn").trim();
  150. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials";
  151. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn;
  152. System.out.println("params="+params);
  153. String result = doPost(url,params);
  154. System.out.println("result="+result);
  155. if(result.equalsIgnoreCase("false")) {
  156. return null;
  157. }else {
  158. return JSONObject.parseObject(result);
  159. }
  160. }catch (Exception e){
  161. return null;
  162. }
  163. }
  164. public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) {
  165. try{
  166. String enconding = "UTF-8";
  167. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  168. Properties pro = new Properties();
  169. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  170. pro.load(br);
  171. String mes_server_ip = pro.getProperty("mes.server_ip");
  172. String oprno = pro.getProperty("mes.gw").trim();
  173. String lineSn = pro.getProperty("mes.line_sn").trim();
  174. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind";
  175. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  176. System.out.println("params="+params);
  177. String result = doPost(url,params);
  178. System.out.println("result="+result);
  179. if(result.equalsIgnoreCase("false")) {
  180. return null;
  181. }else {
  182. return JSONObject.parseObject(result);
  183. }
  184. }catch (Exception e){
  185. return null;
  186. }
  187. }
  188. public static String doPost(String httpUrl, String param) {
  189. HttpURLConnection connection = null;
  190. InputStream is = null;
  191. OutputStream os = null;
  192. BufferedReader br = null;
  193. String result = null;
  194. try {
  195. URL url = new URL(httpUrl);
  196. connection = (HttpURLConnection) url.openConnection();
  197. connection.setRequestMethod("POST");
  198. connection.setConnectTimeout(15000);
  199. connection.setReadTimeout(60000);
  200. connection.setDoOutput(true);
  201. connection.setDoInput(true);
  202. // 必须显式指定 UTF-8:打包成 exe 后 Windows JVM 默认编码是 GBK,
  203. // 若使用 param.getBytes()(无字符集),中文参数会按 GBK 发出,服务端按 UTF-8 解析即乱码。
  204. // 点检走 WebView 浏览器提交,本身就是 UTF-8,所以点检不受影响。
  205. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  206. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  207. os = connection.getOutputStream();
  208. os.write(param.getBytes("UTF-8"));
  209. if (connection.getResponseCode() == 200) {
  210. is = connection.getInputStream();
  211. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  212. StringBuffer sbf = new StringBuffer();
  213. String temp = null;
  214. while ((temp = br.readLine()) != null) {
  215. sbf.append(temp);
  216. sbf.append("\r\n");
  217. }
  218. result = sbf.toString();
  219. }
  220. } catch (MalformedURLException e) {
  221. e.printStackTrace();
  222. } catch (IOException e) {
  223. e.printStackTrace();
  224. } finally {
  225. if (null != br) {
  226. try {
  227. br.close();
  228. } catch (IOException e) {
  229. e.printStackTrace();
  230. }
  231. }
  232. if (null != os) {
  233. try {
  234. os.close();
  235. } catch (IOException e) {
  236. e.printStackTrace();
  237. }
  238. }
  239. if (null != is) {
  240. try {
  241. is.close();
  242. } catch (IOException e) {
  243. e.printStackTrace();
  244. }
  245. }
  246. connection.disconnect();
  247. }
  248. return result;
  249. }
  250. /**
  251. * 查询工作记录
  252. * @param oprno 工位号(空则查所有工位)
  253. * @param sn 工件码(可选,用于搜索)
  254. * @param pageNo 页码
  255. * @param pageSize 每页条数
  256. * @return 工作记录响应
  257. */
  258. public static WorkRecordResp getWorkRecordList(String oprno, String sn, int pageNo, int pageSize) {
  259. WorkRecordResp resp = new WorkRecordResp();
  260. try {
  261. String enconding = "UTF-8";
  262. InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
  263. Properties pro = new Properties();
  264. BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
  265. pro.load(br);
  266. String mes_server_ip = pro.getProperty("mes.server_ip");
  267. String lineSn = pro.getProperty("mes.line_sn").trim();
  268. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/ ";
  269. StringBuilder params = new StringBuilder();
  270. params.append("__ajax=json");
  271. params.append("&lineSn=").append(lineSn);
  272. params.append("&pageNo=").append(pageNo);
  273. params.append("&pageSize=").append(pageSize);
  274. if (oprno != null && !oprno.isEmpty()) {
  275. params.append("&oprno=").append(oprno);
  276. }
  277. // 注意:不传auto参数,这样"所有工位"查询时会返回整条产线所有工位的数据
  278. if (sn != null && !sn.isEmpty()) {
  279. params.append("&sn=").append(sn);
  280. }
  281. log.info("查询工作记录: url=" + url + ", params=" + params.toString());
  282. String result = doPost(url, params.toString());
  283. log.info("查询工作记录结果: result=" + result);
  284. if (result == null || result.trim().isEmpty()) {
  285. resp.setResult(false);
  286. resp.setMessage("请求返回空结果");
  287. return resp;
  288. }
  289. JSONObject jsonObj = JSONObject.parseObject(result);
  290. if (jsonObj == null) {
  291. resp.setResult(false);
  292. resp.setMessage("解析响应失败");
  293. return resp;
  294. }
  295. resp.setResult(true);
  296. resp.setPageNo(jsonObj.getIntValue("pageNo"));
  297. resp.setPageSize(jsonObj.getIntValue("pageSize"));
  298. resp.setCount(jsonObj.getLongValue("count"));
  299. List<WorkRecordData> list = new ArrayList<>();
  300. JSONArray listArray = jsonObj.getJSONArray("list");
  301. if (listArray != null) {
  302. for (int i = 0; i < listArray.size(); i++) {
  303. JSONObject item = listArray.getJSONObject(i);
  304. WorkRecordData data = new WorkRecordData();
  305. data.setId(item.getString("id"));
  306. data.setSn(item.getString("sn"));
  307. data.setOprno(item.getString("oprno"));
  308. data.setUpdateBy(item.getString("updateBy"));
  309. data.setUpdateDate(item.getString("updateDate"));
  310. data.setContent(item.getString("content"));
  311. list.add(data);
  312. }
  313. }
  314. resp.setList(list);
  315. } catch (Exception e) {
  316. log.error("查询工作记录异常: " + e.getMessage());
  317. e.printStackTrace();
  318. resp.setResult(false);
  319. resp.setMessage("查询异常: " + e.getMessage());
  320. }
  321. return resp;
  322. }
  323. }