package com.rj.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.rj.config.AppConfig;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
 * 时区工具类
 * 提供统一的时区相关工具方法
 * 
 * @author Auto Generated
 * @since 2025-01-01
 */
@Component
public class TimeZoneUtils {
    
    private static AppConfig appConfig;
    
    @Autowired
    public void setAppConfig(AppConfig appConfig) {
        TimeZoneUtils.appConfig = appConfig;
    }
    
    /**
     * 获取应用配置的时区ID
     * 
     * @return ZoneId
     */
    public static ZoneId getZoneId() {
        String timezone = appConfig != null ? appConfig.getTimezone() : "Asia/Shanghai";
        return ZoneId.of(timezone);
    }
    
    /**
     * 获取当前时区的当前时间
     * 
     * @return LocalDateTime
     */
    public static LocalDateTime now() {
        return ZonedDateTime.now(getZoneId()).toLocalDateTime();
    }
    
    /**
     * 将指定时区的时间转换为应用时区的LocalDateTime
     * 
     * @param zonedDateTime 带时区的时间
     * @return LocalDateTime
     */
    public static LocalDateTime toLocalDateTime(ZonedDateTime zonedDateTime) {
        if (zonedDateTime == null) {
            return null;
        }
        return zonedDateTime.withZoneSameInstant(getZoneId()).toLocalDateTime();
    }
}

