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

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.common.utils.JSONTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.VpnConn;
import com.bocloud.ims.interfaces.resource.VpnConnService;
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/vpnconns")
@Api(value = "VPN站点管理", tags = "VPN站点管理")
public class VpnConnController {

	@Autowired
	private VpnConnService vpnConnService;

	/**
	 * 查询VPN站点连接列表
	 *
	 * @param pager
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询vpn站点列表", httpMethod = "GET")
	@GetMapping
	public BocloudResult<GridBean<VpnConn>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return vpnConnService.list(pager, context);
	}

	/**
	 * 创建
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "创建VPN站点", httpMethod = "POST")
	@PostMapping
	public BocloudResult<String> create(@RequestBody VpnConn vpnConn,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return vpnConnService.create(vpnConn, context);
	}

	/**
	 * 修改
	 *
	 * @param id
	 * @param bean
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "修改VPN站点", httpMethod = "PUT")
	@PutMapping("/{id}")
	public BocloudResult<String> modify(@PathVariable(value = Common.ID) Long id, @RequestBody VpnConn bean,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		bean.setId(id);
		return vpnConnService.modify(bean, context);
	}

	/**
	 * 删除
	 *
	 * @param id
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "删除VPN站点", httpMethod = "DELETE")
	@DeleteMapping("/{id}")
	public BocloudResult<String> remove(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return vpnConnService.remove(id, context);
	}

	/**
	 * 详情
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "查询VPN站点详情", httpMethod = "GET")
	@GetMapping("/{id}")
	public BocloudResult<VpnConn> detail(@PathVariable(value = Common.ID) Long id) {
		return vpnConnService.detail(id);
	}

	/**
	 * 删除vpn连接
	 *
	 * @param params
	 * @param context
	 * @return
	 */
	@ApiOperation(tags = { "CMC", "CSC" }, value = "删除VPNCONN", httpMethod = "DELETE")
	@DeleteMapping
	public BocloudResult<String> removeVpnConn(@RequestParam(value = Common.PARAMS) String params,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			return vpnConnService.removeVpnConn(params, context);
		} else {
			return ResultTools.formatErrResult();
		}
	}

}
