package com.nantian.cloud.query.controller.jljc;

import com.nantian.cloud.query.entity.jljc.MultiDimensionDataSource;
import com.nantian.cloud.query.service.jljc.MultiDimensionDataSourceService;
import com.nantian.cloud.query.service.jly.JlyMultiDimensionDataSourceService;
import com.nantian.cloud.query.service.record.RecordMultiDimensionDataSourceService;
import com.nantian.sc.cloud.common.core.util.R;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

/**
 * 多维度查询数据源管理服务控制器
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-02-15 16:12:46
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/multiDimensionDataSource")
public class MultiDimensionDataSourceController {
    private final RecordMultiDimensionDataSourceService recordMultiDimensionDataSourceService;
    private final JlyMultiDimensionDataSourceService jlyMultiDimensionDataSourceService;
    private final MultiDimensionDataSourceService multiDimensionDataSourceService;

    /**
     * 获取所有数据源
     *
     * @return
     */
    @GetMapping("/queryAllDataSource")
    public R queryAllDataSource() {
        return R.ok(multiDimensionDataSourceService.list());
    }

    @PostMapping("/save")
    @ApiOperation(value = "新增数据源", notes = "新增数据源")
    @ApiImplicitParam(name = "multiDimensionDict", value = "数据源对象", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody MultiDimensionDataSource multiDimensionDataSource) {
        return R.ok(multiDimensionDataSourceService.save(multiDimensionDataSource));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改数据源", notes = "修改数据源")
    @ApiImplicitParam(name = "multiDimensionDict", value = "数据源对象", required = true, dataType = "json", paramType = "path")
    public R updateById(@RequestBody MultiDimensionDataSource multiDimensionDataSource) {
        return R.ok(multiDimensionDataSourceService.updateById(multiDimensionDataSource));
    }

    @PostMapping("/deleted")
    @ApiOperation(value = "删除数据源", notes = "删除数据源")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
    public R removeById(@RequestBody MultiDimensionDataSource multiDimensionDataSource) {
        return R.ok(multiDimensionDataSourceService.removeById(multiDimensionDataSource.getId()));
    }

    @GetMapping("/queryAllTables")
    public R queryAllTables(@Param("dataBaseName") String dataBaseName) {
        if ("jly".equals(dataBaseName)){
            return R.ok(jlyMultiDimensionDataSourceService.queryTablesByDataSource(dataBaseName));
        }else if ("jljc".equals(dataBaseName)){
            return R.ok(multiDimensionDataSourceService.queryTablesByDataSource(dataBaseName));
        }else {
            return R.ok(recordMultiDimensionDataSourceService.queryTablesByDataSource(dataBaseName));
        }
    }

    @GetMapping("/queryAllColumns")
    public R queryColumnsByDataSourceAndTable(@Param("dataBaseName") String dataBaseName, @Param("tableName") String tableName) {
        if ("jly".equals(dataBaseName)){
            return R.ok(jlyMultiDimensionDataSourceService.queryColumnsByDataSourceAndTable(dataBaseName,tableName));
        }else if ("jljc".equals(dataBaseName)){
            return R.ok(multiDimensionDataSourceService.queryColumnsByDataSourceAndTable(dataBaseName,tableName));
        }else {
            return R.ok(recordMultiDimensionDataSourceService.queryColumnsByDataSourceAndTable(dataBaseName,tableName));
        }

    }
}
