package cn.hmsoft.frame.util;

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

import cn.hmsoft.application.SpringHelper;
import cn.hmsoft.frame.data.model.FrameNation;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.jdbc.entity.QueryOrder;

/*************************************
 * 民族信息
 * 
 * @author: YeFeng
 * @date: 2015-11-17 22:18:22
 * @version: 1.1.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 * @Description:
 */
public abstract class FrameNationUtil {

	private static Map<Integer, FrameNation> nationMap = null;
	private static List<FrameNation> nationArray = null;

	private static void loadNation(boolean refresh) {
		if (nationMap == null || refresh) {
			synchronized (FrameNationUtil.class) {
				nationArray = SpringHelper.AppGobalDao.all(FrameNation.class, new QueryOrder("nation_id"));
				nationMap = new HashMap<Integer, FrameNation>();
				for (FrameNation n : nationArray)
					nationMap.put(n.getNation_id(), n);
			}
		}
	}

	public static Map<Integer, FrameNation> getNationMap(boolean refresh) {
		loadNation(refresh);
		return nationMap;
	}

	public static List<FrameNation> getNationArray(boolean refresh) {
		loadNation(refresh);
		return nationArray;
	}
	
	public static String getNationName(int nation_id) {
		loadNation(false);
		if(nationMap.containsKey(nation_id))
			return nationMap.get(nation_id).getNation_name();
		return "";
	}

	public static boolean checkNation(int nation_id) {
		loadNation(false);
		if (StringHelper.isEmpty(nation_id))
			throw new BusinessException("提交的民族信息为空，请检查");
		if (nationMap.containsKey(nation_id))
			return true;
		else
			throw new BusinessException("提交的民族信息不在允许的范围内，请检查");
	}
}
