package com.ynfy.buss.exam.job;

import com.ynfy.buss.exam.exam.service.IExamService;
import com.ynfy.buss.exam.examstatistics.service.IExamStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 考试成绩统计任务
 */
@Slf4j
public class ExamStatisticsJob implements Job {

    @Autowired
    private IExamStatisticsService examStatisticsService;

    @Autowired
    private IExamService examService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            log.info("考试成绩统计任务执行开始......");
            //获取正在进行中的考试
            List<String> examIdList = examService.listExamIng();
            if (!CollectionUtils.isEmpty(examIdList)) {
                //考试结果统计
                examIdList.stream().forEach(examId -> examStatisticsService.calcExamDimension(examId));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("考试成绩统计任务异常：{}", e.getMessage());
        }
    }
}
