package com.mes.ui; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.mes.util.Base64Utils; import com.mes.util.JdbcUtils; import com.mes.util.QmPlcPoint; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; public class DataUtil { public static final Logger log = LoggerFactory.getLogger(DataUtil.class); public static JSONObject checkQuality(String sn, String user){ return qmCheck(sn, user); } public static JSONObject qmCheck(String sn, String user){ try{ String mes_server_ip = MesClient.mes_server_ip; String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim(); String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim(); String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/qmcheck"; String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&workNum="+user; log.info("params="+params); String result = doPost(url,params); log.info("result="+result); if(result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) { return null; }else { JdbcUtils.insertData(oprno, "100000", params, "AQDW", sn); return JSONObject.parseObject(result); } }catch (Exception e){ log.info("e="+e.getMessage()); return null; } } public static JSONObject sendQmResult(String sn, String user, QmPlcPoint.QmTestData testData){ try{ String mes_server_ip = MesClient.mes_server_ip; String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim(); String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim(); String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/qmresult"; String testTime = QmPlcPoint.formatTestTime(testData.date, testData.time); String title = testData.name == null ? "" : testData.name; String cxm = testData.name == null ? "" : testData.name; String result = testData.result == null ? "NG" : testData.result; String testPressure = testData.testPressure == null ? "" : testData.testPressure.trim(); String leakVal = testData.leakVal == null ? "" : testData.leakVal.trim(); String remark = QmPlcPoint.buildRemark(testData); String testPressureBase64 = Base64Utils.getBase64(testPressure); String leakValBase64 = Base64Utils.getBase64(leakVal); String testTimeBase64 = Base64Utils.getBase64(testTime); String titleBase64 = Base64Utils.getBase64(title); log.info("qmresult upload leakVal='{}' base64='{}', testPressure='{}' base64='{}'", leakVal, leakValBase64, testPressure, testPressureBase64); StringBuilder params = new StringBuilder(); params.append("__ajax=json"); appendUrlParam(params, "oprno", oprno); appendUrlParam(params, "lineSn", lineSn); appendUrlParam(params, "sn", sn); appendUrlParam(params, "workNum", user); appendUrlParam(params, "result", result); appendUrlParam(params, "cxm", cxm); appendUrlParam(params, "cq", testData.pressurizingTime); appendUrlParam(params, "by", testData.equalizingTime); appendUrlParam(params, "cs", testData.testingTime); appendUrlParam(params, "testPressure", testPressureBase64); appendUrlParam(params, "testPressureUnit", testData.testPressureUnit); appendUrlParam(params, "leakVal", leakValBase64); appendUrlParam(params, "leakValUnit", testData.leakValUnit); appendUrlParam(params, "testTime", testTimeBase64); appendUrlParam(params, "title", titleBase64); appendUrlParam(params, "remark", remark); appendUrlParam(params, "deviceType", "S7-200SMART"); log.info("qmresult params="+params); String response = doPost(url, params.toString()); log.info("qmresult result="+response); if(response == null || response.trim().isEmpty() || response.equalsIgnoreCase("false")) { return null; } JdbcUtils.insertData(oprno, "100000", params.toString(), "MQDW", sn); return JSONObject.parseObject(response); }catch (Exception e){ log.info("sendQmResult e="+e.getMessage()); return null; } } private static void appendUrlParam(StringBuilder params, String name, String value) { try { params.append("&").append(name).append("="); if (value != null && !value.isEmpty()) { params.append(URLEncoder.encode(value, StandardCharsets.UTF_8.name())); } } catch (UnsupportedEncodingException e) { params.append("&").append(name).append("="); if (value != null) { params.append(value); } } } public static JSONObject bindWarehouse(String sn,String materielSn,String user,String craft ){ String ret = "OK"; try{ String mes_server_ip = MesClient.mes_server_ip; String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim(); String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim(); String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pcbindRecord"; String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&result="+ret+"&ucode="+user + "&craft="+craft+"&materielSn="+materielSn; log.info("params="+params); String result = doPost(url,params); log.info("result="+result); if(result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) { return null; }else { JdbcUtils.insertData(oprno, "100000", params, "MQDW", sn); return JSONObject.parseObject(result); } }catch (Exception e){ log.info("e="+e.getMessage()); return null; } } public static JSONObject sendQuality(String sn,String ret,String user){ try{ String mes_server_ip = MesClient.mes_server_ip; String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim(); String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim(); String url = "http://"+mes_server_ip+":8980/js/a/mes/mesProductRecord/pcresult"; String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&sn="+sn+"&result="+ret+"&ucode="+user; log.info("params="+params); String result = doPost(url,params); log.info("result="+result); if(result == null || result.trim().isEmpty() || result.equalsIgnoreCase("false")) { return null; }else { JdbcUtils.insertData(oprno, "100000", params, "MQDW", sn); return JSONObject.parseObject(result); } }catch (Exception e){ log.info("e="+e.getMessage()); return null; } } public static String rightPad(final String str, final int size) { if (str == null) { return null; } String strret = str; if(str.length() > size){ strret = str.substring(0,size); } return String.format("%-"+size+"s", strret); } public static JSONObject getBindMaterail() { try{ String mes_server_ip = MesClient.mes_server_ip; String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim(); String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim(); String url = "http://"+mes_server_ip+":8980/js/a/mes/mesLineProcessMaterial/materials"; String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn; System.out.println("params="+params); String result = doPost(url,params); System.out.println("result="+result); if(result.equalsIgnoreCase("false")) { return null; }else { return JSONObject.parseObject(result); } }catch (Exception e){ return null; } } public static JSONObject saveBindMaterail(String batchSn,String craft,String materialId,String type) { try{ String mes_server_ip = MesClient.mes_server_ip; String oprno = MesClient.mes_gw == null ? "" : MesClient.mes_gw.trim(); String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim(); String url = "http://"+mes_server_ip+":8980/js/a/mes/mesMaterialPrebind/bind"; String params = "__ajax=json&oprno="+oprno+"&lineSn="+lineSn+"&batchSn="+batchSn+"&craft="+craft+"&materialId="+materialId+"&type="+type; System.out.println("params="+params); String result = doPost(url,params); System.out.println("result="+result); if(result.equalsIgnoreCase("false")) { return null; }else { return JSONObject.parseObject(result); } }catch (Exception e){ return null; } } public static String doPost(String httpUrl, String param) { HttpURLConnection connection = null; InputStream is = null; OutputStream os = null; BufferedReader br = null; String result = null; try { URL url = new URL(httpUrl); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setConnectTimeout(15000); connection.setReadTimeout(60000000); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0"); os = connection.getOutputStream(); os.write(param.getBytes(StandardCharsets.UTF_8)); int code = connection.getResponseCode(); log.info("HTTP POST " + httpUrl + " code=" + code); if (code == 200) { is = connection.getInputStream(); } else { is = connection.getErrorStream(); } if (is != null) { br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer sbf = new StringBuffer(); String temp = null; while ((temp = br.readLine()) != null) { sbf.append(temp); sbf.append("\r\n"); } result = sbf.toString(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != os) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null) { connection.disconnect(); } } return result; } /** * 查询工作记录 * @param oprno 工位号(空则查所有工位) * @param sn 工件码(可选,用于搜索) * @param pageNo 页码 * @param pageSize 每页条数 * @return 工作记录响应 */ public static WorkRecordResp getWorkRecordList(String oprno, String sn, int pageNo, int pageSize) { WorkRecordResp resp = new WorkRecordResp(); try { String mes_server_ip = MesClient.mes_server_ip; String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim(); String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/workData"; StringBuilder params = new StringBuilder(); params.append("__ajax=json"); params.append("&lineSn=").append(lineSn); params.append("&pageNo=").append(pageNo); params.append("&pageSize=").append(pageSize); System.out.println("oprno="+oprno); //todo if (oprno != null && !oprno.isEmpty()) { params.append("&oprno=").append(oprno); } // 注意:不传auto参数,这样"所有工位"查询时会返回整条产线所有工位的数据 if (sn != null && !sn.isEmpty()) { params.append("&sn=").append(sn); } log.info("查询工作记录: url=" + url + ", params=" + params.toString()); String result = doPost(url, params.toString()); log.info("查询工作记录结果: result=" + result); if (result == null || result.trim().isEmpty()) { resp.setResult(false); resp.setMessage("请求返回空结果"); return resp; } JSONObject jsonObj = JSONObject.parseObject(result); if (jsonObj == null) { resp.setResult(false); resp.setMessage("解析响应失败"); return resp; } resp.setResult(true); resp.setPageNo(jsonObj.getIntValue("pageNo")); resp.setPageSize(jsonObj.getIntValue("pageSize")); resp.setCount(jsonObj.getLongValue("count")); List list = new ArrayList<>(); JSONArray listArray = jsonObj.getJSONArray("list"); if (listArray != null) { for (int i = 0; i < listArray.size(); i++) { JSONObject item = listArray.getJSONObject(i); WorkRecordData data = new WorkRecordData(); data.setId(item.getString("id")); data.setSn(item.getString("sn")); data.setOprno(item.getString("oprno")); data.setUpdateBy(item.getString("updateBy")); data.setUpdateDate(item.getString("updateDate")); data.setContent(item.getString("content")); list.add(data); } } resp.setList(list); } catch (Exception e) { log.error("查询工作记录异常: " + e.getMessage()); e.printStackTrace(); resp.setResult(false); resp.setMessage("查询异常: " + e.getMessage()); } return resp; } }