package com.zsc.edu.gateway.framework.springEvent;

import com.zsc.edu.gateway.modules.iot.device.repo.DeviceRepository;
import com.zsc.edu.gateway.modules.iot.record.entity.RecordData;
import com.zsc.edu.gateway.modules.iot.tsl.entity.EventLog;
import com.zsc.edu.gateway.modules.iot.tsl.repo.EventLogRepository;
import com.zsc.edu.gateway.modules.iot.tsl.repo.EventRepository;
import jakarta.annotation.Resource;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author zhuang
 */
@Component
public class TemperatureExceededEventListener implements ApplicationListener<TemperatureExceededEvent> {

    @Resource
    private EventLogRepository eventLogRepository;

    @Resource
    private DeviceRepository deviceRepository;

    @Resource
    private EventRepository eventRepository;

    @EventListener
    @Override
    public void onApplicationEvent(TemperatureExceededEvent event) {
        RecordData recordData = event.getRecordData();
        System.out.println(event.getEventName() + "数值如下:" + event.getReducedParameter());
        // 创建 EventLog 对象
        EventLog eventLog = new EventLog();
        eventLog.setName(event.getEventName());
        eventLog.setRecordId(recordData.getId());
        eventLog.setTriggerTime(LocalDateTime.now());
        eventLog.setIsRead(false);
        // 保存 EventLog 对象到数据库
        eventLogRepository.insert(eventLog);
    }
}