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

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.LbListenerRule;
import com.bocloud.ims.interfaces.resource.LbListenerRuleService;
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;

/**
 * @ClassName: LbListenerRuleController
 * @Description:
 * @Author lxy
 * @Date 2021/10/19 16:13
 */
@RestController
@RequestMapping("/v1/lb/listener/rules")
@Api(value = "负载均衡监听器转发规则", tags = "负载均衡监听器转发规则")
public class LbListenerRuleController {
    @Autowired
    private LbListenerRuleService lbListenerRuleService;

    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查询监听器转发规则列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "监听器转发规则", operation = "查看负载均衡监听器列表")
    @GetMapping
    public BocloudResult<GridBean<LbListenerRule>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) throws Exception {
        return lbListenerRuleService.list(pager, context);
    }

    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "创建监听器转发规则", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "监听器转发规则", operation = "创建监听器转发规则")
    @PostMapping
    public BocloudResult create(@RequestBody LbListenerRule listenerRule, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return lbListenerRuleService.create(listenerRule, context);
    }

    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "修改监听器转发规则", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "监听器转发规则", operation = "修改监听器转发规则")
    @PutMapping("/{id}")
    public BocloudResult modify(@PathVariable(value = Common.ID) Long id, @RequestBody LbListenerRule listenerRule,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        listenerRule.setId(id);
        return lbListenerRuleService.modify(listenerRule, context.getTarget());
    }

    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查看监听器转发规则详情", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "监听器转发规则", operation = "查看监听器转发规则详情")
    @GetMapping("/{id}")
    public BocloudResult<LbListenerRule> detail(@PathVariable(value = Common.ID) Long id) {
        return lbListenerRuleService.detail(id);
    }

    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "删除监听器转发规则", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "监听器转发规则", operation = "删除监听器转发规则")
    @DeleteMapping("/{id}")
    public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return lbListenerRuleService.remove(id, context);
    }

    @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 lbListenerRuleService.batchRemove(idArray, context);
    }
}
