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.SecurityGroupRule;
import com.bocloud.ims.interfaces.resource.SecurityGroupRuleService;
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/sgroups/rules")
@Api(value = "安全组规则", tags = "安全组规则")
public class SecurityGroupRuleController {

    @Autowired
    private SecurityGroupRuleService securityGroupRuleService;

    /**
     * 列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询安全组规则列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "安全组规则", operation = "查看安全组规则列表")
    @GetMapping
    public BocloudResult<GridBean<SecurityGroupRule>> list(Pager pager) {
        return securityGroupRuleService.list(pager);
    }

    /**
     * 创建
     *
     * @param rule
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建安全组规则",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "安全组规则", operation = "创建安全组规则")
    @PostMapping
    public BocloudResult<SecurityGroupRule> create(@RequestBody SecurityGroupRule rule,
                                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        rule.setCreatorId(context.getTarget());
        rule.setMenderId(context.getTarget());
        rule.setTenantId(context.getTenant());
        return securityGroupRuleService.create(rule);
    }

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

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查询安全组规则详情",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "安全组规则", operation = "查看安全组规则详情")
    @GetMapping(value = "/{id}")
    public BocloudResult<SecurityGroupRule> detail(@PathVariable(value = Common.ID) Long id) {
        return securityGroupRuleService.detail(id);
    }

    /**
     * 批量删除安全组规则
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "安全组规则批量删除",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "安全组规则", operation = "批量删除安全组规则")
    @DeleteMapping
    public BocloudResult batchRemove(@RequestBody List<Long> idArray,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        return securityGroupRuleService.batchRemove(idArray, context);
    }
}
