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

import com.alibaba.fastjson.JSONObject;
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.Port;
import com.bocloud.ims.interfaces.resource.PortService;
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.*;

import java.util.List;

@RestController
@RequestMapping("/v1")
@Api(value = "端口", tags = "端口")
public class PortController {

    @Autowired
    private PortService portService;

    /**
     * 列表
     *
     * @param pager
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询端口列表", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "端口", operation = "查询端口列表")
    @GetMapping("/ports")
    public BocloudResult<GridBean<Port>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return portService.list(pager);
    }

    /**
     * 创建
     *
     * @param port
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建端口", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "端口", operation = "创建端口")
    @PostMapping("/ports")
    public BocloudResult create(@RequestBody Port port,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return portService.create(port, context);
    }

    /**
     * 修改
     *
     * @param id
     * @param port
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改端口", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "端口", operation = "修改端口")
    @PutMapping(value = "/ports/{id}")
    public BocloudResult modify(@PathVariable(value = Common.ID) Long id,
                                @RequestBody Port port,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        port.setId(id);
        return portService.modify(JSONObject.toJSONString(port), context);
    }

    /**
     * 删除
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除端口", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "端口", operation = "删除端口")
    @DeleteMapping(value = "/ports/{id}")
    public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return portService.remove(id, context);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查看端口详情", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "端口", operation = "查看端口详情")
    @GetMapping(value = "/ports/{id}")
    public BocloudResult<Port> detail(@PathVariable(value = Common.ID) Long id) {
        return portService.detail(id);
    }

    /**
     * 查询与虚拟机绑定的连接外网的端口
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "根据networkId查询端口", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "端口", operation = "根据networkId查询端口")
    @GetMapping("/networks/{id}/ports")
    public BocloudResult<List<Port>> listByNetworkId(@RequestParam(value = Common.PARAMS, required = true) String params,
                                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        return portService.listByNetworkId(params);
    }

    /**
     * 批量删除端口
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "端口批量删除", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "端口", operation = "批量删除端口")
    @DeleteMapping("/ports")
    public BocloudResult batchRemove(@RequestBody List<Long> ids,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        return portService.batchRemove(ids, context);
    }
}
