package com.qmth.themis.business.cache;

import com.qmth.themis.business.constant.SpringContextHolder;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.enums.*;
import com.qmth.themis.business.service.TOeExamRecordService;
import com.qmth.themis.business.util.RedisUtil;

import java.util.Objects;

/**
 * 考试记录缓存hash值操作
 *
 * @Description:
 * @Author: xiatian
 * @Date: 2020-07-29
 */
public class ExamRecordCacheUtil {

    private static TOeExamRecordService examRecordService = SpringContextHolder.getBean(TOeExamRecordService.class);
    private static RedisUtil redisUtil = SpringContextHolder.getBean(RedisUtil.class);

    public static Long getId(Long recordId) {
        return (Long) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.id.getCode());
    }

    public static Long getExamStudentId(Long recordId) {
        return (Long) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.exam_student_id.getCode());
    }

    public static Long getPaperId(Long recordId) {
        return (Long) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.paper_id.getCode());
    }

    public static void setFirstStartTime(Long recordId, Long date, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.first_start_time.getCode(), date);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.first_start_time.name(), date);
        }
    }

    public static void setStatus(Long recordId, ExamRecordStatusEnum status, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.status.getCode(), status);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.status.name(), status);
        }
    }

    public static Integer getDurationSeconds(Long recordId) {
        return (Integer) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.duration_seconds.getCode());
    }

    public static Double getObjectiveScore(Long recordId) {
        return (Double) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.objective_score.getCode());
    }

    public static Long getExamId(Long recordId) {
        return (Long) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.exam_id.getCode());
    }

    public static void setObjectiveScore(Long recordId, Double objectiveScore, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.objective_score.getCode(), objectiveScore);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.objective_score.name(), objectiveScore);
        }
    }

    public static void setFinishTime(Long recordId, Long finishTime, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.finish_time.getCode(), finishTime);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.finish_time.name(), finishTime);
        }
    }

    public static Long getFinishTime(Long recordId) {
        return (Long) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.finish_time.getCode());
    }

    public static void setDurationSeconds(Long recordId, Integer durationSeconds, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.duration_seconds.getCode(), durationSeconds);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.duration_seconds.name(), durationSeconds);
        }
    }

    public static void setFinishType(Long recordId, FinishTypeEnum finishType, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.finish_type.getCode(), finishType);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.finish_type.name(), finishType);
        }
    }

    public static FinishTypeEnum getFinishType(Long recordId) {
        return (FinishTypeEnum) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.finish_type.getCode());
    }

    public static Long getClientLastSyncTime(Long recordId) {
        return (Long) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.client_last_sync_time.getCode());
    }

    public static Integer getAlreadyBreakCount(Long recordId) {
        return (Integer) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.already_break_count.getCode());
    }

    public static void setAlreadyBreakCount(Long recordId, Integer alreadyBreakCount, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.already_break_count.getCode(), alreadyBreakCount);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.already_break_count.name(), alreadyBreakCount);
        }
    }

    public static ExamRecordStatusEnum getStatus(Long recordId) {
        return (ExamRecordStatusEnum) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.status.getCode());
    }

    public static Long getLastBreakId(Long recordId) {
        return (Long) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.last_break_id.getCode());
    }

    public static Long getLastBreakTime(Long recordId) {
        return (Long) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.last_break_time.getCode());
    }

    public static void setEntryAuthenticationResult(Long recordId, VerifyExceptionEnum entryAuthenticationResult, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.entry_authentication_result.getCode(), entryAuthenticationResult);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.entry_authentication_result.name(), entryAuthenticationResult);
        }
    }

    public static void setEntryAuthenticationId(Long recordId, Long entryAuthenticationId, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.entry_authentication_id.getCode(), entryAuthenticationId);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.entry_authentication_id.name(), entryAuthenticationId);
        }
    }

    public static Long getExamActivityId(Long recordId) {
        return (Long) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.exam_activity_id.getCode());
    }

    public static void setWarningCount(Long recordId, Integer warningCount, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.warning_count.getCode(), warningCount);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.warning_count.name(), warningCount);
        }
    }

    public static Integer getWarningCount(Long recordId) {
        return (Integer) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.warning_count.getCode());
    }

    public static void setBreachStatus(Long recordId, Integer breachStatus, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.breach_status.getCode(), breachStatus);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.breach_status.name(), breachStatus);
        }
    }

    public static Integer getBreachStatus(Long recordId) {
        return (Integer) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.breach_status.getCode());
    }

    public static void setInProcessLivenessVerifyCount(Long recordId, Integer inProcessLivenessVerifyCount, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.in_process_liveness_verify_count.getCode(), inProcessLivenessVerifyCount);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.in_process_liveness_verify_count.name(), inProcessLivenessVerifyCount);
        }
    }

    public static Integer getInProcessLivenessVerifyCount(Long recordId) {
        return (Integer) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.in_process_liveness_verify_count.getCode());
    }

    public static void setMonitorKey(Long recordId, String monitorKey, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.monitor_key.getCode(), monitorKey);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.monitor_key.name(), monitorKey);
        }
    }

    public static String getMonitorKey(Long recordId) {
        if (Objects.isNull(redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.monitor_key.getCode()))) {
            setMonitorKey(recordId, String.valueOf(redisUtil.getRedisSequence()), true);
        }
        return (String) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.monitor_key.getCode());
    }

    public static String getMonitorLiveUrl(Long recordId, String source) {
        return (String) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), SystemConstant.MONITOR_LIVE_URL_ + source);
    }

    public static String getMonitorLiveUrlMobileFirst(Long recordId) {
        return (String) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), SystemConstant.MONITOR_LIVE_URL_ + MonitorVideoSourceEnum.MOBILE_FIRST.name());
    }

    public static String getMonitorLiveUrlMobileSecond(Long recordId) {
        return (String) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), SystemConstant.MONITOR_LIVE_URL_ + MonitorVideoSourceEnum.MOBILE_SECOND.name());
    }

    public static String getMonitorLiveUrlClientCamera(Long recordId) {
        return (String) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), SystemConstant.MONITOR_LIVE_URL_ + MonitorVideoSourceEnum.CLIENT_CAMERA.name());
    }

    public static String getMonitorLiveUrlClientScreen(Long recordId) {
        return (String) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), SystemConstant.MONITOR_LIVE_URL_ + MonitorVideoSourceEnum.CLIENT_SCREEN.name());
    }

    public static void setMonitorLiveUrl(Long recordId, String source, String liveUrl, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), SystemConstant.MONITOR_LIVE_URL_ + source, liveUrl);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.monitor_live_url.name(), liveUrl);
        }
    }

    public static MonitorStatusSourceEnum getMonitorStatus(Long recordId, String source) {
        return (MonitorStatusSourceEnum) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), SystemConstant.MONITOR_STATUS_ + source);
    }

    public static void setMonitorStatus(Long recordId, String source, MonitorStatusSourceEnum statusSourceEnum, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), SystemConstant.MONITOR_STATUS_ + source, statusSourceEnum);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.monitor_status_source.name(), statusSourceEnum);
        }
    }

    public static MonitorCallStatusSourceEnum getMonitorCallStatus(Long recordId, String source) {
        return (MonitorCallStatusSourceEnum) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), SystemConstant.MONITOR_CALL_STATUS_ + source);
    }

    public static void setMonitorCallStatus(Long recordId, String source, MonitorCallStatusSourceEnum callStatusSourceEnum) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), SystemConstant.MONITOR_CALL_STATUS_ + source, callStatusSourceEnum);
    }

    public static WebsocketStatusEnum getClientWebsocketStatus(Long recordId) {
        return (WebsocketStatusEnum) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.client_websocket_status.getCode());
    }

    public static void setClientWebsocketStatus(Long recordId, WebsocketStatusEnum websocketStatusEnum, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.client_websocket_status.getCode(), websocketStatusEnum);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.client_websocket_status.name(), websocketStatusEnum);
        }
    }

    public static void setClientCurrentIp(Long recordId, String ip, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.client_current_ip.getCode(), ip);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.client_current_ip.name(), ip);
        }
    }

    public static void setClientWebsocketId(Long recordId, String id, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.client_websocket_id.getCode(), id);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.client_websocket_id.name(), id);
        }
    }

    public static void setClientLastSyncTime(Long recordId, Long date, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.client_last_sync_time.getCode(), date);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.client_last_sync_time.name(), date);
        }
    }

    public static void setPaperDownload(Long recordId, Integer paperDownload, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.paper_download.getCode(), paperDownload);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.paper_download.name(), paperDownload);
        }
    }

    public static void setAnswerProgress(Long recordId, Double progress, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.answer_progress.getCode(), progress);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.answer_progress.name(), progress);
        }
    }

    public static void setLastBreakId(Long recordId, Long breakId, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.last_break_id.getCode(), breakId);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.last_break_id.name(), breakId);
        }
    }

    public static void setLastBreakTime(Long recordId, Long date, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.last_break_time.getCode(), date);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.last_break_time.name(), date);
        }
    }

    public static void setLastStartTime(Long recordId, Long date, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.last_start_time.getCode(), date);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.last_start_time.name(), date);
        }
    }

    public static Long getLastStartTime(Long recordId) {
        return (Long) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.last_start_time.getCode());
    }

    public static Long getStartTime(Long recordId) {
        return (Long) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.start_time.getCode());
    }

    public static Long getEndTime(Long recordId) {
        return (Long) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.end_time.getCode());
    }

    public static Integer getOpeningSeconds(Long recordId) {
        return (Integer) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.opening_seconds.getCode());
    }

    public static Integer getMinDurationSeconds(Long recordId) {
        return (Integer) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.min_duration_seconds.getCode());
    }

    public static Integer getMaxDurationSeconds(Long recordId) {
        return (Integer) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.max_duration_seconds.getCode());
    }

    public static Integer getForceFinish(Long recordId) {
        return (Integer) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.force_finish.getCode());
    }

    public static void setStartTime(Long recordId, Long startTime, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.start_time.getCode(), startTime);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.start_time.name(), startTime);
        }
    }

    public static void setEndTime(Long recordId, Long endTime, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.end_time.getCode(), endTime);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.end_time.name(), endTime);
        }
    }

    public static void setOpeningSeconds(Long recordId, Integer openingSeconds, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.opening_seconds.getCode(), openingSeconds);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.opening_seconds.name(), openingSeconds);
        }
    }

    public static void setMinDurationSeconds(Long recordId, Integer minDurationSeconds, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.min_duration_seconds.getCode(), minDurationSeconds);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.min_duration_seconds.name(), minDurationSeconds);
        }
    }

    public static void setMaxDurationSeconds(Long recordId, Integer maxDurationSeconds, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.max_duration_seconds.getCode(), maxDurationSeconds);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.max_duration_seconds.name(), maxDurationSeconds);
        }
    }

    public static void setForceFinish(Long recordId, Integer forceFinish, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.force_finish.getCode(), forceFinish);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.force_finish.name(), forceFinish);
        }
    }

    public static void setLastPrepareTime(Long recordId, Long date, boolean update) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), ExamRecordFieldEnum.last_prepare_time.getCode(), date);
        if (update) {
            examRecordService.dataUpdateMq(recordId, ExamRecordFieldEnum.last_prepare_time.name(), date);
        }
    }

    public static void setExamFinalFinishTime(Long recordId, Long date) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), "finalFinishTime", date);
    }

    public static Long getExamFinalFinishTime(Long recordId) {
        return (Long) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), "finalFinishTime");
    }

    public static void setMultipleFaceCountErrorRandom(Long recordId, String multipleFaceCountErrorRandom) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), "multipleFaceCountErrorRandom", multipleFaceCountErrorRandom);
    }

    public static void setNoFaceCountErrorRandom(Long recordId, String noFaceCountErrorRandom) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), "noFaceCountErrorRandom", noFaceCountErrorRandom);
    }

    public static void setFaceCompareErrorRandom(Long recordId, String faceCompareErrorRandom) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), "faceCompareErrorRandom", faceCompareErrorRandom);
    }

    public static void setMultipleFaceCountErrorNum(Long recordId, Integer multipleFaceCountErrorNum) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), "multipleFaceCountErrorNum", multipleFaceCountErrorNum);
    }

    public static void setNoFaceCountErrorNum(Long recordId, Integer noFaceCountErrorNum) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), "noFaceCountErrorNum", noFaceCountErrorNum);
    }

    public static void setFaceCompareErrorNum(Long recordId, Integer faceCompareErrorNum) {
        redisUtil.set(RedisKeyHelper.examRecordCacheKey(recordId), "faceCompareErrorNum", faceCompareErrorNum);
    }

    public static String getMultipleFaceCountErrorRandom(Long recordId) {
        return (String) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), "multipleFaceCountErrorRandom");
    }

    public static String getNoFaceCountErrorRandom(Long recordId) {
        return (String) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), "noFaceCountErrorRandom");
    }

    public static String getFaceCompareErrorRandom(Long recordId) {
        return (String) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), "faceCompareErrorRandom");
    }

    public static Integer getMultipleFaceCountErrorNum(Long recordId) {
        return Objects.nonNull(redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), "multipleFaceCountErrorNum")) ? (Integer) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), "multipleFaceCountErrorNum") : 0;
    }

    public static Integer getNoFaceCountErrorNum(Long recordId) {
        return Objects.nonNull(redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), "noFaceCountErrorNum")) ? (Integer) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), "noFaceCountErrorNum") : 0;
    }

    public static Integer getFaceCompareErrorNum(Long recordId) {
        return Objects.nonNull(redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), "faceCompareErrorNum")) ? (Integer) redisUtil.get(RedisKeyHelper.examRecordCacheKey(recordId), "faceCompareErrorNum") : 0;
    }
}
