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 *

* 字典 * * @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 ListHandle buildListHandle(String dictionaryKey, TypeReference> type){ return new ListHandle<>(dictionaryKey, this, type); } /** * 字典的List操作形式 * @param */ public static class ListHandle{ private String dictionaryKey; private DictionaryService dictionaryService; private TypeReference> type; private ListHandle(String dictionaryKey, DictionaryService dictionaryService, TypeReference> type){ this.dictionaryKey = dictionaryKey; this.dictionaryService = dictionaryService; this.type = type; } public List getList(){ String valueString = dictionaryService.get(dictionaryKey); if (Strings.isNullOrEmpty(valueString)) { return Lists.newArrayList(); } return JSON.parseObject(valueString, type); } public void set(List list){ dictionaryService.set(dictionaryKey, JSON.toJSONString(list)); } public void add(T value){ List ts = getList(); ts.add(value); set(ts); } public T get(int index){ List ts = getList(); return ts.get(index); } } public MapHandle buildMapHandle(String dictionaryKey, TypeReference> type){ return new MapHandle<>(dictionaryKey, this, type); } /** * 字典的Map操作形式 * @param * @param */ public static class MapHandle { private String dictionaryKey; private DictionaryService dictionaryService; private TypeReference> type; public MapHandle(String dictionaryKey, DictionaryService dictionaryService, TypeReference> type){ this.dictionaryKey = dictionaryKey; this.dictionaryService = dictionaryService; this.type = type; } public void setMap(Map map){ dictionaryService.set(dictionaryKey, JSON.toJSONString(map)); } public Map 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 map = JSON.parseObject(valueString, type); return map.get(key); } public void put(K key, V value){ String valueString = dictionaryService.get(dictionaryKey); Map 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 map = JSON.parseObject(valueString, type); map.remove(key); setMap(map); } public List getValuesToList(){ String valueString = dictionaryService.get(dictionaryKey); if (Strings.isNullOrEmpty(valueString)) { return Lists.newArrayList(); } Map map = JSON.parseObject(valueString, type); return Lists.newArrayList(map.values()); } } }