All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.acyou.leo.framework.util.IdUtil Maven / Gradle / Ivy

package cn.acyou.leo.framework.util;

import cn.acyou.leo.framework.commons.SnowFlake;
import cn.acyou.leo.framework.exception.ServiceException;
import cn.acyou.leo.framework.util.redis.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.util.UUID;

/**
 * 统一的ID生成工具
 *
 * @author youfang
 * @version [1.0.0, 2020-7-24 下午 08:04]
 **/
@Component
public class IdUtil {
    private static RedisUtils redisUtils;
    private static SnowFlake snowFlake;

    private IdUtil(){

    }

    @Autowired(required = false)
    public void setRedisUtils(RedisUtils redisUtils) {
        IdUtil.redisUtils = redisUtils;
    }

    @Autowired(required = false)
    public void setSnowFlake(SnowFlake snowFlake) {
        IdUtil.snowFlake = snowFlake;
    }

    private static SnowFlake checkGetSnowFlake(){
        if (snowFlake == null) {
            throw new ServiceException("ID生成失败,请先配置SnowFlake!");
        }
        return snowFlake;
    }

    private static RedisUtils checkGetRedisUtil(){
        if (redisUtils == null) {
            throw new ServiceException("ID生成失败,请先配置Redis!");
        }
        return redisUtils;
    }

    /**
     * 雪花算法 nextId
     *
     * @return 字符串
     */
    public static long nextId() {
        return checkGetSnowFlake().nextId();
    }

    /**
     * 雪花算法 nextIdStr
     *
     * @return 字符串
     */
    public static String nextIdStr() {
        return checkGetSnowFlake().nextIdStr();
    }
    /**
     * 雪花算法 nextIdPrefix
     *
     * @return 字符串
     */
    public static String nextIdPrefix(String prefix) {
        return checkGetSnowFlake().nextIdPrefix(prefix);
    }

    /**
     * UUID的字符串
     *
     * @return 字符串
     */
    public static String uuidStr() {
        return UUID.randomUUID().toString();
    }

    /**
     * 没有-的UUID字符串
     *
     * @return 字符串
     */
    public static String uuidStrWithoutLine() {
        return UUID.randomUUID().toString().replace("-", "");
    }


    /**
     * 创建MongoDB ID生成策略实现
* ObjectId由以下几部分组成: * *
     * 1. Time 时间戳。
     * 2. Machine 所在主机的唯一标识符,一般是机器主机名的散列值。
     * 3. PID 进程ID。确保同一机器中不冲突
     * 4. INC 自增计数器。确保同一秒内产生objectId的唯一性。
     * 
*

* 参考:http://blog.csdn.net/qxc1281/article/details/54021882 * * @return ObjectId */ public static String objectId() { return ObjectId.next(); } /** * 公共获取单号 *

     *  getDatePrefixId("RK")    =   RK2020072700001
     * 
* * @param prefix 前缀 * @return 单号 */ public static String getDatePrefixId(String prefix) { return getDatePrefixId(prefix, 5); } /** * 从Redis中获取ID *

* prefix + 20200724 + 00001 * * @param prefix 前缀 * @param length 长度 * @return ID */ public static String getDatePrefixId(String prefix, int length) { String formatDate = DateUtil.getCurrentDateShortFormat(); String key = "SEQ:" + formatDate; //KEY: SEQ:20200724 timeOut:36小时(36 * 60 * 60),销毁一天半之前的Key Long increment = checkGetRedisUtil().increment(key, 1, 129600); long maxV = MathUtil.createMaxLong(length); if (increment > maxV) { throw new ServiceException("ID获取错误:超出最大值!"); } DecimalFormat df = new DecimalFormat(StringUtils.concatLengthChar(length, '0')); return prefix + formatDate + df.format(increment); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy