From e3aed650aa4e232f4583a00fc3de088276a2188b Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期六, 15 一月 2022 13:41:18 +0800
Subject: [PATCH] 优化代码

---
 service-order/src/main/java/com/java110/order/smo/impl/AsynNotifySubServiceImpl.java |  137 ++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 132 insertions(+), 5 deletions(-)

diff --git a/service-order/src/main/java/com/java110/order/smo/impl/AsynNotifySubServiceImpl.java b/service-order/src/main/java/com/java110/order/smo/impl/AsynNotifySubServiceImpl.java
index 7708266..cf5ef2a 100644
--- a/service-order/src/main/java/com/java110/order/smo/impl/AsynNotifySubServiceImpl.java
+++ b/service-order/src/main/java/com/java110/order/smo/impl/AsynNotifySubServiceImpl.java
@@ -3,9 +3,20 @@
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.java110.core.client.RestTemplate;
+import com.java110.core.context.SecureInvocation;
+import com.java110.dto.businessDatabus.BusinessDatabusDto;
 import com.java110.dto.businessTableHis.BusinessTableHisDto;
+import com.java110.dto.order.OrderDto;
 import com.java110.dto.order.OrderItemDto;
+import com.java110.entity.order.Business;
+import com.java110.intf.job.IDataBusInnerServiceSMO;
+import com.java110.order.dao.ICenterServiceDAO;
 import com.java110.order.smo.IAsynNotifySubService;
+import com.java110.utils.cache.DatabusCache;
+import com.java110.utils.cache.MappingCache;
+import com.java110.utils.constant.DomainContant;
+import com.java110.utils.util.StringUtil;
+import com.java110.vo.ResultVo;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -15,12 +26,26 @@
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 @Service
 public class AsynNotifySubServiceImpl implements IAsynNotifySubService {
     private static Logger logger = LoggerFactory.getLogger(AsynNotifySubServiceImpl.class);
-
+    //databus 涓氬姟绫诲瀷
+    private static final String DATABUS_SWITCH = "DATABUS_SWITCH";
+    private static final String DATABUS_SWITCH_ON = "ON"; // 寮�鍏虫墦寮�
     @Autowired
     private RestTemplate restTemplate;
+
+
+    @Autowired
+    private ICenterServiceDAO centerServiceDAOImpl;
+
+    @Autowired
+    private IDataBusInnerServiceSMO dataBusInnerServiceSMOImpl;
 
     public static final String FALLBACK_URL = "http://SERVICE_NAME/businessApi/fallBack";
 
@@ -42,6 +67,102 @@
         }
     }
 
+    @Override
+    public void notifyDatabus(List<Map> orderItemMaps, OrderDto orderDto) {
+
+
+        if (orderItemMaps == null || orderItemMaps.size() < 1) {
+            return ;
+        }
+        //瑙﹀彂databug
+        //鏌ヨ 浜嬪姟椤�
+        Map orderItem = new HashMap();
+        orderItem.put("oId", orderDto.getoId());
+        List<Map> businesses = centerServiceDAOImpl.getBusinessByOId(orderItem);
+        if (businesses == null || businesses.size() < 1) {
+            return;
+        }
+
+        String databusSwitch = MappingCache.getValue(DomainContant.COMMON_DOMAIN, DATABUS_SWITCH);
+
+        if (!DATABUS_SWITCH_ON.equals(databusSwitch)) {
+            return ;
+        }
+        List<BusinessDatabusDto> databusDtos = DatabusCache.getDatabuss();
+
+        if (!hasTypeCd(databusDtos, businesses) || !SecureInvocation.secure(this.getClass())) {
+            return ;
+        }
+
+        try {
+            //鍚屾databus
+            dataBusInnerServiceSMOImpl.exchange(freshBusesses(businesses, orderItemMaps));
+        } catch (Exception e) {
+            logger.error("浼犺緭databus 澶辫触", e);
+        }
+    }
+
+    /**
+     * @param businesses
+     * @param orderItemMaps
+     * @return
+     */
+    private List<Business> freshBusesses(List<Map> businesses, List<Map> orderItemMaps) {
+        List<Business> businessObjs = new ArrayList<>();
+        Business businessObj = null;
+        /**
+         * select t.b_id bId,t.o_id oId,t.create_time createTime,t.action,t.action_obj actionObj,
+         *         t.finish_time finishTime,t.remark,ul.service_name serviceName,ul.log_text logText
+         *         from c_order_item t
+         *         LEFT JOIN unitem_log ul on t.b_id = ul.b_id and t.o_id = ul.o_id and ul.status_cd = '0'
+         *         where 1=1
+         *
+         *         {"afterValue":[{"share_id":"'502021080429730002'","start_time":"'2021-08-01 00:00:35'","ad_type":"'10000'",
+         *         "advert_type":"'3'","ads_id":"'782021080454590009'","ad_name":"'骞垮憡'","end_time":"'2022-02-03 02:10:35'",
+         *         "state":"'2000'","seq":"'1'","url":"'img/20210804/01a629f9-a267-43b8-8d74-9c83b00ed2a7.png'"}],"preValue":[]}
+         */
+        JSONObject data = null;
+        for (Map business : businesses) {
+            for (Map orderItem : orderItemMaps) {
+                if (!business.get("b_id").equals(orderItem.get("bId"))) {
+                    continue;
+                }
+                businessObj = new Business();
+                businessObj.setoId(business.get("o_id").toString());
+                businessObj.setbId(business.get("b_id").toString());
+                businessObj.setBusinessTypeCd(business.get("business_type_cd").toString());
+                JSONObject logText = JSONObject.parseObject(orderItem.get("logText").toString());
+                if ("ADD".equals(orderItem.get("action")) || "MOD".equals(orderItem.get("action"))) {
+                    data = logText.getJSONArray("afterValue").getJSONObject(0);
+                    data = StringUtil.lineToHump(data);
+                    businessObj.setData(data);
+                }
+                if ("DEL".equals(orderItem.get("action"))) {
+                    data = logText.getJSONArray("preValue").getJSONObject(0);
+                    data = StringUtil.lineToHump(data);
+                    businessObj.setData(data);
+                }
+                businessObjs.add(businessObj);
+            }
+        }
+
+        return businessObjs;
+    }
+
+
+    private boolean hasTypeCd(List<BusinessDatabusDto> databusDtos, List<Map> businesses) {
+
+        for (BusinessDatabusDto databusDto : databusDtos) {
+            for (Map business : businesses) {
+                if (databusDto.getBusinessTypeCd().equals(business.get("business_type_cd"))) {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
     /**
      * 鐢熸垚鍥炴粴sql
      *
@@ -55,9 +176,15 @@
                 params = generateBusinessInsertInsertSql(orderItemDto, businessTableHisDto);
                 break;
             case "MOD":
-                params = generateBusinessDelInsertSql(orderItemDto, businessTableHisDto);
+                params = new JSONArray();
+                JSONArray paramDels = generateBusinessDelInsertSql(orderItemDto, businessTableHisDto);
+                for(int delIndex = 0 ; delIndex < paramDels.size(); delIndex ++){
+                    params.add(paramDels.getJSONObject(delIndex));
+                }
                 JSONArray paramAdds = generateBusinessInsertInsertSql(orderItemDto, businessTableHisDto);
-                params.add(paramAdds);
+                for(int addIndex = 0 ; addIndex < paramAdds.size(); addIndex ++){
+                    params.add(paramAdds.getJSONObject(addIndex));
+                }
                 break;
             case "DEL":
                 params = generateBusinessDelInsertSql(orderItemDto, businessTableHisDto);
@@ -92,7 +219,7 @@
             String keySql = "( ";
             String valueSql = " values (";
             for (String key : keyValue.keySet()) {
-                if ("status_cd".equals(key) || "create_time".equals(key)) {
+                if ("status_cd".equals(key) || "create_time".equals(key) || "b_id".equals(key)) {
                     continue;
                 }
                 keySql += (key + ",");
@@ -139,7 +266,7 @@
             String keySql = "( ";
             String valueSql = " values (";
             for (String key : keyValue.keySet()) {
-                if ("status_cd".equals(key) || "create_time".equals(key)) {
+                if ("status_cd".equals(key) || "create_time".equals(key) || "b_id".equals(key)) {
                     continue;
                 }
                 keySql += (key + ",");

--
Gitblit v1.8.0