package com.bonus.sharedstation.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SystemUtil {
	public static boolean isEmpty(String str) {
		if (null == str || "".equals(str) || "null".equalsIgnoreCase(str) || "undefined".equalsIgnoreCase(str)) {
			return true;
		}
		return false;
	}

	public static String nullToEmpty(Object obj) {

		if (obj == null) {
			return "";
		}

		return String.valueOf(obj);
	}

	public static String nullToEmpty1(Object obj) {
		if (null == obj || "".equals(obj) || "null".equalsIgnoreCase(String.valueOf(obj))
				|| "undefined".equalsIgnoreCase(String.valueOf(obj))) {
			return "";
		}
		return String.valueOf(obj);
	}
	
	public static void main(String[] args) {
		System.out.println(1111111);
		int evaluate = countAge("342425202208096572");
		System.out.println(evaluate);
	}
	

	/**
	 * 根据身份证的号码算出当前身份证持有者的年龄 idNumber 身份证号
	 * 
	 * @return 年龄
	 */
	/*public static int countAge(String idNumber) {
		idNumber = idNumber.trim();
		if (idNumber.length() != 18 && idNumber.length() != 15) {
			return 0;
		}
		String year;
		String yue;
		String day;
		if (idNumber.length() == 18) {
			year = idNumber.substring(6).substring(0, 4);// 得到年份
			yue = idNumber.substring(10).substring(0, 2);// 得到月份
			day = idNumber.substring(12).substring(0, 2);// 得到日
		} else {
			year = "19" + idNumber.substring(6, 8);// 年份
			yue = idNumber.substring(8, 10);// 月份
			day = idNumber.substring(10, 12);// 日
		}
		Date date = new Date();// 得到当前的系统时间
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String fyear = format.format(date).substring(0, 4);// 当前年份
		String fyue = format.format(date).substring(5, 7);// 月份
		String fday = format.format(date).substring(8, 10);//
		int age = 0;
		if (Integer.parseInt(yue) == Integer.parseInt(fyue)) {// 如果月份相同
			if (Integer.parseInt(day) <= Integer.parseInt(fday)) {// 说明已经过了生日或者今天是生日
				age = Integer.parseInt(fyear) - Integer.parseInt(year);
			}
		} else {

			if (Integer.parseInt(yue) < Integer.parseInt(fyue)) {
				// 如果当前月份大于出生月份
				age = Integer.parseInt(fyear) - Integer.parseInt(year);
			} else {
				// 如果当前月份小于出生月份,说明生日还没过
				age = Integer.parseInt(fyear) - Integer.parseInt(year) - 1;
			}
		}
		// System.out.println("age = " + age);
		return age;
	}*/
	/**
	 * 根据身份证号获取年龄
	 */
	public static int countAge(String sfzjh){
		sfzjh = sfzjh.trim();
        if(sfzjh == null || "".equals(sfzjh) || sfzjh.contains("*")){
            return 0;
        }

        if (sfzjh.length() != 15 && sfzjh.length() != 18){
            return 0;
        }
        int age = 0;
        Calendar cal = Calendar.getInstance();
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH)+1;
        int dayNow = cal.get(Calendar.DATE);

        int year = Integer.valueOf(sfzjh.substring(6, 10));
        int month = Integer.valueOf(sfzjh.substring(10,12));
        int day = Integer.valueOf(sfzjh.substring(12,14));

        if ((month < monthNow) || (month == monthNow && day<= dayNow) ){
            age = yearNow - year;
        }else {
            age = yearNow - year-1;
        }
        return age;
    }
	
	/**
     * 根据身份证号判断性别 奇数代表男 偶数代表女
     * @param idNumber
     * @return
     */
    public static String gender(String idNumber) {
        int gender = 0;
        if(idNumber.length() == 18){
            //如果身份证号18位，取身份证号倒数第二位
            char c = idNumber.charAt(idNumber.length() - 2);
            gender = Integer.parseInt(String.valueOf(c));
        }else{
            //如果身份证号15位，取身份证号最后一位
            char c = idNumber.charAt(idNumber.length() - 1);
            gender = Integer.parseInt(String.valueOf(c));
        }
        if(gender % 2 == 1){
            return "男";
//        	return "1";
        }else{
            return "女";
//        	return "2";
        }
    }
	
	/**
     * 根据身份证号判断性别 奇数代表男 偶数代表女
     * @param idNumber
     * @return
     */
    public static int getIntTypeGender(String idNumber) {
        int gender = 0;
        if(idNumber.length() == 18){
            //如果身份证号18位，取身份证号倒数第二位
            char c = idNumber.charAt(idNumber.length() - 2);
            gender = Integer.parseInt(String.valueOf(c));
        }else{
            //如果身份证号15位，取身份证号最后一位
            char c = idNumber.charAt(idNumber.length() - 1);
            gender = Integer.parseInt(String.valueOf(c));
        }
        if(gender % 2 == 1){
        	return 1;
        }else{
        	return 2;
        }
    }

	/**
	 * businessModule:业务模块首字母(例肠胃境为G) 生成订单号（16位）：业务模块首字母 + 时间（精确到秒）+3位随机数
	 */
	public static synchronized String getOrderNum(String businessModule) {
		String dateToStr = dateToStr(new Date(),"yyMMddHHmmss");
		return businessModule + dateToStr;
	}

    public static String dateToStr(Date dateDate, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * 截取字符串
     * 
     * @param str 字符串
     * @param start 开始
     * @param end 结束
     * @return 结果
     */
    public static String substring(final String str, int start, int end)
    {
        if (str == null)
        {
            return "";
        }

        if (end < 0)
        {
            end = str.length() + end;
        }
        if (start < 0)
        {
            start = str.length() + start;
        }

        if (end > str.length())
        {
            end = str.length();
        }

        if (start > end)
        {
            return "";
        }

        if (start < 0)
        {
            start = 0;
        }
        if (end < 0)
        {
            end = 0;
        }

        return str.substring(start, end);
    }

}
