wuxw7
2018-05-11 9a8bc3ca15205dd798a20ce444915932fe1880a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.java110.center.smo.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.java110.center.dao.ICenterServiceDAO;
import com.java110.center.smo.ICenterServiceCacheSMO;
import com.java110.common.cache.AppRouteCache;
import com.java110.common.cache.MappingCache;
import com.java110.common.cache.ServiceSqlCache;
import com.java110.common.constant.CommonConstant;
import com.java110.common.constant.ResponseConstant;
import com.java110.common.exception.SMOException;
import com.java110.common.factory.DataTransactionFactory;
import com.java110.entity.center.AppRoute;
import com.java110.entity.mapping.Mapping;
import com.java110.entity.service.DataQuery;
import com.java110.entity.service.ServiceSql;
import com.java110.service.dao.IQueryServiceDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 刷新缓存
 * Created by wuxw on 2018/4/18.
 */
@Service("centerServiceCacheSMOImpl")
public class CenterServiceCacheSMOImpl implements ICenterServiceCacheSMO {
 
    @Autowired
    ICenterServiceDAO centerServiceDAOImpl;
 
    @Autowired
    IQueryServiceDAO queryServiceDAOImpl;
 
    @Override
    public void flush(DataQuery dataQuery) throws SMOException{
 
 
 
        //1.0 封装 AppRoute
        flushAppRoute(dataQuery);
 
        //2.0 分装 Mapping
        flushMapping(dataQuery);
 
        //3.0 分装 ServiceSql
        flushServiceSql(dataQuery);
 
        dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,"刷新成功"));
    }
 
    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);
        }
    }
    /**
     * 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 ;
        }
        List<ServiceSql> serviceSqls = queryServiceDAOImpl.qureyServiceSqlAll();
 
        if(serviceSqls == null || serviceSqls.size() == 0){
            return;
        }
        for(ServiceSql serviceSql: serviceSqls){
            ServiceSqlCache.setServiceSql(serviceSql);
        }
    }
 
    /**
     * 刷新 Mapping 数据
     */
    private void flushMapping(DataQuery dataQuery) {
 
        JSONObject params = dataQuery.getRequestParams();
 
        if(!CommonConstant.CACHE_MAPPING.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))){
            return ;
        }
 
        List<Mapping> mappings = centerServiceDAOImpl.getMappingInfoAll();
 
        for(Mapping mapping : mappings){
            MappingCache.setVaule(mapping);
        }
 
        Map<String,List<Mapping>> mappingMap = new HashMap<String,List<Mapping>>();
        List<Mapping> mappingsNew = null;
        for(Mapping mapping : mappings){
            if(mappingMap.containsKey(mapping.getDomain())){
                mappingsNew = mappingMap.get(mapping.getDomain());
                mappingsNew.add(mapping);
            }else{
                mappingsNew = new ArrayList<Mapping>();
                mappingsNew.add(mapping);
                mappingMap.put(mapping.getDomain(),mappingsNew);
            }
        }
 
        for (String domain : mappingMap.keySet()) {
            MappingCache.setValue(mappingMap.get(domain));
        }
    }
 
    /**
     * 刷新AppRoute数据
     */
    private void flushAppRoute(DataQuery dataQuery){
 
        JSONObject params = dataQuery.getRequestParams();
 
        if(!CommonConstant.CACHE_APP_ROUTE_SERVICE.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))){
            return ;
        }
        List<Map> appInfos = centerServiceDAOImpl.getAppRouteAndServiceInfoAll();
        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())){
                appRoutes = appRoustsMap.get(appInfo.get("app_id").toString());
                appRoutes.add(AppRoute.newInstance().builder(appInfo));
            }else{
                appRoutes = new ArrayList<AppRoute>();
                appRoutes.add(AppRoute.newInstance().builder(appInfo));
                appRoustsMap.put(appInfo.get("app_id").toString(),appRoutes);
            }
        }
 
        for (String appId : appRoustsMap.keySet()) {
            AppRouteCache.setAppRoute(appRoustsMap.get(appId));
        }
 
    }
 
    public ICenterServiceDAO getCenterServiceDAOImpl() {
        return centerServiceDAOImpl;
    }
 
    public void setCenterServiceDAOImpl(ICenterServiceDAO centerServiceDAOImpl) {
        this.centerServiceDAOImpl = centerServiceDAOImpl;
    }
 
    public IQueryServiceDAO getQueryServiceDAOImpl() {
        return queryServiceDAOImpl;
    }
 
    public void setQueryServiceDAOImpl(IQueryServiceDAO queryServiceDAOImpl) {
        this.queryServiceDAOImpl = queryServiceDAOImpl;
    }
}