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.ims.entity.resource.ResourceGroup;
import com.bocloud.ims.interfaces.resource.ResourceGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: AzureResourceGroupsController
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2019/8/8 13:42
 */
@Api(value = "资源组", tags = "资源组管理")
@RestController
@RequestMapping("/v1/resource/groups")
public class ResourceGroupController {

    @Qualifier("resourceGroupService")
    @Autowired
    private ResourceGroupService resourceGroupService;

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

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

    /**
     * 创建
     *
     * @param resourceGroup
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "资源组", operation = "创建资源组")
    @PostMapping
    public BocloudResult<String> create(@RequestBody ResourceGroup resourceGroup,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return resourceGroupService.create(resourceGroup, context);
    }

    /**
     * 删除资源组
     *
     * @param id
     * @param context
     * @return
     */
    @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 resourceGroupService.remove(id, context);
    }

    /**
     * 编辑资源组
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "编辑",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "资源组", operation = "编辑资源组")
    @PutMapping("/{id}")
    public BocloudResult<String> modify(@PathVariable(value = Common.ID) Long id,
                                        @RequestParam(value = Common.PARAMS) String params, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return resourceGroupService.modify(id, params, context);
    }

    /**
     * 关联资源
     *
     * @param groupId
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "关联",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "资源组", operation = "关联资源")
    @PutMapping("/relateDevs/{id}")
    public BocloudResult relateDevices(@PathVariable(value = Common.ID) Long groupId,
                                       @RequestParam(value = Common.PARAMS) String params, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return resourceGroupService.relateDevices(groupId, params, context);
    }

    /**
     * 移除资源
     *
     * @param groupId
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "移除",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "资源组", operation = "移除资源")
    @PutMapping("/removeDevs/{id}")
    public BocloudResult removeDevices(@PathVariable(value = Common.ID) Long groupId,
                                       @RequestParam(value = Common.PARAMS) String params, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return resourceGroupService.removeDevices(groupId, params, context);
    }

    /**
     * 查询未关联到该资产组的资产
     *
     * @param groupId
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询未关联到该资产组的资产",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "资源组", operation = "查询未关联到该资产组的资产")
    @GetMapping("/addResource/{id}")
    public BocloudResult getOtherResource(@PathVariable(value = Common.ID) Long groupId,
                                          @RequestParam(name = "resourceIp") String resourceIp) {
        return resourceGroupService.getOtherResource(groupId, resourceIp);
    }
}
