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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.themis.business.bean.exam.LivenessVerifyBean;
import com.qmth.themis.business.cache.LivenessVerifyCacheUtil;
import com.qmth.themis.business.cache.bean.LivenessVerifyCacheBean;
import com.qmth.themis.business.dao.TOeLivenessVerifyHistoryMapper;
import com.qmth.themis.business.entity.TOeLivenessVerifyHistory;
import com.qmth.themis.business.enums.LivenessTypeEnum;
import com.qmth.themis.business.enums.VerifyExceptionEnum;
import com.qmth.themis.business.service.TOeExamBreakHistoryService;
import com.qmth.themis.business.service.TOeExamRecordService;
import com.qmth.themis.business.service.TOeLivenessVerifyHistoryService;
import com.qmth.themis.business.util.UidUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description: 活体验证记录 服务实现类
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/25
 */
@Service
public class TOeLivenessVerifyHistoryServiceImpl
        extends ServiceImpl<TOeLivenessVerifyHistoryMapper, TOeLivenessVerifyHistory>
        implements TOeLivenessVerifyHistoryService {

    @Resource
    TOeExamBreakHistoryService examBreakHistoryService;

    @Resource
    TOeExamRecordService examRecordService;

    @Resource
    UidUtil uidUtil;

    @Resource
    TOeLivenessVerifyHistoryMapper tOeLivenessVerifyHistoryMapper;

    @Override
    public LivenessVerifyBean verify(Long recordId, String type, String actions, Integer retry, Long startTime,
                                     Long finishTime, String exception) {
        LivenessVerifyCacheBean cache = new LivenessVerifyCacheBean();
        cache.setId(uidUtil.getId());
        cache.setExamRecordId(recordId);
        cache.setType(LivenessTypeEnum.valueOf(type));
        cache.setActions(actions);
        cache.setRetry(retry);
        cache.setStartTime(startTime);
        cache.setFinishTime(finishTime);
        cache.setException(VerifyExceptionEnum.valueOf(exception));
        LivenessVerifyCacheUtil.setLivenessVerifyCacheBean(recordId, cache.getId(), cache);
        LivenessVerifyBean ret = new LivenessVerifyBean();
        ret.setId(cache.getId());
        ret.setUpdateTime(System.currentTimeMillis());
        if (LivenessTypeEnum.RESUME_START.equals(cache.getType())) {
            examBreakHistoryService.saveAuthenticationResult(recordId, cache.getId(), cache.getException());
        } else {
            examRecordService.saveLivenessVerify(cache.getType(), recordId, cache.getId(), cache.getException());
        }
        return ret;
    }

    @Transactional
    @Override
    public void save(Long id, Long recordId, String type, String actions, Integer retry, Long startTime,
                     Long finishTime, String exception, String batchNo) {
        TOeLivenessVerifyHistory entity = new TOeLivenessVerifyHistory();
        entity.setId(id);
        entity.setExamRecordId(recordId);
        entity.setType(LivenessTypeEnum.valueOf(type));
        entity.setActions(actions);
        entity.setRetry(retry);
        entity.setStartTime(startTime);
        entity.setFinishTime(finishTime);
        entity.setException(VerifyExceptionEnum.valueOf(exception));
        entity.setBatchNo(batchNo);
        saveOrUpdate(entity);
    }

    /**
     * 活检动作错误异常
     *
     * @param recordId
     * @param exception
     * @param type
     * @param batchNo
     * @return
     */
    @Override
    public Map<String, Object> livenessActionError(Long recordId, String exception, String type, String batchNo) {
        return tOeLivenessVerifyHistoryMapper.livenessActionError(recordId, exception, type, batchNo);
    }
}
