fix: 设备24小时内到期,电话通知修改。
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package com.intc.iot.task;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.intc.fishery.domain.Device;
|
||||
import com.intc.fishery.mapper.DeviceMapper;
|
||||
import com.intc.iot.handler.DeviceDataHandler;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 设备即将到期告警定时任务
|
||||
* 每小时扫描一次设备表,对到期时间距离当前不到24小时的设备触发电话通知,提醒用户续费。
|
||||
*
|
||||
* @author intc-iot
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnBean(DeviceDataHandler.class)
|
||||
public class DeviceExpirationWarnTask {
|
||||
|
||||
private final DeviceDataHandler deviceDataHandler;
|
||||
|
||||
private final DeviceMapper deviceMapper;
|
||||
|
||||
/**
|
||||
* 每2小时执行一次,扫描即将到期(24小时内)的设备并触发电话通知
|
||||
*/
|
||||
@Scheduled(fixedDelay = 7200000)
|
||||
public void checkDeviceExpirationAndNotify() {
|
||||
try {
|
||||
Date now = new Date();
|
||||
// 24小时后的时间点
|
||||
Date deadline = new Date(now.getTime() + 24L * 60 * 60 * 1000);
|
||||
|
||||
// 查询所有满足条件的设备:
|
||||
// 1. deadTime 不为空
|
||||
// 2. deadTime > 当前时间(尚未到期)
|
||||
// 3. deadTime <= 当前时间 + 24小时(即将到期)
|
||||
// 4. userId 不为空(已绑定用户)
|
||||
List<Device> expiringDevices = deviceMapper.selectList(
|
||||
new LambdaQueryWrapper<Device>()
|
||||
.isNotNull(Device::getDeadTime)
|
||||
.gt(Device::getDeadTime, now)
|
||||
.le(Device::getDeadTime, deadline)
|
||||
.isNotNull(Device::getUserId)
|
||||
.select(Device::getId, Device::getUserId, Device::getSerialNum,
|
||||
Device::getDeviceName, Device::getDeviceType, Device::getPondId,
|
||||
Device::getDeadTime)
|
||||
);
|
||||
|
||||
if (expiringDevices == null || expiringDevices.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
log.info("[到期告警] 扫描到 {} 台即将到期的设备,开始逐一通知", expiringDevices.size());
|
||||
|
||||
for (Device device : expiringDevices) {
|
||||
try {
|
||||
deviceDataHandler.triggerDeviceExpirationAlarm(device);
|
||||
} catch (Exception e) {
|
||||
log.error("[到期告警] 处理单台设备异常,deviceId={}: {}", device.getId(), e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("[到期告警] 扫描设备到期告警异常: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user