package cn.fateverse.query.controller;

import cn.fateverse.common.core.result.Result;
import cn.fateverse.common.core.result.page.TableDataInfo;
import cn.fateverse.common.core.utils.ObjectUtils;
import cn.fateverse.common.decrypt.annotation.Encrypt;
import cn.fateverse.common.decrypt.annotation.EncryptField;
import cn.fateverse.query.entity.dto.MockParam;
import cn.fateverse.query.entity.dto.PortalDto;
import cn.fateverse.query.entity.query.PortalQuery;
import cn.fateverse.query.entity.vo.PortalIdWrapper;
import cn.fateverse.query.entity.vo.PortalVo;
import cn.fateverse.query.entity.vo.SimplePortalVo;
import cn.fateverse.query.service.PortalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 接口管理表(portal 传送门  接口作为入口 传送到其他类型) Controller
 *
 * @author clay
 * @date 2024-04-15
 */
@Api(value = "接口管理表", tags = "接口管理表")
@RestController
@RequestMapping("/portal")
public class PortalController {

    private final PortalService portalService;


    public PortalController(PortalService portalService) {
        this.portalService = portalService;
    }

    @ApiOperation("获取接口管理表详细信息")
    @Encrypt(Encrypt.Position.IN)
    @GetMapping("/{portalId}")
    @PreAuthorize("@ss.hasPermission('query:portal:info')")
    public Result<PortalVo> info(@PathVariable @EncryptField String portalId) {
        ObjectUtils.checkPk(portalId);
        Long value = Long.valueOf(portalId);
        PortalVo portal = portalService.searchById(value);
        return Result.ok(portal);
    }


    @ApiOperation("详情接口")
    @Encrypt(Encrypt.Position.IN)
    @GetMapping("/detail/{portalId}")
    public Result<PortalVo> detail(@PathVariable @EncryptField String portalId) {
        ObjectUtils.checkPk(portalId);
        Long value = Long.valueOf(portalId);
        PortalVo portal = portalService.searchDetailById(value);
        return Result.ok(portal);
    }


    @ApiOperation("获取接口管理表列表")
    @GetMapping
    @Encrypt
    @PreAuthorize("@ss.hasPermission('query:portal:list')")
    public Result<TableDataInfo<SimplePortalVo>> list(PortalQuery query) {
        TableDataInfo<SimplePortalVo> dataInfo = portalService.searchList(query);
        return Result.ok(dataInfo);
    }

    @ApiOperation("新增接口")
    @Encrypt(Encrypt.Position.OUT)
    @PostMapping
    @PreAuthorize("@ss.hasPermission('query:portal:add')")
    public Result<PortalIdWrapper> add(@RequestBody @Validated PortalDto portalDto) {
        PortalIdWrapper wrapper = portalService.save(portalDto);
        return Result.ok(wrapper);
    }

    @ApiOperation("修改接口")
    @Encrypt(Encrypt.Position.OUT)
    @PutMapping
    @PreAuthorize("@ss.hasPermission('query:portal:edit')")
    public Result<PortalIdWrapper> edit(@RequestBody @Validated PortalDto portalDto) {
        ObjectUtils.checkPk(portalDto.getPortalId());
        PortalIdWrapper wrapper = portalService.edit(portalDto);
        return Result.ok(wrapper);
    }


    @ApiOperation("模拟执行")
    @PostMapping("/mock/execute")
    @PreAuthorize("@ss.hasPermission('query:portal:execute')")
    public Result<Object> mockExecute(@RequestBody @Validated MockParam mockParam) {
        if (ObjectUtils.isEmpty(mockParam.getCode())) {
            return Result.error("代码不能为空");
        }
        Object result = portalService.mockExecute(mockParam);
        return Result.ok(result);
    }

    @ApiOperation("获取接口数据")
    @PostMapping("/mock/data")
    @PreAuthorize("@ss.hasPermission('query:portal:data')")
    public Result<Object> mockData(@RequestBody @Validated MockParam mockParam) {
        Object result = portalService.mockData(mockParam);
        return Result.ok(result);
    }


    @ApiOperation("撤销发布")
    @Encrypt
    @PutMapping("/cancel/{portalId}")
    @PreAuthorize("@ss.hasPermission('query:portal:edit')")
    public Result<PortalIdWrapper> cancel(@PathVariable @EncryptField String portalId) {
        ObjectUtils.checkPk(portalId);
        Long value = Long.valueOf(portalId);
        portalService.cancel(value);
        return Result.ok();
    }


    @ApiOperation("删除接口")
    @Encrypt
    @DeleteMapping("/{portalId}")
    @PreAuthorize("@ss.hasPermission('query:portal:remove')")
    public Result<Void> remove(@PathVariable @EncryptField String portalId) {
        ObjectUtils.checkPk(portalId);
        Long value = Long.valueOf(portalId);
        portalService.delete(value);
        return Result.ok();
    }


}
