package ink.wgink.common.startup;

import ink.wgink.interfaces.init.IInitBaseMenu;
import ink.wgink.interfaces.init.IInitBaseTable;
import ink.wgink.interfaces.init.IInitBaseUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: CommonStartup
 * @Description: 启动
 * @Author: WangGeng
 * @Date: 2021/6/14 22:14
 * @Version: 1.0
 **/
@Component
public class CommonStartup implements ApplicationRunner {

    private ApplicationContext applicationContext;
    @Autowired(required = false)
    private IInitBaseMenu initBaseMenu;
    @Autowired(required = false)
    private IInitBaseUser initBaseUser;

    public CommonStartup(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 建表
        Map<String, IInitBaseTable> initBaseTableMap = applicationContext.getBeansOfType(IInitBaseTable.class);
        for (Map.Entry<String, IInitBaseTable> kv : initBaseTableMap.entrySet()) {
            kv.getValue().createTable();
        }
        // 初始化菜单
        if (initBaseMenu != null) {
            initBaseMenu.init();
        }
        // 创建用户
        if (initBaseUser != null) {
            initBaseUser.init();
        }
    }
}
