package com.fufan.mtsaas.util;

import java.util.Random;

public class PublicMethod {

    private static volatile long SerialNum = 0L;
    public synchronized static long getSerialID() {
        long num;
        do {
            num = System.currentTimeMillis();
        } while (SerialNum == num);
        return SerialNum = num;
    }
    private static final String[] X36_ARRAY = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z".split(",");
    private static final String[] RANDOM_ARRAY = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z".split(",");

    public static String tenTo36(Long num) {
        StringBuffer sBuffer = new StringBuffer();

        if(num == 0) {
            sBuffer.append("0");
        }
        while(num > 0) {
            sBuffer.append(X36_ARRAY[(int) (num % 36)]);
            num = num / 36;
        }

        return sBuffer.reverse().toString();
    }

    private static String getRandomAddData(){
        Random r = new Random();
       return  RANDOM_ARRAY[r.nextInt(RANDOM_ARRAY.length)];
    }

    public static String getRandomData(String brokerId,String no,Integer length){
        String aa = getSerialID() + no;
        String result = tenTo36(Long.valueOf(aa));
        result = brokerId + result;
        long random = length - result.length() -1;
        StringBuilder stringBuilder = new StringBuilder();
        if(random > 0){
            for (int i= 0; i < random ;i++){
                stringBuilder.append(getRandomAddData());
            }
            result = result + stringBuilder;
        }
         return result;
    }




//    public static void main(String[] args) {
//        System.out.println(System.nanoTime());
//    }
public static void main(String[] args) throws InterruptedException {
    for (;;){
        System.out.println(getRandomData("1","",0));
    }
  }
}
