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

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.enums.ResourceCategory;
import com.bocloud.cmp.model.common.ApplicationParam;
import com.bocloud.cmp.model.common.CancelationParam;
import com.bocloud.cmp.model.common.ExtensionParam;
import com.bocloud.cmp.params.azure.CloudDatabaseServerOperationParams;
import com.bocloud.cmp.params.azure.UserAssignParams;
import com.bocloud.cmp.vm.model.ResourceAssignParam;
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.db.AzureDatabaseServer;
import com.bocloud.ims.interfaces.resource.ResourceAllocateService;
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.web.bind.annotation.*;

import java.util.ArrayList;
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;
    @Autowired
    private ResourceAllocateService resourceAllocateService;

    /**
     * 分配给用户
     *
     * @param id
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "MySQL用户分配", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "MySQL", operation = "MySQL用户分配")
    @PostMapping("/{id}/users")
    public BocloudResult assign(@PathVariable(value = Common.ID) Long id,
                                @RequestBody UserAssignParams params,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        JSONObject obj = new JSONObject();
        obj.put("projectId", params.getProjectId());
        obj.put("tenantId", params.getTenantId());
        obj.put("userId", params.getUserId());
        obj.put("id", params.getId());
        return resourceAllocateService.assign(obj, ResourceCategory.MySQL, context);
    }

    /**
     * 分配给用户
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "MySQL批量用户分配", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "MySQL", operation = "MySQL批量用户分配")
    @PostMapping("/users/batch")
    public BocloudResult batchAssign(@RequestBody UserAssignParams params, @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null == params) {
            return ResultTools.formatErrResult();
        }
        List<Long> ids = params.getIds();
        List<BocloudResult> succList = new ArrayList<>();
        List<BocloudResult> failList = new ArrayList<>();
        for (Long id : ids) {
            JSONObject obj = new JSONObject();
            obj.put("projectId", params.getProjectId());
            obj.put("tenantId", params.getTenantId());
            obj.put("userId", params.getUserId());
            obj.put("id", id);
            BocloudResult result = resourceAllocateService.assign(obj, ResourceCategory.MySQL, context);
            if (result.isSuccess()) {
                succList.add(result);
            } else {
                failList.add(result);
            }
        }
        return new BocloudResult(true, "MySQL分配成功【" + succList.size() + "】个，失败【" + failList.size() + "】个");
    }

    @DeleteMapping("/{id}/users")
    public BocloudResult userReclaim(@PathVariable(value = Common.ID) Long id,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        ResourceAssignParam param = new ResourceAssignParam();
        param.setId(id);
        return resourceAllocateService.reclaim(param, ResourceCategory.MySQL, context);
    }

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

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


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

    /**
     * 删除
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServerv", 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 params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "批量删除", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServerv", operation = "删除cloudDatabaseServer")
    @DeleteMapping
    public BocloudResult removeBatch(
            @RequestBody CloudDatabaseServerOperationParams params,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        List<Long> ids = params.getIds();
        for (Long id : ids) {
            service.remove(id, context);
        }
        return new BocloudResult(true, "操作成功");
    }


    /**
     * 调整配置
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "调整配置", 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 params,
                                      @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.pricingLayer(params, context);
    }

    /**
     * mysql申请
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "mysql申请", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServer", operation = "mysql申请")
    @PostMapping(value = "/apply")
    public BocloudResult apply(@RequestBody ApplicationParam params,
                               @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.apply(params);
    }

    /**
     * mysql延期
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "mysql延期", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServer", operation = "mysql延期")
    @PostMapping(value = "/extension")
    public BocloudResult extension(@RequestBody ExtensionParam params,
                                   @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.extension(params);
    }

    /**
     * mysql删除
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "mysql删除", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "cloudDatabaseServer", operation = "mysql删除")
    @PostMapping(value = "/cancelation")
    public BocloudResult cancelation(@RequestBody CancelationParam params,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.cancelation(params);
    }

}