package com.ynfy.buss.exam.job;

import com.alibaba.fastjson.JSON;
import com.ynfy.buss.exam.exam.dto.ExamSubmitDTO;
import com.ynfy.buss.exam.exam.enums.PaperState;
import com.ynfy.buss.exam.exam.service.IExamService;
import com.ynfy.buss.exam.userexam.entity.UserExam;
import com.ynfy.buss.exam.userexam.service.IUserExamService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * 超时自动交卷任务
 *
 * @author bool
 */
@Slf4j
public class BreakExamJob implements Job {

    @Autowired
    public RedisUtil redisUtil;
    @Autowired
    private IExamService examService;
    @Autowired
    private IUserExamService userExamService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDetail detail = jobExecutionContext.getJobDetail();
        String key = String.valueOf(detail.getJobDataMap().get("parameter"));
        log.info("{} 定时任务：处理到期的交卷", key);
        //获取userExamId以及paperId  19位截取
        String userExamId=key.substring(0, 19);
        String paperId=key.substring(key.length() - 19);
        UserExam userExam = userExamService.getById(userExamId);
        if (!Objects.isNull(userExam) && !userExam.getState().equals(PaperState.FINISHED)) {
            Object data = redisUtil.get(key);
            // 强制交卷
            if (!Objects.isNull(data)) {
                log.info("yyz定时任务有获取到redis缓存的答案");
                ExamSubmitDTO dto = JSON.parseObject(String.valueOf(data), ExamSubmitDTO.class);
                String biaozhi=examService.submitExamPaper(dto);
                if (biaozhi.equals("0")) {
                    System.out.println("最后一套试卷直接提交考试");
                }else {
                    //提交试卷同时开启下一套试卷
                    examService.createExamPapers(userExam.getUserId(), userExamService.getById(dto.getUserExamId()).getExamId());
                }
                return;
            }else {
                log.info("yyz定时任务未获取到redis缓存的答案");
                //直接没有做题，redis中查找不到答案时自动交卷
                ExamSubmitDTO dto =new ExamSubmitDTO(userExam.getId(),paperId, null, null);
                String biaozhi=examService.submitExamPaper(dto);
                if (biaozhi.equals("0")) {
                    System.out.println("最后一套试卷直接提交考试");
                }else {
                    //提交试卷同时开启下一套试卷
                    examService.createExamPapers(userExam.getUserId(), userExamService.getById(dto.getUserExamId()).getExamId());
                }
                return;
            }
        }
//        UserExam userExam = userExamService.getById(key);
//        if (!Objects.isNull(userExam) && !userExam.getState().equals(PaperState.FINISHED)) {
//            Object data = redisUtil.get(key);
//            // 强制交卷
//            if (!Objects.isNull(data)) {
//                ExamSubmitDTO dto = JSON.parseObject(String.valueOf(data), ExamSubmitDTO.class);
//                examService.submitExam(dto);
//                return;
//            }
//        }
        examService.deleteAutoCommitJob(key);
    }
}
