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

import com.qmth.teachcloud.common.contant.SpringContextHolder;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.TBTask;
import com.qmth.teachcloud.common.enums.TaskResultEnum;
import com.qmth.teachcloud.common.enums.TaskStatusEnum;
import com.qmth.teachcloud.common.service.CommonCacheService;
import com.qmth.teachcloud.common.service.TBTaskService;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.report.business.entity.TBExamCourse;
import com.qmth.teachcloud.report.business.enums.PublishStatusEnum;
import com.qmth.teachcloud.report.business.service.AnalyzeForReportService;
import com.qmth.teachcloud.report.business.service.TBExamCourseService;
import com.qmth.teachcloud.report.business.service.TBExamService;
import com.qmth.teachcloud.report.business.templete.calculate.AsyncCalculateTaskTemplete;
import com.qmth.teachcloud.report.business.templete.service.TaskCalculateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @Description:
 * @Author: CaoZixuan
 * @Date:
 */
@Service
public class AsyncDataCalculateTempleteService extends AsyncCalculateTaskTemplete {
    @Resource
    private TBExamService tbExamService;
    @Resource
    private AnalyzeForReportService analyzeForReportService;
    @Resource
    private CommonCacheService commonCacheService;
    @Resource
    private TBExamCourseService tbExamCourseService;


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

    @Override
    public Result calculateTask(Map<String, Object> map) {
        TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
        tbTask.setStatus(TaskStatusEnum.RUNNING);

        TBTaskService tbTaskService = SpringContextHolder.getBean(TBTaskService.class);
        tbTaskService.updateById(tbTask);

        TBExamCourse tbExamCourse = (TBExamCourse) map.get("tbExamCourse");
        PublishStatusEnum oldStatus =  tbExamCourse.getPublishStatus();

        String exception = null;

        try {
            TaskCalculateService taskCalculateService = SpringContextHolder.getBean(TaskCalculateService.class);
            taskCalculateService.dataCalculate(map);
            tbExamCourse.setPublishStatus(PublishStatusEnum.UN_PUBLISH);
            tbTask.setResult(TaskResultEnum.SUCCESS);
        }catch (Exception e){
            log.error(SystemConstant.LOG_ERROR, e);
            exception = e.getMessage();
            tbTask.setRemark(exception);
            tbTask.setResult(TaskResultEnum.ERROR);
            tbExamCourse.setPublishStatus(oldStatus);
        }finally {
            tbExamCourseService.updateById(tbExamCourse);
            tbTask.setStatus(TaskStatusEnum.FINISH);
            tbTaskService.updateById(tbTask);
            if (Objects.isNull(exception)){
                commonCacheService.removeExamStudentReportCache();
                commonCacheService.removeCollegeDeanCache();
                commonCacheService.removeCollegeCourseCache();
                commonCacheService.removeSurveyTeacherViewCache();
            }
        }
        return ResultUtil.ok(map);
    }
}
