package com.pm.web.controller.device;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
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.device.service.IIbmsDeviceAlarmsService;
import com.pm.common.core.page.TableDataInfo;

/**
 * 设备告警Controller
 *
 * @author system
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/device/alarm")
public class IbmsDeviceAlarmsController extends BaseController
{
    @Autowired
    private IIbmsDeviceAlarmsService ibmsDeviceAlarmService;

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

        // 获取统计信息
        PageData stats = ibmsDeviceAlarmService.selectAlarmStatistics(pd);

        TableDataInfo dataTable = getDataTable(list);
        // 将统计信息添加到返回结果中
        if (stats != null) {
            Map<String, Object> extData = new HashMap<>();
            extData.put("stats", stats);
            dataTable.setExtData(extData);
        }

        return dataTable;
    }

    /**
     * 导出设备告警列表
     */
    @PreAuthorize("@ss.hasPermi('device:alarm: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 = {
                    "告警编码,alarmCode",
                    "设备编码,deviceCode",
                    "设备名称,deviceName",
                    "设备类型,deviceType",
                    "子系统类型,subsystemType",
                    "告警级别,alarmLevel",
                    "告警类型,alarmType",
                    "告警描述,alarmDesc",
                    "楼栋,buildingName",
                    "楼层,floorName",
                    "房间,roomName",
                    "空间位置,spaceLocation",
                    "告警时间,alarmTime",
                    "复位状态,isReset",
                    "复位时间,resetTime",
                    "复位人,resetBy",
                    "服务单号,serviceOrderNo",
                    "处理状态,handleStatus"
            };
            ExcelUtilPageData.exportXLSX(response, null, null, filename, titles, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

    /**
     * 获取告警详情（包含处理记录）
     */
    @PreAuthorize("@ss.hasPermi('device:alarm:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getDetail(@PathVariable("id") Long id)
    {
        return success(ibmsDeviceAlarmService.getAlarmDetail(id));
    }

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

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

    /**
     * 复位告警
     */
    @PreAuthorize("@ss.hasPermi('device:alarm:reset')")
    @Log(title = "设备告警复位", businessType = BusinessType.UPDATE)
    @PostMapping("/reset")
    public AjaxResult reset(@RequestBody PageData pd)
    {
        return toAjax(ibmsDeviceAlarmService.resetAlarm(pd));
    }

    /**
     * 创建服务单
     */
    @PreAuthorize("@ss.hasPermi('device:alarm:createOrder')")
    @Log(title = "创建服务单", businessType = BusinessType.OTHER)
    @PostMapping("/createOrder")
    public AjaxResult createServiceOrder(@RequestBody PageData pd)
    {
        Map<String, Object> result = ibmsDeviceAlarmService.createServiceOrder(pd);
        if ((boolean) result.get("success")) {
            return AjaxResult.success(result);
        } else {
            return AjaxResult.error((String) result.get("msg"));
        }
    }

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