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

import com.bocloud.cmp.model.ServerLabelParam;
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.ims.entity.resource.Label;
import com.bocloud.ims.interfaces.resource.LabelService;
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.*;

/**
 * @author zhangdi
 * @since 2018/7/2
 */
@RestController
@RequestMapping("/v1/tags")
@Api(value = "命名规则", tags = "命名规则")
public class LabelController {

    @Autowired
    private LabelService labelService;

    /**
     * 获取标签列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "获取命名规则列表", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "标签", operation = "获取标签列表")
    @GetMapping
    public BocloudResult<GridBean<Label>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return labelService.list(pager, context);
    }

    /**
     * 添加标签
     *
     * @param label
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "添加标签", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "标签", operation = "添加标签")
    @PostMapping
    public BocloudResult<Label> create(@RequestBody Label label) {
        return labelService.create(label);
    }

    /**
     * 修改标签
     *
     * @param id
     * @param Label
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "修改标签", httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "标签", operation = "修改标签")
    @PutMapping("/{id}")
    public BocloudResult<Label> modify(@PathVariable(value = Common.ID) Long id, @RequestBody Label Label) {
        Label.setId(id);
        return labelService.modify(Label);
    }

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

    /**
     * 查看标签详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "查看标签详情", httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "标签", operation = "查看标签详情")
    @GetMapping("/{id}")
    public BocloudResult<Label> detail(@PathVariable(value = Common.ID) Long id) {
        return labelService.detail(id);
    }

    /**
     * 查询资源标签
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC", "CSC"}, value = "创建资源标签", httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "标签", operation = "查看标签详情")
    @PostMapping("/resources")
    public BocloudResult createResources(@RequestBody ServerLabelParam serverLabelParam,
                                         @Value(Common.REQ_CONTEXT) RequestContext context) {
        return labelService.createResources(serverLabelParam, context);
    }

}