package xyz.luxnk.lproject;

import org.apache.commons.mail.HtmlEmail;
import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.el.opt.custom.CustomMake;
import org.nutz.integration.quartz.NutQuartzCronJobFactory;
import org.nutz.ioc.Ioc;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;
import xyz.luxnk.lproject.bean.UserInfo;
import xyz.luxnk.lproject.util.SnowflakeIdWorker;

import java.util.Date;

/**
 * 应用初始化时执行
 */
public class MainSetup implements Setup {

    @Override
    public void init(NutConfig nc) {
        Ioc ioc = nc.getIoc();
        Dao dao = ioc.get(Dao.class);
        Daos.createTablesInPackage(dao, "xyz.luxnk.lproject", false);
        CustomMake.me().register("snowflake", ioc.get(SnowflakeIdWorker.class));

        // 如果用户表中没有数据，插入一条默认数据
        if (dao.count(UserInfo.class) == 0) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUsername("Luxnk");
            userInfo.setPassword("111111");
            userInfo.setCreateTime(new Date());
            userInfo.setUpdateTime(new Date());
            dao.insert(userInfo);
        }

        // 获取NutQuartzCronJobFactory从而触发计划任务的初始化与启动
        ioc.get(NutQuartzCronJobFactory.class);
    }

    @Override
    public void destroy(NutConfig nc) {
        // 应用销毁之前要执行的东西可以写在这里

    }
}
