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

import com.bocloud.cmp.model.PortGroupBean;
import com.bocloud.cmp.model.PortGroupModel;
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.PortGroup;
import com.bocloud.ims.interfaces.resource.NetworkService;
import com.bocloud.ims.interfaces.resource.PortGroupService;
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.*;

import java.util.List;

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

    @Autowired
    private PortGroupService portGroupService;
    @Autowired
    private NetworkService networkService;

    /**
     * 查看端口组列表
     *
     * @param pager
     * @param condition
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "查询端口组列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "端口组", operation = "查看端口组列表")
    @GetMapping
    public BocloudResult<GridBean<PortGroup>> list(Pager pager) {
        return portGroupService.list(pager);
    }

    /**
     * 通过condition查询端口组列表
     *
     * @param pager
     * @param condition
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "通过condition查询端口组列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "端口组", operation = "查看端口组列表")
    @GetMapping("/condition")
    public BocloudResult<List<PortGroup>> condition(@RequestParam(value = Common.CONDITION, required = false) String condition) {
        return portGroupService.condition(condition);
    }

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

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

    /**
     * 更新端口组
     *
     * @param id
     * @param portGroupBean
     * @param context
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "更新端口组",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "端口组", operation = "更新端口组")
    @PutMapping("/{id}")
    public BocloudResult modifyPortGroup(@PathVariable(Common.ID) Long id,
                                         @RequestBody PortGroupBean portGroupBean, @Value(Common.REQ_CONTEXT) RequestContext context) {
        portGroupBean.setId(id);
        return networkService.modifyPortGroup(portGroupBean, context);
    }

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

}
