package com.usoftchina.bi.server.controller.dataSource;

import com.usoftchina.bi.auth.annotation.Auth;
import com.usoftchina.bi.server.model.pojo.annotation.CheckToken;
import com.usoftchina.bi.server.model.po.Databases;
import com.usoftchina.bi.core.base.RepEntity;
import com.usoftchina.bi.core.base.TestPage;
import com.usoftchina.bi.server.model.pojo.annotation.Log;
import com.usoftchina.bi.server.model.vo.configVo.DatabasesInfo;
import com.usoftchina.bi.server.service.dataSource.DataBasesService;
import com.usoftchina.bi.server.service.dataSource.ImplementSqlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/DataBase")
@Api(description = "数据连接相关接口")
public class DataBasesController {
    @Autowired
    DataBasesService dataBasesService;
    @Autowired
    ImplementSqlService implementSqlService;
    /*
   数据库
   保存数据库配置
   */
    @ApiOperation(value = "保存数据库配置", notes = "保存数据库配置", response = RepEntity.class)
    @CheckToken
    @Auth(user = "admin")
    @Log(module = "数据链接", name = "#body.name", token = "#token")
    @PostMapping("/inputDatabases")
    public RepEntity saveDatabases(@RequestHeader String token, @RequestBody DatabasesInfo body){
        return dataBasesService.inputDatabases(body);
    }

    /*
    查看数据库配置列表
     */
    @ApiOperation(value = "查看数据库配置列表", notes = "查看数据库配置列表", response = RepEntity.class)
    @CheckToken
    @Auth(user = "admin")
    @GetMapping("/getDatabases")
    public RepEntity getDatabases(@RequestHeader String token, TestPage body){
        return dataBasesService.getListDatabases(body);
    }

    /*
    删除数据库配置
     */
    @ApiOperation(value = "删除数据库配置", notes = "删除数据库配置", response = RepEntity.class)
    @CheckToken
    @Auth(user = "admin")
    @PostMapping("/delDatabases")
    public RepEntity delDatabases(@RequestHeader String token,@RequestBody Long body){
        return dataBasesService.delDatabases(body);
    }

    /*
    更新数据库配置
     */
    @ApiOperation(value = "更新数据库配置", notes = "更新数据库配置", response = RepEntity.class)
    @CheckToken
    @Auth(user = "admin")
    @Log(module = "数据链接", name = "#databasesInfo.name", token = "#token")
    @PostMapping("/updatabases")
    public RepEntity updateDatabases(@RequestHeader String token,@RequestBody DatabasesInfo databasesInfo){
        return dataBasesService.updatabases(databasesInfo);
    }

    /*
    查看列
     */
    @ApiOperation(value = "查看列", notes = "查看列", response = RepEntity.class)
    @PostMapping("/getColumnData")
    public RepEntity getColumnData(@RequestBody int body){
        return dataBasesService.getColumnData(body);
    }

    /*
    通过图表Id查看列数据
     */
    @ApiOperation(value = "通过图表ID查看列数据", notes = "通过图表ID查看列数据", response = RepEntity.class)
    @CheckToken
    @PostMapping("/getColumnByChartId")
    public RepEntity getColumnByChartId(@RequestHeader String token,@RequestBody int body){
        return dataBasesService.getColumnByChartId(body);
    }


    /*
    测试数据库连接
     */
    @ApiOperation(value = "测试数据库连接", notes = "测试数据库连接", response = RepEntity.class)
    @CheckToken
    @Auth(user = "admin")
    @PostMapping("/testConnect")
    public RepEntity testConnect(@RequestHeader String token,@RequestBody Databases body){
        return implementSqlService.connectTest(body);
    }
}
