package cn.hmsoft.frame.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.frame.data.model.FrameDict;

public class FrameDictUtil {

	private static Map<String, List<FrameDict>> mapConfig = null;
	private static Map<String, FrameDict> mapValueConfig = null;
	private static List<FrameDict> listConfig = null;

	public static List<FrameDict> allDict() {
		return allDict(false);
	}

	public static List<FrameDict> allDict(boolean refresh) {
		if (mapConfig == null || refresh) {
			synchronized (FrameDictUtil.class) {
				if (mapConfig == null || refresh) {
					Map<String, List<FrameDict>> tempmapConfig = new HashMap<String, List<FrameDict>>();
					Map<String, FrameDict> tempmapValueConfig = new HashMap<String, FrameDict>();
					String sql = "select * from frame_dict order by dict_name,dict_order";
					listConfig = SpringHelper.AppGobalDao.listBySql(FrameDict.class, sql);
					for (FrameDict fd : listConfig) {
						tempmapValueConfig.put(fd.getDict_name() + "_" + fd.getDict_value(), fd);
						if (!tempmapConfig.containsKey(fd.getDict_name())) {
							tempmapConfig.put(fd.getDict_name() , new ArrayList<FrameDict>());
						}
						tempmapConfig.get(fd.getDict_name()).add(fd);
					}
					mapConfig = tempmapConfig;
					mapValueConfig = tempmapValueConfig;
				}

			}
		}
		return listConfig;

	}

	public static List<FrameDict> getFrameDict(String dictName) {
		allDict();
		if (mapConfig.containsKey(dictName))
			return mapConfig.get(dictName);
		return new ArrayList<FrameDict>();
	}

	public static FrameDict getFrameDict(String dictName, String dictValue) {
		allDict();
		if (mapValueConfig.containsKey(dictName + "_" + dictValue))
			return mapValueConfig.get(dictName + "_" + dictValue);
		return null;
	}

	public static String getFrameDictText(String dictName, String dictValue) {
		FrameDict fd = getFrameDict(dictName, dictValue);
		if (fd != null)
			return fd.getDict_text();
		return null;
	}
}
