package org.dromara.system.config;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import org.dromara.common.core.constant.BizConst;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Date;

/**
 * @author chenYing
 * @date 2025-07-22 10:46:19
 **/
@Configuration
public class SnowflakeConfig {
    private static RedisTemplate redisTemplate;

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

    @Bean
    public Snowflake createSnowflake() {
        Long val = redisTemplate.opsForValue().increment(BizConst.HIS_SNOW_FLAKE, 1);
        long workerId = val / 32 % 32;
        long dataCenterId = val % 32;
        return new Snowflake(DateUtil.parseDate("2025-01-01"), workerId, dataCenterId, false);
    }
}
