package com.ozs.mdm.controller;

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

import com.ozs.mdm.service.KpiManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ozs.mdm.dto.ColumnInfoDTO;
import com.ozs.mdm.dto.KpiListColumnDTO;
import com.ozs.mdm.dto.MdmInfoColumn;
import com.ozs.mdm.dto.MdmInfoDatabase;
import com.ozs.mdm.dto.MdmInfoTable;
import com.ozs.mdm.util.R;
import com.ozs.mdm.util.S;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = { "提供给的KPI管理相关接口" })
@RestController
public class KpiManageController {
	private final Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private KpiManageService kpiManageServicel;

	@ApiOperation("获取已建数据库列表选项-非原始库")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "resourceName", value = "资源名称(模糊搜索)", required = false, paramType = "query", dataType = "String") })
	@GetMapping("/mdm/kpiManage/listDatabase")
	public List<MdmInfoDatabase> listDatabase(
			@RequestParam(name = "resourceName", required = false) String resourceName) {
		try {
			return kpiManageServicel.listDatabase(resourceName);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@ApiOperation("通过数据库Id获取数据表列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "databaseId", value = "数据库ID", required = false, paramType = "query", dataType = "String") })
	@GetMapping("/mdm/kpiManage/listTableByDatabaseId")
	public List<MdmInfoTable> listTableByDatabaseId(
			@RequestParam(name = "databaseId", required = false) String databaseId) {
		try {
			return kpiManageServicel.listTableByDatabaseId(databaseId);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@ApiOperation("通过数据库Id和表Id获取表和表字段信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "databaseId", value = "数据库ID", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "tableId", value = "表ID", required = false, paramType = "query", dataType = "String") })
	@GetMapping("/mdm/kpiManage/listColumnByDatabaseId")
	public List<KpiListColumnDTO> listColumnByDatabaseId(
			@RequestParam(name = "databaseId", required = false) String databaseId,
			@RequestParam(name = "tableId", required = false) String tableId) {
		try {
			return kpiManageServicel.listColumnByDatabaseId(databaseId, tableId);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@ApiOperation("通过数据表Id获取数据表列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "tableId", value = "数据表ID", required = false, paramType = "query", dataType = "String") })
	@GetMapping("/mdm/kpiManage/listColumnByTableId")
	public List<MdmInfoColumn> listColumnByTableId(@RequestParam(name = "tableId", required = false) String tableId) {
		try {
			return kpiManageServicel.listColumnByTableId(tableId);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@ApiOperation("获取数据库资源列表接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "databaseId", value = "数据库ID", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "tableId", value = "数据表ID", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "page", value = "当前页数", required = false, paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "sort", value = "排序字段", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "order", value = "排序方式", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, paramType = "query", dataType = "Integer") })
	@GetMapping("/mdm/kpiManage/listColumn")
	public R<List<KpiListColumnDTO>> listColumn(@RequestParam(name = "databaseId", required = false) String databaseId,
			@RequestParam(name = "tableId", required = false) String tableId,
			@RequestParam(required = false, defaultValue = "update_time") String sort,
			@RequestParam(required = false) String order,
			@RequestParam(required = false, defaultValue = "1") Integer page,
			@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
		try {
			return kpiManageServicel.listColumn(databaseId, tableId, sort, order, page, pageSize);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@ApiOperation("通过ID数组批量字段信息")
	// @ApiImplicitParams({
	// @ApiImplicitParam(name = "ids", value = "字段id数组", required = false,
	// paramType = "query", dataType = "Array[string]") })
	@GetMapping("/mdm/kpiManage/listColumnByIds")
	public List<KpiListColumnDTO> listColumnByIds(@RequestParam List<String> ids) {
		try {
			return kpiManageServicel.listColumnByIds(ids);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@ApiOperation("通过ID批量字段信息")
	@ApiImplicitParam(name = "id", value = "字段id", required = false, paramType = "path", dataType = "String")
	@GetMapping("/mdm/kpiManage/getColumnInfoById/{id}")
	public R<ColumnInfoDTO> getColumnInfoByIds(@PathVariable String id) {
		try {
			return kpiManageServicel.getColumnInfoByIds(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("获取已建数据库列表选项-原始库")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "resourceName", value = "资源名称(模糊搜索)", required = false, paramType = "query", dataType = "String") })
	@GetMapping("/mdm/kpiManage/listOriginal")
	public List<MdmInfoDatabase> listOriginal(
			@RequestParam(name = "resourceName", required = false) String resourceName) {
		try {
			return kpiManageServicel.listOriginal(resourceName);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@ApiOperation("通过ID获取数据库信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "数据库ID", required = false, paramType = "path", dataType = "String") })
	@GetMapping("/mdm/kpiManage/database/{id}")
	public MdmInfoDatabase getDatabase(@PathVariable(name = "id", required = false) String id) {
		try {
			return kpiManageServicel.getDatabase(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@ApiOperation("执行sql获取查询数据")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "databaseId", value = "数据库ID", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "sql", value = "执行SQL", required = false, paramType = "query", dataType = "String") })
	@PostMapping("/mdm/listData")
	public List<Map<String, Object>> listData(@RequestParam("databaseId") String databaseId,
			@RequestBody Map<String, String> sql) {
		try {
			return kpiManageServicel.listData(databaseId, sql.get("sql"));
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	@ApiOperation("执行sql修改语句")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "databaseId", value = "数据库ID", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "sql", value = "执行SQL", required = false, paramType = "query", dataType = "String") })
	@PostMapping("/mdm/executeSql")
	public Integer executeSql(@RequestParam("databaseId") String databaseId,
			@RequestBody Map<String, String> sql) {
		try {
			return kpiManageServicel.executeSql(databaseId, sql.get("sql"));
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}
}
