package com.ruoyi.xq.kit;

import com.ruoyi.xq.constant.RedisHttpConstant;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

@Component
public class CodeKit {
    @Autowired
    private RedissonClient redissonClient;


    public String nextCode(String prefix){
        String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        String key = String.format(RedisHttpConstant.CODE_REDIS, prefix, date);
        RAtomicLong atomicLong = redissonClient.getAtomicLong(key);
        long num = atomicLong.incrementAndGet();
        atomicLong.expire(Duration.ofDays(2));
        String code = String.format("%s%s%04d", prefix, date, num);
        return code;
    }
    public String nextCodeActivity(){
        return nextCode("AC");
    }
}
