字典工具类

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ccp.admin.common.mapper.CodeDictMapper;
import com.ccp.common.component.BeanContext;
import com.ccp.common.entity.common.CodeDict;
import com.ccp.common.redis.RedisUtil;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;


public class DictUtils {
	
	private static CodeDictMapper dictDao = BeanContext.getBean(CodeDictMapper.class);

	private static RedisUtil redisUtil = (RedisUtil) BeanContext.getBean("redisUtil");

	public static final String CACHE_DICT_MAP = "dictMap";
	
	public static String getDictLabel(Integer value, String type, String defaultValue){
		if (value != null){
			for (CodeDict dict : getDictList(type)){
				if (type.equals(dict.getDictType()) && value.equals(dict.getDictCode())){
					return dict.getDictName();
				}
			}
		}
		return defaultValue;
	}
	
	public static String getDictLabels(String values, String type, String defaultValue){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(values)){
			List<String> valueList = Lists.newArrayList();
			for (String value : StringUtils.split(values, ",")){
				valueList.add(getDictLabel(Integer.valueOf(value), type, defaultValue));
			}
			return StringUtils.join(valueList, ",");
		}
		return defaultValue;
	}

	public static String getDictValue(String label, String type, String defaultLabel){
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(label)){
			for (CodeDict dict : getDictList(type)){
				if (type.equals(dict.getDictType()) && label.equals(dict.getDictCode())){
					return dict.getDictName();
				}
			}
		}
		return defaultLabel;
	}
	
	public static List<CodeDict> getDictList(String type){
		@SuppressWarnings("unchecked")
		Map<String, List<CodeDict>> dictMap = (Map<String, List<CodeDict>>) redisUtil.get(CACHE_DICT_MAP);
		if (dictMap==null||dictMap.isEmpty()){
			dictMap = Maps.newHashMap();
			for (CodeDict dict : dictDao.findAllList()){
				List<CodeDict> dictList = dictMap.get(dict.getDictType());
				if (dictList != null){
					dictList.add(dict);
				}else{
					dictMap.put(dict.getDictType(), Lists.newArrayList(dict));
				}
			}
			redisUtil.set(CACHE_DICT_MAP, dictMap);
		}
		List<CodeDict> dictList = dictMap.get(type);
		if (dictList == null){
			dictList = Lists.newArrayList();
		}
		return dictList;
	}
	
	/**
	 * 返回字典列表(JSON)
	 * @param type
	 * @return
	 */
	public static String getDictListJson(String type){
		return JSON.toJSONString(getDictList(type));
	}
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值