package com.cm.common.plugin.controller.apis.database.table;

import com.cm.common.annotation.CheckRequestBodyAnnotation;
import com.cm.common.base.AbstractController;
import com.cm.common.constants.ISystemConstant;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.pojo.dtos.database.table.TableColumnDTO;
import com.cm.common.plugin.pojo.dtos.database.table.TableDTO;
import com.cm.common.plugin.pojo.vos.database.table.TableColumnVO;
import com.cm.common.plugin.pojo.vos.database.table.TableOldColumnVO;
import com.cm.common.plugin.pojo.vos.database.table.TableVO;
import com.cm.common.plugin.service.database.table.ITableService;
import com.cm.common.result.ErrorResult;
import com.cm.common.result.SuccessResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: TableController
 * @Description: 数据库表处理
 * @Author: WangGeng
 * @Date: 2019-11-14 10:43
 * @Version: 1.0
 **/
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/database/table")
public class TableController extends AbstractController {

    @Autowired
    private ITableService tableService;

    @ApiOperation(value = "建表", notes = "建表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savetable")
    @CheckRequestBodyAnnotation
    public SuccessResult saveTable(@RequestBody TableVO tableVO) {
        return tableService.saveTable(tableVO);
    }

    @ApiOperation(value = "删表", notes = "删表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("removetable/{tableName}")
    public SuccessResult removeTable(@PathVariable("tableName") String tableName) {
        return tableService.removeTable(tableName);
    }

    @ApiOperation(value = "加列", notes = "加列接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("savetablecolumn/{tableName}")
    @CheckRequestBodyAnnotation
    public SuccessResult saveTableColumn(@PathVariable("tableName") String tableName, @RequestBody TableColumnVO tableColumnVO) {
        return tableService.saveTableColumn(tableName, tableColumnVO);
    }

    @ApiOperation(value = "删列", notes = "删列接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path"),
            @ApiImplicitParam(name = "columnName", value = "列名", paramType = "path"),
    })
    @DeleteMapping("removetablecolumn/{tableName}/{columnName}")
    public SuccessResult removeTableColumn(@PathVariable("tableName") String tableName, @PathVariable("columnName") String columnName) {
        return tableService.removeTableColumn(tableName, columnName);
    }

    @ApiOperation(value = "修改列", notes = "修改列")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatetablecolumn/{tableName}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateTableColumn(@PathVariable("tableName") String tableName, @RequestBody TableOldColumnVO tableOldColumnVO) {
        return tableService.updateTableColumn(tableName, tableOldColumnVO);
    }

    @ApiOperation(value = "修改列类型", notes = "修改列类型")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("updatetablecolumntype/{tableName}")
    @CheckRequestBodyAnnotation
    public SuccessResult updateTableColumnType(@PathVariable("tableName") String tableName, @RequestBody TableColumnVO tableColumnVO) {
        return tableService.updateTableColumnType(tableName, tableColumnVO);
    }

    @ApiOperation(value = "通过表名查询表", notes = "通过表名查询表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("gettablebytablename/{tableName}")
    public TableDTO getTableByTableName(@PathVariable("tableName") String tableName) {
        return tableService.getTableByTableName(tableName);
    }

    @ApiOperation(value = "表列表", notes = "表列表接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listtables")
    public List<TableDTO> listTables() throws SearchException {
        Map<String, Object> params = requestParams();
        return tableService.listTables(params);
    }

    @ApiOperation(value = "表详情", notes = "表详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tableName", value = "表名", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listtablecolumns/{tableName}")
    public List<TableColumnDTO> listTableColumns(@PathVariable("tableName") String tableName) throws SearchException {
        Map<String, Object> params = requestParams();
        params.put("tableName", tableName);
        return tableService.listTableColumns(params);
    }

}
