
package cn.orionsec.ops.runner;

import cn.orionsec.kit.lang.utils.io.Files1;
import cn.orionsec.ops.constant.app.RepositoryStatus;
import cn.orionsec.ops.dao.ApplicationRepositoryDAO;
import cn.orionsec.ops.entity.domain.ApplicationRepositoryDO;
import cn.orionsec.ops.utils.Utils;
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.io.File;
import java.util.List;

@Component
@Order(2500)
@Slf4j
public class AppRepositoryStatusCleanRunner implements CommandLineRunner {

    @Resource
    private ApplicationRepositoryDAO applicationRepositoryDAO;

    @Override
    public void run(String... args) {
        log.info("重置版本仓库状态-开始");
        // 清空初始化中的数据
        this.cleanInitializing();
        // 检查已初始化的数据
        this.checkFinished();
        log.info("重置版本仓库状态-结束");
    }

    /**
     * 清空初始化中的数据
     */
    private void cleanInitializing() {
        LambdaQueryWrapper<ApplicationRepositoryDO> wrapper = new LambdaQueryWrapper<ApplicationRepositoryDO>()
                .eq(ApplicationRepositoryDO::getRepoStatus, RepositoryStatus.INITIALIZING.getStatus());
        List<ApplicationRepositoryDO> repoList = applicationRepositoryDAO.selectList(wrapper);
        for (ApplicationRepositoryDO repo : repoList) {
            Long id = repo.getId();
            // 更新状态
            ApplicationRepositoryDO update = new ApplicationRepositoryDO();
            update.setId(id);
            update.setRepoStatus(RepositoryStatus.UNINITIALIZED.getStatus());
            applicationRepositoryDAO.updateById(update);
            // 删除文件夹
            File clonePath = new File(Utils.getRepositoryEventDir(id));
            Files1.delete(clonePath);
            log.info("重置版本仓库状态-重置 id: {}, clonePath: {}", id, clonePath);
        }
    }

    /**
     * 检查已初始化的数据
     */
    private void checkFinished() {
        LambdaQueryWrapper<ApplicationRepositoryDO> wrapper = new LambdaQueryWrapper<ApplicationRepositoryDO>()
                .eq(ApplicationRepositoryDO::getRepoStatus, RepositoryStatus.OK.getStatus());
        List<ApplicationRepositoryDO> repoList = applicationRepositoryDAO.selectList(wrapper);
        for (ApplicationRepositoryDO repo : repoList) {
            // 检查是否存在
            Long id = repo.getId();
            File clonePath = new File(Utils.getRepositoryEventDir(id));
            if (Files1.isDirectory(clonePath)) {
                continue;
            }
            // 更新状态
            ApplicationRepositoryDO update = new ApplicationRepositoryDO();
            update.setId(id);
            update.setRepoStatus(RepositoryStatus.UNINITIALIZED.getStatus());
            applicationRepositoryDAO.updateById(update);
            log.info("重置版本仓库状态-重置 id: {}, clonePath: {}", id, clonePath);
        }
    }

}
