package com.kmall.admin.controller.faceid;

import com.kmall.admin.dto.FaceByOrderDto;
import com.kmall.admin.dto.OrderRecognitionDto;
import com.kmall.admin.entity.FaceLivenessRecognitionReturnDataEntity;
import com.kmall.admin.entity.OrderEntity;
import com.kmall.admin.service.OrderService;
import com.kmall.admin.service.faceid.FaceIdService;
import com.kmall.admin.utils.ParamUtils;
import com.kmall.common.constant.Dict;
import com.kmall.common.utils.PageUtils;
import com.kmall.common.utils.Query;
import com.kmall.manager.dto.FaceIdRequestDto;
import com.kmall.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author huangyq
 * @version 1.0
 * 2019-11-27 14:09
 */
@RestController
public class CheckFaceLivenessRecogController {
    @Autowired
    private FaceIdService faceIdService;
    @Autowired
    private OrderService orderService;

    /**
     * 人脸识别
     * @param faceByOrderDto
     * @return
     */
    @RequestMapping("face/checkLivenessRecognition")
    @RequiresPermissions("face:checkLivenessRecognition")
    public R checkLivenessRecognition(@RequestBody FaceByOrderDto faceByOrderDto) {
        String msg = faceIdService.checkLivenessRecognition(faceByOrderDto);
        return R.ok(msg);
    }

    /**
     * 列表
     */
    @RequestMapping("face/orderList")
    public R list(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
        params = ParamUtils.setTimeMap(params);
        //查询列表数据
        Query query = new Query(params);
        query.put("isOnfiilineOrder", Dict.isOnfflineOrder.item_0.getItem());
        List<OrderRecognitionDto> orderList = orderService.queryOrderListByRecognition(query);
        int total = orderService.queryOrderListByRecognitionTotal(query);

        PageUtils pageUtil = new PageUtils(orderList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 发送模板
     * @param orderId
     * @return
     */
    @RequestMapping("face/sendWxTemp/{orderId}")
//    @RequiresPermissions("face:sendWxTemp")
    public R sendWxTemp(@PathVariable("orderId") long orderId) {
        faceIdService.sendWxTemp(orderId);
        return R.ok();
    }


    /**
     * 手动操作人脸核验成功
     * @param orderId
     * @return
     */
    @RequestMapping("face/manualCheckFace/{orderId}")
//    @RequiresPermissions("face:sendWxTemp")
    public R manualCheckFace(@PathVariable("orderId") long orderId) {
        faceIdService.manualCheckFace(orderId);
        return R.ok();
    }

    @RequestMapping("face/get/{faceLivenessId}")
//    @RequiresPermissions("face:sendWxTemp")
    public R get(@PathVariable("faceLivenessId") long faceLivenessId) {
        FaceLivenessRecognitionReturnDataEntity entity = faceIdService.get(faceLivenessId);
        return R.ok().put("faceLiveness",entity);
    }
}
