package cn.hanghui.outapi.platform.alipay.controller;


import cn.hanghui.outapi.platform.alipay.entity.rest.request.AlipayAmpeSignOpenApiRequest;
import cn.hanghui.outapi.platform.alipay.entity.rest.request.AlipayGroupIdOpenApiRequest;
import cn.hanghui.outapi.platform.alipay.entity.rest.request.AlipayOpenIotvspUserwithimageRequest;
import cn.hanghui.outapi.platform.alipay.entity.rest.request.AlipayOpenIotvspUserwithimageShendaRequest;
import cn.hanghui.outapi.platform.alipay.entity.rest.request.AlipayOperateRecordCreateWithVidOpenApiRequest;
import cn.hanghui.outapi.platform.alipay.entity.rest.request.AlipayUserDeleteBatchOpenApiRequest;
import cn.hanghui.outapi.platform.alipay.entity.rest.request.AlipayUserDeleteOpenApiRequest;
import cn.hanghui.outapi.platform.alipay.entity.rest.request.AlipayUserDeleteOpenApiShendaRequest;
import cn.hanghui.outapi.platform.alipay.entity.rest.request.AlipayUserStateOpenApiRequest;
import cn.hanghui.outapi.platform.alipay.entity.rest.request.ZolozCustomerSmileliveInitializeRequest;
import cn.hanghui.outapi.platform.alipay.service.AlipayOpenApiService;
import cn.hanghui.outapi.platform.common.controller.BaseController;
import cn.hanghui.outapi.platform.common.entity.rest.RestCode;
import cn.hanghui.outapi.platform.common.entity.rest.RestResponse;
import cn.hanghui.outapi.platform.common.entity.rest.RestResult;
import cn.hanghui.outapi.platform.common.utils.AppTokenUtil;
import cn.hanghui.outapi.platform.common.utils.ClassUtil;
import cn.hanghui.outapi.platform.common.utils.Preconditions;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.alipay.controller
 * @class AlipayOpenApiController
 * @date 2023/5/5 下午2:19
 * @description
 */
@Validated
@Slf4j
@Api(tags = "支付宝开放接口")
@RestController
@RequestMapping("/alipay/api/v1/open")
public class AlipayOpenApiController extends BaseController {

    @Autowired
    private AlipayOpenApiService alipayOpenApiService;

    @ApiOperation(value = "查询封闭场景的机构用户库id")
    @PostMapping("/iotvsp/groupid/query")
    public RestResult queryOrgGroupId(@RequestBody AlipayGroupIdOpenApiRequest req) {
        return RestResponse.ok(alipayOpenApiService.queryOrgGroupId(req));
    }

    @ApiOperation(value = "查询机构用户在库结果")
    @PostMapping("/iotvsp/userstate/query")
    public RestResult queryUserState(@RequestBody AlipayUserStateOpenApiRequest req) {
        return RestResponse.ok(alipayOpenApiService.queryUserState(req));
    }

    @ApiOperation(value = "机构用户在库出库")
    @PostMapping("/iotvsp/user/delete")
    public RestResult userDelete(@RequestBody AlipayUserDeleteOpenApiRequest req) {
        alipayOpenApiService.deleteUser(req);
        return RestResponse.ok();
    }

    @ApiOperation(value = "录入机构用户信息（自定义vid）")
    @PostMapping("/iotvsp/userwithvid/create")
    public RestResult userwithvid(@RequestBody AlipayOperateRecordCreateWithVidOpenApiRequest req) {
        return RestResponse.ok(alipayOpenApiService.createOperateRecordWithVidV2(req));
    }

    @ApiOperation(value = "获取设备签名等信息")
    @PostMapping("/ampe/sign")
    public RestResult ampeSign(@RequestBody AlipayAmpeSignOpenApiRequest req) {
        return RestResponse.ok(alipayOpenApiService.ampeSign(req));
    }

    @ApiOperation(value = "录入机构用户信息并直接入库（自定义vid）")
    @PostMapping("/iotvsp/userwithimage/create")
    public RestResult userwithimage(@RequestBody AlipayOpenIotvspUserwithimageRequest req) {
        return alipayOpenApiService.createOperateRecordWithoutImage(req);
    }

    @ApiOperation(value = "接口实名入库（自定义vid）")
    @PostMapping("/iotvsp/userwithimage/common/create")
    public RestResult userwithverifiedcommon(@RequestBody AlipayOpenIotvspUserwithimageRequest req) {
//        log.error("禁止访问资源 请联系管理员");
//        return RestResponse.error("禁止访问资源 请联系管理员");
        return alipayOpenApiService.createOperateRecordWithverifiedcommon(req);
    }

    @ApiOperation(value = "设备端实名入库（自定义vid）")
    @PostMapping("/iotvsp/userwithverified/create")
    public RestResult userwithverified(@RequestBody AlipayOpenIotvspUserwithimageRequest req) {
        return alipayOpenApiService.createOperateRecordWithverified(req);
    }

    @ApiOperation(value = "接口和设备匿名入库（自定义vid）")
    @PostMapping("/iotvsp/userwithanonymous/create")
    public RestResult userwithanonymous(@RequestBody AlipayOpenIotvspUserwithimageRequest req) {
        return alipayOpenApiService.createOperateRecordWithanonymous(req);
    }

    @ApiOperation(value = "机构用户批量出库For Job")
    @PostMapping("/iotvsp/user/delete/batch")
    public RestResult userDeleteBatch(@RequestBody AlipayUserDeleteBatchOpenApiRequest req) {
        alipayOpenApiService.deleteUserBatch(req);
        return RestResponse.ok();
    }

    @ApiOperation(value = "刷脸核身Windows-BPaaSFaceSmileLifeVerify 1v1")
    @PostMapping("/customerSmileliveInitialize")
    public RestResult zolozGetMetaInfo(@RequestBody ZolozCustomerSmileliveInitializeRequest req) {
        log.info("--->刷脸核身Windows-BPaaSFaceSmileLifeVerify入参:{}", JSONObject.toJSONString(req));
        List<String> tokens = AppTokenUtil.getTokens();
        if (req == null || req.getToken() == null || "".equals(req.getToken().trim()) || !tokens.contains(req.getToken().trim())) {
            return RestResult.error(RestCode.FORBIDDEN.getMsg());
        }
        if (req.getZimMetaInfo() == null) {
            return RestResult.error("参数有误");
        }
        return alipayOpenApiService.customerSmileliveInitialize(req);
    }


    /***  以下为深大临时方案 后面去除  ****/
    @ApiOperation(value = "接口实名入库（自定义vid）")
    @PostMapping("/iotvsp/sd/userwithimage/create")
    public RestResult userwithverifiedCommonShenda(@RequestBody AlipayOpenIotvspUserwithimageShendaRequest req) {
        ClassUtil.throwBusinessException(Preconditions.isBlank(req.getToken()) || !AppTokenUtil.getTokens().contains(req.getToken().trim()), RestCode.FORBIDDEN.getCode(), RestCode.FORBIDDEN.getMsg());
        ClassUtil.throwBusinessException(Preconditions.isBlank(req.getCertNo()) || Preconditions.isBlank(req.getCertName()), RestCode.PARAMS_IDCARD_INCOMPLETE.getCode(), "参数有误");
        AlipayOpenIotvspUserwithimageRequest request = BeanUtil.copyProperties(req, AlipayOpenIotvspUserwithimageRequest.class);
        handlePreParams(request);
        log.error("【接口实名入库】禁止访问资源 请联系管理员！");
        return RestResponse.error("禁止访问资源 请联系管理员！");
//        return alipayOpenApiService.createOperateRecordWithverifiedcommon(request);
    }

    private void handlePreParams(AlipayOpenIotvspUserwithimageRequest req) {
        req.setAppId("2021003181682188");
    }

    @ApiOperation(value = "设备端实名入库（自定义vid）")
    @PostMapping("/iotvsp/sd/userwithverified/create")
    public RestResult userwithverifieShenda(@RequestBody AlipayOpenIotvspUserwithimageShendaRequest req) {
        ClassUtil.throwBusinessException(Preconditions.isBlank(req.getToken()) || !AppTokenUtil.getTokens().contains(req.getToken().trim()), RestCode.FORBIDDEN.getCode(), RestCode.FORBIDDEN.getMsg());
        ClassUtil.throwBusinessException(Preconditions.isBlank(req.getFtoken()) || Preconditions.isBlank(req.getCertNo()) || Preconditions.isBlank(req.getCertName()), RestCode.PARAMS_IDCARD_INCOMPLETE.getCode(), "参数有误");
        AlipayOpenIotvspUserwithimageRequest request = BeanUtil.copyProperties(req, AlipayOpenIotvspUserwithimageRequest.class);
        handlePreParams(request);
//        return alipayOpenApiService.createOperateRecordWithverified(request);
        log.error("【设备端实名入库】禁止访问资源 请联系管理员！");
        return RestResponse.error("禁止访问资源 请联系管理员！");
    }

    @ApiOperation(value = "接口和设备匿名入库（自定义vid）")
    @PostMapping("/iotvsp/sd/userwithanonymous/create")
    public RestResult userwithanonymousShenda(@RequestBody AlipayOpenIotvspUserwithimageShendaRequest req) {
        ClassUtil.throwBusinessException(Preconditions.isBlank(req.getToken()) || !AppTokenUtil.getTokens().contains(req.getToken().trim()), RestCode.FORBIDDEN.getCode(), RestCode.FORBIDDEN.getMsg());
        ClassUtil.throwBusinessException((Preconditions.isBlank(req.getFtoken()) && Preconditions.isBlank(req.getImage())) || Preconditions.isBlank(req.getCertNo()) || Preconditions.isBlank(req.getCertName()), RestCode.PARAMS_IDCARD_INCOMPLETE.getCode(), "参数有误");
        AlipayOpenIotvspUserwithimageRequest request = BeanUtil.copyProperties(req, AlipayOpenIotvspUserwithimageRequest.class);
        handlePreParams(request);
//        return alipayOpenApiService.createOperateRecordWithanonymous(request);
        log.error("【接口和设备匿名入库】禁止访问资源 请联系管理员！");
        return RestResponse.error("禁止访问资源 请联系管理员！");
    }

    @ApiOperation(value = "机构用户在库出库")
    @PostMapping("/iotvsp/sd/user/delete")
    public RestResult userDeleteShenda(@RequestBody AlipayUserDeleteOpenApiShendaRequest req) {
        ClassUtil.throwBusinessException(Preconditions.isBlank(req.getToken()) || !AppTokenUtil.getTokens().contains(req.getToken().trim()), RestCode.FORBIDDEN.getCode(), RestCode.FORBIDDEN.getMsg());
        ClassUtil.throwBusinessException(Preconditions.isBlank(req.getVidList()), RestCode.PARAMS_IDCARD_INCOMPLETE.getCode(), "参数有误");
        AlipayUserDeleteOpenApiRequest request = BeanUtil.copyProperties(req, AlipayUserDeleteOpenApiRequest.class);
        request.setAppId("2021003181682188");
        log.error("【机构用户在库出库】禁止访问资源 请联系管理员！");
        return RestResponse.error("禁止访问资源 请联系管理员！");
//        alipayOpenApiService.deleteUser(request);
//        return RestResponse.ok();
    }

}
