yangys
2024-09-04 04c57331cf84c8f606c2838dcb6fe5463fb9b68c
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
package com.qianwen.smartman.common.cache;
 
import java.util.List;
import com.qianwen.smartman.common.constant.ExtCacheConstant;
import com.qianwen.smartman.common.enums.DictEnum;
import com.qianwen.core.cache.utils.CacheUtil;
import com.qianwen.core.tool.utils.SpringUtil;
import com.qianwen.smartman.modules.system.entity.Dict;
import com.qianwen.smartman.modules.system.service.IDictService;
 
public class DictCache {
    private static final String DICT_ID = "dict:id:";
    private static final String DICT_KEY = "dict:key:";
    private static final String DICT_VALUE = "dict:value:";
    private static final String DICT_LIST = "dict:list:";
    private static final IDictService dictService = (IDictService) SpringUtil.getBean(IDictService.class);
 
    public static Dict getById(Long id) {
        return (Dict) CacheUtil.get("blade:dict", DICT_ID, id, () -> {
            return (Dict) dictService.getById(id);
        }, ExtCacheConstant.TENANT_MODE);
    }
 
    public static String getKey(DictEnum code, String dictValue) {
        return getKey(code.getName(), dictValue);
    }
 
    public static String getKey(String code, String dictValue) {
        return (String) CacheUtil.get("blade:dict", DICT_KEY + code + ":", dictValue, () -> {
            List<Dict> list = getList(code);
            return (String) list.stream().filter(dict -> {
                return dict.getDictValue().equalsIgnoreCase(dictValue);
            }).map((v0) -> {
                return v0.getDictKey();
            }).findFirst().orElse("");
        }, ExtCacheConstant.TENANT_MODE);
    }
 
    public static String getValue(DictEnum code, Integer dictKey) {
        return getValue(code.getName(), dictKey);
    }
 
    public static String getValue(String code, Integer dictKey) {
        return (String) CacheUtil.get("blade:dict", DICT_VALUE + code + ":", String.valueOf(dictKey), () -> {
            return dictService.getValue(code, String.valueOf(dictKey));
        }, ExtCacheConstant.TENANT_MODE);
    }
 
    public static String getValue(DictEnum code, String dictKey) {
        return getValue(code.getName(), dictKey);
    }
 
    public static String getValue(String code, String dictKey) {
        return CacheUtil.get("blade:dict", DICT_VALUE + code + ":", dictKey, () -> {
            return dictService.getValue(code, dictKey);
        }, ExtCacheConstant.TENANT_MODE);
    }
 
    public static List<Dict> getList(String code) {
        return CacheUtil.get("blade:dict", DICT_LIST, code, () -> {
            return dictService.getList(code);
        }, ExtCacheConstant.TENANT_MODE);
    }
}