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.dto.SearchInfo;
import cn.fateverse.query.service.PageService;
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-08-05
 */
@Api(value = "上线自定义查询页面",tags = "上线自定义查询页面")
@RestController
@RequestMapping("/page")
public class PageController {


    private final PageService pageService;


    public PageController(PageService pageService) {
        this.pageService = pageService;
    }


    @ApiOperation("获取到自定义查询的信息")
    @GetMapping("/{queryId}")
    @Encrypt
    public Result<CustomQueryResult> info(@PathVariable @EncryptField String queryId){
        if (ObjectUtils.isEmpty(queryId)) {
            return Result.error("请求参数为空");
        }
        return pageService.searchQueryInfoAndData(Long.valueOf(queryId));
    }

    @ApiOperation("查询数据")
    @PostMapping("/data")
    @Encrypt
    public Result<TableDataInfo<Map<String, Object>>> searchData(@RequestBody @Validated SearchInfo searchInfo){
        TableDataInfo<Map<String, Object>> tableDataInfo = pageService.searchQueryData(searchInfo);
        return Result.ok(tableDataInfo);
    }




}
