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

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.ims.entity.resource.LBAddBean;
import com.bocloud.ims.entity.resource.LbListener;
import com.bocloud.ims.interfaces.resource.LbListenerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

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.*;

@RestController
@RequestMapping("/v1/lb/listeners")
@Api(value = "负载均衡监听器", tags = "负载均衡监听器")
public class LbListenerController {

	@Autowired
	private LbListenerService lbListenerService;

	/**
	 * 查看负载均衡监听器列表
	 *
	 * @param pager
	 * @param context
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查询负载均衡监听器列表",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "负载均衡监听器", operation = "查看负载均衡监听器列表")
	@GetMapping
	public BocloudResult<GridBean<LbListener>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) throws Exception {
		return lbListenerService.list(pager, context);
	}

	/**
	 * 创建负载均衡监听器
	 *
	 * @param bean
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "创建负载均衡监听器",  httpMethod = "POST")
	@Log(module = BoCloudService.IMS, target = "负载均衡监听器", operation = "创建负载均衡监听器")
	@PostMapping
	public BocloudResult create(@RequestBody LBAddBean bean, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return lbListenerService.create(bean, context, null);
	}

	/**
	 * 修改
	 *
	 * @param id
	 * @param listener
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "修改负载均衡监听器",  httpMethod = "PUT")
	@Log(module = BoCloudService.IMS, target = "负载均衡监听器", operation = "修改负载均衡监听器")
	@PutMapping("/{id}")
	public BocloudResult modify(@PathVariable(value = Common.ID) Long id, @RequestBody LbListener listener,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		listener.setId(id);
		return lbListenerService.modify(listener, context.getTarget());
	}

	/**
	 * 详情
	 *z
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC",
			"CSC" }, value = "查看负载均衡监听器详情",  httpMethod = "GET")
	@Log(module = BoCloudService.IMS, target = "负载均衡监听器", operation = "查看负载均衡监听器")
	@GetMapping("/{id}")
	public BocloudResult<LbListener> detail(@PathVariable(value = Common.ID) Long id) {
		return lbListenerService.detail(id);
	}

	/**
	 * 删除
	 *
	 * @param id
	 * @return
	 */
	@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 lbListenerService.remove(id, context);
	}

	/**
	 * 批量删除负载均衡监听器
	 *
	 * @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 lbListenerService.batchRemove(idArray, context);
	}
}
