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

import com.bocloud.cmp.params.azure.CloudRedisOperationParams;
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.redis.CloudRedis;
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.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;

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

    /**
     * 更新
     *
     * @param entityParams
     * @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 entityParams,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.modify(entityParams, context);
    }

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


    /**
     * 删除
     *
     * @param id
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "删除cloudRedis", 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 ids
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "批量删除cloudRedis", httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "cloudRedis", operation = "批量删除cloudRedis")
    @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 = "用户分配cloudRedis", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "cloudRedis", operation = "用户分配cloudRedis")
    @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 = "批量用户分配cloudRedis", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "cloudRedis", operation = "批量用户分配cloudRedis")
    @PostMapping("/users/batch")
    public BocloudResult assignBatch(
            @RequestBody UserAssignParams params,
            @Value(Common.REQ_CONTEXT) RequestContext context) {
        return service.assignBatch(params, context);
    }

}