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.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.RouteTableAssociation;
import com.bocloud.ims.interfaces.resource.RouteTableAssociationService;
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.*;

@RestController
@RequestMapping("/v1/routetables/associations")
@Api(value = "路由关联", tags = "路由关联")
public class RouteTableAssociationController {

    @Autowired
    private RouteTableAssociationService associationService;

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

    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "修改关联",  httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "路由关联", operation = "修改关联")
    @PatchMapping
    public BocloudResult<String> replace(@RequestBody RouteTableAssociation association,
                                         @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != association) {
            return associationService.replace(association, context);
        }
        return ResultTools.formatErrResult();
    }

    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "创建关联",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "路由关联", operation = "创建关联")
    @PostMapping
    public BocloudResult<String> associate(@RequestBody RouteTableAssociation association,
                                           @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != association) {
            return associationService.associate(association, context);
        }
        return ResultTools.formatErrResult();
    }

    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "删除关联",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "路由关联", operation = "删除关联")
    @DeleteMapping("/{id}")
    public BocloudResult<String> disassociate(@PathVariable(value = Common.ID) Long id,
                                              @Value(Common.REQ_CONTEXT) RequestContext context,
                                              @RequestBody RouteTableAssociation association) {
        if (null != association) {
            return associationService.disassociate(association, context);
        }
        return ResultTools.formatErrResult();
    }

}
