package com.pm.web.controller.device;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pm.common.annotation.Log;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.enums.BusinessType;
import com.pm.common.config.PageData;
import com.pm.common.utils.poi.ExcelUtilPageData;
import com.pm.waring.service.IIbmsDeviceAlarmService;
import com.pm.common.core.page.TableDataInfo;

/**
 * 设备告警Controller
 *
 * @author LiXiangFei
 * @date 2025-04-18
 */
@RestController
@RequestMapping("/device/waring")
public class IbmsDeviceAlarmController extends BaseController
{
    @Autowired
    private IIbmsDeviceAlarmService ibmsDeviceAlarmService;

    /**
     * 查询设备告警列表
     */
    @PreAuthorize("@ss.hasPermi('device:waring:list')")
    @GetMapping("/list")
    public TableDataInfo list()
    {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> list = ibmsDeviceAlarmService.selectIbmsDeviceAlarmList(pd);
        return getDataTable(list);
    }

    /**
     * 导出设备告警列表
     */
    @PreAuthorize("@ss.hasPermi('device:waring:export')")
    @Log(title = "设备告警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        try {
            PageData pd = this.getPageData();
            List<PageData> list = ibmsDeviceAlarmService.selectIbmsDeviceAlarmList(pd);
            String filename = "设备告警数据";
            String[] titles = {
                    "告警ID,id",
                    "告警编码,alarmCode",
                    "设备ID,deviceId",
                    "点位ID,pointId",
                    "告警级别(1-一般,2-重要,3-紧急),alarmLevel",
                    "告警类型,alarmType",
                    "告警标题,alarmTitle",
                    "告警内容,alarmContent",
                    "触发值,triggerValue",
                    "正常值,normalValue",
                    "开始时间,startTime",
                    "结束时间,endTime",
                    "持续时间(秒),duration",
                    "状态(0-未处理,1-已处理,2-已忽略),status",
                    "处理人ID,handlerId",
                    "处理人姓名,handlerName",
                    "处理时间,handleTime",
                    "处理方法,handleMethod",
                    "关联服务单ID,serviceOrderId"
            };
            ExcelUtilPageData.exportXLSX(response, null, null, filename, titles, list);
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    /**
     * 获取设备告警详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:waring:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ibmsDeviceAlarmService.selectIbmsDeviceAlarmById(id));
    }

    /**
     * 新增设备告警
     */
    @PreAuthorize("@ss.hasPermi('device:waring:add')")
    @Log(title = "设备告警", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PageData pd)
    {
        return toAjax(ibmsDeviceAlarmService.insertIbmsDeviceAlarm(pd));
    }

    /**
     * 修改设备告警
     */
    @PreAuthorize("@ss.hasPermi('device:waring:edit')")
    @Log(title = "设备告警", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PageData pd)
    {
        return toAjax(ibmsDeviceAlarmService.updateIbmsDeviceAlarm(pd));
    }

    /**
     * 删除设备告警
     */
    @PreAuthorize("@ss.hasPermi('device:waring:remove')")
    @Log(title = "设备告警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ibmsDeviceAlarmService.deleteIbmsDeviceAlarmByIds(ids));
    }
}
