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.vo.CustomQueryResult;
import cn.fateverse.query.entity.vo.TopoVo;
import cn.fateverse.query.entity.dto.SearchInfo;
import cn.fateverse.query.entity.dto.TopoDto;
import cn.fateverse.query.service.TopologySearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Clay
 * @date 2023-07-21
 */
@Api(value = "拓扑图万能查询", tags = "拓扑图万能查询")
@RestController
@RequestMapping("/topo/search")
public class TopologySearchController {

    private final TopologySearchService topologySearchService;

    public TopologySearchController(TopologySearchService topologySearchService) {
        this.topologySearchService = topologySearchService;
    }

    @ApiOperation("获取到表格的拖拽数据信息")
    @GetMapping("/{queryId}")
    @Encrypt
    public Result<TopoVo> topology(@PathVariable @EncryptField String queryId) {
        TopoVo topoVo = topologySearchService.search(Long.parseLong(queryId));
        return Result.ok(topoVo);
    }

    @ApiOperation("保存topo参数")
    @PostMapping
    @Encrypt
    public Result<Void> save(@RequestBody @Validated TopoDto topo) {
        topologySearchService.saveData(topo);
        return Result.ok();
    }

    @ApiOperation("预览")
    @Encrypt
    @PostMapping("/preview")
    public Result<CustomQueryResult> preview(@RequestBody @Validated TopoDto topo) {
        return topologySearchService.preview(topo);
    }

    @ApiOperation("撤销")
    @Encrypt
    @PutMapping("/cancel/{queryId}")
    public Result<Void> cancel(@PathVariable @EncryptField String queryId){
        topologySearchService.cancel(Long.valueOf(queryId));
        return Result.ok();
    }

    @ApiOperation("发布")
    @Encrypt
    @PostMapping("/publish")
    public Result<Void> publish(@RequestBody @Validated TopoDto topo) {
        if (ObjectUtils.isEmpty(topo.getMenuId())) {
            return Result.error("上级菜单不能为空!");
        }
        topologySearchService.publish(topo);
        return Result.ok();
    }

    @ApiOperation("预览弹窗数据获取")
    @Encrypt
    @PostMapping("/preview/data")
    public Result<TableDataInfo<Map<String, Object>>> previewData(@RequestBody @Validated SearchInfo searchInfo) {
        TableDataInfo<Map<String, Object>> tableDataInfo = topologySearchService.previewData(searchInfo.getList(), searchInfo.getLongQueryId());
        return Result.ok(tableDataInfo);
    }


}
