DataUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSONArray;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.mes.util.ConfigUtil;
  5. import com.mes.util.JdbcUtils;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import java.io.*;
  9. import java.net.HttpURLConnection;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  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. public static JSONObject checkQuality(String sn, String user){
  18. try{
  19. Properties pro = ConfigUtil.loadProperties();
  20. String mes_server_ip = pro.getProperty("mes.server_ip");
  21. String oprno = pro.getProperty("mes.gw").trim();
  22. String lineSn = pro.getProperty("mes.line_sn").trim();
  23. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pccheck";
  24. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&ucode="+user;
  25. log.info("params="+params);
  26. String result = doPost(url,params);
  27. log.info("result="+result);
  28. if(result.equalsIgnoreCase("false")) {
  29. return null;
  30. }else {
  31. JdbcUtils.insertData(oprno, "100000", params, "AQDW", sn);
  32. return JSONObject.parseObject(result);
  33. }
  34. }catch (Exception e){
  35. log.info("e="+e.getMessage());
  36. return null;
  37. }
  38. }
  39. public static JSONObject createQuality(String sn, String user,String OKNgResult){
  40. try{
  41. Properties pro = ConfigUtil.loadProperties();
  42. String mes_server_ip = pro.getProperty("mes.server_ip");
  43. String oprno = pro.getProperty("mes.gw").trim();
  44. String lineSn = pro.getProperty("mes.line_sn").trim();
  45. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pccreate";
  46. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&ucode="+user+"&result="+OKNgResult;
  47. log.info("params="+params);
  48. String result = doPost(url,params);
  49. log.info("result="+result);
  50. return JSONObject.parseObject(result);
  51. }catch (Exception e){
  52. log.info("e="+e.getMessage());
  53. return null;
  54. }
  55. }
  56. public static JSONObject sendQuality(String sn,String ret,String user){
  57. try{
  58. Properties pro = ConfigUtil.loadProperties();
  59. String mes_server_ip = pro.getProperty("mes.server_ip");
  60. String oprno = pro.getProperty("mes.gw").trim();
  61. String lineSn = pro.getProperty("mes.line_sn").trim();
  62. String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pcresult";
  63. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&result="+ret+"&ucode="+user;
  64. log.info("params="+params);
  65. String result = doPost(url,params);
  66. log.info("result="+result);
  67. if(result.equalsIgnoreCase("false")) {
  68. return null;
  69. }else {
  70. JdbcUtils.insertData(oprno, "100000", params, "MQDW", sn);
  71. return JSONObject.parseObject(result);
  72. }
  73. }catch (Exception e){
  74. log.info("e="+e.getMessage());
  75. return null;
  76. }
  77. }
  78. public static String rightPad(final String str, final int size) {
  79. if (str == null) {
  80. return null;
  81. }
  82. String strret = str;
  83. if(str.length() > size){
  84. strret = str.substring(0,size);
  85. }
  86. return String.format("%-"+size+"s", strret);
  87. }
  88. public static JSONObject getBindMaterail() {
  89. try{
  90. Properties pro = ConfigUtil.loadProperties();
  91. String mes_server_ip = pro.getProperty("mes.server_ip");
  92. String oprno = pro.getProperty("mes.gw").trim();
  93. String lineSn = pro.getProperty("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. Properties pro = ConfigUtil.loadProperties();
  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/mesMaterialPrebind/bind";
  115. String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type;
  116. System.out.println("params="+params);
  117. String result = doPost(url,params);
  118. System.out.println("result="+result);
  119. if(result.equalsIgnoreCase("false")) {
  120. return null;
  121. }else {
  122. return JSONObject.parseObject(result);
  123. }
  124. }catch (Exception e){
  125. return null;
  126. }
  127. }
  128. public static String doPost(String httpUrl, String param) {
  129. HttpURLConnection connection = null;
  130. InputStream is = null;
  131. OutputStream os = null;
  132. BufferedReader br = null;
  133. String result = null;
  134. try {
  135. URL url = new URL(httpUrl);
  136. connection = (HttpURLConnection) url.openConnection();
  137. connection.setRequestMethod("POST");
  138. connection.setConnectTimeout(15000);
  139. connection.setReadTimeout(60000);
  140. connection.setDoOutput(true);
  141. connection.setDoInput(true);
  142. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  143. connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  144. os = connection.getOutputStream();
  145. os.write(param.getBytes());
  146. if (connection.getResponseCode() == 200) {
  147. is = connection.getInputStream();
  148. br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  149. StringBuffer sbf = new StringBuffer();
  150. String temp = null;
  151. while ((temp = br.readLine()) != null) {
  152. sbf.append(temp);
  153. sbf.append("\r\n");
  154. }
  155. result = sbf.toString();
  156. }
  157. } catch (MalformedURLException e) {
  158. e.printStackTrace();
  159. } catch (IOException e) {
  160. e.printStackTrace();
  161. } finally {
  162. if (null != br) {
  163. try {
  164. br.close();
  165. } catch (IOException e) {
  166. e.printStackTrace();
  167. }
  168. }
  169. if (null != os) {
  170. try {
  171. os.close();
  172. } catch (IOException e) {
  173. e.printStackTrace();
  174. }
  175. }
  176. if (null != is) {
  177. try {
  178. is.close();
  179. } catch (IOException e) {
  180. e.printStackTrace();
  181. }
  182. }
  183. connection.disconnect();
  184. }
  185. return result;
  186. }
  187. /**
  188. * 查询工作记录
  189. * @param oprno 工位号(空则查所有工位)
  190. * @param sn 工件码(可选,用于搜索)
  191. * @param pageNo 页码
  192. * @param pageSize 每页条数
  193. * @return 工作记录响应
  194. */
  195. public static WorkRecordResp getWorkRecordList(String oprno, String sn, int pageNo, int pageSize) {
  196. WorkRecordResp resp = new WorkRecordResp();
  197. try {
  198. Properties pro = ConfigUtil.loadProperties();
  199. String mes_server_ip = pro.getProperty("mes.server_ip");
  200. String lineSn = pro.getProperty("mes.line_sn").trim();
  201. // 与 WebView 工作记录页对应的数据接口(其它工位客户端同为 workData)
  202. String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/workData";
  203. StringBuilder params = new StringBuilder();
  204. params.append("__ajax=json");
  205. params.append("&lineSn=").append(lineSn);
  206. params.append("&pageNo=").append(pageNo);
  207. params.append("&pageSize=").append(pageSize);
  208. if (oprno != null && !oprno.isEmpty()) {
  209. params.append("&oprno=").append(oprno);
  210. }
  211. // 注意:不传auto参数,这样"所有工位"查询时会返回整条产线所有工位的数据
  212. if (sn != null && !sn.isEmpty()) {
  213. params.append("&sn=").append(sn);
  214. }
  215. log.info("查询工作记录: url=" + url + ", params=" + params.toString());
  216. String result = doPost(url, params.toString());
  217. log.info("查询工作记录结果: result=" + result);
  218. if (result == null || result.trim().isEmpty()) {
  219. resp.setResult(false);
  220. resp.setMessage("请求返回空结果");
  221. return resp;
  222. }
  223. JSONObject jsonObj = JSONObject.parseObject(result);
  224. if (jsonObj == null) {
  225. resp.setResult(false);
  226. resp.setMessage("解析响应失败");
  227. return resp;
  228. }
  229. resp.setResult(true);
  230. resp.setPageNo(jsonObj.getIntValue("pageNo"));
  231. resp.setPageSize(jsonObj.getIntValue("pageSize"));
  232. resp.setCount(jsonObj.getLongValue("count"));
  233. List<WorkRecordData> list = new ArrayList<>();
  234. JSONArray listArray = jsonObj.getJSONArray("list");
  235. if (listArray != null) {
  236. for (int i = 0; i < listArray.size(); i++) {
  237. JSONObject item = listArray.getJSONObject(i);
  238. WorkRecordData data = new WorkRecordData();
  239. data.setId(item.getString("id"));
  240. data.setSn(item.getString("sn"));
  241. data.setOprno(item.getString("oprno"));
  242. data.setUpdateBy(parseUpdateBy(item));
  243. data.setUpdateDate(item.getString("updateDate"));
  244. data.setContent(item.getString("content"));
  245. list.add(data);
  246. }
  247. }
  248. resp.setList(list);
  249. } catch (Exception e) {
  250. log.error("查询工作记录异常: " + e.getMessage());
  251. e.printStackTrace();
  252. resp.setResult(false);
  253. resp.setMessage("查询异常: " + e.getMessage());
  254. }
  255. return resp;
  256. }
  257. /**
  258. * 解析作业人员字段:支持字符串,或用户对象中的 loginName/userName/userCode
  259. */
  260. private static String parseUpdateBy(JSONObject item) {
  261. if (item == null) {
  262. return "";
  263. }
  264. Object updateByObj = item.get("updateBy");
  265. if (updateByObj == null) {
  266. // 兼容其它可能字段
  267. String ucode = item.getString("ucode");
  268. return ucode != null ? ucode : "";
  269. }
  270. if (updateByObj instanceof JSONObject) {
  271. JSONObject userObj = (JSONObject) updateByObj;
  272. String name = userObj.getString("loginName");
  273. if (name == null || name.isEmpty()) {
  274. name = userObj.getString("userName");
  275. }
  276. if (name == null || name.isEmpty()) {
  277. name = userObj.getString("userCode");
  278. }
  279. if (name == null || name.isEmpty()) {
  280. name = userObj.getString("id");
  281. }
  282. return name != null ? name : "";
  283. }
  284. String updateBy = String.valueOf(updateByObj).trim();
  285. if ("null".equalsIgnoreCase(updateBy)) {
  286. return "";
  287. }
  288. return updateBy;
  289. }
  290. }