package com.qmth.themis.exam.api;

import com.qmth.themis.business.bean.exam.FaceVerifyBean;
import com.qmth.themis.business.bean.exam.FaceVerifyParamBean;
import com.qmth.themis.business.cache.ExamRecordCacheUtil;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.dto.MqDto;
import com.qmth.themis.business.enums.MqTagEnum;
import com.qmth.themis.business.service.MqDtoService;
import com.qmth.themis.business.service.TOeFaceVerifyHistoryService;
import com.qmth.themis.business.util.MqUtil;
import com.qmth.themis.business.util.ServletUtil;
import com.qmth.themis.common.enums.ExceptionResultEnum;
import com.qmth.themis.common.exception.BusinessException;
import com.qmth.themis.common.util.IpUtil;
import com.qmth.themis.common.util.Result;
import com.qmth.themis.common.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 人脸验证
 *
 * @Description:
 * @Author: xiatian
 * @Date: 2020-07-31
 */
@Api(tags = "人脸验证")
@RestController
@RequestMapping(SystemConstant.PREFIX_URL_OE + "/face")
public class TEFaceController {

    @Resource
    TOeFaceVerifyHistoryService faceVerifyHistoryService;

    @Resource
    MqDtoService mqDtoService;

    @Resource
    MqUtil mqUtil;

    @ApiOperation(value = "人脸验证结果")
    @RequestMapping(value = "/verify", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "人脸验证信息")})
    @Transactional
    public Result verify(@RequestBody FaceVerifyParamBean param) {
        if (param.getRecordId() == null) {
            throw new BusinessException(ExceptionResultEnum.RECORD_ID_IS_NULL);
        }
        if (param.getType() == null) {
            throw new BusinessException("type不能为空");
        }
        if (param.getPhotoUrl() == null) {
            throw new BusinessException("照片文件URL不能为空");
        }
        if (param.getSimilarity() == null) {
            throw new BusinessException("相似度分数不能为空");
        }
        if (param.getRealness() == null) {
            throw new BusinessException("真实性结果不能为空");
        }
        if (param.getTime() == null) {
            throw new BusinessException("验证时间不能为空");
        }
        if (param.getException() == null) {
            throw new BusinessException("exception不能为空");
        }


        FaceVerifyBean ret = faceVerifyHistoryService.verify(param.getRecordId(), param.getType(), param.getPhotoUrl(),
                param.getFaceCount(),
                param.getSimilarity(), param.getRealness(), param.getTime(), param.getException());
        Map<String, Object> transMap = new HashMap<String, Object>();
        transMap.put(SystemConstant.ID, ret.getId());
        transMap.put(SystemConstant.RECORD_ID, param.getRecordId());
        transMap.put(SystemConstant.TYPE, param.getType());
        transMap.put("photoUrl", param.getPhotoUrl());
        transMap.put("faceCount", param.getFaceCount());
        transMap.put("similarity", param.getSimilarity());
        transMap.put("realness", param.getRealness());
        transMap.put(SystemConstant.HEADER_TIME, param.getTime());
        transMap.put("exception", param.getException());
        String ip = IpUtil.getRemoteIp(ServletUtil.getRequest());
        ExamRecordCacheUtil.setClientCurrentIp(param.getRecordId(), ip);
        //mq发送消息start
        MqDto mqDto = new MqDto(mqUtil.getTopic(), MqTagEnum.FACE_VERIFY_SAVE.name(), transMap, MqTagEnum.FACE_VERIFY_SAVE, param.getRecordId().toString(), ip);
        mqDtoService.assembleSendAsyncMsg(mqDto);
        return ResultUtil.ok(ret);
    }

}
