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.ims.entity.resource.Environment;
import com.bocloud.ims.interfaces.resource.EnvironmentService;
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.*;

/**
 * 环境实体类Controller
 *
 * @author wangshuai
 * @version 4.1.0-SNAPSHOT
 * @since 2018/12/24
 */
@RestController
@RequestMapping("/v1/environments")
@Api(value = "所属环境", tags = "所属环境")
public class EnvironmentController {
    @Autowired
    private EnvironmentService environmentService;

    /**
     * 查看环境列表
     *
     * @param pager
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "获取所属环境列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "环境", operation = "查看环境列表")
    @GetMapping
    public BocloudResult<GridBean<Environment>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return environmentService.list(pager, context);
    }

    /**
     * 添加环境
     *
     * @param environment
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "添加所属环境",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "环境", operation = "添加环境")
    @PostMapping
    public BocloudResult<Environment> create(@RequestBody Environment environment,
                                             @Value(Common.REQ_CONTEXT) RequestContext context) {
        return environmentService.create(environment, context);
    }

    /**
     * 修改环境
     *
     * @param id
     * @param environment
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "修改所属环境",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "环境", operation = "修改环境")
    @PutMapping("/{id}")
    public BocloudResult<Environment> modify(@PathVariable(value = Common.ID) Long id, @RequestBody Environment environment,
                                             @Value(Common.REQ_CONTEXT) RequestContext context) {
        environment.setId(id);
        return environmentService.modify(environment, context);
    }

    /**
     * 删除环境
     *
     * @param id
     * @param context
     * @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,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return environmentService.remove(id, context);
    }

    /**
     * 查看环境明细
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查看所属环境详情",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "环境", operation = "查看环境详情")
    @GetMapping("/{id}")
    public BocloudResult<Environment> detail(@PathVariable(value = Common.ID) Long id) {
        return environmentService.detail(id);
    }
}
