3a48231af524465a6d86417d95340f035a824718.svn-base 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. package QCM.JHY01.JHY0102;
  2. import CoreFS.SA01.CoreIComponent;
  3. import CoreFS.SA06.CoreReturnObject;
  4. import QCM.JHY01.JHY0101.QuerryQltySample;
  5. import com.alibaba.fastjson.JSON;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.alibaba.fastjson.JSONException;
  8. import com.alibaba.fastjson.JSONObject;
  9. import org.apache.log4j.Logger;
  10. import java.io.*;
  11. import java.net.HttpURLConnection;
  12. import java.net.URL;
  13. import java.sql.*;
  14. import java.text.SimpleDateFormat;
  15. import java.util.Date;
  16. import java.util.TreeSet;
  17. import java.util.UUID;
  18. public class SendEntrust extends CoreIComponent{
  19. private Logger logger = Logger.getLogger(this.getClass().getName());
  20. CoreReturnObject cro = new CoreReturnObject();
  21. /**
  22. * 调用LIMS接收委托接口,实现委托下发
  23. * @return cro
  24. * @throws IOException
  25. */
  26. public CoreReturnObject SendLIMES(String Entrust,String sendId,String sendName){
  27. //"http://10.70.0.237:9060/jbDataToJhWebService/SetOrdCommResult"
  28. String urlStr = "http://172.16.0.169:82/Limsinterface/http/Inspection";
  29. //String urlStr = "http://172.16.0.188:8081/open/lims/4mes/Inspection";
  30. ResultSet rs =null;
  31. Connection cn = null;
  32. PreparedStatement pSta1 = null;
  33. try {
  34. StringBuffer sqlucomm = new StringBuffer();
  35. // JSONObject cretjson = JSON.parseObject(Entrust);
  36. // JSONArray ja = cretjson.getJSONArray("DETAIL");
  37. String[] ja=Entrust.split(",");
  38. String msInfo="";
  39. if (ja!=null && ja.length>0) {
  40. for (int i = 0; i < ja.length; i++) {
  41. String sp = ja[i];
  42. if ("".equals(msInfo)) {
  43. msInfo="'"+sp+"'";
  44. }else{
  45. msInfo=msInfo+"'"+sp+"'";
  46. }
  47. if (i !=ja.length-1) {
  48. msInfo=msInfo+",";
  49. }
  50. }
  51. }
  52. //查询检验
  53. sqlucomm.append("SELECT d.SMP_NO,d.SPECIMEN_NO,d.HEAT_NO,d.BATCH_NO,d.INSPECTION_LOT,d.FREQ_CODE,d.FREQ_NAME,"
  54. + " d.MATERIAL_NO,d.SMP_TYPE_CODE,d.SMP_TYPE_NAME,d.CONSIGN_NO,d.CONSIGN_NO_SEQ,d.TEST_QTY,d.SMP_QTY,"
  55. + " d.SMP_LOCATION,d.BOARD_NO,d.QUOTE_CONSIGN_NO,d.OLD_CONSIGN_NO,d.PLINE_CODE,d.PLINE_NAME,d.CHEM_ITEM,"
  56. + "d.SOURCE,d.PRODUCT_CNT,m.SMP_CATG, o.steel_code,o.steel_name "
  57. + " 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'");
  58. rs = this.getDao("testDao").ExceuteQueryForResultSet(sqlucomm.toString());
  59. if (rs==null) {
  60. cro.setV_errCode(-1);
  61. cro.setV_errMsg("待发送的检验委托数据已经不存在,请核实数据!");
  62. return cro;
  63. }
  64. JSONArray ranQltyArraySmp=QuerryQltySample.resultSetToJsonArry(rs);
  65. //this.getDao("testDao").closeRs(rs);
  66. rs.close();
  67. if (ranQltyArraySmp==null || ranQltyArraySmp.size()<1) {
  68. cro.setV_errCode(-1);
  69. cro.setV_errMsg("待发送的检验委托数据已经不存在,请核实数据!");
  70. return cro;
  71. }
  72. //获取数据进行验证
  73. TreeSet<String> sizes=new TreeSet<String>();
  74. for (int i = 0; i < ranQltyArraySmp.size(); i++) {
  75. JSONObject obj=ranQltyArraySmp.getJSONObject(i);
  76. sizes.add(obj.getString("SMP_CATG"));
  77. }
  78. if (sizes.size()!=1) {
  79. cro.setV_errCode(-1);
  80. cro.setV_errMsg("待发送的检验委托数据只能同时发送一种类型的待检数据,请核实数据!");
  81. return cro;
  82. }
  83. JSONArray tarDate=new JSONArray();
  84. Date date=new Date();
  85. SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  86. String dateString=simpleDateFormat.format(date);
  87. String consignNo=new SimpleDateFormat("yyyyMMdd").format(date);
  88. sqlucomm.setLength(0);
  89. sqlucomm.append("SELECT max(to_number(substr(CONSIGN_NO,-3,3))) BIGSEQ from QCM_JHY_SAMPLE_CONSIGN_D where CONSIGN_NO like '"+consignNo+"%'");
  90. rs=this.getDao("testDao").ExceuteQueryForResultSet(sqlucomm.toString());
  91. if (rs.next()) {
  92. int count=rs.getInt("BIGSEQ");
  93. if (count>0) {
  94. count++;
  95. }else{
  96. count=1;
  97. }
  98. consignNo=consignNo+String.format("%03d", count);
  99. }
  100. //this.getDao("testDao").closeRs(rs);
  101. rs.close();
  102. int seq=1;
  103. //生成委托编号
  104. for (Object array : ranQltyArraySmp) {
  105. JSONObject obj = (JSONObject)array;
  106. obj.put("SEND_ID", sendId);
  107. obj.put("SEND_NAME", sendName);
  108. obj.put("CONSIGN_NO", consignNo);
  109. obj.put("CONSIGN_NO_SEQ", seq++);
  110. obj.put("SEND_TIME", dateString);
  111. //目标json
  112. JSONObject tarObj=new JSONObject();
  113. //根据取样编号查询检验项目
  114. sqlucomm.setLength(0);
  115. sqlucomm.append("select * from QCM_JHY_SAMPLE_CONSIGN_D_ITEM where SPECIMEN_NO='"+obj.getString("SPECIMEN_NO")+"' and QUOTE_SPECIMEN_NO is null");
  116. rs = this.getDao("testDao").ExceuteQueryForResultSet(sqlucomm.toString());
  117. if (rs==null) {
  118. continue;
  119. }
  120. JSONArray items=QuerryQltySample.resultSetToJsonArry(rs);
  121. //this.getDao("testDao").closeRs(rs);
  122. rs.close();
  123. sqlucomm.setLength(0);
  124. sqlucomm.append("select * from QCM_JHY_SAMPLE_R_ORD where SMP_NO='"+obj.getString("SMP_NO")+"' ");
  125. rs = this.getDao("testDao").ExceuteQueryForResultSet(sqlucomm.toString());
  126. if (rs==null) {
  127. cro.setV_errCode(-1);
  128. cro.setV_errMsg("订单信息不存在,请核实数据!");
  129. return cro;
  130. }
  131. JSONArray ordArr=QuerryQltySample.resultSetToJsonArry(rs);
  132. //this.getDao("testDao").closeRs(rs);
  133. rs.close();
  134. obj.put("THICK", ordArr.getJSONObject(0).getString("THICK"));
  135. obj.put("WIDTH", ordArr.getJSONObject(0).getString("WIDTH"));
  136. obj.put("LENGTH", ordArr.getJSONObject(0).getString("LENGTH"));
  137. //主表数据
  138. createTarObj(tarObj,obj);
  139. tarObj.put("CREATE_ID", sendId);
  140. tarObj.put("CREATE_NAME", sendName);
  141. //如果是材质检验委托
  142. if ("B".equals(obj.getString("SMP_CATG"))) {
  143. JSONArray arrayItem=new JSONArray();
  144. for(int i = 0 ; i < items.size() ; i++){
  145. JSONObject oldItem = items.getJSONObject(i);
  146. JSONObject tarItem=new JSONObject();
  147. //子表数据
  148. createItem(tarItem,oldItem);
  149. arrayItem.add(tarItem);
  150. }
  151. tarObj.put("items", arrayItem);
  152. }else{
  153. JSONObject chemItem=JSON.parseObject(obj.getString("CHEM_ITEM"));
  154. tarObj.putAll(chemItem);
  155. }
  156. tarDate.add(tarObj);
  157. }
  158. logger.info("-------jon数组--"+tarDate.toJSONString());
  159. //插入日志信息
  160. //createSmpleLoglist(tarDate.toJSONString(), "调用LIMS接收委托接口,实现委托发送:SendLIMES",sendName);
  161. String tar=tarDate.toString();
  162. String result = "";//调用url
  163. try{
  164. result = httpURLPOSTCase(urlStr,tar);//调用url
  165. }catch(Exception e){
  166. e.printStackTrace();
  167. throw new Exception("发送LIMS系统失败,请联系LIMS相关人员!");
  168. }
  169. String resultlist =result.substring(8, 11);
  170. if (!resultlist.equals("200") || resultlist == null || resultlist.equals("")) {
  171. //createSmpleLoglist(result, "调用LIMS接收委托接口失败,实现委托发送:SendLIMES",sendName);//调用url发送失败接口
  172. cro.setV_errCode(-1);
  173. cro.setV_errMsg("发送失败!"+result);
  174. return cro;
  175. }
  176. if(resultlist.equals("200")){
  177. cn = this.getDao("testDao").getConnection();
  178. String sql="UPDATE QCM_JHY_SAMPLE_CONSIGN_D SET STATUS = '2',CONSIGN_NO=?,CONSIGN_NO_SEQ=? "
  179. + " ,SEND_ID=?,SEND_NAME=?,SEND_TIME=sysdate "
  180. + " WHERE SPECIMEN_NO = ?";
  181. pSta1 = cn.prepareStatement(sql.toString());
  182. for (Object array : ranQltyArraySmp) {
  183. JSONObject send=(JSONObject)array;
  184. pSta1.setString(1, send.getString("CONSIGN_NO"));
  185. pSta1.setString(2, send.getString("CONSIGN_NO_SEQ"));
  186. pSta1.setString(3, sendId);
  187. pSta1.setString(4, sendName);
  188. pSta1.setString(5, send.getString("SPECIMEN_NO"));
  189. pSta1.addBatch();
  190. }
  191. pSta1.executeBatch();
  192. pSta1.clearBatch();
  193. pSta1.close();
  194. cn.commit();
  195. //cn.close();
  196. cro.setV_errCode(1);
  197. cro.setV_errMsg("发送成功");
  198. }
  199. } catch (Exception e) {
  200. // TODO Auto-generated catch block
  201. e.printStackTrace();
  202. }finally {
  203. try {
  204. if (null !=pSta1) {
  205. pSta1.close();
  206. }
  207. if (null !=cn) {
  208. cn.close();
  209. }
  210. if (null !=rs) {
  211. rs.close();
  212. }
  213. } catch (SQLException e) {
  214. // TODO Auto-generated catch block
  215. e.printStackTrace();
  216. }
  217. }
  218. return cro;
  219. }
  220. /**
  221. * 项目转换
  222. * @param tarItem
  223. * @param oldItem
  224. */
  225. private void createItem(JSONObject tarItem, JSONObject oldItem) {
  226. // TODO Auto-generated method stub
  227. tarItem.put("specimenNo", oldItem.getString("SPECIMEN_NO"));
  228. tarItem.put("seq", oldItem.getInteger("SEQ"));
  229. tarItem.put("phyCodeL", oldItem.getString("PHY_CODE_L"));
  230. tarItem.put("phyNameL", oldItem.getString("PHY_NAME_L"));
  231. tarItem.put("phyCodeM", oldItem.getString("PHY_CODE_M"));
  232. tarItem.put("phyNameM", oldItem.getString("PHY_NAME_M"));
  233. tarItem.put("phyCodeS", oldItem.getString("PHY_CODE_S"));
  234. tarItem.put("phyNameS", oldItem.getString("PHY_NAME_S"));
  235. tarItem.put("itemCodeD", oldItem.getString("ITEM_CODE_D"));
  236. tarItem.put("itemNameD", oldItem.getString("ITEM_NAME_D"));
  237. tarItem.put("itemCodeT", oldItem.getString("ITEM_CODE_T"));
  238. tarItem.put("itemNameT", oldItem.getString("ITEM_NAME_T"));
  239. tarItem.put("itemCodeS", oldItem.getString("ITEM_CODE_S"));
  240. tarItem.put("itemNameS", oldItem.getString("ITEM_NAME_S"));
  241. tarItem.put("itemCodeL", oldItem.getString("ITEM_CODE_L"));
  242. tarItem.put("itemNameL", oldItem.getString("ITEM_DESC_L"));
  243. tarItem.put("groupSeq", oldItem.getString("GROUP_SEQ"));
  244. tarItem.put("memo", oldItem.getString("MEMO"));
  245. tarItem.put("judgeBasis", oldItem.getString("JUDGE_BASIS"));
  246. tarItem.put("unti", oldItem.getString("PHY_UNIT"));
  247. tarItem.put("testQty", oldItem.getString("TEST_QTY"));
  248. tarItem.put("smpQty", oldItem.getString("SMP_QTY"));
  249. // tarItem.put("createTime", oldItem.getString("ITEM_NAME_S"));
  250. // tarItem.put("createName", oldItem.getString("ITEM_NAME_S"));
  251. }
  252. /**
  253. * 创建目标对象
  254. * @param tarObj
  255. * @param obj
  256. */
  257. private void createTarObj(JSONObject tarObj, JSONObject obj) {
  258. // TODO Auto-generated method stub
  259. tarObj.put("smpTage", obj.getString("SMP_CATG"));
  260. tarObj.put("smpNo", obj.getString("SMP_NO"));
  261. tarObj.put("specimenNo", obj.getString("SPECIMEN_NO"));
  262. tarObj.put("heatNo", obj.getString("HEAT_NO"));
  263. tarObj.put("batchNo", obj.getString("BATCH_NO"));
  264. tarObj.put("inspectionLot", obj.getString("INSPECTION_LOT"));
  265. tarObj.put("freqCode", obj.getString("FREQ_CODE"));
  266. tarObj.put("freqName", obj.getString("FREQ_NAME"));
  267. tarObj.put("materialNo", obj.getString("MATERIAL_NO"));
  268. if ("0".equals(obj.getString("SMP_TYPE_CODE"))) {
  269. tarObj.put("smpTypeCode", "routineSampleTask");
  270. }else{
  271. tarObj.put("smpTypeCode", "reviewSampleTask");
  272. }
  273. tarObj.put("smpTypeName", obj.getString("SMP_TYPE_NAME"));
  274. tarObj.put("smpTage", obj.getString("SMP_CATG"));
  275. tarObj.put("smpLocation", obj.getString("SMP_LOCATION"));
  276. tarObj.put("boardNo", obj.getString("BOARD_NO"));
  277. tarObj.put("plineCode", obj.getString("PLINE_CODE"));
  278. tarObj.put("plineName", obj.getString("PLINE_NAME"));
  279. tarObj.put("status", obj.getString("STATUS"));
  280. tarObj.put("thick", obj.getString("THICK"));
  281. tarObj.put("width", obj.getString("WIDTH"));
  282. tarObj.put("length", obj.getString("LENGTH"));
  283. tarObj.put("sendName", obj.getString("SEND_NAME"));
  284. tarObj.put("sendTime", obj.getString("SEND_TIME"));
  285. tarObj.put("sendId", obj.getString("SEND_ID"));
  286. tarObj.put("consignNo", obj.getString("CONSIGN_NO"));
  287. tarObj.put("consignNoSeq", obj.getIntValue("CONSIGN_NO_SEQ"));
  288. tarObj.put("productCnt", obj.getString("PRODUCT_CNT"));
  289. tarObj.put("checkMemo", obj.getString("MEMO"));
  290. tarObj.put("steelCode", obj.getString("STEEL_CODE"));
  291. tarObj.put("steelName", obj.getString("STEEL_NAME"));
  292. if ("A".equals(obj.getString("SMP_CATG"))) {
  293. tarObj.put("testQty", obj.getString("TEST_QTY"));
  294. tarObj.put("smpQty", obj.getString("SMP_QTY"));
  295. }
  296. }
  297. /**
  298. * 将resultset 转为json数组
  299. * @param rs
  300. * @return
  301. * @throws SQLException
  302. * @throws JSONException
  303. */
  304. public static JSONArray resultSetToJsonArry(ResultSet rs) throws SQLException, JSONException {
  305. JSONArray array = new JSONArray();
  306. ResultSetMetaData metaData = rs.getMetaData();
  307. int columnCount = metaData.getColumnCount();
  308. while (rs.next()) {
  309. JSONObject jsonObj = new JSONObject();
  310. for (int i = 1; i <= columnCount; i++) {
  311. String columnName = metaData.getColumnLabel(i);
  312. String value = rs.getString(columnName);
  313. jsonObj.put(columnName, value);
  314. }
  315. array.add(jsonObj);
  316. }
  317. return array;
  318. }
  319. /**
  320. * 将resultset转为json
  321. * @param rs
  322. * @return
  323. * @throws SQLException
  324. * @throws JSONException
  325. */
  326. public static JSONObject resultSetToJson(ResultSet rs) throws SQLException, JSONException {
  327. JSONObject jsonobject = new JSONObject();
  328. ResultSetMetaData metaData = rs.getMetaData();
  329. int columnCount = metaData.getColumnCount();
  330. // 遍历每一列
  331. for (int i = 1; i <= columnCount; i++) {
  332. String columnName =metaData.getColumnLabel(i);
  333. String value = rs.getString(columnName);
  334. jsonobject.put(columnName, value);
  335. }
  336. return jsonobject;
  337. }
  338. /**
  339. * 发送Http Post请求
  340. * @throws Exception */
  341. public String httpURLPOSTCase(String methodUrl,String data) throws Exception {
  342. HttpURLConnection connection = null;
  343. PrintWriter pw = null;
  344. BufferedReader reader = null;
  345. String line = "";
  346. StringBuilder result = new StringBuilder();
  347. try {
  348. URL url = new URL(methodUrl);
  349. connection = (HttpURLConnection)url.openConnection();// 根据URL生成HttpURLConnection
  350. connection.setDoOutput(true);// 设置是否向connection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true,默认情况下是false
  351. connection.setDoInput(true); // 设置是否从connection读入,默认情况下是true;
  352. connection.setRequestMethod("POST");// 设置请求方式为post,默认GET请求
  353. connection.setUseCaches(false);// post请求不能使用缓存设为false
  354. connection.setConnectTimeout(10000);// 连接主机的超时时间
  355. connection.setReadTimeout(10000);// 从主机读取数据的超时时间
  356. connection.setInstanceFollowRedirects(true);// 设置该HttpURLConnection实例是否自动执行重定向
  357. connection.setRequestProperty("accept", "*/*");
  358. connection.setRequestProperty("connection", "Keep-Alive");// 连接复用
  359. connection.setRequestProperty("charset", "utf-8");//utf-8 GBK
  360. connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//; charset=utf-8
  361. //connection.setRequestProperty("Content-Type", "application/json");//;charset=utf-8
  362. connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
  363. connection.connect();// 建立TCP连接,getOutputStream会隐含的进行connect,所以此处可以不要
  364. // pw = new PrintWriter(connection.getOutputStream());// 创建输入输出流,用于往连接里面输出携带的参数
  365. //pw.write("data="+data);//json数据
  366. // pw.flush();
  367. //pw.close();
  368. OutputStream outputstream = connection.getOutputStream();
  369. String jsonStr = "data="+data;
  370. outputstream.write(jsonStr.getBytes("UTF-8"));
  371. outputstream.flush();
  372. outputstream.close();
  373. if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
  374. reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));// 发送http请求
  375. // 循环读取流
  376. while (null != (line = reader.readLine())) {
  377. result.append(line);
  378. }
  379. reader.close();
  380. }
  381. } catch (IOException e) {
  382. e.printStackTrace();
  383. throw new Exception("发送LIMS系统失败,具体原因为["+e.getMessage()+"]");
  384. } finally {
  385. connection.disconnect();
  386. }
  387. return result.toString();
  388. }
  389. /**
  390. * 生成不带 "-" 的UUID
  391. *
  392. * @return
  393. */
  394. public static String getUUID() {
  395. UUID uuid = UUID.randomUUID();
  396. String str = uuid.toString();
  397. String uuidStr = str.replace("-", "");
  398. return uuidStr;
  399. }
  400. private void createSmpleLoglist(String josnString ,String memo,String sendName){
  401. StringBuffer sqlucomm1 = new StringBuffer();
  402. sqlucomm1.append("Delete From QCM_JHY_SAMPLE_LOG ");
  403. sqlucomm1.append("where CREATE_TIME <= to_date(to_char(sysdate - 90, 'yyyy-MM-dd hh24:mi:ss'),'yyyy-MM-dd hh24:mi:ss') ");
  404. try {
  405. this.getDao("testDao").ExcuteNonQuery(sqlucomm1.toString());
  406. } catch (SQLException e) {
  407. // TODO Auto-generated catch block
  408. e.printStackTrace();
  409. logger.info("---------------------------插入日志表失败-----------------------"+sqlucomm1);
  410. }
  411. int len = 0;
  412. if(josnString.length()<= 3500){
  413. len = josnString.length();
  414. }else{
  415. len = 3000;
  416. }
  417. String josnli = josnString.substring(0, len);
  418. StringBuffer sqlucomm = new StringBuffer();
  419. sqlucomm.append("INSERT INTO QCM_JHY_SAMPLE_LOG (GUID,MEMO,");
  420. sqlucomm.append("CREATE_NAME,CREATE_TIME,JSON_TEXT) VALUES ('");
  421. sqlucomm.append(getUUID()+"','"+memo+"',");
  422. sqlucomm.append("'"+sendName+"',sysdate,'"+josnli+"')");
  423. try {
  424. this.getDao("testDao").ExcuteNonQuery(sqlucomm.toString());
  425. } catch (SQLException e) {
  426. // TODO Auto-generated catch block
  427. e.printStackTrace();
  428. logger.info("---------------------------插入日志表失败-----------------------"+sqlucomm);
  429. }
  430. }
  431. }