package com.ruoyi.rocketmq.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageDomain;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.rocketmq.domain.RmAlarmPolicy;
import com.ruoyi.rocketmq.service.IRmAlarmPolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 告警策略Controller
 * 
 * @author gyt
 * @date 2025-09-12
 */
@RestController
@RequestMapping("/alarmPolicy")
public class RmAlarmPolicyController extends BaseController
{
    @Autowired
    private IRmAlarmPolicyService rmAlarmPolicyService;

    /**
     * 查询告警策略列表
     */
    @RequiresPermissions("rocketmq:policy:list")
    @GetMapping("/list")
    public TableDataInfo list(RmAlarmPolicy rmAlarmPolicy)
    {
        PageDomain pageDomain = new PageDomain();
        pageDomain.setPageNum(rmAlarmPolicy.getPageNum());
        pageDomain.setPageSize(rmAlarmPolicy.getPageSize());
        startPage(pageDomain);
        List<RmAlarmPolicy> list = rmAlarmPolicyService.selectRmAlarmPolicyList(rmAlarmPolicy);
        return getDataTable(list);
    }

    /**
     * 导出告警策略列表
     */
    @RequiresPermissions("rocketmq:policy:export")
    @Log(title = "告警策略", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RmAlarmPolicy rmAlarmPolicy)
    {
        List<RmAlarmPolicy> list = rmAlarmPolicyService.selectRmAlarmPolicyList(rmAlarmPolicy);
        ExcelUtil<RmAlarmPolicy> util = new ExcelUtil<RmAlarmPolicy>(RmAlarmPolicy.class);
        util.exportExcel(response, list, "告警策略数据");
    }

    /**
     * 获取告警策略详细信息
     */
    @RequiresPermissions("rocketmq:policy:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(rmAlarmPolicyService.getRmAlarmPolicyMsgById(id));
    }

    /**
     * 新增告警策略
     */
    @RequiresPermissions("rocketmq:policy:add")
    @Log(title = "告警策略", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RmAlarmPolicy rmAlarmPolicy)
    {
        return toAjax(rmAlarmPolicyService.addRmAlarmPolicy(rmAlarmPolicy));
    }

    /**
     * 修改告警策略
     */
    @RequiresPermissions("rocketmq:policy:edit")
    @Log(title = "告警策略", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RmAlarmPolicy rmAlarmPolicy)
    {
        return toAjax(rmAlarmPolicyService.updateRmAlarmPolicy(rmAlarmPolicy));
    }

    /**
     * 下发告警策略
     */
    @RequiresPermissions("rocketmq:policy:query")
    @GetMapping(value = "/issueAlarmPolicy")
    public AjaxResult issueAlarmPolicy(Long id)
    {
        return success(rmAlarmPolicyService.issueAlarmPolicy(id));
    }


}
