Переглянути джерело

优化代码加入databus

java110 5 роки тому
батько
коміт
1994bbc2a0

+ 87 - 4
service-api/src/main/java/com/java110/api/listener/system/TransferListener.java

@@ -6,25 +6,44 @@ import com.java110.api.listener.AbstractServiceApiListener;
 import com.java110.core.annotation.Java110Listener;
 import com.java110.core.annotation.Java110Listener;
 import com.java110.core.context.DataFlowContext;
 import com.java110.core.context.DataFlowContext;
 import com.java110.core.event.service.api.ServiceDataFlowEvent;
 import com.java110.core.event.service.api.ServiceDataFlowEvent;
+import com.java110.dto.businessDatabus.BusinessDatabusDto;
+import com.java110.dto.order.OrderDto;
 import com.java110.entity.center.AppService;
 import com.java110.entity.center.AppService;
+import com.java110.entity.order.Business;
+import com.java110.intf.job.IDataBusInnerServiceSMO;
+import com.java110.utils.cache.DatabusCache;
+import com.java110.utils.cache.MappingCache;
 import com.java110.utils.constant.CommonConstant;
 import com.java110.utils.constant.CommonConstant;
+import com.java110.utils.constant.DomainContant;
 import com.java110.utils.constant.ServiceCodeConstant;
 import com.java110.utils.constant.ServiceCodeConstant;
 import com.java110.utils.util.StringUtil;
 import com.java110.utils.util.StringUtil;
 import com.java110.vo.ResultVo;
 import com.java110.vo.ResultVo;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.*;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.client.HttpStatusCodeException;
 import org.springframework.web.client.HttpStatusCodeException;
 import org.springframework.web.client.RestTemplate;
 import org.springframework.web.client.RestTemplate;
 
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.Map;
 
 
 @Java110Listener("transferListener")
 @Java110Listener("transferListener")
 public class TransferListener extends AbstractServiceApiListener {
 public class TransferListener extends AbstractServiceApiListener {
     private final static Logger logger = LoggerFactory.getLogger(TransferListener.class);
     private final static Logger logger = LoggerFactory.getLogger(TransferListener.class);
+    //databus 业务类型
+    private static final String DATABUS_SWITCH = "DATABUS_SWITCH";
+    private static final String DATABUS_SWITCH_ON = "ON"; // 开关打开
 
 
 
 
+    @Autowired
+    private IDataBusInnerServiceSMO dataBusInnerServiceSMOImpl;
+
     @Autowired
     @Autowired
     private RestTemplate restTemplate;
     private RestTemplate restTemplate;
 
 
@@ -45,8 +64,9 @@ public class TransferListener extends AbstractServiceApiListener {
         HttpEntity<String> httpEntity = new HttpEntity<String>(reqJson.toJSONString(), header);
         HttpEntity<String> httpEntity = new HttpEntity<String>(reqJson.toJSONString(), header);
         String orgRequestUrl = context.getRequestHeaders().get("REQUEST_URL");
         String orgRequestUrl = context.getRequestHeaders().get("REQUEST_URL");
 
 
+        String serviceCode = "/" + reqHeader.get(CommonConstant.HTTP_RESOURCE) + "/" + reqHeader.get(CommonConstant.HTTP_ACTION);
 
 
-        String requestUrl = service.getUrl() + "/" + reqHeader.get(CommonConstant.HTTP_RESOURCE) + "/" + reqHeader.get(CommonConstant.HTTP_ACTION);
+        String requestUrl = service.getUrl() + serviceCode;
 
 
         ResponseEntity responseEntity = null;
         ResponseEntity responseEntity = null;
         if (!StringUtil.isNullOrNone(orgRequestUrl)) {
         if (!StringUtil.isNullOrNone(orgRequestUrl)) {
@@ -63,13 +83,26 @@ public class TransferListener extends AbstractServiceApiListener {
             } else {
             } else {
                 responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
                 responseEntity = restTemplate.exchange(requestUrl, HttpMethod.POST, httpEntity, String.class);
             }
             }
+            HttpHeaders headers = responseEntity.getHeaders();
+            String oId = "-1";
+            if (headers.containsKey(OrderDto.O_ID)) {
+                oId = headers.get(OrderDto.O_ID).get(0);
+            }
+
+            //进入databus
+            if (!CommonConstant.HTTP_METHOD_GET.equals(service.getMethod())) {
+
+                dealDatabus(serviceCode, reqJson, oId);
+            }
+
+
         } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
         } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
             logger.error("请求下游服务【" + requestUrl + "】异常,参数为" + httpEntity + e.getResponseBodyAsString(), e);
             logger.error("请求下游服务【" + requestUrl + "】异常,参数为" + httpEntity + e.getResponseBodyAsString(), e);
             String body = e.getResponseBodyAsString();
             String body = e.getResponseBodyAsString();
 
 
-            if(StringUtil.isJsonObject(body)){
+            if (StringUtil.isJsonObject(body)) {
                 JSONObject bodyObj = JSONObject.parseObject(body);
                 JSONObject bodyObj = JSONObject.parseObject(body);
-                if(bodyObj.containsKey("message") && !StringUtil.isEmpty(bodyObj.getString("message"))){
+                if (bodyObj.containsKey("message") && !StringUtil.isEmpty(bodyObj.getString("message"))) {
                     body = bodyObj.getString("message");
                     body = bodyObj.getString("message");
                 }
                 }
             }
             }
@@ -99,6 +132,56 @@ public class TransferListener extends AbstractServiceApiListener {
 
 
     }
     }
 
 
+    /**
+     * databus 处理
+     * databus 是一种数据清洗和加载器,通过驱动的方式 对数据进行清洗。
+     *
+     * @param serviceCode
+     * @param reqJson
+     */
+    private void dealDatabus(String serviceCode, JSONObject reqJson, String oId) {
+        String databusSwitch = MappingCache.getValue(DomainContant.COMMON_DOMAIN, DATABUS_SWITCH);
+
+        if (!DATABUS_SWITCH_ON.equals(databusSwitch)) {
+            return;
+        }
+
+        List<BusinessDatabusDto> databusDtos = DatabusCache.getDatabuss();
+
+        if (!hasTypeCd(databusDtos, serviceCode)) {
+            return;
+        }
+
+        List<Business> businesses = new ArrayList<>();
+        Business business = null;
+        business = new Business();
+        business.setbId("-1");
+        business.setBusinessTypeCd(serviceCode);
+        business.setoId(oId);
+        business.setData(reqJson);
+        businesses.add(business);
+
+
+        try {
+            //同步databus
+            dataBusInnerServiceSMOImpl.exchange(businesses);
+        } catch (Exception e) {
+            logger.error("传输databus 失败", e);
+        }
+    }
+
+    private boolean hasTypeCd(List<BusinessDatabusDto> databusDtos, String serviceCode) {
+
+        for (BusinessDatabusDto databusDto : databusDtos) {
+            if (databusDto.getBusinessTypeCd().equals(serviceCode)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+
     @Override
     @Override
     public String getServiceCode() {
     public String getServiceCode() {
         return ServiceCodeConstant.SERVICE_CODE_SYSTEM_TRANSFER;
         return ServiceCodeConstant.SERVICE_CODE_SYSTEM_TRANSFER;