package com.xhkjedu.smarking.task;

import com.xhkjedu.smarking.mapper.exam.MsExamMapper;
import com.xhkjedu.smarking.mapper.exam.MsSubjectMapper;
import com.xhkjedu.smarking.model.exam.MsSubject;
import com.xhkjedu.smarking.service.stupaper.MsPaperStudentQuestionService;
import com.xhkjedu.utils.N_Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description：考试科目状态定时任务
 * @Author: WN
 * @Date: 2024/11/15 10:10:48
 **/
@Slf4j
@Component
public class ExamSubjectTask {

    @Resource
    private MsSubjectMapper msSubjectMapper;
    @Resource
    private MsPaperStudentQuestionService msPaperStudentQuestionService;
    @Resource
    private MsExamMapper msExamMapper;

    /*
     * @Description 单科开始考试
     * @Date 2024/11/15 10:59:27
     * @Author WN
     * @Return void
     **/
    @Async("threadPoolTaskScheduler")
    @Scheduled(cron = "${cron.updateSubjectState}")
    public void startSubject() {
        int timestamp = N_Utils.getSecondTimestamp();
        String strtime = N_Utils.getStrtimeTimestamp(timestamp, "yyyy-MM-dd HH:mm");
        msSubjectMapper.updateStartSubject(strtime);
    }

    /*
     * @Description 单科结束考试
     * @Date 2024/11/15 10:59:27
     * @Author WN
     * @Return void
     **/
    @Async("threadPoolTaskScheduler")
    @Scheduled(cron = "${cron.updateSubjectState}")
    public void endSubject() {
        int timestamp = N_Utils.getSecondTimestamp();
        String strtime = N_Utils.getStrtimeTimestamp(timestamp, "yyyy-MM-dd HH:mm");
        //获取待结束科目（延后5分钟）
        List<Integer> msids = msSubjectMapper.listMsidsForEndSubject(strtime);
        if(N_Utils.isListNotEmpty(msids)){
            msSubjectMapper.updateEndSubject(timestamp, msids);
            //获取科目对应的考试下所有科目
            List<MsSubject> subjects = msSubjectMapper.listSubjectAndStateByMsids(msids);
            Map<Integer,List<MsSubject>> subjectMap = subjects.stream().collect(Collectors.groupingBy(MsSubject::getExamid));
            List<Integer> examids = new ArrayList<>();
            for(Map.Entry<Integer,List<MsSubject>> entry : subjectMap.entrySet()){
                List<MsSubject> subList = entry.getValue();
                List<MsSubject> endSubList=subList.stream().filter(ms -> ms.getMsstate() == 2).collect(Collectors.toList());
                if(!endSubList.isEmpty() && endSubList.size() == subList.size()){
                    examids.add(entry.getKey());
                }
            }
            //考试下所有科目已经结束考试，更改考试状态为已结束
            if(!examids.isEmpty()){
                msExamMapper.updateBatchExamState(examids);
            }
        }
    }

    @Scheduled(cron = "${cron.questionCheckState}")
    public void unLockQuestion() {
        // int timestamp = N_Utils.getSecondTimestamp();

    }

    /*
     * @Description 合并学生作答多图
     * @Date 2024/11/15 14:21:05
     * @Author WN
     * @Return void
     **/
    @Scheduled(cron = "${cron.mergeStupic}")
    public void paperStuQuestionAnswerMerge() {
        msPaperStudentQuestionService.paperStuQuestionAnswerMerge();
    }
}
