package com.kmall.api.api.faceid;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.kmall.api.annotation.LoginUser;
import com.kmall.manager.dto.FaceIdRequestDto;
import com.kmall.api.entity.UserVo;
import com.kmall.api.service.faceid.ApiFaceIdService;
import com.kmall.api.util.ApiBaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author huangyq
 * @version 1.0
 * 2019-11-21 15:52
 */
@RestController
@RequestMapping("/api/face")
public class ApiFaceIdController extends ApiBaseAction {
    @Autowired
    private ApiFaceIdService apiFaceIdService;

    /**
     * 活体人脸核身接口（待小程序开放人脸识别类目）
     */
    @PostMapping("checkLivenessRecognition")
    public Object checkLivenessRecognition(@LoginUser UserVo loginUser) {
        JSONObject json = this.getJsonRequest();
        FaceIdRequestDto faceIdRequestDto = new FaceIdRequestDto();
        faceIdRequestDto.setIdCard(json.getString("idCard"));
        faceIdRequestDto.setName(json.getString("name"));
        faceIdRequestDto.setVideoBase64(json.getString("videoBase64"));
        faceIdRequestDto.setLivenessType(json.getString("livenessType"));
        faceIdRequestDto.setValidateData(json.getString("validateData"));
        faceIdRequestDto.setMerchId(json.getString("merchId"));
        Long storeId = json.getLong("storeId");
        String msg = apiFaceIdService.checkLivenessRecognition(faceIdRequestDto, storeId, getUserId());
        return toResponsSuccess(msg);
    }

    /**
     * 根据订单号获取是否人脸认证成功
     * @param loginUser
     * @param orderIds
     * @return 1：认证成功，2：没有保税展示跨境订单，0/null：未认证
     */
    @GetMapping("getIsFaceCheckByOrderId")
    public Object getIsFaceCheckByOrderId(@LoginUser UserVo loginUser, Long[] orderIds) {
        String isFaceCheck = apiFaceIdService.getFaceDataByOrderId(orderIds);
        Map resultObj = Maps.newHashMap();
        resultObj.put("isFaceCheck", isFaceCheck);
        return toResponsSuccess(resultObj);
    }
}
