package com.ozs.mdm.controller;

import java.util.List;

import com.ozs.mdm.dto.*;
import com.ozs.mdm.dto.MdmDimDatabaseType;
import com.ozs.mdm.dto.MdmDimResourceCharset;
import com.ozs.mdm.dto.MdmDimResourceType;
import com.ozs.mdm.dto.MdmInfoDatabase;
import com.ozs.mdm.service.AccountApi;
import com.ozs.mdm.service.DatabaseManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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 = { "数据管理—>数据库管理接口" })
@RestController
public class DatabaseManageController {
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private AccountApi accountApi;
	@Autowired
	private DatabaseManageService databaseManageServicel;


	@ApiOperation("新建数据库资源接口")
	@PostMapping("/mdm/databaseManage/saveDatabase")
	public R<String> saveDatabase(@RequestBody MdmInfoDatabase database, @RequestHeader("token") String token) {
		try {
			database.setUpdateUserId(accountApi.parseTokenId(token));
			return databaseManageServicel.saveDatabase(database);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("获取数据库类型列表")
	@GetMapping("/mdm/databaseManage/listDatabaseType")
	public R<List<MdmDimDatabaseType>> listDatabaseType() {
		try {
			return databaseManageServicel.listDatabaseType();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("获取资源类型列表")
	@GetMapping("/mdm/databaseManage/listResourceType")
	public R<List<MdmDimResourceType>> listResourceType() {
		try {
			return databaseManageServicel.listResourceType();
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("获取字符集列表")
	@GetMapping("/mdm/databaseManage/listCharsetType")
	public R<List<MdmDimResourceCharset>> listCharsetType() {
		try {
			return databaseManageServicel.listCharsetType();
		} 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"),
			@ApiImplicitParam(name = "resourceType", value = "资源类型", required = false, paramType = "query", dataType = "Integer"),
			@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/databaseManage/listDatabase")
	public R<List<MdmInfoDatabase>> listDatabase(
			@RequestParam(name = "resourceName", required = false) String resourceName,
			@RequestParam(name = "resourceType", required = false) Integer resourceType,
			@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 databaseManageServicel.listDatabase(resourceName, resourceType, sort, order, page, pageSize);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("测试数据库资源连接")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "ip", value = "ip", required = true, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "resourcePort", value = "资源端口", required = true, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "username", value = "连接用户名", required = true, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "password", value = "连接密码", required = true, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "databaseName", value = "数据库名", required = true, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "resourceType", value = "资源类型", required = true, paramType = "query", dataType = "Integer") })
	@GetMapping("/mdm/databaseManage/getTestConnection")
	public R<String> getTestConnection(@RequestParam("ip") String ip, @RequestParam("resourcePort") String resourcePort,
			@RequestParam("username") String username, @RequestParam("password") String password,
			@RequestParam("databaseName") String databaseName, @RequestParam("resourceType") Integer resourceType) {
		try {
			return databaseManageServicel.getTestConnection(ip, resourcePort, username, password, databaseName,
					resourceType);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("通过ID获取数据库信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "String") })
	@GetMapping("/mdm/databaseManage/getDatabaseById/{id}")
	public R<MdmInfoDatabase> getDatabaseById(@PathVariable String id) {
		try {
			return databaseManageServicel.getDatabaseById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("修改数据库资源接口")
	@PutMapping("/mdm/databaseManage/updateDatabase")
	public R<String> updateDatabase(@RequestBody MdmInfoDatabase database, @RequestHeader("token") String token) {
		try {
			database.setUpdateUserId(accountApi.parseTokenId(token));
			return databaseManageServicel.updateDatabase(database);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("根据ID删除数据库资源接口")
	@ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "String")
	@DeleteMapping("/mdm/databaseManage/removeDatabaseById/{id}")
	public R<String> removeDatabaseById(@PathVariable String id) {
		try {
			return databaseManageServicel.removeDatabase(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("刷新原始库表和字段数据")
	@ApiImplicitParam(name = "id", value = "数据库ID", required = true, paramType = "path", dataType = "String")
	@PutMapping("/mdm/databaseManage/refresh/{id}")
	public R<String> updateOriginalDatabase(@PathVariable("id") String id) {
		try {
			return databaseManageServicel.updateOriginalDatabase(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}
}
