package com.bocloud.ims.booter.controller.resource.database;

import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.middleware.RdsDb;
import com.bocloud.ims.entity.middleware.RdsPublicCloudInstance;
import com.bocloud.ims.entity.middleware.RdsUser;
import com.bocloud.ims.interfaces.resource.database.RdsPublicCloudInstanceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author ymh
 * @date 2021/08/13
 */
@RestController
@RequestMapping("/v1/rds/pc")
public class RdsPublicCloudInstanceController {

	@Autowired
	private RdsPublicCloudInstanceService rdsPublicCloudInstanceService;

	/**
	 * 查询RDS实例列表
	 *
	 * @param pager
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询RDS实例列表", httpMethod = "GET")
	@GetMapping(value = "/instances")
	public BocloudResult list(Pager pager) {
		return rdsPublicCloudInstanceService.list(pager);
	}

	/**
	 * 查询RDS实例详情
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询RDS实例详情", httpMethod = "GET")
	@GetMapping(value = "/instances/{id}")
	public BocloudResult getRdsInsDetail(@PathVariable(value = Common.ID) Long id) throws Exception {
		return rdsPublicCloudInstanceService.detail(id);
	}

	/**
	 * 创建rds账号
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "创建rds账号", httpMethod = "POST")
	@PostMapping(value = "/instances/{id}/users")
	public BocloudResult createUser(@PathVariable(value = Common.ID) Long id, @RequestBody RdsUser rdsUser,
			@Value(Common.REQ_CONTEXT) RequestContext context) throws Exception {
		return rdsPublicCloudInstanceService.createUser(id, rdsUser, context);
	}

	/**
	 * 创建rds数据库
	 *
	 * @param id
	 * @param rdsDb
	 * @param context
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "创建rds数据库", httpMethod = "POST")
	@PostMapping(value = "/instances/{id}/dbs")
	public BocloudResult createDb(@PathVariable(value = Common.ID) Long id, @RequestBody RdsDb rdsDb,
			@Value(Common.REQ_CONTEXT) RequestContext context) throws Exception {
		return rdsPublicCloudInstanceService.createDb(id, rdsDb, context);
	}

	/**
	 * 删除rds数据库
	 *
	 * @param id
	 * @param dbId
	 * @param context
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "删除rds数据库", httpMethod = "DELETE")
	@DeleteMapping(value = "/instances/{id}/dbs/{dbId}")
	public BocloudResult deleteDb(@PathVariable(value = Common.ID) Long id, @PathVariable(value = "dbId") Long dbId,
			@Value(Common.REQ_CONTEXT) RequestContext context) throws Exception {
		return rdsPublicCloudInstanceService.deleteDb(id, dbId, context);
	}

	/**
	 * rds账号列表
	 *
	 * @param pager
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "rds账号列表", httpMethod = "GET")
	@GetMapping(value = "/instances/{id}/users")
	public BocloudResult getUsers(Pager pager) throws Exception {
		return rdsPublicCloudInstanceService.getUsers(pager);
	}

	/**
	 * 删除rds账号
	 *
	 * @param id
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "删除rds账号", httpMethod = "DELETE")
	@DeleteMapping(value = "/instances/{id}/users/{userId}")
	public BocloudResult deleteUser(@PathVariable(value = Common.ID) Long id,
			@PathVariable(value = "userId") Long userId, @Value(Common.REQ_CONTEXT) RequestContext context)
			throws Exception {
		return rdsPublicCloudInstanceService.deleteUser(id, userId, context);
	}

	/**
	 * 重置rds账号密码
	 *
	 * @param id
	 * @param rdsUser
	 * @param context
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "重置rds账号密码", httpMethod = "PUT")
	@PutMapping(value = "/instances/{id}/users/{userId}/resetpassword")
	public BocloudResult resetAccountPassword(@PathVariable(value = Common.ID) Long id, @RequestBody RdsUser rdsUser,
			@Value(Common.REQ_CONTEXT) RequestContext context) throws Exception {
		return rdsPublicCloudInstanceService.resetAccountPassword(id, rdsUser, context);
	}

	/**
	 * rds账号高级权限重置
	 *
	 * @param id
	 * @param rdsUser
	 * @param context
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "rds账号高级权限重置", httpMethod = "PUT")
	@PutMapping(value = "/instances/{id}/users/{userId}/resetaccount")
	public BocloudResult resetAccount(@PathVariable(value = Common.ID) Long id, @RequestBody RdsUser rdsUser,
			@Value(Common.REQ_CONTEXT) RequestContext context) throws Exception {
		return rdsPublicCloudInstanceService.resetAccount(id, rdsUser, context);
	}

	/**
	 * 账号权限授权
	 *
	 * @param id
	 * @param rdsUser
	 * @param context
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "账号权限授权", httpMethod = "PUT")
	@PutMapping(value = "/instances/{id}/users/{userId}/dbs")
	public BocloudResult reset(@PathVariable(value = Common.ID) Long id, @RequestBody RdsUser rdsUser,
			@Value(Common.REQ_CONTEXT) RequestContext context) throws Exception {
		return rdsPublicCloudInstanceService.reset(id, rdsUser, context);
	}

	/**
	 * 数据库列表
	 *
	 * @param pager
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "数据库列表", httpMethod = "PUT")
	@GetMapping(value = "/instances/{id}/dbs")
	public BocloudResult getDbs(Pager pager) throws Exception {
		return rdsPublicCloudInstanceService.getDbs(pager);
	}

	/**
	 * 创建RDS实例
	 *
	 * @param instance
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "创建RDS实例", httpMethod = "POST")
	@PostMapping(value = "/instances")
	public BocloudResult save(@RequestBody RdsPublicCloudInstance instance,
			@Value(Common.REQ_CONTEXT) RequestContext context) throws Exception {
		if (null != instance) {
			return rdsPublicCloudInstanceService.create(instance, context);
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 查询RDS实例重启
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询RDS实例重启", httpMethod = "PATCH")
	@PatchMapping(value = "/instances/{id}")
	public BocloudResult restart(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) throws Exception {
		return rdsPublicCloudInstanceService.restart(id, context);
	}

	/**
	 * 释放RDS实例
	 *
	 * @param id
	 * @param context
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "释放RDS实例", httpMethod = "DELETE")
	@DeleteMapping(value = "/instances/{id}")
	public BocloudResult delete(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) throws Exception {
		return rdsPublicCloudInstanceService.delete(id, context);
	}

	/**
	 * 查看用户可选的地域和可用区
	 *
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查看用户可选的地域和可用区", httpMethod = "GET")
	@GetMapping(value = "/regions")
	public BocloudResult getRegions(@RequestParam(value = "vendorId") Long vendorId,
			@RequestParam(value = "type") String type, @RequestParam(value = "volType") String volType)
			throws Exception {
		return rdsPublicCloudInstanceService.getRegions(vendorId, type, volType);
	}

	/**
	 * 查询规格
	 *
	 * @param category
	 * @param vendorId
	 * @param regionId
	 * @param zone
	 * @param engine
	 * @param version
	 * @param volType
	 * @param payType
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询规格", httpMethod = "GET")
	@GetMapping(value = "/flavor")
	public BocloudResult flavors(@RequestParam(value = "category") String category,
			@RequestParam(value = "vendorId") Long vendorId, @RequestParam(value = "regionId") String regionId,
			@RequestParam(value = "zone") String zone, @RequestParam(value = "engine") String engine,
			@RequestParam(value = "version") String version, @RequestParam(value = "volType") String volType,
			@RequestParam(value = "payType") String payType) throws Exception {
		return rdsPublicCloudInstanceService.flavors(category, vendorId, regionId, zone, engine, version, volType,
				payType);
	}

	/**
	 * 查询参数模板
	 *
	 * @param vendorId
	 * @param engine
	 * @param version
	 * @param regionId
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询参数模板", httpMethod = "GET")
	@GetMapping(value = "/param/groups")
	public BocloudResult paramGroups(@RequestParam(value = "vendorId") Long vendorId,
			@RequestParam(value = "engine") String engine, @RequestParam(value = "version") String version,
			@RequestParam(value = "regionId") String regionId) throws Exception {
		return rdsPublicCloudInstanceService.paramGroups(vendorId, regionId, engine, version);
	}

}