package vip.xiaonuo.coldchain.core.event;

/**
 * @author jackzhou
 * @version 1.0
 * @project jfcloud-coldchain
 * @description
 * @date 2024/11/12 15:31:43
 */

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import vip.xiaonuo.coldchain.modular.monitordevice.service.MonitorDeviceService;
import vip.xiaonuo.coldchain.modular.monitortarget.service.MonitorTargetService;

import java.util.Date;

@Slf4j
@RequiredArgsConstructor
@Component
public class MonitorDeviceLoginEventListener {
    private final MonitorDeviceService monitorDeviceService;
    private final MonitorTargetService monitorTargetService;

    @Async("coldChainAsyncTask")
    @EventListener
    public void handleMonitorDeviceLoginEvent(MonitorDeviceLoginEvent event) {
        Integer deviceId = event.getDeviceId();
        String ip = event.getIp();
        Date timestamps = event.getTimestamps();
        try {
            monitorDeviceService.updateLastLoginTimeByDeviceId(deviceId, timestamps);
            monitorTargetService.updateLastLoginTimeByDeviceId(deviceId, timestamps);
        } catch (Exception e) {
        }
    }
}
