package com.wsm.common.util;

import org.joda.time.DateTime;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberUtils {
	
	private static long orderNum = 00;
	
	private static long productNum = 00;

	/**
     * 生成订单编号 
     * @return 
     */  
    public static synchronized String getOrderNo() {  
        orderNum ++;  
        long orderNo = DateTime.now().getMillis() * 10000;  
        orderNo += orderNum;
        return "O"+orderNo;  
    }
    
    /** 
     * 生成商品编号 
     * @return 
     */  
    public static synchronized String getGoodsNo() {
    	productNum ++;  
        long productNo = DateTime.now().getMillis() * 10000;  
        productNo += productNum;;  
        return "P"+productNo;  
    }


    public static synchronized String create15() {
        String strDate = new DateTime().toString("yyMMddHHmmss");
        String random = getRandom620(3);
        return strDate+random;
    }

    /**
     * 获取6-10 的随机位数数字
     * @param length	想要生成的长度
     * @return result
     */
    public static String getRandom620(Integer length) {
        String result = "";
        Random rand = new Random();
        int n = 20;
        if (null != length && length > 0) {
            n = length;
        }
        int randInt = 0;
        for (int i = 0; i < n; i++) {
            randInt = rand.nextInt(10);
            result += randInt;
        }
        return result;
    }

    //验证手机号码
    public static boolean isPhoneNumber(String number) {
        String rgx = "^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$";
        return isCorrect(rgx, number);
    }

    //验证身份证号码
    public static boolean isIdCardNumber(String number) {
        String rgx = "^\\d{15}|^\\d{17}([0-9]|X|x)$";
        return isCorrect(rgx, number);
    }

    //正则验证
    public static boolean isCorrect(String rgx, String res) {
        Pattern p = Pattern.compile(rgx);
        Matcher m = p.matcher(res);
        return m.matches();
    }

}
