package net.educoder.ecsonar.task;

import net.educoder.ecsonar.services.DbOperateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: youys
 * @Date: 2022/1/18
 * @Description: 教学课堂质量分析 查询分析结果
 */
public class SonarQueryResultRunnable implements Runnable {

    private static final Logger LOGGER = LoggerFactory.getLogger(SonarQueryResultRunnable.class);

    private static final String SUCCESS = "SUCCESS";

    private String projectName;
    private String detailId;
    private DbOperateService dbOperateService;

    public SonarQueryResultRunnable(String projectName, String detailId, DbOperateService dbOperateService) {
        this.projectName = projectName;
        this.detailId = detailId;
        this.dbOperateService = dbOperateService;
    }

    @Override
    public void run() {

        int count = 0;
        String status;
        while((status = dbOperateService.queryCeActivityStatus(projectName)) == null && count++ <= 60){
            LOGGER.info("detailId:[{}],sonar还未执行完,次数:{}", detailId, count);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {}
        }
        // 查询
        LOGGER.info("detailId:[{}], 查询sonar执行状态[{}]", detailId, status);
        // 更新状态
        dbOperateService.updateTaskInfoDetail(detailId, SUCCESS.equals(status) ? 1 : -1);

    }
}
