
package cn.orionsec.ops.runner;

import cn.orionsec.ops.constant.app.ReleaseStatus;
import cn.orionsec.ops.dao.ApplicationReleaseDAO;
import cn.orionsec.ops.entity.domain.ApplicationReleaseDO;
import cn.orionsec.ops.task.TaskRegister;
import cn.orionsec.ops.task.TaskType;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Order(3100)
@Slf4j
public class AppReleaseTimedTaskRestoreRunner implements CommandLineRunner {

    @Resource
    private ApplicationReleaseDAO applicationReleaseDAO;

    @Resource
    private TaskRegister register;

    @Override
    public void run(String... args) {
        log.info("重新加载发布任务-开始");
        Wrapper<ApplicationReleaseDO> wrapper = new LambdaQueryWrapper<ApplicationReleaseDO>()
                .eq(ApplicationReleaseDO::getReleaseStatus, ReleaseStatus.WAIT_SCHEDULE.getStatus());
        List<ApplicationReleaseDO> releaseList = applicationReleaseDAO.selectList(wrapper);
        for (ApplicationReleaseDO release : releaseList) {
            Long id = release.getId();
            log.info("重新加载发布任务-提交 releaseId: {}", id);
            register.submit(TaskType.RELEASE, release.getTimedReleaseTime(), id);
        }
        log.info("重新加载发布任务-结束");
    }

}
