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

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qmth.themis.business.cache.ExamRecordCacheUtil;
import com.qmth.themis.business.cache.RedisKeyHelper;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.dto.MqDto;
import com.qmth.themis.business.enums.ExamRecordStatusEnum;
import com.qmth.themis.business.enums.MqTagEnum;
import com.qmth.themis.business.enums.MqTopicEnum;
import com.qmth.themis.business.service.CommonService;
import com.qmth.themis.business.service.MqDtoService;
import com.qmth.themis.business.service.TOeExamRecordService;
import com.qmth.themis.business.util.RedisUtil;

/**
 * @Description:
 * @Author: xiatian
 * @Date: 2020-08-04
 */
@Service
public class CommonServiceImpl implements CommonService {
	private final static Logger log = LoggerFactory.getLogger(CommonService.class);

	@Resource
	TOeExamRecordService examRecordService;
	@Resource
	RedisUtil redisUtil;

	@Resource
	MqDtoService mqDtoService;

	@Override
	public void persisted(Long recordId) {
		String lockKey = SystemConstant.REDIS_LOCK_EXAM_RECORD_PERSISTED_PREFIX + recordId;
		Boolean lock = redisUtil.lock(lockKey, SystemConstant.REDIS_CACHE_TIME_OUT);
		if (!lock) {
			log.debug("persisted doing,request ignore recordId:"+recordId);
			return;
		}

		try {
			ExamRecordStatusEnum status = ExamRecordCacheUtil.getStatus(recordId);
			if (!ExamRecordStatusEnum.FINISHED.equals(status)) {
				log.debug("status error ,status:"+status.name()+" recordId:"+recordId);
				return;
			}
			Double score = ExamRecordCacheUtil.getObjectiveScore(recordId);
			if (score == null) {
				log.debug("score is null ,recordId:"+recordId);
				//算分未完成的 发送10秒延迟消息
				Map<String, Object> transMap = new HashMap<String, Object>();
				transMap.put("recordId", recordId);
				String level = "10s";
				Integer time = SystemConstant.mqDelayLevel.get(level);
				LocalDateTime dt = LocalDateTime.now();
				dt = dt.plusSeconds(Long.parseLong(level.replace("s", "")));
				Map<String, Object> propMap = new HashMap<String, Object>();
				propMap.put("timeOut", time);
				propMap.put("mqExecTime", dt.toInstant(ZoneOffset.of("+8")).toEpochMilli());
				MqDto mqDto = new MqDto(MqTopicEnum.THEMIS_TOPIC.getCode(), MqTagEnum.EXAM_RECORD_PERSISTED.name(),
						transMap, MqTagEnum.EXAM_RECORD_PERSISTED, recordId.toString(), propMap, recordId.toString());

				mqDtoService.assembleSendAsyncDelayMsg(mqDto);

				return;
			}

			log.debug("start persisted,recordId:"+recordId);
			examRecordService.saveDataByCache(recordId);
			// 清除缓存
//			ExamActivityRecordCacheUtil.removeActivityRecordCache(ExamRecordCacheUtil.getExamActivityId(recordId),
//					recordId);
			redisUtil.delete(RedisKeyHelper.examRecordCacheKey(recordId));
			redisUtil.delete(RedisKeyHelper.examAnswerKey(recordId));
			redisUtil.delete(RedisKeyHelper.audioLeftPlayCountKey(recordId));
			redisUtil.delete(RedisKeyHelper.livenessVerifyCacheKey(recordId));
			redisUtil.delete(RedisKeyHelper.faceVerifyCacheKey(recordId));
			log.debug("end persisted,recordId:"+recordId);
		} finally {
			redisUtil.releaseLock(lockKey);
		}
	}

}
