package com.xingxi.common.config;

import com.xingxi.common.utils.uuid.IdWorker;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

@Data
@Configuration
@EnableConfigurationProperties(IdWorkCfg.class)
@ConfigurationProperties("idworkcfg")
public class IdWorkCfg {
    /**
     * 日志对象
     */
    private static final Logger logger = LoggerFactory.getLogger(IdWorkCfg.class);
    /**
     * 服务器id
     */
    private Long workerId;
    /**
     * 数据中以为id
     */
    private Long dataCenterId;

    /**
     * 得到db=1的RedisDao
     */
    @Bean
    public IdWorker getIdWorker() {
        logger.info("idWork配置：workerId={},dataCenterId={}", workerId, dataCenterId);
        Assert.notNull(workerId, "workerId不能为空");
        Assert.notNull(dataCenterId, "dataCenterId不能为空");
        return new IdWorker(workerId, dataCenterId);
    }
}
