package com.example.demo.order;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 简单的进程内单号生成器：前缀 + yyyyMMdd + 4位流水
 * 说明：演示用，生产建议使用数据库序列或 Redis 自增确保多实例唯一。
 */
public class OrderNumberGenerator {
    private static final ConcurrentHashMap<String, AtomicInteger> dateCounters = new ConcurrentHashMap<>();
    private static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("yyyyMMdd");

    public static String next(String prefix) {
        String day = LocalDateTime.now().format(DATE);
        String key = prefix + day;
        int seq = dateCounters.computeIfAbsent(key, k -> new AtomicInteger(0)).incrementAndGet();
        return prefix + day + String.format("%04d", seq);
    }
}


