package com.storlead.framework.common.util;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.util.Assert;

import java.util.Random;

/**
 * 随机数工具生成类
 *
 * @author blank
 * @since 2018/5/17 上午10:36
 */
public class RandomCodeUtil {


    /**
     * 生成4位数随机验证码
     */
    public static int getCheckCode() {
        Random random = new Random(System.currentTimeMillis());
        int num = random.nextInt(9999) % 10000;
        return num < 1000 ? 1000 + num : num;
    }

    /**
     * 返回 1000-9999之间的4位随机数
     *
     * @return 1000-9999之间的4位随机数
     * @author blank
     * @since 2018/5/17 上午10:42
     */
    public static int RandomCheckCode() {
        int num = RandomUtils.nextInt(0, 9999);
        return num < 1000 ? 1000 + num : num;
    }

    /**
     * 返回指定[1~9]位数的随机数
     *
     * @param numLength 位数
     * @return 指定位数的随机数
     * @author blank
     * @since 2018/5/17 上午10:49
     */
    public static int randomCode(int numLength) {
        Assert.isTrue(numLength >= 1 && numLength <= 9, " Due the Integer Number limit the numLength can neither smaller than 1 nor larger than 9!");

        int startNum;
        int endNum;
        switch (numLength) {

            case 1:
                startNum = 0;
                endNum = 9;
                break;
            case 2:
                startNum = 10;
                endNum = 99;
                break;
            case 3:
                startNum = 100;
                endNum = 999;
                break;
            case 5:
                startNum = 10000;
                endNum = 99999;
                break;
            case 6:
                startNum = 100000;
                endNum = 999999;
                break;
            case 7:
                startNum = 1000000;
                endNum = 9999999;
                break;
            case 8:
                startNum = 10000000;
                endNum = 99999999;
                break;
            case 9:
                startNum = 100000000;
                endNum = 999999999;
                break;

            default:
                startNum = 1000;
                endNum = 9999;
                break;
        }

        return RandomUtils.nextInt(startNum, endNum);
    }


    public static void main(String[] args) {
        System.out.println(randomCode(19));

    }
}
