package cn.fateverse.query.controller;

import cn.fateverse.common.core.entity.Option;
import cn.fateverse.common.core.utils.LongUtils;
import cn.fateverse.query.entity.TableInfo;
import cn.fateverse.query.entity.dto.ImportDto;
import cn.fateverse.query.entity.query.TableQuery;
import cn.fateverse.query.entity.vo.TableVo;
import cn.fateverse.query.service.TableService;
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.log.annotation.Log;
import cn.fateverse.common.log.enums.BusinessType;
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.*;

import java.util.List;


/**
 * 自定义查询表格维护 Controller
 *
 * @author clay
 * @date 2023-07-16
 */
@Api(value = "自定义查询表格维护管理", tags = "自定义查询表格维护管理")
@RestController
@RequestMapping("/table")
public class TableController {

    private final TableService tableService;

    public TableController(TableService tableService) {
        this.tableService = tableService;
    }

    @ApiOperation("获取自定义查询表格维护列表")
    @GetMapping
    @PreAuthorize("@ss.hasPermission('query:table:list')")
    public Result<TableDataInfo<TableVo>> list(TableQuery query) {
        TableDataInfo<TableVo> dataInfo = tableService.searchList(query);
        return Result.ok(dataInfo);
    }

    @ApiOperation("根据数据源获取到表格option信息")
    @GetMapping("/option/{datasourceId}")
    public Result<List<Option>> option(@PathVariable Long datasourceId){
        List<Option> optionList = tableService.searchOption(datasourceId);
        return Result.ok(optionList);
    }

    @ApiOperation("根据表格id获取列的option信息")
    @GetMapping("/column/option/{tableId}")
    public Result<List<Option>> columnOption(@PathVariable Long tableId){
        List<Option> optionList = tableService.searchColumnOption(tableId);
        return Result.ok(optionList);
    }

    @ApiOperation("获取详细信息")
    @GetMapping("/{tableId}")
    @PreAuthorize("@ss.hasPermission('query:table:info')")
    public Result<TableInfo> info(@PathVariable Long tableId) {
        TableInfo tableInfo = tableService.searchById(tableId);
        return Result.ok(tableInfo);
    }

    @ApiOperation("导入表信息")
    @PostMapping("/import")
    @Log(title = "自定义查询导入表信息", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermission('query:table:import')")
    public Result<Long> importTable(@RequestBody @Validated ImportDto dto) {
        if (dto.getTables().isEmpty() || LongUtils.isNull(dto.getDataSourceId())) {
            return Result.error("缺少必要参数!");
        }
        return tableService.importTable(dto);
    }

    @ApiOperation("同步数据库")
    @PutMapping("/sync/{tableId}")
    @Log(title = "同步数据库", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermission('query:table:sync')")
    public Result<Void> syncTable(@PathVariable Long tableId) {
        if (ObjectUtils.isEmpty(tableId)){
            return Result.error("缺少必要参数!");
        }
        tableService.syncTable(tableId);
        return Result.ok("同步成功");
    }

    @ApiOperation("修改自定义查询表格维护")
    @PutMapping
    @Log(title = "修改自定义查询表格维护", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermission('query:table:edit')")
    public Result<Void> edit(@RequestBody @Validated TableInfo table) {
        ObjectUtils.checkPk(table.getTable().getTableId());
        if (null == table.getColumnList() || table.getColumnList().isEmpty()) {
            return Result.error("列信息不能为空!");
        }
        if (null == table.getRoleIds() || table.getRoleIds().isEmpty()) {
            return Result.error("角色信息不能为空!");
        }
        tableService.edit(table);
        return Result.ok();
    }

    @ApiOperation("删除自定义查询表格维护")
    @DeleteMapping("/{tableId}")
    @Log(title = "删除自定义查询表格维护", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermission('query:table:del')")
    public Result<Void> batchDel(@PathVariable Long tableId) {
        if (null == tableId || 0L == tableId) {
            return Result.error("缺少必要参数!");
        }
        tableService.removeById(tableId);
        return Result.ok();
    }

}