package com.qmth.themis.task.quartz.service.impl;

import com.qmth.themis.business.cache.ExamActivityRecordCacheUtil;
import com.qmth.themis.business.cache.ExamRecordCacheUtil;
import com.qmth.themis.business.cache.RedisKeyHelper;
import com.qmth.themis.business.cache.bean.ExamActivityCacheBean;
import com.qmth.themis.business.cache.bean.ExamActivityRecordCacheBean;
import com.qmth.themis.business.cache.bean.ExamCacheBean;
import com.qmth.themis.business.cache.bean.ExamStudentCacheBean;
import com.qmth.themis.business.enums.ExamRecordStatusEnum;
import com.qmth.themis.business.enums.FinishTypeEnum;
import com.qmth.themis.business.service.TEExamActivityService;
import com.qmth.themis.business.service.TEExamService;
import com.qmth.themis.business.service.TEExamStudentService;
import com.qmth.themis.business.util.RedisUtil;
import com.qmth.themis.task.quartz.service.QuartzLogicService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description: quartz逻辑 impl
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/7/31
 */
@Service
public class QuartzLogicServiceImpl implements QuartzLogicService {
    private final static Logger log = LoggerFactory.getLogger(QuartzLogicServiceImpl.class);

    @Resource
    TEExamActivityService teExamActivityService;

    @Resource
    TEExamService teExamService;

    @Resource
    RedisUtil redisUtil;

    @Resource
    TEExamStudentService teExamStudentService;

    /**
     * 考试场次quartz逻辑
     *
     * @param key
     */
    @Override
    @Transactional
    public void execExamActivityJobLogic(String key) {
        log.info("execExamActivityJobLogic key:{}", key);
        String[] strings = key.split("\\.");
        ExamActivityCacheBean examActivityCacheBean = teExamActivityService.getExamActivityCacheBean(Long.parseLong(strings[1]));
        ExamCacheBean examCacheBean = teExamService.getExamCacheBean(examActivityCacheBean.getExamId());
        if (Objects.nonNull(examCacheBean) && examCacheBean.getEnable() == 1 && Objects.nonNull(examActivityCacheBean) && examActivityCacheBean.getEnable() == 1) {
            Integer forceFinish = examCacheBean.getForceFinish();
            Map<String, Object> objectMap = redisUtil.getHashEntries(RedisKeyHelper.examActivityRecordCacheKey(examActivityCacheBean.getId()));
            if (Objects.nonNull(forceFinish) && forceFinish.intValue() == 1) {//强制收卷
                if (Objects.nonNull(objectMap) && objectMap.size() > 0) {
                    objectMap.forEach((k, v) -> {
                        ExamActivityRecordCacheBean examActivityRecordCacheBean = (ExamActivityRecordCacheBean) v;
                        ExamRecordStatusEnum examRecordStatusEnum = examActivityRecordCacheBean.getStatus();
                        //获取该考试批次下所有未交卷的考生的考试记录
                        if (Objects.nonNull(examRecordStatusEnum) && !Objects.equals(ExamRecordStatusEnum.FINISHED, examRecordStatusEnum) && !Objects.equals(ExamRecordStatusEnum.PERSISTED, examRecordStatusEnum)) {
                            Long recordId = Long.parseLong(k);
                            Long recordEndTime = ExamRecordCacheUtil.getEndTime(recordId);
                            if (Objects.nonNull(examActivityCacheBean.getFinishTime()) && Objects.nonNull(recordEndTime) && examActivityCacheBean.getFinishTime().longValue() == recordEndTime.longValue()) {
                                Integer durationSeconds = ExamRecordCacheUtil.getDurationSeconds(recordId);
                                Long examStudentId = ExamRecordCacheUtil.getExamStudentId(recordId);
                                ExamStudentCacheBean examStudentCacheBean = teExamStudentService.getExamStudentCacheBean(examStudentId);
                                //交卷
                                teExamService.finish(examStudentCacheBean.getStudentId(), Long.parseLong(k), FinishTypeEnum.AUTO.name(), durationSeconds);
                                // 清除缓存
//                                ExamActivityRecordCacheUtil.removeActivityRecordCache(ExamRecordCacheUtil.getExamActivityId(recordId), recordId);
                            }
                        }
                    });
                }
            } else {
                if (Objects.nonNull(objectMap) && objectMap.size() > 0) {
                    objectMap.forEach((k, v) -> {
                        ExamActivityRecordCacheBean examActivityRecordCacheBean = (ExamActivityRecordCacheBean) v;
                        ExamRecordStatusEnum examRecordStatusEnum = examActivityRecordCacheBean.getStatus();
                        //获取该考试批次下所有未交卷的考生的考试记录
                        if (Objects.nonNull(examRecordStatusEnum) && !Objects.equals(ExamRecordStatusEnum.FINISHED, examRecordStatusEnum) && !Objects.equals(ExamRecordStatusEnum.PERSISTED, examRecordStatusEnum) && !Objects.equals(ExamRecordStatusEnum.ANSWERING, examRecordStatusEnum)) {
                            Long recordId = Long.parseLong(k);
                            Long recordEndTime = ExamRecordCacheUtil.getEndTime(recordId);
                            if (Objects.nonNull(examActivityCacheBean.getFinishTime()) && Objects.nonNull(recordEndTime) && examActivityCacheBean.getFinishTime().longValue() == recordEndTime.longValue()) {
                                Integer durationSeconds = ExamRecordCacheUtil.getDurationSeconds(recordId);
                                Long examStudentId = ExamRecordCacheUtil.getExamStudentId(recordId);
                                ExamStudentCacheBean examStudentCacheBean = teExamStudentService.getExamStudentCacheBean(examStudentId);
                                //交卷
                                teExamService.finish(examStudentCacheBean.getStudentId(), Long.parseLong(k), FinishTypeEnum.AUTO.name(), durationSeconds);
                                // 清除缓存
//                                ExamActivityRecordCacheUtil.removeActivityRecordCache(ExamRecordCacheUtil.getExamActivityId(recordId), recordId);
                            }
                        }
                    });
                }
            }
        }
    }

    /**
     * 考生quartz逻辑
     *
     * @param key
     */
    @Override
    public void execExamStudentJobLogic(String key) {
        log.info("execExamStudentJobLogic key:{}", key);
        String[] strings = key.split("\\.");
        String[] exams = strings[1].split("-");
        ExamStudentCacheBean examStudentCacheBean = teExamStudentService.getExamStudentCacheBean(Long.parseLong(exams[0]));
        Long recordId = Long.parseLong(exams[1]);
        ExamRecordStatusEnum examRecordStatusEnum = ExamRecordCacheUtil.getStatus(recordId);
        ExamActivityCacheBean examActivityCacheBean = teExamActivityService.getExamActivityCacheBean(examStudentCacheBean.getExamActivityId());
        //获取该考试批次下所有未交卷的考生的考试记录
        if (Objects.nonNull(examRecordStatusEnum) && !Objects.equals(ExamRecordStatusEnum.FINISHED, examRecordStatusEnum) && !Objects.equals(ExamRecordStatusEnum.FINISHED, examRecordStatusEnum) && !Objects.equals(ExamRecordStatusEnum.ANSWERING, examRecordStatusEnum)) {
            Long recordEndTime = ExamRecordCacheUtil.getEndTime(recordId);
            if (examActivityCacheBean.getFinishTime() == recordEndTime) {
                Integer durationSeconds = ExamRecordCacheUtil.getDurationSeconds(recordId);
                //交卷
                teExamService.finish(examStudentCacheBean.getStudentId(), recordId, FinishTypeEnum.AUTO.name(), durationSeconds);
            }
        }
    }
}
