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

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

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.RouteTable;
import com.bocloud.ims.interfaces.resource.RouteTableService;

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

@RestController
@RequestMapping("/v1/routetables")
@Api(value = "路由表", tags = "路由表")
public class RouteTableController {

    @Autowired
    private RouteTableService routeTableService;

    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询路由表列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "路由表", operation = "查询路由表列表")
    @GetMapping
    public BocloudResult<GridBean<RouteTable>> list(Pager pager) {
        return routeTableService.list(pager);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建路由表", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "路由表", operation = "创建路由表")
    @PostMapping
    public BocloudResult<RouteTable> create(@RequestBody RouteTable routeTable,
        @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != routeTable) {
            return routeTableService.create(routeTable, context);
        }
        return ResultTools.formatErrResult();
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除路由表", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "路由表", operation = "删除路由表")
    @DeleteMapping("/{id}")
    public BocloudResult<String> remove(@PathVariable(value = Common.ID) Long id,
        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return routeTableService.remove(id, context);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "更新路由表", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "路由表", operation = "更新路由表")
    @PutMapping(value = "/{id}")
    public BocloudResult<RouteTable> modify(@PathVariable(value = Common.ID) Long id,
        @RequestBody RouteTable routeTable, @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != routeTable) {
            return routeTableService.modify(routeTable, context);
        }
        return ResultTools.formatErrResult();
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "查看路由表详情", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "路由表", operation = "查看路由表详情")
    @GetMapping("/{id}")
    public BocloudResult<RouteTable> detail(@PathVariable(value = Common.ID) Long id) {
        return routeTableService.detail(id);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "查看未关联子网的路由表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "路由表", operation = "查看未关联子网的路由表")
    @GetMapping("/subnet/{subnetId}")
    public BocloudResult<List<RouteTable>> listNotAttachBySubnet(@PathVariable(value = "subnetId") Long subnetId) {
        return routeTableService.listNotAttachBySubnet(subnetId);
    }

}
