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

import com.bocloud.cmp.params.azure.CloudDatabaseServerOperationParams;
import com.bocloud.cmp.params.azure.UserAssignParams;
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.db.AzureDatabaseServer;
import com.bocloud.ims.interfaces.resource.azure.db.CloudDatabaseServerService;
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;

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: DatabaseForMysqlController
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2020/12/18 15:51
 */
@Api(value = "cloudDatabaseServer", tags = "cloudDatabaseServer管理")
@RestController
@RequestMapping("/v1/cloud/database/servers")
public class CloudDatabaseServerController {

    @Autowired
    private CloudDatabaseServerService service;

    /**
     * 查询列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "列表cloudDatabaseServer", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServer", operation = "列表cloudDatabaseServer")
    @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 = "详情cloudDatabaseServer", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServer", operation = "详情cloudDatabaseServer")
    @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 = "创建cloudDatabaseServer", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServer", operation = "创建cloudDatabaseServer")
    @PostMapping
    public BocloudResult create(@RequestBody AzureDatabaseServer entityParams,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.create(entityParams, context);
    }

    /**
     * 修改
     *
     * @param entityParams
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "更新cloudDatabaseServer", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServer", operation = "更新cloudDatabaseServer")
    @PutMapping(value = "/{id}")
    public BocloudResult modify(@PathVariable(value = Common.ID) Long id,
                                @RequestBody AzureDatabaseServer entityParams,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.modify(entityParams, context);
    }

    /**
     * 操作
     *
     * @param entityParams
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "操作cloudDatabaseServer", httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServer", operation = "操作cloudDatabaseServer")
    @PatchMapping(value = "/{id}")
    public BocloudResult operations(@PathVariable(value = Common.ID) Long id,
                                    @RequestBody CloudDatabaseServerOperationParams entityParams,
                                    @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.operations(entityParams, context);
    }

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

    /**
     * 批量删除
     *
     * @param ids
     * @param context
     * @return
     */
    @ApiOperation(value = "批量删除", response = BocloudResult.class, httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServer", operation = "删除cloudDatabaseServer")
    @DeleteMapping
    public BocloudResult removeBatch(
            @RequestParam(value = "ids", required = false) List<Long> ids,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        for (Long id : ids) {
            service.remove(id, context);
        }
        return new BocloudResult(true, "操作成功");
    }

    /**
     * 用户分配
     *
     * @param id
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "用户分配cloudDatabaseServer", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServer", operation = "用户分配cloudDatabaseServer")
    @PostMapping("/{id}/users")
    public BocloudResult assign(@PathVariable(value = Common.ID) Long id,
                                @RequestBody UserAssignParams params,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.assign(params, context);
    }

    /**
     * 批量用户分配
     *
     * @param
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "批量用户分配cloudDatabaseServer", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServer", operation = "批量用户分配cloudDatabaseServer")
    @PostMapping("/users/batch")
    public BocloudResult assignBatch(
            @RequestBody UserAssignParams params,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.assignBatch(params, context);
    }

    /**
     * 调整配置
     *
     * @param entityParams
     * @param context
     * @return
     */
    @ApiOperation(value = "调整配置", response = BocloudResult.class, httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServer", operation = "调整配置cloudDatabaseServer")
    @PutMapping(value = "/{id}/pricing/layers")
    public BocloudResult pricingLayer(@PathVariable(value = Common.ID) Long id,
                                      @RequestBody AzureDatabaseServer entityParams,
                                      @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.pricingLayer(entityParams, context);
    }


}