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

import com.bocloud.cmp.params.azure.FrontEndOperationParams;
import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.ims.entity.resource.azure.slb.FrontEnd;
import com.bocloud.ims.interfaces.resource.azure.slb.FrontEndService;
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;

@Api(value = "前端IP配置", tags = "前端IP配置管理")
@RestController
@RequestMapping("/v1/frontends")
public class FrontEndController {

    @Autowired
    private FrontEndService service;

    /**
     * 列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "列表前端IP配置", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "前端IP配置", operation = "列表前端IP配置")
    @GetMapping
    public BocloudResult list(Pager pager,
                              @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.list(pager, context);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "详情前端IP配置", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "前端IP配置", operation = "详情前端IP配置")
    @GetMapping("/{id}")
    public BocloudResult detail(@PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.detail(id, context);
    }

    /**
     * 创建
     *
     * @param entityParams
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建前端IP配置", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "前端IP配置", operation = "创建前端IP配置")
    @PostMapping
    public BocloudResult create(@RequestBody FrontEnd entityParams,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.create(entityParams, context);
    }

    /**
     * 创建
     *
     * @param entityParams
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "批量创建前端IP配置", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "前端IP配置", operation = "批量创建前端IP配置")
    @PostMapping("/creates")
    public BocloudResult creates(@RequestBody List<FrontEnd> entityParams,
                                 @Value(Common.REQ_CONTEXT) RequestContext context) {
        //金山云poc支持批量创建
        for (int i = 0; i < entityParams.size(); i++) {
            service.create(entityParams.get(i), context);
        }
        return new BocloudResult(true, "批量任务正在下发，请耐心等待！");
    }

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

    /**
     * 删除
     *
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "批量删除前端IP配置", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "前端IP配置", operation = "批量删除前端IP配置")
    @DeleteMapping
    public BocloudResult removeBatch(@RequestBody FrontEndOperationParams params,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        for (Long id : params.getIds()) {
            service.remove(id, context);
        }
        return new BocloudResult(true, "操作成功");
    }


}
