package com.chinacreator.video.util;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import com.chinacreator.video.bean.AreaInfo;
import com.chinacreator.video.mapper.CallerHeadDao;


public class AreaUtil {
	
	private static final String IP_REG = "(\\d{1,3})(?:\\.(\\d{1,3})){3}";
		
	/**AreaUtil
	 * 根据用户手机号码获取地区信息
	 * <p>
	 * @param userip 用户IP地址
	 * @return
	 */
	public static AreaInfo getAreaInfoByUserid(String userid) throws Exception {
		if(StringUtils.isNotEmpty(userid) && UsermobUtil.isValid(userid)) {
			return SpringContextUtil.getBean(CallerHeadDao.class).getAreaInfo(userid.substring(0, 7));
		}
		return null;
	}
	
	
	/**
	 * 把IP地址转变为数字串
	 * <p>
	 * 除第一段，IP段位数不足3位，前面补0
	 * @param ip
	 * @return
	 */
	public static String ip2num(String ip) {
		String[] ips = ip.split("\\.", -1);
		String numIp = ips[0];
		for(int i = 1; i < ips.length; i++) {
			numIp += String.format("%03d", NumberUtils.toInt(ips[i]));
		}
		return numIp;
	}
	
	public static void main(String[] args) throws Exception {
		String userid = "18774826258";
		AreaInfo  ai = AreaUtil.getAreaInfoByUserid(userid);
		System.out.println(ai.getArea());
	}
	
}
