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.bo.BaseToDataInfo;
import com.usoftchina.bi.server.model.bo.GroupUpdateId;
import com.usoftchina.bi.server.model.bo.ToSql;
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.ChangeOrderInfo;
import com.usoftchina.bi.server.model.vo.configVo.DataConnectorInfo;
import com.usoftchina.bi.server.model.vo.configVo.GroupInfo;
import com.usoftchina.bi.server.model.vo.dataVo.DataSourceCopyInfo;
import com.usoftchina.bi.server.service.dataSource.DataConnectorService;
import com.usoftchina.bi.server.service.dataSource.ImplementSqlService;
import com.usoftchina.bi.server.model.pojo.annotation.TargetDataSource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Connector")
@Api(description = "数据源相关接口")
public class DataConnectorController {
    @Autowired
    DataConnectorService dataConnectorService;

    @Autowired
    ImplementSqlService implementSqlService;

    /*
    保存数据源配置
     */
    @ApiOperation(value = "保存数据源配置", notes = "保存数据源配置", response = RepEntity.class)
    @CheckToken
    @Auth(user = "admin")
    @Log(module = "数据源", name = "#body.dataName", token = "#token")
    @PostMapping("/inputDataConnector")
    public RepEntity saveDataConnector(@RequestHeader String token, @RequestBody DataConnectorInfo body) {
        return dataConnectorService.setDataConnector(token,body);
    }

    /*
    查看数据源列表
     */
    @ApiOperation(value = "查看数据源列表", notes = "查看数据源列表", response = RepEntity.class)
    @CheckToken
    @GetMapping("/getListDataConnector")
    public RepEntity getListDataConnector(@RequestHeader String token, TestPage page){
        return dataConnectorService.getListConnector(token, page);
    }

    /*
    查看单个数据源配置
     */
    @ApiOperation(value = "查看单个数据源配置", notes = "查看单个数据源配置", response = RepEntity.class)
    @CheckToken
    @Auth(user = "admin")
    @GetMapping("/getDataConnector")
    public RepEntity getDataConnector(@RequestHeader String token,@RequestParam("id") int id){
        return dataConnectorService.getConnector(id);
    }

    /*
    更新数据源配置
     */
    @ApiOperation(value = "更新数据源配置", notes = "更新数据源配置", response = RepEntity.class)
    @CheckToken
    @Auth(user = "admin")
    @Log(module = "数据源", name = "#body.dataName", token = "#token")
    @PostMapping("/updateData")
    public RepEntity updateData(@RequestHeader String token,@RequestBody DataConnectorInfo body){
        return dataConnectorService.updateData(body);
    }

    /*
    转交数据源
     */
    @ApiOperation(value = "转交数据源", notes = "转交数据源", response = RepEntity.class)
    @CheckToken
    @PostMapping("/changeDbOrder")
    public RepEntity changeDbOrder(@RequestHeader String token,@RequestBody ChangeOrderInfo body){
        return dataConnectorService.changeOrder(body);
    }

    /*
    更新数据源配置的分组
     */
    @ApiOperation(value = "更新数据源配置分组", notes = "更新数据源配置分组", response = RepEntity.class)
    @Auth(user = "admin")
    @CheckToken
    @PostMapping("/updateConnectConfigGroup")
    public RepEntity updateConConfigGroup(@RequestHeader String token,@RequestBody GroupUpdateId body){
        return dataConnectorService.updateConfigGroup(token, body);
    }


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

    /*
    执行数据源配置
    */
    @ApiOperation(value = "执行数据源配置", notes = "执行数据源配置", response = RepEntity.class)
    @TargetDataSource
    @CheckToken
    @PostMapping("/implementSql")
    @Auth(user = "admin")
    public RepEntity implementSql(@RequestHeader String token,@RequestBody ToSql body){
        return implementSqlService.implementSql(body);
    }

    /*
    添加数据源分组
     */
    @ApiOperation(value = "添加数据源分组", notes = "添加数据源分组", response = RepEntity.class)
    @CheckToken
    @PostMapping("/setConnectorGroup")
    @Auth(user = "admin")
    public RepEntity setConnectorGroup(@RequestHeader String token,@RequestBody GroupInfo body){
        return dataConnectorService.setDataConnectorGroup(token, body);
    }

    /**
     * 批量更新数据源分组
     * @param token
     * @param groupInfoList
     * @return
     */
    @ApiOperation(value = "批量更新数据源分组", notes = "批量更新数据源分组", response = RepEntity.class)
    @CheckToken
    @PostMapping("/batchUpdateConnectorGroup")
    @Auth(user = "admin")
    public RepEntity saveConnectorGroups(@RequestHeader String token, @RequestBody List<GroupInfo> groupInfoList){
        return dataConnectorService.saveConnectorGroups(token, groupInfoList);
    }

    /*
    更新数据源分组
     */
    @ApiOperation(value = "更新数据源分组", notes = "更新数据源分组", response = RepEntity.class)
    @CheckToken
    @Auth(user = "admin")
    @PostMapping("/updataDataConnectorGroup")
    public RepEntity updataDataConnectorGroup(@RequestHeader String token,@RequestBody GroupInfo body){
        return dataConnectorService.updataDataConnectorGroup(token, body);
    }

    /*
    批量更新数据源分组
     */
    @ApiOperation(value = "批量更新数据源分组", notes = "批量更新数据源分组", response = RepEntity.class)
    @CheckToken
    @Auth(user = "admin")
    @PostMapping("/updataConnectorGroup")
    public RepEntity updataConnectorGroup(@RequestHeader String token,@RequestBody List<GroupInfo> body){
        return dataConnectorService.updataConnectorGroups(body);
    }

    /*
    删除数据源分组
     */
    @ApiOperation(value = "删除数据源分组", notes = "删除数据源分组", response = RepEntity.class)
    @Auth(user = "admin")
    @CheckToken
    @PostMapping("/delDataConnectorGroup")
    public RepEntity delDataConnectorGroup(@RequestHeader String token,@RequestBody Long body){
        return dataConnectorService.delDataConnectorGroup(token, body);
    }

    /*
    查询数据源分组
     */
    @ApiOperation(value = "查询数据源分组", notes = "查询数据源分组", response = RepEntity.class)
    @CheckToken
    @PostMapping("/getConnectorGroup")
    public RepEntity getConnectorGroup(@RequestHeader String token){
        return dataConnectorService.getConnectorGroup();
    }

    /*
    查询数据源数据
     */
    @ApiOperation(value = "查询数据源数据", notes = "查询数据源数据", response = RepEntity.class)
    @CheckToken
    @PostMapping("/getConnectorData")
    public RepEntity getConnectorData(@RequestHeader String token, @RequestBody BaseToDataInfo body){
        return dataConnectorService.getConnectorData(token, body);
    }

    /**
     * 数据源复制
     * @param token
     * @param dataSourceCopyInfo
     * @return
     */
    @ApiOperation(value = "复制数据源", notes = "复制数据源", response = RepEntity.class)
    @CheckToken
    @PostMapping("/copyDataSource")
    public RepEntity copyDataSource(@RequestHeader String token, @RequestBody DataSourceCopyInfo dataSourceCopyInfo){
        return dataConnectorService.copyDataSource(token, dataSourceCopyInfo);
    }
}
