package org.dromara.system.config;

import cn.hutool.core.date.format.FastDateFormat;
import cn.hutool.core.util.StrUtil;
import org.dromara.common.core.constant.BizConst;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author chenYing
 * @date 2025-06-30 17:38:53
 **/
@Component
public class RedisUtil {
    private static RedisTemplate redisTemplate;

    public RedisUtil(RedisTemplate redisTemplate) {
        RedisUtil.redisTemplate = redisTemplate;
    }

    public static String generateFoodCode() {
        String today = FastDateFormat.getInstance(BizConst.DATE_PATTERN_FOR_SIMPLE_DAY).format(new Date());
        String key = BizConst.FOOD_INGREDIENT_CODE + today;
        Long val = redisTemplate.opsForValue().increment(key, 1);
        if (Long.valueOf(1L).equals(val)) {
            redisTemplate.expire(key, 24, TimeUnit.HOURS);
        }
        return BizConst.FOOD_INGREDIENT_CODE_PREFIX + today + StrUtil.padPre(String.valueOf(val), 5, "0");
    }

    public static String generatePatientNumber( String prefix) {
        String today = FastDateFormat.getInstance(BizConst.DATE_PATTERN_FOR_SIMPLE_DAY).format(new Date());
        String key = BizConst.PATIENT_NUMBER + today;
        Long val = redisTemplate.opsForValue().increment(key, 1);
        if (Long.valueOf(1L).equals(val)) {
            redisTemplate.expire(key, 24, TimeUnit.HOURS);
        }
        return prefix + today + StrUtil.padPre(String.valueOf(val), 6, "0");
    }
}
