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.ResourceAlarm;
import com.bocloud.ims.interfaces.resource.ResourceAlarmService;
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;

@RestController
@RequestMapping("/v1/alarms")
@Api(value = "告警管理", tags = "告警管理")
public class ResourceAlarmController {

    @Autowired
    private ResourceAlarmService alarmService;

    /**
     * 查询告警列表
     *
     * @param pager
     * @return
     * @throws Exception
     */
    @ApiOperation(tags={"CMC","CSC"},value = "查询告警列表",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "平台告警", operation = "查询告警列表")
    @GetMapping
    public BocloudResult<GridBean<ResourceAlarm>> list(Pager pager) throws Exception {
        return alarmService.list(pager);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "查看字典详情",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "平台告警", operation = "查看字典详情")
    @GetMapping("/{id}")
    public BocloudResult<ResourceAlarm> detail(@PathVariable(value = Common.ID) Long id) {
        return alarmService.detail(id);
    }

    /**
     * 告警操作
     *
     * @param id
     * @param action
     * @param params
     * @param user
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "告警操作",  httpMethod = "PATCH")
    @Log(module = BoCloudService.IMS, target = "平台告警", operation = "告警操作")
    @PatchMapping("/{id}")
    public BocloudResult<String> action(@PathVariable(value = Common.ID) Long id,
                                @RequestParam(value = Common.ACTION, required = false) String action,
                                @RequestParam(value = Common.PARAMS, required = false) String params,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return alarmService.action(id, action, params, context.getTarget());
    }

    /**
     * 删除告警
     *
     * @param id
     * @param user
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "删除告警",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "平台告警", operation = "删除告警")
    @DeleteMapping("/{id}")
    public BocloudResult<String> remove(@PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return alarmService.remove(id, context.getTarget());
    }

    /**
     * 创建告警
     *
     * @param alarm
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "创建告警",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "事件告警", operation = "创建告警")
    @PostMapping
    public BocloudResult<ResourceAlarm> create(@RequestBody ResourceAlarm alarm) {
        return alarmService.create(alarm);
    }

    /**
     * 根据目标删除告警
     *
     * @param target
     * @return
     */
    @ApiOperation(tags={"CMC","CSC"},value = "根据目标删除告警",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "事件告警", operation = "根据目标删除告警")
    @DeleteMapping("/{target}/target")
    public BocloudResult<String> removeByTarget(@PathVariable("target") String target,
                                        @RequestParam(value = Common.TYPE) String type) {
        return alarmService.removeByTarget(target, type);
    }
}