package com.qmth.teachcloud.report.business.templete.strategy;

import com.qmth.teachcloud.common.contant.SpringContextHolder;
import com.qmth.teachcloud.common.enums.TaskStatusEnum;
import com.qmth.teachcloud.report.business.bean.params.CalculateParams;
import com.qmth.teachcloud.report.business.entity.TBSyncTask;
import com.qmth.teachcloud.report.business.service.AnalyzeDataCalculateService;
import com.qmth.teachcloud.report.business.service.AnalyzeDataCheckService;
import com.qmth.teachcloud.report.business.service.AnalyzeForReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * @Description: 科目(试卷)计算service
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2022/6/7
 */
@Service
public class CourseCodeSyncTaskService extends CalculateTaskTemplate {
    @Resource
    private AnalyzeDataCheckService analyzeDataCheckService;
    @Resource
    private AnalyzeDataCalculateService analyzeDataCalculateService;

    private final static Logger log = LoggerFactory.getLogger(CourseCodeSyncTaskService.class);

    /**
     * 数据校验
     *
     * @param calculateParams
     * @throws IOException
     */
    @Override
    public void dataVaild(CalculateParams calculateParams) throws IOException {
        log.info("CourseCodeSyncTaskService dataVaild come in");
        TBSyncTask tbSyncTask = calculateParams.getTbSyncTask();
        tbSyncTask.setStatus(TaskStatusEnum.DATA_VALID);
        updateProgress(calculateParams);

        analyzeDataCheckService.analyzeDataCheck(calculateParams);
    }

    /**
     * 数据计算
     *
     * @param calculateParams
     * @throws IOException
     */
    @Override
    public void calculate(CalculateParams calculateParams) throws IOException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchMethodException {
        log.info("CourseCodeSyncTaskService calculate come in");
        AnalyzeForReportService analyzeForReportService = SpringContextHolder.getBean(AnalyzeForReportService.class);
        TBSyncTask tbSyncTask = calculateParams.getTbSyncTask();
        tbSyncTask.setStatus(TaskStatusEnum.RUNNING);
        updateProgress(calculateParams);

//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put(calculateParams.getCourseCode().get(0), GradeAnalyzePaperStatusEnum.CALCULATING);
//        tbSyncTask.setObj(jsonObject.toJSONString());
//        updateProgress(calculateParams);

        analyzeDataCalculateService.dataCalculateStart(calculateParams);
    }
}
