package com.chinacreator.video.util;

import org.springframework.util.StringUtils;

import com.chinacreator.video.bean.CPInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.CPDao;


public class ConfigUtil {
	
	/**
	 * 加密手机号码
	 * @param userid 手机号码或者伪码
	 * @param cpid
	 * @throws Exception 
	 */
	public static String encrypt(String userid, String cpid) throws Exception {
		CPInfo cpInfo = getCPInfo(cpid, null);
		return DesUtil.encode(userid, cpInfo.getNetpwd());
	}
	
	/**
	 * 解密手机号码
	 * @param userid 手机号码或者伪码
	 * @param cpid
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String userid, String cpid) throws Exception {
		CPInfo cpInfo = getCPInfo(cpid, null);
		return DesUtil.decode(userid, cpInfo.getNetpwd());
	}

	
	/**
	 * 获取CP信息
	 * <p>
	 * 如果省份不为空，则检查该省份是否开放该业务
	 * @param cpid
	 * @param province
	 * @return
	 * @throws Exception
	 */
	public static CPInfo getCPInfo(String cpid, String province) throws Exception {
		if(StringUtils.isEmpty(cpid)) {
			throw new BussinessException("7004", "CP标识为空");
		}
		CPDao cpDao = SpringContextUtil.getBean(CPDao.class);
		CPInfo cpInfo = cpDao.findById(cpid);
		if(cpInfo == null) {
			throw new BussinessException("7005", "CP标识无效");
		}
		if(cpInfo.getStatus() == 1 || cpInfo.getStatus() == 2) {
			throw new BussinessException("9002", "该业务暂未开放，敬请期待。");
		}
		return cpInfo;
	}
	
}
