package com.qmth.themis.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.themis.business.cache.ExamBreakCacheUtil;
import com.qmth.themis.business.cache.ExamRecordCacheUtil;
import com.qmth.themis.business.dao.TOeExamBreakHistoryMapper;
import com.qmth.themis.business.dto.MqDto;
import com.qmth.themis.business.entity.TOeExamBreakHistory;
import com.qmth.themis.business.enums.ExceptionEnum;
import com.qmth.themis.business.enums.MqTagEnum;
import com.qmth.themis.business.enums.VerifyExceptionEnum;
import com.qmth.themis.business.service.MqDtoService;
import com.qmth.themis.business.service.TOeExamBreakHistoryService;
import com.qmth.themis.business.util.MqUtil;
import com.qmth.themis.common.exception.BusinessException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description: 考试断点记录 服务实现类
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/25
 */
@Service
public class TOeExamBreakHistoryServiceImpl extends ServiceImpl<TOeExamBreakHistoryMapper, TOeExamBreakHistory> implements TOeExamBreakHistoryService {

    @Resource
    MqUtil mqUtil;

    @Resource
    MqDtoService mqDtoService;

    /**
     * 保存验证信息
     *
     * @param recordId
     * @param entryAuthenticationId
     * @param entryAuthenticationResult
     */
    @Override
    public void saveAuthenticationResult(Long recordId, Long entryAuthenticationId, VerifyExceptionEnum entryAuthenticationResult) {
        Long examBreakId = ExamRecordCacheUtil.getLastBreakId(recordId);
        if (examBreakId == null) {
            throw new BusinessException("未找到断点ID");
        }
        if (ExamBreakCacheUtil.getExamRecordId(examBreakId) == null) {
            throw new BusinessException("未找到断点信息");
        }
        ExamBreakCacheUtil.setEntryAuthenticationId(examBreakId, entryAuthenticationId);
        ExamBreakCacheUtil.setEntryAuthenticationResult(examBreakId, entryAuthenticationResult);
        sendExamRecordBreakHistoryDataMq(examBreakId);
    }

    /**
     * 考试断点记录数据持久化
     *
     * @param breakId
     */
    @Override
    public void examRecordBreakHistoryDataSave(Long breakId) {
        Long breakTime = ExamBreakCacheUtil.getBreakTime(breakId);
        ExceptionEnum exceptionEnum = ExamBreakCacheUtil.getBreakReason(breakId);
        String resumeReason = ExamBreakCacheUtil.getResumeReason(breakId);
        Long prepareTime = ExamBreakCacheUtil.getPrepareTime(breakId);
        Long startTime = ExamBreakCacheUtil.getStartTime(breakId);
        VerifyExceptionEnum verifyExceptionEnum = ExamBreakCacheUtil.getEntryAuthenticationResult(breakId);
        Long entryAuthenticationId = ExamBreakCacheUtil.getEntryAuthenticationId(breakId);
        Long recordId = ExamBreakCacheUtil.getExamRecordId(breakId);

        TOeExamBreakHistory tOeExamBreakHistory = new TOeExamBreakHistory();
        tOeExamBreakHistory.setId(breakId);
        tOeExamBreakHistory.setExamRecordId(recordId);
        tOeExamBreakHistory.setBreakTime(breakTime);
        tOeExamBreakHistory.setBreakReason(exceptionEnum);
        tOeExamBreakHistory.setResumeReason(resumeReason);
        tOeExamBreakHistory.setPrepareTime(prepareTime);
        tOeExamBreakHistory.setStartTime(startTime);
        tOeExamBreakHistory.setEntryAuthenticationResult(verifyExceptionEnum);
        tOeExamBreakHistory.setEntryAuthenticationId(entryAuthenticationId);
        this.saveOrUpdate(tOeExamBreakHistory);
    }

    /**
     * 发送mq更新考试断点记录
     *
     * @param breakId
     */
    @Override
    public void sendExamRecordBreakHistoryDataMq(Long breakId) {
        MqDto mqDto = new MqDto(mqUtil.getTopic(), MqTagEnum.EXAM_BREAK_HISTORY_PERSISTED.name(), breakId.toString());
        mqDtoService.assembleSendAsyncMsg(mqDto);
    }
}