/*
 * 文 件 名:  AlarmRuleController
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  <描述>
 * 修 改 人:  lvwenbin
 * 修改时间:  2026/2/3
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.ruoyi.ems.controller;

import com.huashe.common.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
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.common.security.utils.SecurityUtils;
import com.ruoyi.ems.domain.AlarmRule;
import com.ruoyi.ems.service.IAlarmRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 告警规则Controller
 */
@RestController
@RequestMapping("/alarm/rule")
@Api(value = "AlarmRuleController", description = "告警规则管理")
public class AlarmRuleController extends BaseController {

    @Autowired
    private IAlarmRuleService ruleService;

    /**
     * 查询告警规则列表
     */
    @RequiresPermissions("ems:alarm-rule:list")
    @GetMapping("/list")
    @ApiOperation("查询告警规则列表")
    public TableDataInfo list(AlarmRule rule) {
        startPage();
        List<AlarmRule> list = ruleService.selectList(rule);
        return getDataTable(list);
    }

    /**
     * 查询全部规则（不分页）
     */
    @RequiresPermissions("ems:alarm-rule:list")
    @GetMapping("/listAll")
    @ApiOperation("查询全部告警规则")
    public AjaxResult listAll(AlarmRule rule) {
        List<AlarmRule> list = ruleService.selectList(rule);
        return success(list);
    }

    /**
     * 导出告警规则列表
     */
    @RequiresPermissions("ems:alarm-rule:export")
    @Log(title = "告警规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出告警规则")
    public void export(HttpServletResponse response, AlarmRule rule) {
        List<AlarmRule> list = ruleService.selectList(rule);
        ExcelUtil<AlarmRule> util = new ExcelUtil<>(AlarmRule.class);
        util.exportExcel(response, list, "告警规则数据");
    }

    /**
     * 获取告警规则详情
     */
    @RequiresPermissions("ems:alarm-rule:query")
    @GetMapping("/{id}")
    @ApiOperation("获取告警规则详情")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ruleService.selectById(id));
    }

    /**
     * 根据规则代码获取详情
     */
    @RequiresPermissions("ems:alarm-rule:query")
    @GetMapping("/code/{ruleCode}")
    @ApiOperation("根据规则代码获取详情")
    public AjaxResult getByCode(@PathVariable("ruleCode") String ruleCode) {
        return success(ruleService.selectByRuleCode(ruleCode));
    }

    /**
     * 新增告警规则
     */
    @RequiresPermissions("ems:alarm-rule:add")
    @Log(title = "告警规则", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增告警规则")
    public AjaxResult add(@RequestBody AlarmRule rule) {
        rule.setCreateBy(SecurityUtils.getUsername());
        return toAjax(ruleService.insert(rule));
    }

    /**
     * 修改告警规则
     */
    @RequiresPermissions("ems:alarm-rule:edit")
    @Log(title = "告警规则", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改告警规则")
    public AjaxResult edit(@RequestBody AlarmRule rule) {
        rule.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(ruleService.update(rule));
    }

    /**
     * 删除告警规则
     */
    @RequiresPermissions("ems:alarm-rule:remove")
    @Log(title = "告警规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除告警规则")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ruleService.deleteByIds(ids));
    }

    /**
     * 启用/禁用规则
     */
    @RequiresPermissions("ems:alarm-rule:edit")
    @Log(title = "告警规则", businessType = BusinessType.UPDATE)
    @PutMapping("/enable/{ruleCode}/{enabled}")
    @ApiOperation("启用/禁用规则")
    public AjaxResult updateEnabled(@PathVariable("ruleCode") String ruleCode,
        @PathVariable("enabled") Integer enabled) {
        return toAjax(ruleService.updateEnabled(ruleCode, enabled));
    }

    /**
     * 批量启用/禁用
     */
    @RequiresPermissions("ems:alarm-rule:edit")
    @Log(title = "告警规则", businessType = BusinessType.UPDATE)
    @PutMapping("/batchEnable")
    @ApiOperation("批量启用/禁用")
    public AjaxResult batchUpdateEnabled(@RequestBody List<String> ruleCodes,
        @RequestParam("enabled") Integer enabled) {
        return toAjax(ruleService.batchUpdateEnabled(ruleCodes, enabled));
    }

    /**
     * 复制规则
     */
    @RequiresPermissions("ems:alarm-rule:add")
    @Log(title = "告警规则", businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    @ApiOperation("复制规则")
    public AjaxResult copyRule(@RequestParam("sourceRuleCode") String sourceRuleCode,
        @RequestParam("newRuleName") String newRuleName) {
        AlarmRule newRule = ruleService.copyRule(sourceRuleCode, newRuleName);
        return success(newRule);
    }

    /**
     * 查询指定分组的规则
     */
    @RequiresPermissions("ems:alarm-rule:list")
    @GetMapping("/group/{groupCode}")
    @ApiOperation("查询指定分组的规则")
    public AjaxResult listByGroup(@PathVariable("groupCode") String groupCode) {
        return success(ruleService.selectByGroupCode(groupCode));
    }

    /**
     * 查询适用于巡检的规则
     */
    @RequiresPermissions("ems:alarm-rule:list")
    @GetMapping("/forInspection")
    @ApiOperation("查询适用于巡检的规则")
    public AjaxResult listForInspection(@RequestParam(value = "deviceModel", required = false) String deviceModel) {
        return success(ruleService.selectRulesForInspection(deviceModel));
    }

    /**
     * 查询适用于实时监测的规则
     */
    @RequiresPermissions("ems:alarm-rule:list")
    @GetMapping("/forRealtime")
    @ApiOperation("查询适用于实时监测的规则")
    public AjaxResult listForRealtime(@RequestParam(value = "deviceModel", required = false) String deviceModel,
        @RequestParam(value = "attrKey", required = false) String attrKey) {
        return success(ruleService.selectRulesForRealtime(deviceModel, attrKey));
    }
}