From 17764bd12c42512189ea5c71fac8be2614fb7589 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期四, 22 十月 2020 15:38:15 +0800
Subject: [PATCH] 优化代码

---
 service-order/src/main/java/com/java110/order/smo/impl/CenterServiceCacheSMOImpl.java |  169 ++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 108 insertions(+), 61 deletions(-)

diff --git a/service-order/src/main/java/com/java110/order/smo/impl/CenterServiceCacheSMOImpl.java b/service-order/src/main/java/com/java110/order/smo/impl/CenterServiceCacheSMOImpl.java
index cd1d999..8c3d9d9 100644
--- a/service-order/src/main/java/com/java110/order/smo/impl/CenterServiceCacheSMOImpl.java
+++ b/service-order/src/main/java/com/java110/order/smo/impl/CenterServiceCacheSMOImpl.java
@@ -1,23 +1,21 @@
 package com.java110.order.smo.impl;
 
 import com.alibaba.fastjson.JSONObject;
-import com.java110.utils.cache.ServiceBusinessCache;
+import com.java110.core.factory.DataTransactionFactory;
+import com.java110.db.dao.IQueryServiceDAO;
+import com.java110.dto.basePrivilege.BasePrivilegeDto;
+import com.java110.entity.center.AppRoute;
+import com.java110.entity.mapping.Mapping;
 import com.java110.entity.order.ServiceBusiness;
+import com.java110.entity.service.ServiceSql;
 import com.java110.order.dao.ICenterServiceDAO;
 import com.java110.order.smo.ICenterServiceCacheSMO;
-import com.java110.utils.cache.AppRouteCache;
-import com.java110.utils.cache.MappingCache;
-import com.java110.utils.cache.ServiceSqlCache;
+import com.java110.service.context.DataQuery;
+import com.java110.utils.cache.*;
 import com.java110.utils.constant.CommonConstant;
 import com.java110.utils.constant.ResponseConstant;
 import com.java110.utils.exception.SMOException;
 import com.java110.utils.util.Assert;
-import com.java110.core.factory.DataTransactionFactory;
-import com.java110.entity.center.AppRoute;
-import com.java110.entity.mapping.Mapping;
-import com.java110.service.context.DataQuery;
-import com.java110.entity.service.ServiceSql;
-import com.java110.db.dao.IQueryServiceDAO;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -44,8 +42,7 @@
     IQueryServiceDAO queryServiceDAOImpl;
 
     @Override
-    public void flush(DataQuery dataQuery) throws SMOException{
-
+    public void flush(DataQuery dataQuery) throws SMOException {
 
 
         //1.0 灏佽 AppRoute
@@ -60,14 +57,18 @@
         //4.0 鍒锋柊涓氬姟淇℃伅
         flushServiceBusiness(dataQuery);
 
-        dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,"鍒锋柊鎴愬姛"));
+        //5.0 鍒锋柊鍩虹鏉冮檺
+        flushPrivilege(dataQuery);
+
+        dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS, "鍒锋柊鎴愬姛"));
     }
 
     /**
      * 鏍规嵁缂撳瓨绫诲埆鍒锋柊缂撳瓨
+     *
      * @param headers 缂撳瓨绫诲埆
      */
-    public void flush(Map<String,String> headers) throws SMOException{
+    public void flush(Map<String, String> headers) throws SMOException {
 
         flushAppRoute(headers);
 
@@ -78,6 +79,9 @@
 
         //4.0 鍒锋柊涓氬姟淇℃伅
         flushServiceBusiness(headers);
+
+        //5.0 鍒锋柊鍩虹鏉冮檺
+        flushPrivilege(headers);
     }
 
     /**
@@ -95,22 +99,27 @@
         doFlushServiceSql();
 
         doFlushServiceBusiness();
+
+        //5.0 鍒锋柊鍏ㄦ柊
+        doFlushPrivilege();
     }
 
-    private void checkCacheParam(DataQuery dataQuery) throws SMOException{
+
+    private void checkCacheParam(DataQuery dataQuery) throws SMOException {
         JSONObject params = dataQuery.getRequestParams();
-        if(params == null || !params.containsKey(CommonConstant.CACHE_PARAM_NAME)){
-            throw new SMOException(ResponseConstant.RESULT_PARAM_ERROR,"璇锋眰鎶ユ枃閿欒锛屾湭鍖呭惈瀛楁 "+CommonConstant.CACHE_PARAM_NAME);
+        if (params == null || !params.containsKey(CommonConstant.CACHE_PARAM_NAME)) {
+            throw new SMOException(ResponseConstant.RESULT_PARAM_ERROR, "璇锋眰鎶ユ枃閿欒锛屾湭鍖呭惈瀛楁 " + CommonConstant.CACHE_PARAM_NAME);
         }
     }
+
     /**
      * 3.0 鍒嗚 ServiceSql
      */
     private void flushServiceSql(DataQuery dataQuery) {
 
         JSONObject params = dataQuery.getRequestParams();
-        if(!CommonConstant.CACHE_SERVICE_SQL.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))){
-            return ;
+        if (!CommonConstant.CACHE_SERVICE_SQL.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))) {
+            return;
         }
         // 鍒锋柊
         doFlushServiceSql();
@@ -119,8 +128,8 @@
     private void flushServiceBusiness(DataQuery dataQuery) {
 
         JSONObject params = dataQuery.getRequestParams();
-        if(!CommonConstant.CACHE_SERVICE_BUSINESS.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))){
-            return ;
+        if (!CommonConstant.CACHE_SERVICE_BUSINESS.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))) {
+            return;
         }
         // 鍒锋柊
         doFlushServiceBusiness();
@@ -129,12 +138,12 @@
     /**
      * 3.0 鍒嗚 ServiceSql
      */
-    private void flushServiceSql(Map<String,String> headers) {
+    private void flushServiceSql(Map<String, String> headers) {
 
-        Assert.hasKey(headers,CommonConstant.CACHE_PARAM,"鏈寘鍚玞ache鍙傛暟"+headers.toString());
-        if(!CommonConstant.CACHE_SERVICE_SQL.equals(headers.get(CommonConstant.CACHE_PARAM))
-                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))){
-            return ;
+        Assert.hasKey(headers, CommonConstant.CACHE_PARAM, "鏈寘鍚玞ache鍙傛暟" + headers.toString());
+        if (!CommonConstant.CACHE_SERVICE_SQL.equals(headers.get(CommonConstant.CACHE_PARAM))
+                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))) {
+            return;
         }
         // 鍒锋柊
         doFlushServiceSql();
@@ -143,12 +152,12 @@
     /**
      * 3.0 鍒嗚 ServiceSql
      */
-    private void flushServiceBusiness(Map<String,String> headers) {
+    private void flushServiceBusiness(Map<String, String> headers) {
 
-        Assert.hasKey(headers,CommonConstant.CACHE_PARAM,"鏈寘鍚玞ache鍙傛暟"+headers.toString());
-        if(!CommonConstant.CACHE_SERVICE_BUSINESS.equals(headers.get(CommonConstant.CACHE_PARAM))
-                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))){
-            return ;
+        Assert.hasKey(headers, CommonConstant.CACHE_PARAM, "鏈寘鍚玞ache鍙傛暟" + headers.toString());
+        if (!CommonConstant.CACHE_SERVICE_BUSINESS.equals(headers.get(CommonConstant.CACHE_PARAM))
+                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))) {
+            return;
         }
         // 鍒锋柊
         doFlushServiceBusiness();
@@ -160,22 +169,22 @@
 
         List<ServiceSql> serviceSqls = queryServiceDAOImpl.qureyServiceSqlAll();
 
-        if(serviceSqls == null || serviceSqls.size() == 0){
+        if (serviceSqls == null || serviceSqls.size() == 0) {
             return;
         }
         //鍒犻櫎鍘熷鏁版嵁
         ServiceSqlCache.removeData(ServiceSqlCache._SUFFIX_SERVICE_SQL);
 
-        for(ServiceSql serviceSql: serviceSqls){
+        for (ServiceSql serviceSql : serviceSqls) {
             ServiceSqlCache.setServiceSql(serviceSql);
         }
     }
 
-    private void doFlushServiceBusiness(){
+    private void doFlushServiceBusiness() {
         logger.debug("寮�濮嬪埛鏂� ServiceBusiness鏁版嵁鍒皉edis鏁版嵁搴撲腑");
         List<ServiceBusiness> serviceBusinesses = queryServiceDAOImpl.qureyServiceBusiness();
 
-        if(serviceBusinesses == null || serviceBusinesses.size() == 0){
+        if (serviceBusinesses == null || serviceBusinesses.size() == 0) {
             return;
         }
         //鍒犻櫎鍘熷鏁版嵁
@@ -193,23 +202,53 @@
 
         JSONObject params = dataQuery.getRequestParams();
 
-        if(!CommonConstant.CACHE_MAPPING.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))){
-            return ;
+        if (!CommonConstant.CACHE_MAPPING.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))) {
+            return;
         }
 
         doFlushMapping();
     }
 
+
     /**
      * 鍒锋柊 Mapping 鏁版嵁
      */
-    private void flushMapping(Map<String,String> headers) {
+    private void flushPrivilege(DataQuery dataQuery) {
 
-        Assert.hasKey(headers,CommonConstant.CACHE_PARAM,"鏈寘鍚玞ache鍙傛暟"+headers.toString());
+        JSONObject params = dataQuery.getRequestParams();
 
-        if(!CommonConstant.CACHE_MAPPING.equals(headers.get(CommonConstant.CACHE_PARAM))
-                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))){
-            return ;
+        if (!CommonConstant.CACHE_PRIVILEGE.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))) {
+            return;
+        }
+
+        doFlushPrivilege();
+    }
+
+    /**
+     * 鍒锋柊 Mapping 鏁版嵁
+     */
+    private void flushPrivilege(Map<String, String> headers) {
+
+        Assert.hasKey(headers, CommonConstant.CACHE_PARAM, "鏈寘鍚玞ache鍙傛暟" + headers.toString());
+
+        if (!CommonConstant.CACHE_PRIVILEGE.equals(headers.get(CommonConstant.CACHE_PARAM))
+                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))) {
+            return;
+        }
+
+        doFlushPrivilege();
+    }
+
+    /**
+     * 鍒锋柊 Mapping 鏁版嵁
+     */
+    private void flushMapping(Map<String, String> headers) {
+
+        Assert.hasKey(headers, CommonConstant.CACHE_PARAM, "鏈寘鍚玞ache鍙傛暟" + headers.toString());
+
+        if (!CommonConstant.CACHE_MAPPING.equals(headers.get(CommonConstant.CACHE_PARAM))
+                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))) {
+            return;
         }
 
         doFlushMapping();
@@ -220,20 +259,20 @@
         List<Mapping> mappings = centerServiceDAOImpl.getMappingInfoAll();
         //鍒犻櫎鍘熷鏁版嵁
         MappingCache.removeData(MappingCache._SUFFIX_MAPPING);
-        for(Mapping mapping : mappings){
+        for (Mapping mapping : mappings) {
             MappingCache.setVaule(mapping);
         }
 
-        Map<String,List<Mapping>> mappingMap = new HashMap<String,List<Mapping>>();
+        Map<String, List<Mapping>> mappingMap = new HashMap<String, List<Mapping>>();
         List<Mapping> mappingsNew = null;
-        for(Mapping mapping : mappings){
-            if(mappingMap.containsKey(mapping.getDomain())){
+        for (Mapping mapping : mappings) {
+            if (mappingMap.containsKey(mapping.getDomain())) {
                 mappingsNew = mappingMap.get(mapping.getDomain());
                 mappingsNew.add(mapping);
-            }else{
+            } else {
                 mappingsNew = new ArrayList<Mapping>();
                 mappingsNew.add(mapping);
-                mappingMap.put(mapping.getDomain(),mappingsNew);
+                mappingMap.put(mapping.getDomain(), mappingsNew);
             }
         }
 
@@ -242,15 +281,23 @@
         }
     }
 
+    private void doFlushPrivilege() {
+        logger.debug("寮�濮嬪埛鏂� Mapping鏁版嵁鍒皉edis鏁版嵁搴撲腑");
+        List<BasePrivilegeDto> basePrivilegeDtos = centerServiceDAOImpl.getPrivilegeAll();
+        //鍒犻櫎鍘熷鏁版嵁
+        PrivilegeCache.removeData(PrivilegeCache.DEFAULT_PRIVILEGE);
+        PrivilegeCache.setValue(basePrivilegeDtos);
+    }
+
     /**
      * 鍒锋柊AppRoute鏁版嵁
      */
-    private void flushAppRoute(DataQuery dataQuery){
+    private void flushAppRoute(DataQuery dataQuery) {
 
         JSONObject params = dataQuery.getRequestParams();
 
-        if(!CommonConstant.CACHE_APP_ROUTE_SERVICE.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))){
-            return ;
+        if (!CommonConstant.CACHE_APP_ROUTE_SERVICE.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))) {
+            return;
         }
         doFlushAppRoute();
 
@@ -259,13 +306,13 @@
     /**
      * 鍒锋柊AppRoute鏁版嵁
      */
-    private void flushAppRoute(Map<String,String> headers){
+    private void flushAppRoute(Map<String, String> headers) {
 
-        Assert.hasKey(headers,CommonConstant.CACHE_PARAM,"鏈寘鍚玞ache鍙傛暟"+headers.toString());
+        Assert.hasKey(headers, CommonConstant.CACHE_PARAM, "鏈寘鍚玞ache鍙傛暟" + headers.toString());
 
-        if(!CommonConstant.CACHE_APP_ROUTE_SERVICE.equals(headers.get(CommonConstant.CACHE_PARAM))
-                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))){
-            return ;
+        if (!CommonConstant.CACHE_APP_ROUTE_SERVICE.equals(headers.get(CommonConstant.CACHE_PARAM))
+                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))) {
+            return;
         }
         doFlushAppRoute();
 
@@ -274,16 +321,16 @@
     private void doFlushAppRoute() {
         logger.debug("寮�濮嬪埛鏂� AppRoute鏁版嵁鍒皉edis鏁版嵁搴撲腑");
         List<Map> appInfos = centerServiceDAOImpl.getAppRouteAndServiceInfoAll();
-        Map<String,List<AppRoute>> appRoustsMap = new HashMap<String,List<AppRoute>>();
+        Map<String, List<AppRoute>> appRoustsMap = new HashMap<String, List<AppRoute>>();
         List<AppRoute> appRoutes = null;
-        for(Map appInfo : appInfos){
-            if(appRoustsMap.containsKey(appInfo.get("app_id").toString())){
+        for (Map appInfo : appInfos) {
+            if (appRoustsMap.containsKey(appInfo.get("app_id").toString())) {
                 appRoutes = appRoustsMap.get(appInfo.get("app_id").toString());
                 appRoutes.add(AppRoute.newInstance().builder(appInfo));
-            }else{
+            } else {
                 appRoutes = new ArrayList<AppRoute>();
                 appRoutes.add(AppRoute.newInstance().builder(appInfo));
-                appRoustsMap.put(appInfo.get("app_id").toString(),appRoutes);
+                appRoustsMap.put(appInfo.get("app_id").toString(), appRoutes);
             }
         }
         //鍒犻櫎鍘熷鏁版嵁

--
Gitblit v1.8.0