package com.qmth.themis.exam.api;

import com.qmth.themis.business.bean.exam.LivenessVerifyBean;
import com.qmth.themis.business.bean.exam.LivenessVerifyParamBean;
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.TOeLivenessVerifyHistoryService;
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-08-03
 */
@Api(tags = "活体验证")
@RestController
@RequestMapping(SystemConstant.PREFIX_URL_OE + "/liveness")
public class TELivenessController {

    @Resource
    TOeLivenessVerifyHistoryService livenessVerifyHistoryService;

    @Resource
    MqDtoService mqDtoService;

    @Resource
    MqUtil mqUtil;

    @ApiOperation(value = "活体验证结果")
    @RequestMapping(value = "/verify", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "活体验证信息")})
    @Transactional
    public Result verify(@RequestBody LivenessVerifyParamBean param) {
        if (param.getRecordId() == null) {
            throw new BusinessException(ExceptionResultEnum.RECORD_ID_IS_NULL);
        }
        if (param.getType() == null) {
            throw new BusinessException("type不能为空");
        }
        if (param.getActions() == null) {
            throw new BusinessException("动作详情不能为空");
        }
        if (param.getRetry() == null) {
            throw new BusinessException("整体第几次重试不能为空");
        }
        if (param.getStartTime() == null) {
            throw new BusinessException("验证开始时间不能为空");
        }
        if (param.getFinishTime() == null) {
            throw new BusinessException("验证结束时间不能为空");
        }
        if (param.getException() == null) {
            throw new BusinessException("验证异常不能为空");
        }

        LivenessVerifyBean ret = livenessVerifyHistoryService.verify(param.getRecordId(), param.getType(),
                param.getActions(), param.getRetry(), param.getStartTime(), param.getFinishTime(),
                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("actions", param.getActions());
        transMap.put("retry", param.getRetry());
        transMap.put("startTime", param.getStartTime());
        transMap.put("finishTime", param.getFinishTime());
        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.LIVENESS_VERIFY_SAVE.name(), transMap,
                MqTagEnum.LIVENESS_VERIFY_SAVE, param.getRecordId().toString(), ip);
        mqDtoService.assembleSendAsyncMsg(mqDto);
        return ResultUtil.ok(ret);
    }

}
