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

import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
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.Rack;
import com.bocloud.ims.interfaces.resource.RackService;
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;

/**
 * 机柜相关接口
 *
 * @author dmw
 */
@RestController
@RequestMapping("/v1/racks")
@Api(value = "机柜", tags = "机柜")
public class RackController {
    @Autowired
    private RackService rackService;

    /**
     * 获取机柜列表
     *
     * @param pager
     * @return 数据结果集
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查看机柜列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "机柜", operation = "获取机柜列表")
    @GetMapping
    public BocloudResult<GridBean<Rack>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return rackService.list(pager, context);
    }

    /**
     * 获取机柜列表
     *
     * @param condition
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "通过condition方式查询机柜列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "机柜", operation = "获取机柜列表")
    @GetMapping("/condition")
    public BocloudResult<List<Rack>> condition(@RequestParam(value = Common.CONDITION, required = false) String condition,
                                               @Value(Common.REQ_CONTEXT) RequestContext context) {
        return rackService.condition(condition, context);
    }

    /**
     * 添加机柜
     *
     * @param params  机柜属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
     * @param context .getTarget() 操作者ID
     * @return 添加结果
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "添加机柜",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "机柜", operation = "添加机柜")
    @PostMapping
    public BocloudResult<Rack> create(@RequestBody Rack rack, @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != rack) {
            return rackService.create(rack, context.getTarget());
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 修改机柜
     *
     * @param params  机柜属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
     * @param id      需要修改的机柜ID
     * @param context .getTarget() 操作者ID
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "修改机柜",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "机柜", operation = "修改机柜")
    @PutMapping(value = "/{id}")
    public BocloudResult<String> modify(@RequestBody Rack rack, @PathVariable(value = Common.ID) Long id,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != rack) {
            return rackService.modify(rack, context.getTarget());
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 移除机柜
     *
     * @param id      机柜ID
     * @param context .getTarget() 操作者ID
     * @return 操作结果
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "移除机柜",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "机柜", operation = "移除机柜")
    @DeleteMapping(value = "/{id}")
    public BocloudResult<String> remove(@PathVariable(value = Common.ID) Long id,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        return rackService.remove(id, context.getTarget());
    }

    /**
     * 查看机柜详细信息
     *
     * @param id      机柜ID
     * @param context .getTarget() 操作者ID
     * @return 操作结果
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查看机柜基本信息",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "机柜", operation = "查看机柜详情")
    @GetMapping(value = "/{id}")
    public BocloudResult detail(@PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context,
                                @RequestParam(value = Common.ACTION, required = false) String action) {
        return rackService.details(id, action);
    }

    /**
     * 查看机柜基本信息
     *
     * @param id      机柜ID
     * @param context .getTarget() 操作者ID
     * @return 操作结果
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查看机柜基本信息",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "机柜", operation = "查看机柜基本信息")
    @RequestMapping(value = "/{id}/basic", method = {RequestMethod.GET})
    public BocloudResult<Rack> basic(@PathVariable(value = Common.ID) Long id) {
        return rackService.basic(id);
    }

    /**
     * 机柜操作
     *
     * @param context
     * @param params
     * @param action
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "机柜操作",  httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "机柜", operation = "机柜操作")
    @PatchMapping()
    public BocloudResult<String> operations(@Value(Common.REQ_CONTEXT) RequestContext context,
                                            @RequestParam(value = Common.PARAMS, required = false) String params,
                                            @RequestParam(value = Common.ACTION, required = false) String action) {
        return rackService.operations(context, params, action);
    }

}
