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

import com.bocloud.cmp.model.LbTargetGroupOperationParam;
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.LbTargetGroup;
import com.bocloud.ims.interfaces.resource.LbTargetGroupService;
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.*;

@RestController
@RequestMapping("/v1/lb/targetgroups")
@Api(value = "目标群组", tags = "目标群组")
public class LbTargetGroupController {

	@Autowired
	private LbTargetGroupService lbTargetGroupService;

	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查询目标群组列表",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "目标群组", operation = "查询目标群组列表")
	@GetMapping
	public BocloudResult<GridBean<LbTargetGroup>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return lbTargetGroupService.list(pager, context);
	}

	/**
	 * 创建目标群组
	 *
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "创建目标群组",  httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "目标群组", operation = "创建目标群组")
	@PostMapping
	public BocloudResult<LbTargetGroup> create(@RequestBody LbTargetGroup lbTargetGroup,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != lbTargetGroup) {
			return lbTargetGroupService.create(lbTargetGroup, context);
		}
		return ResultTools.formatErrResult();
	}

	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "删除目标群组",  httpMethod = "DELETE")
	@Log(module = BoCloudService.IMS, target = "目标群组", operation = "删除目标群组")
	@DeleteMapping("/{id}")
	public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return lbTargetGroupService.remove(id, context);
	}

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

	/**
	 * TargetGroup操作类
	 *
	 * @param id
	 * @param lbTargetGroupOperationParam
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = {"CMC",
			"CSC"}, value = "注册目标", httpMethod = "PATCH")
	@Log(module = BoCloudService.IMS, target = "群组目标", operation = "注册目标")
	@PatchMapping(value = "/{id}/operation")
	public BocloudResult operation(@PathVariable(value = Common.ID) Long id,
								   @RequestBody LbTargetGroupOperationParam lbTargetGroupOperationParam,
								   @Value(Common.REQ_CONTEXT) RequestContext context) {
		return lbTargetGroupService.operations(id, lbTargetGroupOperationParam, context);
	}

}
