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

import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
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.resource.Room;
import com.bocloud.ims.interfaces.resource.RoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机房相关接口
 *
 * @author dmw
 */
@RestController
@RequestMapping("/v1/rooms")
@Api(value = "机房", tags = "机房")
public class RoomController {
	@Autowired
	private RoomService roomService;

	/**
	 * 获取机房列表
	 *
	 * @param pager
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查询机房列表",  httpMethod = "GET")
	@GetMapping
	@Log(module = BoCloudService.IMS, target = "机房", operation = "查询机房列表")
	public BocloudResult<GridBean<Room>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return roomService.list(pager, context);
	}

	/**
	 * 获取机房列表
	 *
	 * @param pager
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "通过condition方式查询机房列表",  httpMethod = "GET")
	@GetMapping("/condition")
	@Log(module = BoCloudService.IMS, target = "机房", operation = "查询机房列表")
	public BocloudResult<List<Room>> condition(@RequestParam(value = Common.CONDITION, required = false) String condition,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return roomService.condition(condition, context);
	}

	/**
	 * 添加机房
	 *
	 * @param room
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "添加机房",  httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "机房", operation = "添加机房")
	@PostMapping
	public BocloudResult<Room> create(@RequestBody Room room, @Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != room) {
			return roomService.create(room, context.getTarget());
		}
		return ResultTools.formatErrResult();
	}

	/**
	 * 修改机房
	 *
	 * @param room
	 * @param id
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "修改机房",  httpMethod = "PUT")
	@Log(module = BoCloudService.IMS, target = "机房", operation = "修改机房")
	@PutMapping(value = "/{id}")
	public BocloudResult<String> modify(@RequestBody Room room, @PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != room) {
			room.setId(id);
			return roomService.modify(room, context.getTarget());
		}
		return ResultTools.formatErrResult();
	}

	/**
	 * 移除机房
	 *
	 * @param id
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "移除机房",  httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "机房", operation = "移除机房")
	@DeleteMapping(value = "/{id}")
	public BocloudResult<String> remove(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return roomService.remove(id, context.getTarget());
	}

	/**
	 * 查看机房详细信息
	 *
	 * @param id                  机房ID
	 * @param context.getTarget() 操作者ID
	 * @return 操作结果
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查看机房详情",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "机房", operation = "查看机房详情")
	@GetMapping(value = "/{id}")
	public BocloudResult<Room> detail(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context,
			@RequestParam(value = Common.ACTION, required = false) String action) {
		return roomService.details(id, action);
	}

}
