package com.eshore.gringotts.web;

import com.blinkfox.fenix.EnableFenix;
import com.eshore.gringotts.web.domain.entity.User;
import com.eshore.gringotts.web.domain.service.UserService;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * 启动类
 *
 * @author lanyuanxiaoyao
 * @date 2024-11-14
 */
@SpringBootApplication(scanBasePackages = {"com.eshore.gringotts"})
@EnableFenix
@EnableJpaAuditing
@EnableAsync
@EnableCaching
@EnableConfigurationProperties
@EnableEncryptableProperties
public class WebApplication implements ApplicationRunner {
    private final UserService userService;

    public WebApplication(UserService userService) {
        this.userService = userService;
    }

    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 初始化系统管理员
        userService.initial();
    }

    @Bean
    public AuditorAware<User> auditorAware(UserService userService) {
        return userService::currentLoginUserOptional;
    }
}
