package com.wms_main.repository.utils;

import com.wms_main.constant.AppConstant;

/**
 * 唯一识别号工具类
 */
public class UUIDUtils {


    private static long lastTime = 0L;
    private static short lastCount = 0;

    /**
     * 生成唯一识别号
     * @return 生成的唯一号码
     */
    public synchronized static String getNewUUID() {
        return getNewUUID("");
    }

    /**
     * 生成唯一识别号
     * @return 生成的唯一号码
     */
    public synchronized static String getNewUUID(String code) {
        /* 经测试，单线程情况下生成UUID一万个约为50毫秒，不存在重复的UUID */
        while (true) {
            long thisTime = System.currentTimeMillis();
            if(lastTime == thisTime) {
                lastCount ++;
                if(lastCount > 9999) {
                    lastCount = 10000;
                    continue;
                }
            } else {
                lastTime = thisTime;
                lastCount = 0;
            }
            return code + thisTime + String.format("%02d", AppConstant.APP_ID) + String.format("%04d", lastCount);
        }
    }
}
