skyouc
2025-03-21 725c4e907880e355dc018b24320e0b41d488474b
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
package com.vincent.rsf.server.common.handler.global;
 
import com.google.common.collect.Lists;
import com.vincent.rsf.framework.common.SpringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;
 
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * @Description: 字典工具类,提供给全局使用
 * @Author: Ryan
 * @CreateDate: 2019/8/13 11:11
 * @Version: 1.0
 */
@Slf4j
@Service
public class GlobalDictService implements InitializingBean {
 
    private final ConcurrentHashMap<String, String> LOCAL_LABEL_CACHE = new ConcurrentHashMap();
    private final ConcurrentHashMap<String, Set<Dict>> LOCAL_CACHE = new ConcurrentHashMap();
 
    public void putDict(String type, String value, String label) {
        LOCAL_LABEL_CACHE.put(type + "-" + value, label);
        putToLocalCache(type, value, label);
    }
 
    private void putToLocalCache(String type, String value, String label) {
        Dict dict = new Dict(value, label);
        Set<Dict> dicts = LOCAL_CACHE.get(type);
        if (dicts == null) {
            dicts = new HashSet<>();
        }
        dicts.add(dict);
        LOCAL_CACHE.put(type, dicts);
    }
 
    public String getDictLabel(String type, String value, String defaultLabel) {
        String label = LOCAL_LABEL_CACHE.get(type + "-" + value);
        if (label != null) {
            return label;
        }
        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)) {
            synchronized (type.intern()) {
                label = LOCAL_LABEL_CACHE.get(type + "-" + value);
                if (label != null) {
                    return label;
                }
            }
 
        }
        return defaultLabel;
    }
 
    public String getDictLabel(String type, String value) {
        return this.getDictLabel(type, value, null);
    }
 
    public String getDictLabels(String type, String values, String defaultValue) {
        if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(values)) {
            List<String> valueList = Lists.newArrayList();
            for (String value : StringUtils.split(values, ",")) {
                valueList.add(getDictLabel(value, type, defaultValue));
            }
            return StringUtils.join(valueList, ",");
        }
        return defaultValue;
    }
 
    public String getDictLabels(String type, String defaultValue) {
        List<String> valueList = Lists.newArrayList();
        Set<Dict> dicts = LOCAL_CACHE.get(type);
        if (dicts == null) {
            return defaultValue;
        }
        for (Dict dict : dicts) {
            valueList.add(dict.getLabel());
        }
 
        return StringUtils.join(valueList, ",");
    }
 
 
    public String getDictValue(String type, String label, String defaultValue) {
        if (StringUtils.isBlank(type) || StringUtils.isBlank(label)) {
            return defaultValue;
        }
        Set<Dict> dicts = LOCAL_CACHE.get(type);
        if (dicts != null) {
            for (Dict dict : dicts) {
                if (label.equalsIgnoreCase(dict.getLabel())) {
                    return dict.getValue();
                }
            }
        }
        return defaultValue;
    }
 
    public void clearAll() {
        LOCAL_LABEL_CACHE.clear();
        LOCAL_CACHE.clear();
    }
 
 
    @Override
    public void afterPropertiesSet() throws Exception {
 
 
    }
 
    @Data
    @AllArgsConstructor
    @EqualsAndHashCode
    private class Dict {
        private String value;
        private String label;
    }
 
    public static GlobalDictService getInstance() {
        try {
            GlobalDictService instance = SpringUtils.getBean(GlobalDictService.class);
            return instance;
        } catch (Exception e) {
            return null;
        }
    }
}