package org.dromara.ems.record.controller;

import org.dromara.common.log.annotation.Log;
import org.dromara.ems.common.web.EmsBaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.ems.record.domain.EmsRecordAlarmData;
import org.dromara.ems.record.service.IEmsRecordAlarmDataService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 异常数据记录Controller
 * 
 * @author Yinq
 * @date 2024-05-15
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ems/record/recordAlarmData")
public class EmsRecordAlarmDataController extends EmsBaseController
{
    
    private final IEmsRecordAlarmDataService emsRecordAlarmDataService;

    /**
     * 查询异常数据记录列表
     */
    @SaCheckPermission("ems/record:recordAlarmData:list")
    @GetMapping("/list")
    public TableDataInfo list(EmsRecordAlarmData emsRecordAlarmData)
    {
        startPage();
        List<EmsRecordAlarmData> list = emsRecordAlarmDataService.selectEmsRecordAlarmDataList(emsRecordAlarmData);
        return getDataTable(list);
    }

    /**
     * 导出异常数据记录列表
     */
    @SaCheckPermission("ems/record:recordAlarmData:export")
    @Log(title = "异常数据记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmsRecordAlarmData emsRecordAlarmData)
    {
        List<EmsRecordAlarmData> list = emsRecordAlarmDataService.selectEmsRecordAlarmDataList(emsRecordAlarmData);
        ExcelUtil.exportExcel(list, "异常数据记录数据", EmsRecordAlarmData.class, response);
    }

    /**
     * 获取异常数据记录详细信息
     */
    @SaCheckPermission("ems/record:recordAlarmData:query")
    @GetMapping(value = "/{objId}")
    public R<?> getInfo(@PathVariable("objId") Long objId)
    {
        return success(emsRecordAlarmDataService.selectEmsRecordAlarmDataByObjId(objId));
    }

    /**
     * 新增异常数据记录
     */
    @SaCheckPermission("ems/record:recordAlarmData:add")
    @Log(title = "异常数据记录", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody EmsRecordAlarmData emsRecordAlarmData)
    {
        return toAjax(emsRecordAlarmDataService.insertEmsRecordAlarmData(emsRecordAlarmData));
    }

    /**
     * 修改异常数据记录
     */
    @SaCheckPermission("ems/record:recordAlarmData:edit")
    @Log(title = "异常数据记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody EmsRecordAlarmData emsRecordAlarmData)
    {
        return toAjax(emsRecordAlarmDataService.updateEmsRecordAlarmData(emsRecordAlarmData));
    }

    /**
     * 删除异常数据记录
     */
    @SaCheckPermission("ems/record:recordAlarmData:remove")
    @Log(title = "异常数据记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{objIds}")
    public R<?> remove(@PathVariable Long[] objIds)
    {
        return toAjax(emsRecordAlarmDataService.deleteEmsRecordAlarmDataByObjIds(objIds));
    }

    /**
     * 处理异常数据
     * @param objIds
     * @return
     */
    @PostMapping("/handleExceptions/{objIds}")
    public R<?> handleExceptions(@PathVariable Long[] objIds)
    {
        return toAjax(emsRecordAlarmDataService.handleExceptionsAlarmData(objIds));
    }

    /**
     * 获取异常数据表总数（包含所有告警类型）
     */
    @GetMapping("/getAlarmDataTotalCount")
    public int getAlarmDataTotalCount()
    {
        return emsRecordAlarmDataService.getAlarmDataTotalCount();
    }

    /**
     * 保存WebSocket告警数据（批量保存）
     * 前端传入多个EmsRecordAlarmData实体，每个告警规则对应一条记录
     * 
     * @param alarmDataList 异常数据记录列表
     * @return 结果
     */
    @Log(title = "WebSocket告警数据", businessType = BusinessType.INSERT)
    @PostMapping("/saveWebSocketAlarmData")
    public R<?> saveWebSocketAlarmData(@RequestBody List<EmsRecordAlarmData> alarmDataList)
    {
        if (alarmDataList == null || alarmDataList.isEmpty()) {
            return error("告警数据不能为空");
        }
        
        try {
            // 设置创建人信息
            String username = getUsername();
            for (EmsRecordAlarmData alarmData : alarmDataList) {
            }
            
            int result = emsRecordAlarmDataService.saveWebSocketAlarmDataBatch(alarmDataList);
            if (result > 0) {
                return success("保存成功，共保存 " + result + " 条告警记录");
            } else {
                return error("保存失败，没有新增任何记录");
            }
        } catch (Exception e) {
            logger.error("保存WebSocket告警数据异常", e);
            return error("保存失败：" + e.getMessage());
        }
    }

}
