一、登錄、查詢代碼示例
1 /** 2 * 處理數據,在各自檔案service實現邏輯 3 * @param 4 * @return 5 */ 6 public abstract Map<String, Object> syncData (String result); 7 8 9 10 /** 11 * 返回請求參數 12 * @return 13 */ 14 public abstract JsonArray getRequestData(); 15 16 /** 17 * 調用查詢接口 18 * @return 19 */ 20 public String getQueryAdress(){ 21 return baseProperties.getBillQueryUrl(); 22 } 23 24 25 public boolean login() throws Exception { 26 27 boolean bResult = false; 28 JsonArray jParas = new JsonArray(); 29 jParas.add(baseProperties.getDbId()); 30 jParas.add(baseProperties.getUserName()); 31 jParas.add(baseProperties.getPwd()); 32 jParas.add(baseProperties.getLang()); 33 HttpURLConnection connection = initUrlConn(baseProperties.getLoginUrl(), jParas); 34 String key = null; 35 for (int i = 1; (key = connection.getHeaderFieldKey(i)) != null; i++) { 36 if (key.equalsIgnoreCase("Set-Cookie")) { 37 String tempCookieVal = connection.getHeaderField(i); 38 if (tempCookieVal.startsWith("kdservice-sessionid")) { 39 CookieVal = tempCookieVal; 40 break; 41 } 42 } 43 } 44 45 BufferedReader reader = new BufferedReader(new InputStreamReader( 46 connection.getInputStream())); 47 String line; 48 while ((line = reader.readLine()) != null) { 49 String sResult = new String(line.getBytes(), "utf-8"); 50 bResult = line.contains("\"LoginResultType\":1"); 51 } 52 reader.close(); 53 connection.disconnect(); 54 return bResult; 55 } 56 57 private HttpURLConnection initUrlConn(String url, JsonArray paras) 58 throws Exception { 59 URL postUrl = new URL(baseProperties.getBaseUrl().concat(url)); 60 HttpURLConnection connection = (HttpURLConnection) postUrl 61 .openConnection(); 62 if (CookieVal != null) { 63 connection.setRequestProperty("Cookie", CookieVal); 64 } 65 if (!connection.getDoOutput()) { 66 connection.setDoOutput(true); 67 } 68 connection.setRequestMethod("POST"); 69 connection.setUseCaches(false); 70 connection.setInstanceFollowRedirects(true); 71 connection.setRequestProperty("Content-Type", "application/json"); 72 DataOutputStream out = new DataOutputStream( 73 connection.getOutputStream()); 74 75 UUID uuid = UUID.randomUUID(); 76 int hashCode = uuid.toString().hashCode(); 77 78 JsonObject jObj = new JsonObject(); 79 jObj.addProperty("format", 1); 80 jObj.addProperty("useragent", "ApiClient"); 81 jObj.addProperty("rid", hashCode); 82 jObj.addProperty("parameters", chinaToUnicode(paras.toString())); 83 jObj.addProperty("timestamp", new Date().toString()); 84 jObj.addProperty("v", "1.0"); 85 86 out.writeBytes(jObj.toString()); 87 out.flush(); 88 out.close(); 89 90 return connection; 91 } 92 93 94 /** 95 * 拉取數據並處理 96 * @return 97 */ 98 public Map<String, Object> datax() throws Exception { 99 login(); 100 HttpURLConnection connectionInvoke = initUrlConn(getQueryAdress(), getRequestData()); 101 BufferedReader reader = new BufferedReader(new InputStreamReader( 102 connectionInvoke.getInputStream())); 103 String line; 104 String sResult = null; 105 while ((line = reader.readLine()) != null) { 106 sResult = new String(line.getBytes(), "utf-8"); 107 System.out.println(sResult); 108 } 109 if(sResult.contains("\"IsSuccess\":false")){ 110 ErrorEntity errorEntity = JSONUtil.toBean(JSONUtil.parseFromMap(objectMapper.readValue(sResult,Map.class)),ErrorEntity.class); 111 throw new InfException(StrUtil.format("數據同步失敗,當前同步類型為:{},返回信息為:{}",getCurrentType().getTitle(), errorEntity.getResult().getResponseStatus().getErrors().get(0).getMessage())); 112 } 113 reader.close(); 114 connectionInvoke.disconnect(); 115 Map<String, Object> syncMap = syncData(sResult); 116 return syncMap; 117 } 118 119 public static String chinaToUnicode(String str) { 120 String result = ""; 121 for (int i = 0; i < str.length(); i++) { 122 int chr1 = (char) str.charAt(i); 123 if (chr1 >= 19968 && chr1 <= 171941) { 124 result += "\\u" + Integer.toHexString(chr1); 125 } else { 126 result += str.charAt(i); 127 } 128 } 129 return result; 130 }
二、同步采購訂單代碼示例

1 @Override 2 public DataMessageInfo.BusType getCurrentType() { 3 return DataMessageInfo.BusType.PUNOTICE; 4 } 5 6 @Override 7 public JsonArray getRequestData() { 8 JsonArray jParas = new JsonArray(); 9 JsonObject object = new JsonObject(); 10 object.addProperty("FormId","PUR_PurchaseOrder"); 11 //訂單id,訂單編號,訂單子表id,開始日期,關閉狀態,供應商id,物料id,物料名稱,規格型號,數量,創建時間,修改時間 12 //關閉狀態:A 未關閉 B 已關閉 13 object.addProperty("FieldKeys","FID,FBillNo,FPOOrderEntry_FEntryId,FApproveDate,FCLOSESTATUS,FSupplierId,FMaterialId,FMaterialName,FModel,FQty,FCreateDate,FMODIFYDATE"); 14 15 String ts = null; 16 // ts初始化或取值 17 List<Map<String, Object>> tsList = jdbcTemplate.queryForList("select value from datax_status where task_code = 'puNotice' and keyword = 'ts'"); 18 if (CollectionUtil.isEmpty(tsList)) { 19 ts = DEFAULT_TS; 20 jdbcTemplate.update("insert into datax_status (task_code,keyword,value) values ('puNotice','ts',?)", ts); 21 } else { 22 ts = tsList.get(0).get("value").toString(); 23 } 24 //已審核條件: FDOCUMENTSTATUS=C 25 object.addProperty("FilterString","FDOCUMENTSTATUS='C' and FMODIFYDATE>'"+ts+"'"); 26 object.addProperty("OrderString","FMODIFYDATE ASC"); 27 jParas.add(object); 28 return jParas; 29 } 30 31 @Override 32 @Transactional(rollbackFor=Exception.class) 33 public Map<String, Object> syncData(String result) { 34 Map<String, Object> resultMap = new HashMap<>(); 35 if ("[]".equals(result)) { 36 resultMap.put("msg","暫無訂單需要同步"); 37 return resultMap; 38 } 39 List<List<Object>> datas = new ArrayList<>(); 40 try { 41 datas = objectMapper.readValue(result,List.class); 42 } catch (IOException e) { 43 throw new InfException("轉換異常"); 44 } 45 }
三、采購訂單下推接口參數示例

1 @Override 2 public JsonArray getPushRequestData(String id,String subId) { 3 String sql = "SELECT b.noto_code FROM tr_registry_sub a LEFT JOIN pu_notice b ON a.pk_notice_id =b.id WHERE a.id = '"+subId+"'"; 4 List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql); 5 StringBuffer data = new StringBuffer(); 6 data.append("{'Ids':'','Numbers':'"); 7 for (Map<String, Object> map: mapList){ 8 data.append(map.get("noto_code").toString()+","); 9 } 10 data.deleteCharAt(data.length()-1); 11 data.append("','EntryIds':'','RuleId':'PUR_PurchaseOrder-STK_InStock'}"); 12 JsonArray jParas = new JsonArray(); 13 String formid = "PUR_PurchaseOrder"; 14 jParas.add(formid); 15 jParas.add(data.toString()); 16 return jParas; 17 }
四、入庫單保存接口參數示例

1 @Override 2 public JsonArray getUpdateRequestData(String id,String subId) { 3 StringBuffer data = new StringBuffer(); 4 String sql = "SELECT a.define01,a.third_bill_sub_id,a.re_suttle_real,a.re_ware_id,a.de_suttle_real,b.plate_number,b.reg_code FROM tr_registry_sub a left join tr_registry b on a.reg_id = b.id WHERE a.id = ? AND a.ideleted = 0"; 5 List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql,subId); 6 data.append("{'NeedUpDateFields':['FREALQTY','FEntryID','FInStockEntry','FStockId','F_ora_Text','F_ora_Text1','F_ora_Qty'],'NeedReturnFields':['FREALQTY','FEntryID','FInStockEntry','FStockId','F_ora_Text','F_ora_Text1','F_ora_Qty'],'IsDeleteEntry':'False','Model':{'FID':'"+mapList.get(0).get("define01")+"','F_ora_Text':'"+mapList.get(0).get("plate_number")+"','F_ora_Text1':'"+mapList.get(0).get("reg_code")+"','FInStockEntry':["); 7 for (Map<String, Object> map: mapList){ 8 data.append("{'FEntryID':'"+map.get("third_bill_sub_id")+"','FREALQTY':'"+map.get("re_suttle_real")+"','F_ora_Qty':'"+map.get("de_suttle_real")+"','FStockId':{'FStockId':'"+map.get("re_ware_id")+"'}},"); 9 } 10 data.deleteCharAt(data.length()-1); 11 data.append("]}}"); 12 JsonArray jParas = new JsonArray(); 13 String formid = "STK_InStock"; 14 jParas.add(formid); 15 jParas.add(data.toString()); 16 return jParas; 17 }