package com.hw.ems.record.controller;

import com.hw.common.core.utils.poi.ExcelUtil;
import com.hw.common.core.web.controller.BaseController;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.core.web.page.TableDataInfo;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.common.security.annotation.RequiresPermissions;
import com.hw.ems.record.domain.RecordAlarmRule;
import com.hw.ems.record.service.IRecordAlarmRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 异常告警规则Controller
 *
 * @author sf
 * @date 2023-04-21
 */
@RestController
@RequestMapping("/record/alarmRule")
public class RecordAlarmRuleController extends BaseController {
    @Autowired
    private IRecordAlarmRuleService recordAlarmRuleService;

    /**
     * 查询异常告警规则列表
     */
    @RequiresPermissions("energy:rule:list")
    @GetMapping("/list")
    public TableDataInfo list(RecordAlarmRule recordAlarmRule) {
        startPage();
        List<RecordAlarmRule> list = recordAlarmRuleService.selectRecordAlarmRuleList(recordAlarmRule);
        return getDataTable(list);
    }

    /**
     * 导出异常告警规则列表
     */
    @RequiresPermissions("energy:rule:export")
    @Log(title = "异常告警规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecordAlarmRule recordAlarmRule) {
        List<RecordAlarmRule> list = recordAlarmRuleService.selectRecordAlarmRuleList(recordAlarmRule);
        ExcelUtil<RecordAlarmRule> util = new ExcelUtil<RecordAlarmRule>(RecordAlarmRule.class);
        util.exportExcel(response, list, "异常告警规则数据");
    }

    /**
     * 获取异常告警规则详细信息
     */
    @RequiresPermissions("energy:rule:query")
    @GetMapping(value = "/{objId}")
    public AjaxResult getInfo(@PathVariable("objId") Long objId) {
        return success(recordAlarmRuleService.selectRecordAlarmRuleByObjId(objId));
    }

    /**
     * 新增异常告警规则
     */
    @RequiresPermissions("energy:rule:add")
    @Log(title = "异常告警规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordAlarmRule recordAlarmRule) {
        return toAjax(recordAlarmRuleService.insertRecordAlarmRule(recordAlarmRule));
    }

    /**
     * 修改异常告警规则
     */
    @RequiresPermissions("energy:rule:edit")
    @Log(title = "异常告警规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordAlarmRule recordAlarmRule) {
        return toAjax(recordAlarmRuleService.updateRecordAlarmRule(recordAlarmRule));
    }

    /**
     * 删除异常告警规则
     */
    @RequiresPermissions("energy:rule:remove")
    @Log(title = "异常告警规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{objIds}")
    public AjaxResult remove(@PathVariable Long[] objIds) {
        return toAjax(recordAlarmRuleService.deleteRecordAlarmRuleByObjIds(objIds));
    }
}
