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

import cn.hanghui.outapi.platform.common.config.log.Log;
import cn.hanghui.outapi.platform.common.entity.rest.RestResult;
import cn.hanghui.outapi.platform.tenant.entity.dto.IotPlatformAuthDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.IotPlatformAuthReqDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.IotUserInfoDto;
import cn.hanghui.outapi.platform.tenant.entity.enums.BusinessTypeEnum;
import cn.hanghui.outapi.platform.tenant.entity.enums.LogTagEnum;
import cn.hanghui.outapi.platform.tenant.service.AlipayIotService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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;

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.tenant.controller
 * @class AlipayIotController
 * @date 2023/6/9 上午11:18 IotPlatformAuthDto
 * @description 支付宝服务（三要素授权等）
 */
@Slf4j
@Api(tags = "支付宝小程序服务（三要素授权等）")
@RestController
@RequestMapping("/api/v1/alipay")
@AllArgsConstructor(onConstructor_ = @Autowired)
public class AlipayIotController {

    @Autowired
    private AlipayIotService alipayIotService;

    @ApiOperation(value = "添加/编辑授权")
    @PostMapping("/add")
    @Log(tag = LogTagEnum.ALIPAY,title = "添加/编辑授权",businessTypeEnum = BusinessTypeEnum.INSERT_UPDATE)
    public RestResult<Boolean> addAuth(@RequestBody IotPlatformAuthDto dto) {
        return alipayIotService.addAuth(dto);
    }

    @ApiOperation(value = "删除授权")
    @GetMapping("/del/{id}")
    @Log(tag = LogTagEnum.ALIPAY,title = "删除授权",businessTypeEnum = BusinessTypeEnum.DELETE)
    public RestResult<Boolean> del(@PathVariable(value = "id") @ApiParam(value = "id", required = true) Long id) {
        return alipayIotService.del(id);
    }

    @ApiOperation(value = "停用")
    @GetMapping("/cancel/{id}")
    @Log(tag = LogTagEnum.ALIPAY,title = "停用",businessTypeEnum = BusinessTypeEnum.CLOSE)
    public RestResult<Boolean> cancel(@PathVariable(value = "id") @ApiParam(value = "id", required = true) Long id) {
        return alipayIotService.opStatus(id, Boolean.FALSE);
    }

    @ApiOperation(value = "启用")
    @GetMapping("/enable/{id}")
    @Log(tag = LogTagEnum.ALIPAY,title = "启用",businessTypeEnum = BusinessTypeEnum.OPEN)
    public RestResult<Boolean> enable(@PathVariable(value = "id") @ApiParam(value = "id", required = true) Long id) {
        return alipayIotService.opStatus(id, Boolean.TRUE);
    }


    @ApiOperation(value = "查询商户列表（分页）")
    @PostMapping("/page")
    public RestResult<IPage<IotPlatformAuthDto>> pageApply(@RequestBody IotPlatformAuthReqDto dto) {
        return alipayIotService.page(dto);
    }

    @ApiOperation(value = "校验token是否有权限调用（支付宝小程序调用）")
    @PostMapping("/token/{token}/check")
    public RestResult<Boolean> checkToken(@PathVariable(value = "token") @ApiParam(value = "token", required = true) String token) {
        log.info("--->校验token是否有权限调用请求参数token:{}", token);
        return alipayIotService.checkToken(token);
    }

    @ApiOperation(value = "推送用户三要素信息（支付宝小程序调用）")
    @PostMapping("/user/push")
    public RestResult<Boolean> userPush(@RequestBody IotUserInfoDto dto) {
        log.info("--->推送用户信息三要素请求参数:{}", JSONObject.toJSONString(dto));
        return alipayIotService.userPush(dto);
    }

    @ApiOperation(value = "获取用户三要素信息（支付宝小程序调用）")
    @PostMapping("/user/query")
    public RestResult<IotUserInfoDto> userQuery(@RequestBody IotUserInfoDto dto) {
        log.info("--->获取用户信息三要素请求参数:{}", JSONObject.toJSONString(dto));
        return alipayIotService.userQuery(dto);
    }


}
