package com.stdproject.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stdproject.common.OperationLog;
import com.stdproject.service.IDynamicDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 动态数据管理 前端控制器
 * </p>
 *
 * @author StdProject
 * @since 2024-01-01
 */
@Tag(name = "动态数据管理", description = "提供对数据源表数据的增删改查操作")
@RestController
@RequestMapping("/api/dynamicdata")
public class DynamicDataController {

    @Autowired
    private IDynamicDataService dynamicDataService;

    @Operation(summary = "向指定数据源的表中添加数据")
    @OperationLog(type = "01", module = "动态数据管理", description = "添加表数据")
    @PostMapping("addTableData")
    public boolean addTableData(Long datasourceId,  @RequestParam("tableData") String tableData) throws Exception {
        boolean result = dynamicDataService.addTableData(
                datasourceId,
                tableData
        );
        return result;
    }
    @Operation(summary = "根据主键查询表数据")
    @OperationLog(type = "06", module = "动态数据管理", description = "根据主键查询表数据")
    @PostMapping("getTableDataByPk")
    public Map<String, Object> getTableDataByPk(Long datasourceId,@RequestParam("whereJson")  String whereJson) throws Exception {
        Map<String, Object> result=dynamicDataService.getTableDataByPk(
                datasourceId,
                whereJson
        );
        return result;
    }

    @Operation(summary = "更新表数据")
    @OperationLog(type = "02", module = "动态数据管理", description = "更新表数据")
    @PostMapping("updateTableData")
    public boolean updateTableData(Long datasourceId, @RequestParam("tableData") String tableData) throws Exception {
        boolean result = dynamicDataService.updateTableData(
                datasourceId,
                tableData
        );
        return result;
    }

    @Operation(summary = "删除表数据")
    @OperationLog(type = "03", module = "动态数据管理", description = "删除表数据")
    @PostMapping("deleteTableData")
    public boolean deleteTableData(Long datasourceId, @RequestParam("whereJson") String whereJson)  throws Exception {
        boolean result = dynamicDataService.deleteTableData(
                datasourceId,
                whereJson
        );
        return result;
    }

    @Operation(summary = "分页查询表数据")
    @OperationLog(type = "06", module = "动态数据管理", description = "分页查询表数据")
    @PostMapping("queryTableDataPaged")
    public Page<Map<String, Object>> queryTableDataPaged(Long datasourceId, @RequestParam("queryJson") String queryJson)  throws Exception {
        Page<Map<String, Object>> result = dynamicDataService.queryTableDataPaged(
                datasourceId,
                queryJson
        );
        return result;
    }

}