package com.sckw.core.utils;

import com.sckw.redis.utils.RedissonUtils;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @desc: 订单号生成工具类
 * @author: yzc
 * @date: 2023-07-12 19:10
 */
@Component
public class OrderGenerateUtils {


    /**
     * 格式化的时间字符串
     */
    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMddHHmmss");

    /**
     * redisson生成订单号 name
     */
    private static final String ORDER_NO_GENERATOR = "order_no_generator:";


    /**
     * redisson生成订单号 name（物流订单）
     */
    private static final String LOGISTICS_ORDER_NO_GENERATOR = "logistics_order_no_generator:";


    /**
     * @desc: 生成订单号
     * @author: yzc
     * @date: 2023-07-12 20:20
     * @Param prefix:
     * @return: java.lang.String
     */
    public static String generateOrderNo(String prefix) {
        String date = getNowDateStr();
        long orderId = RedissonUtils.getAtomicLong(ORDER_NO_GENERATOR + date,60L);
        return String.format("%s%s%04d", prefix, date, orderId);
    }

    /**
     * 获取当前时间年月日字符串
     *
     * @return
     */
    private static String getNowDateStr() {
        return SDF.format(new Date());
    }

}
