java110
2021-09-12 ed7d0de6a0e169b846a8dfafbdb9076d8c648445
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
package com.java110.dev.smo.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.java110.core.factory.DataTransactionFactory;
import com.java110.db.dao.IQueryServiceDAO;
import com.java110.dev.dao.IDevServiceDAO;
import com.java110.dev.smo.IDevServiceCacheSMO;
import com.java110.dto.basePrivilege.BasePrivilegeDto;
import com.java110.dto.businessDatabus.BusinessDatabusDto;
import com.java110.dto.businessTableHis.BusinessTableHisDto;
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.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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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("devServiceCacheSMOImpl")
public class DevServiceCacheSMOImpl implements IDevServiceCacheSMO {
 
    private final static Logger logger = LoggerFactory.getLogger(DevServiceCacheSMOImpl.class);
 
    @Autowired
    IDevServiceDAO devServiceDAOImpl;
 
    @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);
 
        //4.0 刷新业务信息
        flushServiceBusiness(dataQuery);
 
        //5.0 刷新基础权限
        flushPrivilege(dataQuery);
 
        //刷新databus
        flushDatabus(dataQuery);
 
//刷新BusinessTableHis
        flushBusinessTableHis(dataQuery);
 
        dataQuery.setResponseInfo(DataTransactionFactory.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS, "刷新成功"));
    }
 
    /**
     * 根据缓存类别刷新缓存
     *
     * @param headers 缓存类别
     */
    public void flush(Map<String, String> headers) throws SMOException {
 
        flushAppRoute(headers);
 
        flushMapping(headers);
 
        //3.0 分装 ServiceSql
        flushServiceSql(headers);
 
        //4.0 刷新业务信息
        flushServiceBusiness(headers);
 
        //5.0 刷新基础权限
        flushPrivilege(headers);
 
        flushDatabus(headers);
 
        flushBusinessTableHis(headers);
    }
 
    /**
     * 用来系统启动刷新
     */
    @Override
    public void startFlush() {
        //1.0 封装 AppRoute
        doFlushAppRoute();
 
        //2.0 分装 Mapping
        doFlushMapping();
 
        //3.0 分装 ServiceSql
        doFlushServiceSql();
        //5.0 刷新全新
        doFlushServiceBusiness();
 
 
        doFlushPrivilege();
 
        //刷新databus
        doFlushDatabus();
 
        //刷新BusinessTableHis
        doFlushBusinessTableHis();
    }
 
 
    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;
        }
        // 刷新
        doFlushServiceSql();
    }
 
    private void flushServiceBusiness(DataQuery dataQuery) {
 
        JSONObject params = dataQuery.getRequestParams();
        if (!CommonConstant.CACHE_SERVICE_BUSINESS.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))) {
            return;
        }
        // 刷新
        doFlushServiceBusiness();
    }
 
    /**
     * 3.0 分装 ServiceSql
     */
    private void flushServiceSql(Map<String, String> headers) {
 
        Assert.hasKey(headers, CommonConstant.CACHE_PARAM, "未包含cache参数" + headers.toString());
        if (!CommonConstant.CACHE_SERVICE_SQL.equals(headers.get(CommonConstant.CACHE_PARAM))
                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))) {
            return;
        }
        // 刷新
        doFlushServiceSql();
    }
 
    /**
     * 3.0 分装 ServiceSql
     */
    private void flushServiceBusiness(Map<String, String> headers) {
 
        Assert.hasKey(headers, CommonConstant.CACHE_PARAM, "未包含cache参数" + headers.toString());
        if (!CommonConstant.CACHE_SERVICE_BUSINESS.equals(headers.get(CommonConstant.CACHE_PARAM))
                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))) {
            return;
        }
        // 刷新
        doFlushServiceBusiness();
    }
 
    private void doFlushServiceSql() {
 
        logger.debug("开始刷新 ServiceSql数据到redis数据库中");
 
        List<ServiceSql> serviceSqls = queryServiceDAOImpl.qureyServiceSqlAll();
 
        if (serviceSqls == null || serviceSqls.size() == 0) {
            return;
        }
        //删除原始数据
        ServiceSqlCache.removeData(ServiceSqlCache._SUFFIX_SERVICE_SQL);
 
        for (ServiceSql serviceSql : serviceSqls) {
            ServiceSqlCache.setServiceSql(serviceSql);
        }
    }
 
    private void doFlushServiceBusiness() {
        logger.debug("开始刷新 ServiceBusiness数据到redis数据库中");
        List<ServiceBusiness> serviceBusinesses = queryServiceDAOImpl.qureyServiceBusiness();
 
        if (serviceBusinesses == null || serviceBusinesses.size() == 0) {
            return;
        }
        //删除原始数据
        ServiceBusinessCache.removeData(ServiceBusinessCache._KEY_SERVICE_BUSINESS);
 
        //设置缓存
        ServiceBusinessCache.setServiceBusiness(serviceBusinesses);
    }
 
 
    /**
     * 刷新 Mapping 数据
     */
    private void flushMapping(DataQuery dataQuery) {
 
        JSONObject params = dataQuery.getRequestParams();
 
        if (!CommonConstant.CACHE_MAPPING.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))) {
            return;
        }
 
        doFlushMapping();
    }
 
 
    /**
     * 刷新 Mapping 数据
     */
    private void flushPrivilege(DataQuery dataQuery) {
 
        JSONObject params = dataQuery.getRequestParams();
 
        if (!CommonConstant.CACHE_PRIVILEGE.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))) {
            return;
        }
 
        doFlushPrivilege();
    }
 
    /**
     * 刷新 Mapping 数据
     */
    private void flushDatabus(DataQuery dataQuery) {
 
        JSONObject params = dataQuery.getRequestParams();
 
        if (!CommonConstant.CACHE_DATABUS.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))) {
            return;
        }
 
        doFlushDatabus();
    }
 
    /**
     * 刷新 doFlushBusinessTableHis 数据
     */
    private void flushBusinessTableHis(DataQuery dataQuery) {
 
        JSONObject params = dataQuery.getRequestParams();
 
        if (!CommonConstant.CACHE_BUSINESS_TABLE_HIS.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))) {
            return;
        }
 
        doFlushBusinessTableHis();
    }
 
 
    /**
     * 刷新 Mapping 数据
     */
    private void flushPrivilege(Map<String, String> headers) {
 
        Assert.hasKey(headers, CommonConstant.CACHE_PARAM, "未包含cache参数" + headers.toString());
 
        if (!CommonConstant.CACHE_PRIVILEGE.equals(headers.get(CommonConstant.CACHE_PARAM))
                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))) {
            return;
        }
 
        doFlushPrivilege();
    }
 
    /**
     * 刷新 databus 数据
     */
    private void flushDatabus(Map<String, String> headers) {
 
        Assert.hasKey(headers, CommonConstant.CACHE_PARAM, "未包含cache参数" + headers.toString());
 
        if (!CommonConstant.CACHE_DATABUS.equals(headers.get(CommonConstant.CACHE_PARAM))
                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))) {
            return;
        }
 
        doFlushDatabus();
    }
 
    /**
     * 刷新 databus 数据
     */
    private void flushBusinessTableHis(Map<String, String> headers) {
 
        Assert.hasKey(headers, CommonConstant.CACHE_PARAM, "未包含cache参数" + headers.toString());
 
        if (!CommonConstant.CACHE_BUSINESS_TABLE_HIS.equals(headers.get(CommonConstant.CACHE_PARAM))
                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))) {
            return;
        }
 
        doFlushBusinessTableHis();
    }
 
    /**
     * 刷新 Mapping 数据
     */
    private void flushMapping(Map<String, String> headers) {
 
        Assert.hasKey(headers, CommonConstant.CACHE_PARAM, "未包含cache参数" + headers.toString());
 
        if (!CommonConstant.CACHE_MAPPING.equals(headers.get(CommonConstant.CACHE_PARAM))
                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))) {
            return;
        }
 
        doFlushMapping();
    }
 
    private void doFlushMapping() {
        logger.debug("开始刷新 Mapping数据到redis数据库中");
        List<Mapping> mappings = devServiceDAOImpl.getMappingInfoAll();
        //删除原始数据
        MappingCache.removeData(MappingCache._SUFFIX_MAPPING);
        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));
        }
    }
 
    private void doFlushPrivilege() {
        logger.debug("开始刷新 Mapping数据到redis数据库中");
        List<BasePrivilegeDto> basePrivilegeDtos = devServiceDAOImpl.getPrivilegeAll();
        //删除原始数据
        PrivilegeCache.removeData(PrivilegeCache.DEFAULT_PRIVILEGE);
        PrivilegeCache.setValue(basePrivilegeDtos);
    }
 
    private void doFlushDatabus() {
        logger.debug("开始刷新 Mapping数据到redis数据库中");
        List<BusinessDatabusDto> businessDatabusDtos = devServiceDAOImpl.getDatabusAll();
        //删除原始数据
        DatabusCache.removeData(DatabusCache.DEFAULT_DATABUS);
        DatabusCache.setValue(businessDatabusDtos);
    }
 
    private void doFlushBusinessTableHis() {
        logger.debug("开始刷新 BusinessTableHis数据到redis数据库中");
        List<BusinessTableHisDto> businessTableHisDtos = devServiceDAOImpl.getBusinessTableHisAll();
        //删除原始数据
        BusinessTableHisCache.removeData(BusinessTableHisCache.DEFAULT_BUSINESS_TABLE_HIS);
        BusinessTableHisCache.setValue(businessTableHisDtos);
    }
 
 
    /**
     * 刷新AppRoute数据
     */
    private void flushAppRoute(DataQuery dataQuery) {
 
        JSONObject params = dataQuery.getRequestParams();
 
        if (!CommonConstant.CACHE_APP_ROUTE_SERVICE.equals(params.getString(CommonConstant.CACHE_PARAM_NAME))) {
            return;
        }
        doFlushAppRoute();
 
    }
 
    /**
     * 刷新AppRoute数据
     */
    private void flushAppRoute(Map<String, String> headers) {
 
        Assert.hasKey(headers, CommonConstant.CACHE_PARAM, "未包含cache参数" + headers.toString());
 
        if (!CommonConstant.CACHE_APP_ROUTE_SERVICE.equals(headers.get(CommonConstant.CACHE_PARAM))
                && !CommonConstant.CACHE_ALL.equals(headers.get(CommonConstant.CACHE_PARAM))) {
            return;
        }
        doFlushAppRoute();
 
    }
 
    private void doFlushAppRoute() {
        logger.debug("开始刷新 AppRoute数据到redis数据库中");
        List<Map> appInfos = devServiceDAOImpl.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);
            }
        }
        //删除原始数据
        AppRouteCache.removeData(AppRouteCache._SUFFIX_APP_ROUTE);
 
        for (String appId : appRoustsMap.keySet()) {
            AppRouteCache.setAppRoute(appRoustsMap.get(appId));
        }
    }
 
 
    public IQueryServiceDAO getQueryServiceDAOImpl() {
        return queryServiceDAOImpl;
    }
 
    public void setQueryServiceDAOImpl(IQueryServiceDAO queryServiceDAOImpl) {
        this.queryServiceDAOImpl = queryServiceDAOImpl;
    }
}