|
@@ -0,0 +1,136 @@
|
|
|
|
+package nckd.jimin.jyyy.bd.task;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import kd.bos.exception.KDBizException;
|
|
|
|
+import kd.bos.ext.fi.plugin.ArApConvert.util.EmptyUtils;
|
|
|
|
+import kd.bos.logging.LogFactory;
|
|
|
|
+import kd.bos.sdk.util.KHttpClientUtils;
|
|
|
|
+import kd.bos.util.StringUtils;
|
|
|
|
+import nckd.jimin.jyyy.bd.common.CommonHelperUtils;
|
|
|
|
+import kd.bos.logging.Log;
|
|
|
|
+import nckd.jimin.jyyy.bd.task.impl.SynSapServiceImpl;
|
|
|
|
+
|
|
|
|
+import java.util.Base64;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+public class SyncSapUtils {
|
|
|
|
+ private static final Log logger = LogFactory.getLog(SynSapServiceImpl.class);
|
|
|
|
+ /**
|
|
|
|
+ * 向SAP系统发送POST请求并返回结果
|
|
|
|
+ * 该方法主要用于与SAP系统进行数据交互,通过POST请求发送JSON数据,并接收SAP系统的响应
|
|
|
|
+ *
|
|
|
|
+ * @param url 请求的URL类型,决定目标URL
|
|
|
|
+ * @param jsonData 要发送的JSON数据
|
|
|
|
+ * @return SAP系统的响应结果,以字符串形式返回
|
|
|
|
+ * @throws KDBizException 如果请求失败,抛出业务异常
|
|
|
|
+ */
|
|
|
|
+ public static String postDataToSAP(String url, String jsonData)
|
|
|
|
+ {
|
|
|
|
+ try {
|
|
|
|
+ Map<String, String> mapentity = CommonHelperUtils.getCommonParams("SAP");
|
|
|
|
+ if(mapentity == null){
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ String param_cost_url = mapentity.get("cost_url");
|
|
|
|
+ String param_account_url = mapentity.get("account_url");
|
|
|
|
+ String param_porject_url = mapentity.get("project_url");
|
|
|
|
+ String param_username = mapentity.get("username");
|
|
|
|
+ String param_password = mapentity.get("password");
|
|
|
|
+
|
|
|
|
+ if( StringUtils.isEmpty(param_username) || StringUtils.isEmpty(param_password)
|
|
|
|
+ || StringUtils.isEmpty(param_cost_url) || StringUtils.isEmpty(param_account_url)
|
|
|
|
+ || StringUtils.isEmpty(param_porject_url)){
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String encoded = Base64.getEncoder().encodeToString((param_username + ":" + param_password).getBytes());
|
|
|
|
+ Map<String, String> header = new HashMap<>();
|
|
|
|
+ header.put("Content-Type", "application/json; charset=UTF-8");
|
|
|
|
+ header.put("Authorization", "Basic " + encoded);
|
|
|
|
+ String post_url = null;
|
|
|
|
+ switch (url) {
|
|
|
|
+ case "account":
|
|
|
|
+ post_url = param_account_url;break;
|
|
|
|
+ case "costcenter":
|
|
|
|
+ post_url = param_cost_url;break;
|
|
|
|
+ case "project":
|
|
|
|
+ post_url = param_porject_url;break;
|
|
|
|
+ default:
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ String result = KHttpClientUtils.postjson(post_url, header, jsonData);
|
|
|
|
+ return result;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new KDBizException("请求SAP接口失败:" + e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getAccountViewData(String companyCode)
|
|
|
|
+ {
|
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
|
+ json.put("BUKRS", EmptyUtils.isNotEmpty(companyCode) ? companyCode : "");
|
|
|
|
+ json.put("SAKNR", "");
|
|
|
|
+ json.put("TXT50", "");
|
|
|
|
+ JSONObject itemMap = new JSONObject();
|
|
|
|
+ itemMap.put("BUKRS", "");
|
|
|
|
+ itemMap.put("SAKNR", "");
|
|
|
|
+ itemMap.put("TXT50", "");
|
|
|
|
+ JSONObject etMap = new JSONObject();
|
|
|
|
+ etMap.put("item", itemMap);
|
|
|
|
+ json.put("ET_ITEM", etMap);
|
|
|
|
+ return json.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getCostCenterData(String companyCode)
|
|
|
|
+ {
|
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
|
+ json.put("BUKRS", EmptyUtils.isNotEmpty(companyCode) ? companyCode : "");
|
|
|
|
+ json.put("KOSTL", "");
|
|
|
|
+ json.put("KTEXT", "");
|
|
|
|
+ json.put("ERSDA", "");
|
|
|
|
+ JSONObject itemMap = new JSONObject();
|
|
|
|
+ itemMap.put("BUKRS", "");
|
|
|
|
+ itemMap.put("KOSTL", "");
|
|
|
|
+ itemMap.put("KTEXT", "");
|
|
|
|
+ itemMap.put("ERSDA", "");
|
|
|
|
+ JSONObject etMap = new JSONObject();
|
|
|
|
+ etMap.put("item", itemMap);
|
|
|
|
+ json.put("ET_ITEM", etMap);
|
|
|
|
+ return json.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getProjectData(String companyCode)
|
|
|
|
+ {
|
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
|
+ json.put("I_PBUKR", EmptyUtils.isNotEmpty(companyCode) ? companyCode : "");
|
|
|
|
+ json.put("I_AEDAT", "");
|
|
|
|
+ json.put("I_ERDAT", "");
|
|
|
|
+ JSONObject itemMap = new JSONObject();
|
|
|
|
+ itemMap.put("I_PBUKR", "");
|
|
|
|
+ itemMap.put("I_AEDAT", "");
|
|
|
|
+ itemMap.put("I_ERDAT", "");
|
|
|
|
+ JSONObject etMap = new JSONObject();
|
|
|
|
+ etMap.put("item", itemMap);
|
|
|
|
+ json.put("ET_ITEM", etMap);
|
|
|
|
+ return json.toString();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取字符串中最后一个 '.' 之前的部分
|
|
|
|
+ *
|
|
|
|
+ * @param str 输入字符串
|
|
|
|
+ * @return 第一个 '.' 之前的部分,如果没有 '.' 则返回NULL
|
|
|
|
+ */
|
|
|
|
+ public static String getProjectNumberForParent(String str) {
|
|
|
|
+ int firstDotIndex = str.indexOf('.');
|
|
|
|
+ if (firstDotIndex == -1) {
|
|
|
|
+ return null; // 没有找到 '.',返回NULL
|
|
|
|
+ }
|
|
|
|
+ return str.substring(0, firstDotIndex);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|