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

import java.util.List;

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.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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
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.resource.kubernetes.KubeContainer;
import com.bocloud.ims.interfaces.resource.kubernetes.KubeContainerService;

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

@RestController
@RequestMapping("/v1/kubernetes/containers")
@Api(value = "容器管理", tags = "容器管理")
public class KubeContainerController {

	@Autowired
	private KubeContainerService kubeContainerService;

	/**
	 * 列表
	 * 
	 * @param page    当前页码
	 * @param rows    页面数据大小
	 * @param params  查询参数，例如：[{"param":{"name":"aaa","password":"1245"},"sign":
	 *                "EQ|UEQ"},{"param":{"name":"aaa","password":"1245"},"sign":
	 *                "EQ|UEQ"}]
	 * @param sorter  排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
	 * @param simple  简单查询标记，只有true和false,为false时返回用户的详细信息，为true时只返回id和name值。
	 * @param context 操作者信息
	 * @return 数据结果集
	 */
	@GetMapping
	@Log(module = BoCloudService.IMS, target = "Kubernetes容器", operation = "查看容器列表")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "容器列表", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return kubeContainerService.list(pager, context);
	}

	/**
	 * 添加
	 * 
	 * @param params
	 * @param context
	 * @return 添加结果
	 */
	@PostMapping
	@Log(module = BoCloudService.IMS, target = "Kubernetes容器", operation = "添加容器")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "容器添加", response = BocloudResult.class, httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult create(@RequestBody KubeContainer kubeContainer,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != kubeContainer) {
			return kubeContainerService.create(kubeContainer, context.getTarget());
		}
		return ResultTools.formatErrResult();

	}

	/**
	 * 修改
	 * 
	 * @param params
	 * @param id
	 * @param context
	 * @return
	 */
	@PutMapping(value = "/{id}")
	@Log(module = BoCloudService.IMS, target = "Kubernetes容器", operation = "修改容器")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "容器编辑", response = BocloudResult.class, httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult modify(@RequestBody KubeContainer kubeContainer, @PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != kubeContainer) {
			kubeContainer.setId(id);
			return kubeContainerService.modify(kubeContainer, context.getTarget());
		}
		return ResultTools.formatErrResult();
	}

	/**
	 * 移除
	 * 
	 * @param id
	 * @param context
	 * @return 操作结果
	 */
	@DeleteMapping(value = "/{id}")
	@Log(module = BoCloudService.IMS, target = "Kubernetes容器", operation = "移除容器")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "容器删除", response = BocloudResult.class, httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return kubeContainerService.remove(id, context.getTarget());
	}

	/**
	 * 移除
	 * 
	 * @param id
	 * @param context
	 * @return 操作结果
	 */
	@DeleteMapping()
	@Log(module = BoCloudService.IMS, target = "Kubernetes容器", operation = "移除容器")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "容器批量删除", response = BocloudResult.class, httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult batchRemove(@RequestBody List<Long> ids, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return kubeContainerService.batchRemove(ids, context.getTarget());
	}

	/**
	 * 详细信息
	 * 
	 * @param id
	 * @return 操作结果
	 */
	@GetMapping(value = "/{id}")
	@Log(module = BoCloudService.IMS, target = "Kubernetes容器", operation = "查看容器详情")
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "容器详情", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult detail(@PathVariable(value = Common.ID) Long id) {
		return kubeContainerService.detail(id);
	}

}
