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

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.model.SecurityGroupBean;
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.JSONTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.SecurityGroup;
import com.bocloud.ims.interfaces.resource.SecurityGroupService;
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;

@RestController
@RequestMapping("/v1/sgroups")
@Api(value = "安全组", tags = "安全组")
public class SecurityGroupController {

	@Autowired
	private SecurityGroupService securityGroupService;

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

	/**
	 * 创建
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "创建安全组",  httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "安全组", operation = "创建安全组")
	@PostMapping
	public BocloudResult<SecurityGroup> create(@RequestBody SecurityGroup securityGroup,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != securityGroup) {
			return securityGroupService.create(securityGroup, context);
		} else {
			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 remove(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return securityGroupService.remove(id, context);
	}

	/**
	 * 详情
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查看安全组详情",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "安全组", operation = "查看安全组详情")
	@GetMapping("/{id}")
	public BocloudResult<SecurityGroup> detail(@PathVariable(value = Common.ID) Long id) {
		return securityGroupService.detail(id);
	}

	/**
	 * 修改
	 *
	 * @param bean
	 * @param id
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "修改安全组",  httpMethod = "PUT")
	@Log(module = BoCloudService.IMS, target = "安全组", operation = "修改安全组")
	@PutMapping("/{id}")
	public BocloudResult<SecurityGroup> modify(@RequestBody SecurityGroupBean bean,
			@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_CONTEXT) RequestContext context) {
		bean.setId(id);
		return securityGroupService.modify(bean, context.getTarget());
	}

	/**
	 * 批量删除安全组
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "安全组批量删除",  httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "安全组", operation = "批量删除安全组")
	@DeleteMapping
	public BocloudResult batchRemove(@RequestBody List<Long> idArray,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return securityGroupService.batchRemove(idArray, context);
	}
}
