From 4e7907983a99e695f2acba4dec8bd73b7dbe6b2b Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期四, 29 六月 2023 14:41:33 +0800
Subject: [PATCH] optimize

---
 java110-core/src/main/java/com/java110/core/factory/CallApiServiceFactory.java |   61 ++++++++++++++++++++++++++----
 1 files changed, 52 insertions(+), 9 deletions(-)

diff --git a/java110-core/src/main/java/com/java110/core/factory/CallApiServiceFactory.java b/java110-core/src/main/java/com/java110/core/factory/CallApiServiceFactory.java
index 58c05ee..9808e05 100755
--- a/java110-core/src/main/java/com/java110/core/factory/CallApiServiceFactory.java
+++ b/java110-core/src/main/java/com/java110/core/factory/CallApiServiceFactory.java
@@ -2,6 +2,7 @@
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import com.java110.core.context.Environment;
 import com.java110.core.context.IPageData;
 import com.java110.core.context.PageData;
 import com.java110.core.log.LoggerFactory;
@@ -25,11 +26,13 @@
 public class CallApiServiceFactory {
 
     private static final String URL_API = "http://api-service/api/";
+    private static final String URL_BOOT_API = "http://127.0.0.1:8008/api/";
     //鏃ュ織
     private static Logger logger = LoggerFactory.getLogger(CallApiServiceFactory.class);
 
     @Autowired
     private RestTemplate restTemplate;
+
 
     /**
      * 鏌ヨ
@@ -43,6 +46,26 @@
     public static <T> T getForApi(String appId, T param, String serviceCode, Class<T> t) {
 
         IPageData pd = PageData.newInstance().builder("-1", "鏈煡", "", "", "", "", "", "", appId);
+
+        List<T> list = getForApis(pd, param, serviceCode, t);
+        if (list != null && list.size() > 0) {
+            return list.get(0);
+        }
+        return null;
+    }
+
+    /**
+     * 鏌ヨ
+     *
+     * @param param       浼犲叆瀵硅薄
+     * @param serviceCode 鏈嶅姟缂栫爜
+     * @param t           杩斿洖绫�
+     * @param <T>
+     * @return
+     */
+    public static <T> T getForApi(String appId, T param, String serviceCode, Class<T> t,String userId) {
+
+        IPageData pd = PageData.newInstance().builder(userId, "鏈煡", "", "", "", "", "", "", appId);
 
         List<T> list = getForApis(pd, param, serviceCode, t);
         if (list != null && list.size() > 0) {
@@ -142,6 +165,10 @@
 
         String url = URL_API + serviceCode;
         RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
+        if (Environment.isStartBootWay()) {
+            url = URL_BOOT_API + serviceCode;
+            restTemplate = ApplicationContextFactory.getBean("outRestTemplate", RestTemplate.class);
+        }
 
 
         ResponseEntity<String> responseEntity = callCenterService(restTemplate, pd, JSONObject.toJSONString(param), url, HttpMethod.POST);
@@ -151,12 +178,17 @@
         }
 
         JSONObject resultVo = JSONObject.parseObject(responseEntity.getBody());
-
-        if (ResultVo.CODE_MACHINE_OK != resultVo.getInteger("code")) {
-            throw new SMOException(resultVo.getString("msg"));
+        Object bObj = null;
+        if(resultVo.containsKey("code")){
+            if (ResultVo.CODE_MACHINE_OK != resultVo.getInteger("code")) {
+                throw new SMOException(resultVo.getString("msg"));
+            }
+            bObj = resultVo.get("data");
+        }else{
+            bObj = resultVo;
         }
 
-        Object bObj = resultVo.get("data");
+
         JSONArray datas = null;
         if (bObj instanceof JSONObject) {
             datas = new JSONArray();
@@ -183,10 +215,16 @@
     public static <T> List<T> getForApis(IPageData pd, T param, String serviceCode, Class<T> t) {
 
         String url = URL_API + serviceCode;
+        RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
+
+        if (Environment.isStartBootWay()) {
+            url = URL_BOOT_API + serviceCode;
+            restTemplate = ApplicationContextFactory.getBean("outRestTemplate", RestTemplate.class);
+        }
+
         if (param != null) {
             url += mapToUrlParam(BeanConvertUtil.beanCovertMap(param));
         }
-        RestTemplate restTemplate = ApplicationContextFactory.getBean("restTemplate", RestTemplate.class);
 
         ResponseEntity<String> responseEntity = callCenterService(restTemplate, pd, "", url, HttpMethod.GET);
 
@@ -196,11 +234,15 @@
 
         JSONObject resultVo = JSONObject.parseObject(responseEntity.getBody());
 
-        if (!"0".equals(resultVo.getString("code"))) {
-            throw new SMOException(resultVo.getString("msg"));
+        Object bObj = null;
+        if(resultVo.containsKey("code")){
+            if (ResultVo.CODE_MACHINE_OK != resultVo.getInteger("code")) {
+                throw new SMOException(resultVo.getString("msg"));
+            }
+            bObj = resultVo.get("data");
+        }else{
+            bObj = resultVo;
         }
-
-        Object bObj = resultVo.get("data");
         JSONArray datas = null;
         if (bObj instanceof JSONObject) {
             datas = new JSONArray();
@@ -239,6 +281,7 @@
 
         HttpEntity<String> httpEntity = new HttpEntity<String>(param, header);
         //logger.debug("璇锋眰涓績鏈嶅姟淇℃伅锛寋}", httpEntity);
+
         try {
             responseEntity = restTemplate.exchange(url, httpMethod, httpEntity, String.class);
         } catch (HttpStatusCodeException e) { //杩欓噷spring 妗嗘灦 鍦�4XX 鎴� 5XX 鏃舵姏鍑� HttpServerErrorException 寮傚父锛岄渶瑕侀噸鏂板皝瑁呬竴涓�

--
Gitblit v1.8.0