DictionaryService.java
5.7 KB
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
package com.brframework.commonwebbase.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.brframework.commonwebbase.dao.DictionaryDao;
import com.brframework.commonwebbase.entity.Dictionary;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by xu
* <p>
* 字典
*
* @author xu
* @date 18-4-19 上午10:07
*/
@Service
@CacheConfig(cacheNames = "default")
public class DictionaryService {
@Autowired
DictionaryDao dictionaryDao;
/**
* 保存key value
*
* @param key
* @param value
*/
@CacheEvict(key = "targetClass + '-key-' + #key")
public void set(String key, String value) {
Dictionary dictionary = dictionaryDao.findByKey(key);
if (dictionary == null) {
dictionary = new Dictionary();
dictionary.setKey(key);
dictionary.setCreateDate(LocalDateTime.now());
}
dictionary.setValue(value);
dictionary.setUpdateDate(LocalDateTime.now());
dictionaryDao.save(dictionary);
}
/**
* 获取 value 对象
*
* @param key
* @return
*/
@Cacheable(key = "targetClass + '-key-' + #key")
public String get(String key) {
Dictionary dictionary = dictionaryDao.findByKey(key);
if (dictionary == null) {
return "";
}
return dictionary.getValue();
}
public <T> ListHandle<T> buildListHandle(String dictionaryKey,
TypeReference<List<T>> type){
return new ListHandle<>(dictionaryKey, this, type);
}
/**
* 字典的List操作形式
* @param <T>
*/
public static class ListHandle<T>{
private String dictionaryKey;
private DictionaryService dictionaryService;
private TypeReference<List<T>> type;
private ListHandle(String dictionaryKey, DictionaryService dictionaryService,
TypeReference<List<T>> type){
this.dictionaryKey = dictionaryKey;
this.dictionaryService = dictionaryService;
this.type = type;
}
public List<T> getList(){
String valueString = dictionaryService.get(dictionaryKey);
if (Strings.isNullOrEmpty(valueString)) {
return Lists.newArrayList();
}
return JSON.parseObject(valueString, type);
}
public void set(List<T> list){
dictionaryService.set(dictionaryKey, JSON.toJSONString(list));
}
public void add(T value){
List<T> ts = getList();
ts.add(value);
set(ts);
}
public T get(int index){
List<T> ts = getList();
return ts.get(index);
}
}
public <K, V> MapHandle<K, V> buildMapHandle(String dictionaryKey,
TypeReference<Map<K, V>> type){
return new MapHandle<>(dictionaryKey, this, type);
}
/**
* 字典的Map操作形式
* @param <K>
* @param <V>
*/
public static class MapHandle<K, V> {
private String dictionaryKey;
private DictionaryService dictionaryService;
private TypeReference<Map<K, V>> type;
public MapHandle(String dictionaryKey, DictionaryService dictionaryService, TypeReference<Map<K, V>> type){
this.dictionaryKey = dictionaryKey;
this.dictionaryService = dictionaryService;
this.type = type;
}
public void setMap(Map<K, V> map){
dictionaryService.set(dictionaryKey, JSON.toJSONString(map));
}
public Map<K, V> getMap(){
String valueString = dictionaryService.get(dictionaryKey);
if (Strings.isNullOrEmpty(valueString)) {
return Maps.newHashMap();
}
return JSON.parseObject(valueString, type);
}
public V get(K key){
String valueString = dictionaryService.get(dictionaryKey);
if (Strings.isNullOrEmpty(valueString)) {
return null;
}
Map<K, V> map = JSON.parseObject(valueString, type);
return map.get(key);
}
public void put(K key, V value){
String valueString = dictionaryService.get(dictionaryKey);
Map<K, V> map;
if (Strings.isNullOrEmpty(valueString)) {
map = new HashMap<>();
} else {
map = JSON.parseObject(valueString, type);
}
map.put(key, value);
setMap(map);
}
public void remove(K key){
String valueString = dictionaryService.get(dictionaryKey);
if (Strings.isNullOrEmpty(valueString)) {
return;
}
Map<K, V> map = JSON.parseObject(valueString, type);
map.remove(key);
setMap(map);
}
public List<V> getValuesToList(){
String valueString = dictionaryService.get(dictionaryKey);
if (Strings.isNullOrEmpty(valueString)) {
return Lists.newArrayList();
}
Map<K, V> map = JSON.parseObject(valueString, type);
return Lists.newArrayList(map.values());
}
}
}