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.common.utils.EnumUtils;
import cn.hanghui.outapi.platform.common.utils.Preconditions;
import cn.hanghui.outapi.platform.tenant.entity.dto.auth.OpenInterfaceAuthDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.auth.OpenInterfaceAuthPageDto;
import cn.hanghui.outapi.platform.tenant.entity.dto.auth.OpenInterfaceAuthVo;
import cn.hanghui.outapi.platform.tenant.entity.enums.BusinessTypeEnum;
import cn.hanghui.outapi.platform.tenant.entity.enums.InterfaceTypeEnum;
import cn.hanghui.outapi.platform.tenant.entity.enums.LogTagEnum;
import cn.hanghui.outapi.platform.tenant.service.OpenInterfaceAuthService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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;

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

/**
 * @author shisl
 * @package cn.hanghui.outapi.platform.tenant.controller
 * @class OpenInterfaceAuthController
 * @date 2024/1/17 下午1:24
 * @description
 */
@Api(tags = "三方api接口授权管理")
@RestController
@RequestMapping("/api/v1/open/auth")
public class OpenInterfaceAuthController {

    @Autowired
    private OpenInterfaceAuthService openInterfaceAuthService;


    @ApiOperation(value = "添加/编辑")
    @PostMapping("/add")
    @Log(tag = LogTagEnum.THIRD_API,title = "添加/编辑服务权限",businessTypeEnum = BusinessTypeEnum.INSERT_UPDATE)
    public RestResult<Boolean> add(@RequestBody OpenInterfaceAuthDto dto) {
        if (Preconditions.isBlank(dto.getPurchaserId()) || Preconditions.isBlank(dto.getInterfaceType()) ) {
            return RestResult.error("参数有误，请重试！");
        }
        return openInterfaceAuthService.add(dto);
    }

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

    @ApiOperation(value = "开启")
    @GetMapping("/enable/{id}")
    @Log(tag = LogTagEnum.THIRD_API,title = "开启服务权限",businessTypeEnum = BusinessTypeEnum.OPEN)
    public RestResult<Boolean> enable(@PathVariable(value = "id") @ApiParam(value = "id", required = true) Long id) {
        return openInterfaceAuthService.updateStatus(id, Boolean.TRUE);
    }

    @ApiOperation(value = "停用")
    @GetMapping("/disable/{id}")
    @Log(tag = LogTagEnum.THIRD_API,title = "停用服务权限",businessTypeEnum = BusinessTypeEnum.CLOSE)
    public RestResult<Boolean> disable(@PathVariable(value = "id") @ApiParam(value = "id", required = true) Long id) {
        return openInterfaceAuthService.updateStatus(id, Boolean.FALSE);
    }

    @ApiOperation(value = "查询列表（分页）")
    @PostMapping("/page")
    public RestResult<IPage<OpenInterfaceAuthVo>> pageApply(@RequestBody OpenInterfaceAuthPageDto dto) {

        return openInterfaceAuthService.page(dto);
    }

    @ApiOperation(value = "查询接口类型列表")
    @PostMapping("/interface/type/list")
    public RestResult<List<Map<Integer, String>>> queryInterfaceTypeTypeList() {
        return RestResult.ok(EnumUtils.enumToListMap(InterfaceTypeEnum.class));
    }


}
