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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.ims.entity.VolumeType;
import com.bocloud.ims.interfaces.resource.VolumeTypeService;
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.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/volume/types")
@Api(value = "云硬盘类型", tags = "云硬盘类型")
public class VolumeTypeController {

	@Autowired
	private VolumeTypeService volumeTypeService;

	/**
	 * 获取云硬盘类型列表
	 * 
	 * @param pager
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "获取云硬盘类型列表", httpMethod = "GET")
	@GetMapping
	public BocloudResult<GridBean<VolumeType>> list(Pager pager) {
		return volumeTypeService.list(pager);

	}

	/**
	 * 通过condition获取云硬盘类型列表
	 * 
	 * @param pager
	 * @param action
	 * @param condition
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "通过condition获取云硬盘类型列表", httpMethod = "GET")
	@GetMapping("/condition")
	public BocloudResult<List<VolumeType>> condition(
			@RequestParam(value = Common.ACTION, required = false) String action,
			@RequestParam(value = Common.CONDITION, required = false) String condition) {
		if ("listByZone".equals(action)) {
			JSONObject conditionObject = JSON.parseObject(condition);
			Long vendorId = conditionObject.getLong("vendorId");
			String zoneName = conditionObject.getString("zoneName");
			return volumeTypeService.listByZone(vendorId, zoneName, false);
		} else {
			return new BocloudResult<>(false, "不支持的查询类型");
		}
	}

	/**
	 * 添加云硬盘类型
	 *
	 * @param type
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "添加云硬盘类型", httpMethod = "POST")
	@PostMapping
	public BocloudResult<VolumeType> create(@RequestBody VolumeType type) {
		return volumeTypeService.create(type);
	}

	/**
	 * 删除云硬盘类型
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "删除云硬盘类型", httpMethod = "DELETE")
	@DeleteMapping("/{id}")
	public BocloudResult remove(@PathVariable(value = Common.ID) Long id) {
		return volumeTypeService.remove(id);
	}

	/**
	 * 查看云硬盘类型详情
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查看云硬盘类型详情", httpMethod = "GET")
	@GetMapping("/{id}")
	public BocloudResult<VolumeType> detail(@PathVariable(value = Common.ID) Long id) {
		return volumeTypeService.detail(id);
	}

	/**
	 * 批量删除云硬盘类型
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "批量删除云硬盘类型", httpMethod = "DELETE")
	@DeleteMapping
	public BocloudResult<String> batchRemove(@RequestBody List<Long> idArray,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return volumeTypeService.batchRemove(idArray, context);
	}
}
