ludc
2023-03-17 752c36b6dab9b0483ebc9cb356cb1a09d45bd578
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
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.system.cache;
 
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.system.entity.*;
import org.springblade.system.feign.ISysClient;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;
 
/**
 * 系统缓存
 *
 * @author Chill
 */
public class SysCache {
    private static final String MENU_ID = "menu:id:";
    private static final String DEPT_ID = "dept:id:";
    private static final String DEPT_NAME = "dept:name:";
    private static final String DEPT_NAME_FUZZY = "dept:nameFuzzy:";
    private static final String DEPT_NAME_ID = "deptName:id:";
    private static final String DEPT_NAMES_ID = "deptNames:id:";
    private static final String DEPT_CHILD_ID = "deptChild:id:";
    private static final String DEPT_CHILDIDS_ID = "deptChildIds:id:";
    private static final String POST_ID = "post:id:";
    private static final String POST_NAME = "post:name:";
    private static final String POST_NAME_FUZZY = "post:nameFuzzy:";
    private static final String POST_NAME_ID = "postName:id:";
    private static final String POST_NAMES_ID = "postNames:id:";
    private static final String ROLE_ID = "role:id:";
    private static final String ROLE_NAME = "role:name:";
    private static final String ROLE_NAME_ID = "roleName:id:";
    private static final String ROLE_NAMES_ID = "roleNames:id:";
    private static final String ROLE_ALIAS_ID = "roleAlias:id:";
    private static final String ROLE_ALIASES_ID = "roleAliases:id:";
    public static final String TENANT_ID = "tenant:id:";
    public static final String TENANT_TENANT_ID = "tenant:tenantId:";
    public static final String TENANT_PACKAGE_ID = "tenant:packageId:";
 
    private static ISysClient sysClient;
 
    private static ISysClient getSysClient() {
        if (sysClient == null) {
            sysClient = SpringUtil.getBean(ISysClient.class);
        }
        return sysClient;
    }
 
    /**
     * 获取菜单
     *
     * @param id 主键
     * @return 菜单
     */
    public static Menu getMenu(Long id) {
        return CacheUtil.get(SYS_CACHE, MENU_ID, id, () -> {
            R<Menu> result = getSysClient().getMenu(id);
            return result.getData();
        });
    }
 
    /**
     * 获取部门
     *
     * @param id 主键
     * @return 部门
     */
    public static Dept getDept(Long id) {
        return CacheUtil.get(SYS_CACHE, DEPT_ID, id, () -> {
            R<Dept> result = getSysClient().getDept(id);
            return result.getData();
        });
    }
 
    /**
     * 获取部门id
     *
     * @param tenantId  租户id
     * @param deptNames 部门名
     * @return 部门id
     */
    public static String getDeptIds(String tenantId, String deptNames) {
        return CacheUtil.get(SYS_CACHE, DEPT_NAME, tenantId + StringPool.DASH + deptNames, () -> {
            R<String> result = getSysClient().getDeptIds(tenantId, deptNames);
            return result.getData();
        });
    }
 
    /**
     * 获取部门id
     *
     * @param tenantId  租户id
     * @param deptNames 部门名模糊查询
     * @return 部门id
     */
    public static String getDeptIdsByFuzzy(String tenantId, String deptNames) {
        return CacheUtil.get(SYS_CACHE, DEPT_NAME_FUZZY, tenantId + StringPool.DASH + deptNames, () -> {
            R<String> result = getSysClient().getDeptIdsByFuzzy(tenantId, deptNames);
            return result.getData();
        });
    }
 
    /**
     * 获取部门名
     *
     * @param id 主键
     * @return 部门名
     */
    public static String getDeptName(Long id) {
        return CacheUtil.get(SYS_CACHE, DEPT_NAME_ID, id, () -> {
            R<String> result = getSysClient().getDeptName(id);
            return result.getData();
        });
    }
 
 
    /**
     * 获取部门名集合
     *
     * @param deptIds 主键集合
     * @return 部门名
     */
    public static List<String> getDeptNames(String deptIds) {
        return CacheUtil.get(SYS_CACHE, DEPT_NAMES_ID, deptIds, () -> {
            R<List<String>> result = getSysClient().getDeptNames(deptIds);
            return result.getData();
        });
    }
 
    /**
     * 获取子部门集合
     *
     * @param deptId 主键
     * @return 子部门
     */
    public static List<Dept> getDeptChild(Long deptId) {
        return CacheUtil.get(SYS_CACHE, DEPT_CHILD_ID, deptId, () -> {
            R<List<Dept>> result = getSysClient().getDeptChild(deptId);
            return result.getData();
        });
    }
 
    /**
     * 获取子部门ID集合
     *
     * @param deptId 主键
     * @return 子部门ID
     */
    public static List<Long> getDeptChildIds(Long deptId) {
        if (deptId == null) {
            return null;
        }
        List<Long> deptIdList = CacheUtil.get(SYS_CACHE, DEPT_CHILDIDS_ID, deptId, List.class);
        if (deptIdList == null) {
            deptIdList = new ArrayList<>();
            List<Dept> deptChild = getDeptChild(deptId);
            if (deptChild != null) {
                List<Long> collect = deptChild.stream().map(Dept::getId).collect(Collectors.toList());
                deptIdList.addAll(collect);
            }
            deptIdList.add(deptId);
            CacheUtil.put(SYS_CACHE, DEPT_CHILDIDS_ID, deptId, deptIdList);
        }
        return deptIdList;
    }
 
    /**
     * 获取岗位
     *
     * @param id 主键
     * @return
     */
    public static Post getPost(Long id) {
        return CacheUtil.get(SYS_CACHE, POST_ID, id, () -> {
            R<Post> result = getSysClient().getPost(id);
            return result.getData();
        });
    }
 
    /**
     * 获取岗位id
     *
     * @param tenantId  租户id
     * @param postNames 岗位名
     * @return
     */
    public static String getPostIds(String tenantId, String postNames) {
        return CacheUtil.get(SYS_CACHE, POST_NAME, tenantId + StringPool.DASH + postNames, () -> {
            R<String> result = getSysClient().getPostIds(tenantId, postNames);
            return result.getData();
        });
    }
 
    /**
     * 获取岗位id
     *
     * @param tenantId  租户id
     * @param postNames 岗位名模糊查询
     * @return
     */
    public static String getPostIdsByFuzzy(String tenantId, String postNames) {
        return CacheUtil.get(SYS_CACHE, POST_NAME_FUZZY, tenantId + StringPool.DASH + postNames, () -> {
            R<String> result = getSysClient().getPostIdsByFuzzy(tenantId, postNames);
            return result.getData();
        });
    }
 
    /**
     * 获取岗位名
     *
     * @param id 主键
     * @return 岗位名
     */
    public static String getPostName(Long id) {
        return CacheUtil.get(SYS_CACHE, POST_NAME_ID, id, () -> {
            R<String> result = getSysClient().getPostName(id);
            return result.getData();
        });
    }
 
    /**
     * 获取岗位名集合
     *
     * @param postIds 主键集合
     * @return 岗位名
     */
    public static List<String> getPostNames(String postIds) {
        return CacheUtil.get(SYS_CACHE, POST_NAMES_ID, postIds, () -> {
            R<List<String>> result = getSysClient().getPostNames(postIds);
            return result.getData();
        });
    }
 
    /**
     * 获取角色
     *
     * @param id 主键
     * @return Role
     */
    public static Role getRole(Long id) {
        return CacheUtil.get(SYS_CACHE, ROLE_ID, id, () -> {
            R<Role> result = getSysClient().getRole(id);
            return result.getData();
        });
    }
 
    /**
     * 获取角色id
     *
     * @param tenantId  租户id
     * @param roleNames 角色名
     * @return
     */
    public static String getRoleIds(String tenantId, String roleNames) {
        return CacheUtil.get(SYS_CACHE, ROLE_NAME, tenantId + StringPool.DASH + roleNames, () -> {
            R<String> result = getSysClient().getRoleIds(tenantId, roleNames);
            return result.getData();
        });
    }
 
    /**
     * 获取角色名
     *
     * @param id 主键
     * @return 角色名
     */
    public static String getRoleName(Long id) {
        return CacheUtil.get(SYS_CACHE, ROLE_NAME_ID, id, () -> {
            R<String> result = getSysClient().getRoleName(id);
            return result.getData();
        });
    }
 
    /**
     * 获取角色别名
     *
     * @param id 主键
     * @return 角色别名
     */
    public static String getRoleAlias(Long id) {
        return CacheUtil.get(SYS_CACHE, ROLE_ALIAS_ID, id, () -> {
            R<String> result = getSysClient().getRoleAlias(id);
            return result.getData();
        });
    }
 
    /**
     * 获取角色名集合
     *
     * @param roleIds 主键集合
     * @return 角色名
     */
    public static List<String> getRoleNames(String roleIds) {
        return CacheUtil.get(SYS_CACHE, ROLE_NAMES_ID, roleIds, () -> {
            R<List<String>> result = getSysClient().getRoleNames(roleIds);
            return result.getData();
        });
    }
 
    /**
     * 获取角色别名集合
     *
     * @param roleIds 主键集合
     * @return 角色别名
     */
    public static List<String> getRoleAliases(String roleIds) {
        return CacheUtil.get(SYS_CACHE, ROLE_ALIASES_ID, roleIds, () -> {
            R<List<String>> result = getSysClient().getRoleAliases(roleIds);
            return result.getData();
        });
    }
 
    /**
     * 获取租户
     *
     * @param id 主键
     * @return Tenant
     */
    public static Tenant getTenant(Long id) {
        return CacheUtil.get(SYS_CACHE, TENANT_ID, id, () -> {
            R<Tenant> result = getSysClient().getTenant(id);
            return result.getData();
        }, Boolean.FALSE);
    }
 
    /**
     * 获取租户
     *
     * @param tenantId 租户id
     * @return Tenant
     */
    public static Tenant getTenant(String tenantId) {
        return CacheUtil.get(SYS_CACHE, TENANT_TENANT_ID, tenantId, () -> {
            R<Tenant> result = getSysClient().getTenant(tenantId);
            return result.getData();
        }, Boolean.FALSE);
    }
 
    /**
     * 获取租户产品包
     *
     * @param tenantId 租户id
     * @return Tenant
     */
    public static TenantPackage getTenantPackage(String tenantId) {
        return CacheUtil.get(SYS_CACHE, TENANT_PACKAGE_ID, tenantId, () -> {
            R<TenantPackage> result = getSysClient().getTenantPackage(tenantId);
            return result.getData();
        }, Boolean.FALSE);
    }
 
}