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

import com.bocloud.cmp.params.azure.AzureContainerParams;
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.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.azure.blob.Container;
import com.bocloud.ims.interfaces.resource.azure.blob.ContainerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * azure容器访问
 *
 * @author wangyu
 * @version 5.0.0-SNAPSHOT
 * @since 2021/2/2
 */
@RestController
@RequestMapping("/v1/azure/containers")
@Api(value = "容器管理管理", tags = "容器管理管理")
public class ContainerController {


    @Autowired
    private ContainerService containerService;

    @ApiOperation(tags = {"CMC", "CSC"}, value = "获取容器列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "获取容器列表")
    @GetMapping
    public BocloudResult list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return containerService.list(pager, context);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "添加容器", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "添加容器")
    @PostMapping
    public BocloudResult create(@RequestBody Container params,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return containerService.create(params, context);
    }


    @ApiOperation(tags = {"CMC", "CSC"}, value = "更新容器元数据", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "更新容器元数据")
    @PutMapping("/{id}/metadata")
    public BocloudResult updateMetadata(@PathVariable(value = Common.ID) Long id,
                                        @RequestBody Map<String, String> params, @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != params) {
            return containerService.updateMetadata(id, params, context);
        } else {
            return ResultTools.formatErrResult();
        }
    }


    @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 containerService.remove(id, context);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "查看容器详情", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "查看容器详情")
    @GetMapping("/{id}")
    public BocloudResult detail(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return containerService.detail(id, context);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "批量删除容器", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "批量删除容器")
    @DeleteMapping
    public BocloudResult batchRemove(@RequestBody AzureContainerParams params,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        return containerService.batchRemove(params, context);
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "分配容器", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "分配容器")
    @PostMapping("/{id}/users")
    public BocloudResult assign(@PathVariable(value = Common.ID) Long id, @RequestBody AzureContainerParams params, @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != params) {
            return containerService.assign(params, context);
        } else {
            return ResultTools.formatErrResult();
        }
    }


    @ApiOperation(tags = {"CMC", "CSC"}, value = "批量分配容器", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "批量分配容器")
    @PostMapping("/users/batch")
    public BocloudResult assignBatch(@RequestBody AzureContainerParams params, @Value(Common.REQ_CONTEXT) RequestContext context) {
        List<Long> ids = params.getIds();
        for (Long id : ids) {
            AzureContainerParams param = new AzureContainerParams();
            BeanUtils.copyProperties(params, param);
            param.setId(id);
            containerService.assign(params, context);
        }
        return new BocloudResult(true, "操作成功");
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "容器操作", httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "容器", operation = "容器操作")
    @PatchMapping("/{id}")
    public BocloudResult operations(@PathVariable(value = Common.ID) Long id,
                                    @RequestBody AzureContainerParams params,
                                    @Value(Common.REQ_CONTEXT) RequestContext context) {
        return containerService.operations(params, context);
    }

}
