package ink.wgink.database.controller.api.mysql.jdbc;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.database.pojo.vos.sql.SqlVO;
import ink.wgink.database.service.jdbc.IJdbcService;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: MysqlJdbcController
 * @Description: Mysql JDBC
 * @Author: wanggeng
 * @Date: 2021/10/26 11:41 下午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "mysql jdbc接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/mysql/jdbc")
public class MysqlJdbcController extends DefaultBaseController {

    @Autowired
    private IJdbcService jdbcService;

    @ApiOperation(value = "是否已经连接", notes = "是否已经连接接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mysqlId", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("is-connected/{mysqlId}")
    public SuccessResultData<Boolean> isConnected(@PathVariable("mysqlId") String mysqlId) {
        Boolean isConnected = jdbcService.isConnected(mysqlId);
        return new SuccessResultData<>(isConnected);
    }

    @ApiOperation(value = "打开连接", notes = "打开连接接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mysqlId", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("open-connection/{mysqlId}")
    public SuccessResult openConnection(@PathVariable("mysqlId") String mysqlId) throws SQLException {
        jdbcService.openConnection(mysqlId);
        return new SuccessResult();
    }

    @ApiOperation(value = "关闭连接", notes = "关闭连接接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mysqlId", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("close-connection/{mysqlId}")
    public SuccessResult closeConnection(@PathVariable("mysqlId") String mysqlId) throws SQLException {
        jdbcService.closeConnection(mysqlId);
        return new SuccessResult();
    }

    @ApiOperation(value = "图示列表", notes = "图示列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mysqlId", value = "主键", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-schemata/{mysqlId}")
    public List<Map<String, Object>> listSchemata(@PathVariable("mysqlId") String mysqlId) throws SQLException {
        return jdbcService.listSchemata(mysqlId);
    }

    @ApiOperation(value = "表列表", notes = "表列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mysqlId", value = "主键", paramType = "path"),
            @ApiImplicitParam(name = "schemata", value = "图示", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-table/{mysqlId}/{schemata}")
    public List<Map<String, Object>> listTable(@PathVariable("mysqlId") String mysqlId,
                                               @PathVariable("schemata") String schemata) throws SQLException {
        return jdbcService.listTable(mysqlId, schemata);
    }

    @ApiOperation(value = "字段列表", notes = "字段列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mysqlId", value = "主键", paramType = "path"),
            @ApiImplicitParam(name = "schemata", value = "图示", paramType = "path"),
            @ApiImplicitParam(name = "table", value = "表名", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-field/{mysqlId}/{schemata}/{table}")
    public List<Map<String, Object>> listField(@PathVariable("mysqlId") String mysqlId,
                                               @PathVariable("schemata") String schemata,
                                               @PathVariable("table") String table) throws SQLException {
        return jdbcService.listField(mysqlId, schemata, table);
    }

    @ApiOperation(value = "数据列表", notes = "数据列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mysqlId", value = "主键", paramType = "path"),
            @ApiImplicitParam(name = "schemata", value = "图示", paramType = "path"),
            @ApiImplicitParam(name = "table", value = "表名", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-data/{mysqlId}/{schemata}/{table}")
    public List<Map<String, Object>> listData(@PathVariable("mysqlId") String mysqlId,
                                              @PathVariable("schemata") String schemata,
                                              @PathVariable("table") String table) throws SQLException {
        return jdbcService.listData(mysqlId, schemata, table);
    }

    @ApiOperation(value = "数据列表", notes = "数据列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mysqlId", value = "主键", paramType = "path"),
            @ApiImplicitParam(name = "schemata", value = "图示", paramType = "path"),
            @ApiImplicitParam(name = "table", value = "表名", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("list-data/{mysqlId}/{schemata}/{table}")
    public List<Map<String, Object>> listData(@PathVariable("mysqlId") String mysqlId,
                                              @PathVariable("schemata") String schemata,
                                              @PathVariable("table") String table,
                                              @RequestBody SqlVO sqlVO) throws SQLException {
        return jdbcService.listData(mysqlId, schemata, table, sqlVO);
    }

    @ApiOperation(value = "数据分页列表", notes = "数据分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mysqlId", value = "主键", paramType = "path"),
            @ApiImplicitParam(name = "schemata", value = "图示", paramType = "path"),
            @ApiImplicitParam(name = "table", value = "表名", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("listpage-data/{mysqlId}/{schemata}/{table}")
    public SuccessResultList<List<Map<String, Object>>> listPageData(@PathVariable("mysqlId") String mysqlId,
                                                                     @PathVariable("schemata") String schemata,
                                                                     @PathVariable("table") String table,
                                                                     @RequestParam(name = "page", defaultValue = "1", required = false) int page,
                                                                     @RequestParam(name = "size", defaultValue = "20", required = false) int size) throws SQLException {
        return jdbcService.listPageData(mysqlId, schemata, table, page, size);
    }

    @ApiOperation(value = "执行SQL", notes = "数据分页列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mysqlId", value = "主键", paramType = "path"),
            @ApiImplicitParam(name = "schemata", value = "图示", paramType = "path"),
            @ApiImplicitParam(name = "table", value = "表名", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("execute-sql/{mysqlId}")
    @CheckRequestBodyAnnotation
    public SuccessResult executeSql(@PathVariable("mysqlId") String mysqlId,
                                    @RequestParam(name = "page", defaultValue = "1", required = false) int page,
                                    @RequestParam(name = "size", defaultValue = "20", required = false) int size,
                                    @RequestBody SqlVO sqlVO) throws SQLException {
        String sql = sqlVO.getSql();
        if (sql.startsWith("select") || sql.startsWith("SELECT")) {
            return jdbcService.listPageQuerySql(mysqlId, page, size, sqlVO);
        }
        jdbcService.updateSql(mysqlId, sqlVO);
        return new SuccessResult();
    }

}
