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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.themis.business.bean.exam.FaceVerifyBean;
import com.qmth.themis.business.cache.FaceVerifyCacheUtil;
import com.qmth.themis.business.cache.bean.FaceVerifyCacheBean;
import com.qmth.themis.business.dao.TOeFaceVerifyHistoryMapper;
import com.qmth.themis.business.entity.TOeFaceVerifyHistory;
import com.qmth.themis.business.enums.ExamTypeEnum;
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.TOeFaceVerifyHistoryService;
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 TOeFaceVerifyHistoryServiceImpl extends ServiceImpl<TOeFaceVerifyHistoryMapper, TOeFaceVerifyHistory>
        implements TOeFaceVerifyHistoryService {

    @Resource
    TOeExamBreakHistoryService examBreakHistoryService;

    @Resource
    TOeExamRecordService examRecordService;

    @Resource
    TOeFaceVerifyHistoryMapper tOeFaceVerifyHistoryMapper;

    @Resource
    UidUtil uidUtil;

    @Override
    public FaceVerifyBean verify(Long recordId, String type, String photoUrl, Integer faceCount, Double similarity,
                                 Double realness, Long time, String exception) {
        FaceVerifyCacheBean cache = new FaceVerifyCacheBean();
        cache.setId(uidUtil.getId());
        cache.setExamRecordId(recordId);
        cache.setFaceCount(faceCount);
        cache.setException(VerifyExceptionEnum.valueOf(exception));
        cache.setPhotoUrl(photoUrl);
        cache.setRealness(realness);
        cache.setSimilarity(similarity);
        cache.setType(ExamTypeEnum.valueOf(type));
        cache.setTime(time);
        FaceVerifyCacheUtil.setFaceVerifyCacheBean(recordId, cache.getId(), cache);
        FaceVerifyBean ret = new FaceVerifyBean();
        ret.setId(cache.getId());
        ret.setUpdateTime(System.currentTimeMillis());
        if (ExamTypeEnum.RESUME_START.equals(cache.getType())) {
            examBreakHistoryService.saveAuthenticationResult(recordId, cache.getId(), cache.getException());
        } else {
            examRecordService.saveFaceVerify(cache.getType(), recordId, cache.getId(), cache.getException());
        }
        return ret;
    }

    @Transactional
    @Override
    public void save(Long id, Long recordId, String type, String photoUrl, Integer faceCount, Double similarity,
                     Double realness, Long time, String exception, String batchNo) {
        TOeFaceVerifyHistory entity = new TOeFaceVerifyHistory();
        entity.setId(id);
        entity.setExamRecordId(recordId);
        entity.setFaceCount(faceCount);
        entity.setException(VerifyExceptionEnum.valueOf(exception));
        entity.setPhotoUrl(photoUrl);
        entity.setRealness(realness);
        entity.setSimilarity(similarity);
        entity.setType(ExamTypeEnum.valueOf(type));
        entity.setTime(time);
        entity.setBatchNo(batchNo);
        saveOrUpdate(entity);
    }

    /**
     * count人脸数量异常
     *
     * @param recordId
     * @param exception
     * @param faceCount
     * @param multipleFace
     * @param type
     * @param batchNo
     * @return
     */
    @Override
    public Map<String, Object> faceCountError(Long recordId, String exception, Integer faceCount, Boolean multipleFace,
                                              String type, String batchNo) {
        return tOeFaceVerifyHistoryMapper.faceCountError(recordId, exception, faceCount, multipleFace, type, batchNo);
    }

    /**
     * count人脸比对异常
     *
     * @param recordId
     * @param exception
     * @param type
     * @param batchNo
     * @return
     */
    @Override
    public Map<String, Object> faceCompareError(Long recordId, String exception, String type, String batchNo) {
        return tOeFaceVerifyHistoryMapper.faceCompareError(recordId, exception, type, batchNo);
    }

    /**
     * 闭眼检测异常
     *
     * @param recordId
     * @param exception
     * @param type
     * @param batchNo
     * @return
     */
    @Override
    public Map<String, Object> eyeCloseError(Long recordId, String exception, String type, String batchNo) {
        return tOeFaceVerifyHistoryMapper.eyeCloseError(recordId, exception, type, batchNo);
    }

    /**
     * 真实性检测异常
     *
     * @param recordId
     * @param exception
     * @param type
     * @param batchNo
     * @return
     */
    @Override
    public Map<String, Object> realnessError(Long recordId, String exception, String type, String batchNo) {
        return tOeFaceVerifyHistoryMapper.realnessError(recordId, exception, type, batchNo);
    }
}
