package QCM.JHY01.JHY0102; import CoreFS.SA01.CoreIComponent; import CoreFS.SA06.CoreReturnObject; import QCM.JHY01.JHY0101.QuerryQltySample; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; import org.apache.log4j.Logger; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.sql.*; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TreeSet; import java.util.UUID; public class SendEntrust extends CoreIComponent{ private Logger logger = Logger.getLogger(this.getClass().getName()); CoreReturnObject cro = new CoreReturnObject(); /** * 调用LIMS接收委托接口,实现委托下发 * @return cro * @throws IOException */ public CoreReturnObject SendLIMES(String Entrust,String sendId,String sendName){ //"http://10.70.0.237:9060/jbDataToJhWebService/SetOrdCommResult" String urlStr = "http://172.16.0.169:82/Limsinterface/http/Inspection"; //String urlStr = "http://172.16.0.188:8081/open/lims/4mes/Inspection"; ResultSet rs =null; Connection cn = null; PreparedStatement pSta1 = null; try { StringBuffer sqlucomm = new StringBuffer(); // JSONObject cretjson = JSON.parseObject(Entrust); // JSONArray ja = cretjson.getJSONArray("DETAIL"); String[] ja=Entrust.split(","); String msInfo=""; if (ja!=null && ja.length>0) { for (int i = 0; i < ja.length; i++) { String sp = ja[i]; if ("".equals(msInfo)) { msInfo="'"+sp+"'"; }else{ msInfo=msInfo+"'"+sp+"'"; } if (i !=ja.length-1) { msInfo=msInfo+","; } } } //查询检验 sqlucomm.append("SELECT d.SMP_NO,d.SPECIMEN_NO,d.HEAT_NO,d.BATCH_NO,d.INSPECTION_LOT,d.FREQ_CODE,d.FREQ_NAME," + " d.MATERIAL_NO,d.SMP_TYPE_CODE,d.SMP_TYPE_NAME,d.CONSIGN_NO,d.CONSIGN_NO_SEQ,d.TEST_QTY,d.SMP_QTY," + " d.SMP_LOCATION,d.BOARD_NO,d.QUOTE_CONSIGN_NO,d.OLD_CONSIGN_NO,d.PLINE_CODE,d.PLINE_NAME,d.CHEM_ITEM," + "d.SOURCE,d.PRODUCT_CNT,m.SMP_CATG, o.steel_code,o.steel_name " + " FROM QCM_JHY_SAMPLE_CONSIGN_D d LEFT JOIN QCM_JHY_SAMPLE_CONSIGN_M m ON d.SMP_NO=m.SMP_NO LEFT JOIN QCM_JHY_SAMPLE_R_ORD O ON d.SMP_NO = o.SMP_NO where d.SPECIMEN_NO in ("+msInfo+") and d.VALIDFLAG='1' and d.STATUS='0'"); rs = this.getDao("testDao").ExceuteQueryForResultSet(sqlucomm.toString()); if (rs==null) { cro.setV_errCode(-1); cro.setV_errMsg("待发送的检验委托数据已经不存在,请核实数据!"); return cro; } JSONArray ranQltyArraySmp=QuerryQltySample.resultSetToJsonArry(rs); //this.getDao("testDao").closeRs(rs); rs.close(); if (ranQltyArraySmp==null || ranQltyArraySmp.size()<1) { cro.setV_errCode(-1); cro.setV_errMsg("待发送的检验委托数据已经不存在,请核实数据!"); return cro; } //获取数据进行验证 TreeSet sizes=new TreeSet(); for (int i = 0; i < ranQltyArraySmp.size(); i++) { JSONObject obj=ranQltyArraySmp.getJSONObject(i); sizes.add(obj.getString("SMP_CATG")); } if (sizes.size()!=1) { cro.setV_errCode(-1); cro.setV_errMsg("待发送的检验委托数据只能同时发送一种类型的待检数据,请核实数据!"); return cro; } JSONArray tarDate=new JSONArray(); Date date=new Date(); SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString=simpleDateFormat.format(date); String consignNo=new SimpleDateFormat("yyyyMMdd").format(date); sqlucomm.setLength(0); sqlucomm.append("SELECT max(to_number(substr(CONSIGN_NO,-3,3))) BIGSEQ from QCM_JHY_SAMPLE_CONSIGN_D where CONSIGN_NO like '"+consignNo+"%'"); rs=this.getDao("testDao").ExceuteQueryForResultSet(sqlucomm.toString()); if (rs.next()) { int count=rs.getInt("BIGSEQ"); if (count>0) { count++; }else{ count=1; } consignNo=consignNo+String.format("%03d", count); } //this.getDao("testDao").closeRs(rs); rs.close(); int seq=1; //生成委托编号 for (Object array : ranQltyArraySmp) { JSONObject obj = (JSONObject)array; obj.put("SEND_ID", sendId); obj.put("SEND_NAME", sendName); obj.put("CONSIGN_NO", consignNo); obj.put("CONSIGN_NO_SEQ", seq++); obj.put("SEND_TIME", dateString); //目标json JSONObject tarObj=new JSONObject(); //根据取样编号查询检验项目 sqlucomm.setLength(0); sqlucomm.append("select * from QCM_JHY_SAMPLE_CONSIGN_D_ITEM where SPECIMEN_NO='"+obj.getString("SPECIMEN_NO")+"' and QUOTE_SPECIMEN_NO is null"); rs = this.getDao("testDao").ExceuteQueryForResultSet(sqlucomm.toString()); if (rs==null) { continue; } JSONArray items=QuerryQltySample.resultSetToJsonArry(rs); //this.getDao("testDao").closeRs(rs); rs.close(); sqlucomm.setLength(0); sqlucomm.append("select * from QCM_JHY_SAMPLE_R_ORD where SMP_NO='"+obj.getString("SMP_NO")+"' "); rs = this.getDao("testDao").ExceuteQueryForResultSet(sqlucomm.toString()); if (rs==null) { cro.setV_errCode(-1); cro.setV_errMsg("订单信息不存在,请核实数据!"); return cro; } JSONArray ordArr=QuerryQltySample.resultSetToJsonArry(rs); //this.getDao("testDao").closeRs(rs); rs.close(); obj.put("THICK", ordArr.getJSONObject(0).getString("THICK")); obj.put("WIDTH", ordArr.getJSONObject(0).getString("WIDTH")); obj.put("LENGTH", ordArr.getJSONObject(0).getString("LENGTH")); //主表数据 createTarObj(tarObj,obj); tarObj.put("CREATE_ID", sendId); tarObj.put("CREATE_NAME", sendName); //如果是材质检验委托 if ("B".equals(obj.getString("SMP_CATG"))) { JSONArray arrayItem=new JSONArray(); for(int i = 0 ; i < items.size() ; i++){ JSONObject oldItem = items.getJSONObject(i); JSONObject tarItem=new JSONObject(); //子表数据 createItem(tarItem,oldItem); arrayItem.add(tarItem); } tarObj.put("items", arrayItem); }else{ JSONObject chemItem=JSON.parseObject(obj.getString("CHEM_ITEM")); tarObj.putAll(chemItem); } tarDate.add(tarObj); } logger.info("-------jon数组--"+tarDate.toJSONString()); //插入日志信息 //createSmpleLoglist(tarDate.toJSONString(), "调用LIMS接收委托接口,实现委托发送:SendLIMES",sendName); String tar=tarDate.toString(); String result = "";//调用url try{ result = httpURLPOSTCase(urlStr,tar);//调用url }catch(Exception e){ e.printStackTrace(); throw new Exception("发送LIMS系统失败,请联系LIMS相关人员!"); } String resultlist =result.substring(8, 11); if (!resultlist.equals("200") || resultlist == null || resultlist.equals("")) { //createSmpleLoglist(result, "调用LIMS接收委托接口失败,实现委托发送:SendLIMES",sendName);//调用url发送失败接口 cro.setV_errCode(-1); cro.setV_errMsg("发送失败!"+result); return cro; } if(resultlist.equals("200")){ cn = this.getDao("testDao").getConnection(); String sql="UPDATE QCM_JHY_SAMPLE_CONSIGN_D SET STATUS = '2',CONSIGN_NO=?,CONSIGN_NO_SEQ=? " + " ,SEND_ID=?,SEND_NAME=?,SEND_TIME=sysdate " + " WHERE SPECIMEN_NO = ?"; pSta1 = cn.prepareStatement(sql.toString()); for (Object array : ranQltyArraySmp) { JSONObject send=(JSONObject)array; pSta1.setString(1, send.getString("CONSIGN_NO")); pSta1.setString(2, send.getString("CONSIGN_NO_SEQ")); pSta1.setString(3, sendId); pSta1.setString(4, sendName); pSta1.setString(5, send.getString("SPECIMEN_NO")); pSta1.addBatch(); } pSta1.executeBatch(); pSta1.clearBatch(); pSta1.close(); cn.commit(); //cn.close(); cro.setV_errCode(1); cro.setV_errMsg("发送成功"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if (null !=pSta1) { pSta1.close(); } if (null !=cn) { cn.close(); } if (null !=rs) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return cro; } /** * 项目转换 * @param tarItem * @param oldItem */ private void createItem(JSONObject tarItem, JSONObject oldItem) { // TODO Auto-generated method stub tarItem.put("specimenNo", oldItem.getString("SPECIMEN_NO")); tarItem.put("seq", oldItem.getInteger("SEQ")); tarItem.put("phyCodeL", oldItem.getString("PHY_CODE_L")); tarItem.put("phyNameL", oldItem.getString("PHY_NAME_L")); tarItem.put("phyCodeM", oldItem.getString("PHY_CODE_M")); tarItem.put("phyNameM", oldItem.getString("PHY_NAME_M")); tarItem.put("phyCodeS", oldItem.getString("PHY_CODE_S")); tarItem.put("phyNameS", oldItem.getString("PHY_NAME_S")); tarItem.put("itemCodeD", oldItem.getString("ITEM_CODE_D")); tarItem.put("itemNameD", oldItem.getString("ITEM_NAME_D")); tarItem.put("itemCodeT", oldItem.getString("ITEM_CODE_T")); tarItem.put("itemNameT", oldItem.getString("ITEM_NAME_T")); tarItem.put("itemCodeS", oldItem.getString("ITEM_CODE_S")); tarItem.put("itemNameS", oldItem.getString("ITEM_NAME_S")); tarItem.put("itemCodeL", oldItem.getString("ITEM_CODE_L")); tarItem.put("itemNameL", oldItem.getString("ITEM_DESC_L")); tarItem.put("groupSeq", oldItem.getString("GROUP_SEQ")); tarItem.put("memo", oldItem.getString("MEMO")); tarItem.put("judgeBasis", oldItem.getString("JUDGE_BASIS")); tarItem.put("unti", oldItem.getString("PHY_UNIT")); tarItem.put("testQty", oldItem.getString("TEST_QTY")); tarItem.put("smpQty", oldItem.getString("SMP_QTY")); // tarItem.put("createTime", oldItem.getString("ITEM_NAME_S")); // tarItem.put("createName", oldItem.getString("ITEM_NAME_S")); } /** * 创建目标对象 * @param tarObj * @param obj */ private void createTarObj(JSONObject tarObj, JSONObject obj) { // TODO Auto-generated method stub tarObj.put("smpTage", obj.getString("SMP_CATG")); tarObj.put("smpNo", obj.getString("SMP_NO")); tarObj.put("specimenNo", obj.getString("SPECIMEN_NO")); tarObj.put("heatNo", obj.getString("HEAT_NO")); tarObj.put("batchNo", obj.getString("BATCH_NO")); tarObj.put("inspectionLot", obj.getString("INSPECTION_LOT")); tarObj.put("freqCode", obj.getString("FREQ_CODE")); tarObj.put("freqName", obj.getString("FREQ_NAME")); tarObj.put("materialNo", obj.getString("MATERIAL_NO")); if ("0".equals(obj.getString("SMP_TYPE_CODE"))) { tarObj.put("smpTypeCode", "routineSampleTask"); }else{ tarObj.put("smpTypeCode", "reviewSampleTask"); } tarObj.put("smpTypeName", obj.getString("SMP_TYPE_NAME")); tarObj.put("smpTage", obj.getString("SMP_CATG")); tarObj.put("smpLocation", obj.getString("SMP_LOCATION")); tarObj.put("boardNo", obj.getString("BOARD_NO")); tarObj.put("plineCode", obj.getString("PLINE_CODE")); tarObj.put("plineName", obj.getString("PLINE_NAME")); tarObj.put("status", obj.getString("STATUS")); tarObj.put("thick", obj.getString("THICK")); tarObj.put("width", obj.getString("WIDTH")); tarObj.put("length", obj.getString("LENGTH")); tarObj.put("sendName", obj.getString("SEND_NAME")); tarObj.put("sendTime", obj.getString("SEND_TIME")); tarObj.put("sendId", obj.getString("SEND_ID")); tarObj.put("consignNo", obj.getString("CONSIGN_NO")); tarObj.put("consignNoSeq", obj.getIntValue("CONSIGN_NO_SEQ")); tarObj.put("productCnt", obj.getString("PRODUCT_CNT")); tarObj.put("checkMemo", obj.getString("MEMO")); tarObj.put("steelCode", obj.getString("STEEL_CODE")); tarObj.put("steelName", obj.getString("STEEL_NAME")); if ("A".equals(obj.getString("SMP_CATG"))) { tarObj.put("testQty", obj.getString("TEST_QTY")); tarObj.put("smpQty", obj.getString("SMP_QTY")); } } /** * 将resultset 转为json数组 * @param rs * @return * @throws SQLException * @throws JSONException */ public static JSONArray resultSetToJsonArry(ResultSet rs) throws SQLException, JSONException { JSONArray array = new JSONArray(); ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); while (rs.next()) { JSONObject jsonObj = new JSONObject(); for (int i = 1; i <= columnCount; i++) { String columnName = metaData.getColumnLabel(i); String value = rs.getString(columnName); jsonObj.put(columnName, value); } array.add(jsonObj); } return array; } /** * 将resultset转为json * @param rs * @return * @throws SQLException * @throws JSONException */ public static JSONObject resultSetToJson(ResultSet rs) throws SQLException, JSONException { JSONObject jsonobject = new JSONObject(); ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); // 遍历每一列 for (int i = 1; i <= columnCount; i++) { String columnName =metaData.getColumnLabel(i); String value = rs.getString(columnName); jsonobject.put(columnName, value); } return jsonobject; } /** * 发送Http Post请求 * @throws Exception */ public String httpURLPOSTCase(String methodUrl,String data) throws Exception { HttpURLConnection connection = null; PrintWriter pw = null; BufferedReader reader = null; String line = ""; StringBuilder result = new StringBuilder(); try { URL url = new URL(methodUrl); connection = (HttpURLConnection)url.openConnection();// 根据URL生成HttpURLConnection connection.setDoOutput(true);// 设置是否向connection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true,默认情况下是false connection.setDoInput(true); // 设置是否从connection读入,默认情况下是true; connection.setRequestMethod("POST");// 设置请求方式为post,默认GET请求 connection.setUseCaches(false);// post请求不能使用缓存设为false connection.setConnectTimeout(10000);// 连接主机的超时时间 connection.setReadTimeout(10000);// 从主机读取数据的超时时间 connection.setInstanceFollowRedirects(true);// 设置该HttpURLConnection实例是否自动执行重定向 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive");// 连接复用 connection.setRequestProperty("charset", "utf-8");//utf-8 GBK connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//; charset=utf-8 //connection.setRequestProperty("Content-Type", "application/json");//;charset=utf-8 connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); connection.connect();// 建立TCP连接,getOutputStream会隐含的进行connect,所以此处可以不要 // pw = new PrintWriter(connection.getOutputStream());// 创建输入输出流,用于往连接里面输出携带的参数 //pw.write("data="+data);//json数据 // pw.flush(); //pw.close(); OutputStream outputstream = connection.getOutputStream(); String jsonStr = "data="+data; outputstream.write(jsonStr.getBytes("UTF-8")); outputstream.flush(); outputstream.close(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));// 发送http请求 // 循环读取流 while (null != (line = reader.readLine())) { result.append(line); } reader.close(); } } catch (IOException e) { e.printStackTrace(); throw new Exception("发送LIMS系统失败,具体原因为["+e.getMessage()+"]"); } finally { connection.disconnect(); } return result.toString(); } /** * 生成不带 "-" 的UUID * * @return */ public static String getUUID() { UUID uuid = UUID.randomUUID(); String str = uuid.toString(); String uuidStr = str.replace("-", ""); return uuidStr; } private void createSmpleLoglist(String josnString ,String memo,String sendName){ StringBuffer sqlucomm1 = new StringBuffer(); sqlucomm1.append("Delete From QCM_JHY_SAMPLE_LOG "); sqlucomm1.append("where CREATE_TIME <= to_date(to_char(sysdate - 90, 'yyyy-MM-dd hh24:mi:ss'),'yyyy-MM-dd hh24:mi:ss') "); try { this.getDao("testDao").ExcuteNonQuery(sqlucomm1.toString()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); logger.info("---------------------------插入日志表失败-----------------------"+sqlucomm1); } int len = 0; if(josnString.length()<= 3500){ len = josnString.length(); }else{ len = 3000; } String josnli = josnString.substring(0, len); StringBuffer sqlucomm = new StringBuffer(); sqlucomm.append("INSERT INTO QCM_JHY_SAMPLE_LOG (GUID,MEMO,"); sqlucomm.append("CREATE_NAME,CREATE_TIME,JSON_TEXT) VALUES ('"); sqlucomm.append(getUUID()+"','"+memo+"',"); sqlucomm.append("'"+sendName+"',sysdate,'"+josnli+"')"); try { this.getDao("testDao").ExcuteNonQuery(sqlucomm.toString()); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); logger.info("---------------------------插入日志表失败-----------------------"+sqlucomm); } } }