package com.qmth.themis.task.quartz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.themis.business.cache.ExamRecordCacheUtil;
import com.qmth.themis.business.cache.RedisKeyHelper;
import com.qmth.themis.business.cache.bean.ExamActivityRecordCacheBean;
import com.qmth.themis.business.cache.bean.ExamStudentCacheBean;
import com.qmth.themis.business.entity.TEExam;
import com.qmth.themis.business.entity.TEExamActivity;
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 org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.quartz.QuartzJobBean;

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

/**
 * @Description: 非强制交卷activity任务
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/9/22
 */
public class MqActivityJob extends QuartzJobBean {
    private final static Logger log = LoggerFactory.getLogger(MqActivityJob.class);

    @Resource
    TEExamService teExamService;

    @Resource
    TEExamActivityService teExamActivityService;

    @Resource
    RedisUtil redisUtil;

    @Resource
    TEExamStudentService teExamStudentService;

    @Override
    protected void executeInternal(JobExecutionContext context) {
        log.info("mq_activity_job进来了,context:{}", context);
        QueryWrapper<TEExam> teExamQueryWrapper = new QueryWrapper<>();
        teExamQueryWrapper.lambda().eq(TEExam::getEnable, 1)
                .eq(TEExam::getForceFinish, 0);
        List<TEExam> teExamList = teExamService.list(teExamQueryWrapper);
        if (Objects.nonNull(teExamList) && teExamList.size() > 0) {
            for (TEExam exam : teExamList) {
                QueryWrapper<TEExamActivity> teExamActivityQueryWrapper = new QueryWrapper<>();
                teExamActivityQueryWrapper.lambda().eq(TEExamActivity::getEnable, 1)
                        .eq(TEExamActivity::getExamId, exam.getId());
                List<TEExamActivity> teExamActivityList = teExamActivityService.list(teExamActivityQueryWrapper);
                if (Objects.nonNull(teExamActivityList) && teExamActivityList.size() > 0) {
                    for (TEExamActivity teExamActivity : teExamActivityList) {
                        Map<String, Object> objectMap = redisUtil.getHashEntries(RedisKeyHelper.examActivityRecordCacheKey(teExamActivity.getId()));
                        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);
                                    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);
                                }
                            });
                        }
                    }
                }
            }
        }
    }
}
