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

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.enums.ResourceCategory;
import com.bocloud.cmp.params.azure.CloudRedisOperationParams;
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.redis.CloudRedis;
import com.bocloud.ims.interfaces.resource.ResourceAllocateService;
import com.bocloud.ims.interfaces.resource.azure.redis.CloudRedisService;
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: CloudRedisController
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/3/1 15:11
 */
@Api(value = "cloudRedis", tags = "cloudRedis管理")
@RestController
@RequestMapping("/v1/cloud/redis")
public class CloudRedisController {
    @Autowired
    private CloudRedisService service;
    @Autowired
    private ResourceAllocateService resourceAllocateService;


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

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

    /**
     * 删除
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "cloudRedis", operation = "cloudRedis删除")
    @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 = "PATCH")
    @Log(module = BoCloudService.IMS, target = "cloudRedis", operation = "cloudRedis操作")
    @PatchMapping("/{id}")
    public BocloudResult operations(@PathVariable(value = Common.ID) Long id,
                                    @RequestBody CloudRedisOperationParams params,
                                    @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.operations(params, context);
    }


    /**
     * 批量删除
     *
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "批量删除", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "cloudRedis", operation = "cloudRedis批量删除")
    @DeleteMapping
    public BocloudResult batchRemove(
            @RequestBody CloudRedisOperationParams params,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.batchRemove(params.getIds(), context);
    }


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

    /**
     * 分配给用户
     *
     * @param id
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "Redis用户分配", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "Redis", operation = "Redis用户分配")
    @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", id);
        return resourceAllocateService.assign(obj, ResourceCategory.Redis, context);
    }

    /**
     * 分配给用户
     *
     * @param params
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "Redis批量用户分配", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "Redis", operation = "Redis批量用户分配")
    @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.Redis, context);
            if (result.isSuccess()) {
                succList.add(result);
            } else {
                failList.add(result);
            }
        }
        return new BocloudResult(true, "Redis分配成功【" + succList.size() + "】个，失败【" + failList.size() + "】个");
    }

    @ApiOperation(tags = {"CMC", "CSC"}, value = "回收资源", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "Redis", operation = "回收资源")
    @DeleteMapping("/{id}/users")
    public BocloudResult userReclaim(@PathVariable(value = Common.ID) Long id,
                                     @Value(Common.REQ_CONTEXT) RequestContext context) {
        ResourceAssignParam object = new ResourceAssignParam();
        object.setId(id);
        return resourceAllocateService.reclaim(object, ResourceCategory.Redis, context);
    }


}