package com.crm.rely.backend.util;


import com.google.common.base.Strings;

import java.util.Date;
import java.util.Random;

/**
 * 获取随机数
 */
public class GetRandom {

    private static int randomNum = 6;
    private static int randomPasswordNum = 8;
    private static String randNumString = "0123456789";
    private static String randLowerCaseLString = "abcdefghijklmnopqrstuvwxyz";
    private static String randUpperCaseString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static String randSpecialString = "!\"#$%&'()*+,-./:;=>?@[\\]^_`{|}~";
    private static Random random = new Random();

    /**
     * 返回默认六位数字符串
     *
     * @return String
     */
    public static String getRandom() {

        return getRandom(randomNum);
    }

    /**
     * 返回指定位数字符串
     *
     * @param num 指定位数
     * @return
     */
    public static String getRandom(int num) {
        return getRandom(randNumString, num);
    }

    public static String getOrderNo(String head) {

        return getOrderNoByFormat("yyyyMMddHHmmssSSS", head);
    }

    public static String getOrderNo() {
        return getOrderNo(null);
    }

    public static synchronized String getOrderNoByFormat(String format, String head) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        String orderNo = DateUtil.format(format, new Date()) + getRandom(3);
        if (Strings.isNullOrEmpty(head)) {
            return orderNo;
        }
        return head + orderNo;
    }

    public static String getSerial(String head) {

        return getOrderNo(head);
    }

    public static String getSerial() {
        return getOrderNo(null);
    }

    /**
     * 返回指定位数字符串
     *
     * @param randString 字符串取值范围
     * @param num        获取的字符串指定位数(最后返回的长度)
     * @return
     */
    public static String getRandom(String randString, int num) {
        StringBuffer randomString = new StringBuffer(num);
        for (int i = 0; i < num; i++) {
            String rand = String.valueOf(getRandomString(randString, random.nextInt(randString
                    .length())));
            randomString.append(rand);
        }
        return randomString.toString();
    }

    /**
     * 获取随机的字符
     *
     * @param randString 字符串取值范围
     * @param num        字符串所在位置
     * @return
     */
    public static String getRandomString(String randString, int num) {
        return String.valueOf(randString.charAt(num));
    }

    public static synchronized String getRandomPasswordString() {
        return getRandomPasswordString(randomPasswordNum);
    }

    public static String getRandomPasswordString(int num) {
        int[] nums = {0, 0, 0, 0};
        StringBuffer randomString = new StringBuffer(num);
        String rand;
        for (int i = 0; i < num; i++) {
            int caseNum = -1;
            if (i >= num - 2) {
                for (int j = 0; j < 4; j++) {
                    if (nums[j] == 0) {
                        caseNum = j;
                        break;
                    }
                }
            }
            if (caseNum < 0) {
                caseNum = random.nextInt(4);
            }
            switch (caseNum) {
                case 0:
                    nums[0] = 1;
                    rand = String.valueOf(getRandomString(randNumString, random.nextInt(randNumString
                            .length())));
                    break;
                case 1:
                    nums[1] = 1;
                    rand = String.valueOf(getRandomString(randLowerCaseLString, random.nextInt(randLowerCaseLString
                            .length())));
                    break;
                case 2:
                    nums[2] = 1;
                    rand = String.valueOf(getRandomString(randUpperCaseString, random.nextInt(randUpperCaseString
                            .length())));
                    break;
                case 3:
                    nums[3] = 1;
                    rand = String.valueOf(getRandomString(randSpecialString, random.nextInt(randSpecialString
                            .length())));
                    break;
                default:
                    rand = "";
                    break;
            }
            randomString.append(rand);


        }
        return randomString.toString();
    }

    public static void main(String[] args) {
        System.out.println(GetRandom.getRandomPasswordString(8));

    }
}
