package com.zy.core.task;
|
|
|
import com.zy.asrs.entity.DeviceDataLog;
|
import com.zy.asrs.service.DeviceDataLogService;
|
import com.zy.common.utils.RedisUtil;
|
import com.zy.core.enums.RedisKeyType;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
|
import java.util.Set;
|
import java.util.List;
|
import java.util.ArrayList;
|
|
@Slf4j
|
@Component
|
public class DeviceLogScheduler {
|
|
@Autowired
|
private DeviceDataLogService deviceDataLogService;
|
@Autowired
|
private RedisUtil redisUtil;
|
|
@Scheduled(cron = "0/3 * * * * ? ")
|
public void execute() {
|
int maxCount = 100;
|
Set<String> keys = redisUtil.scanKeys(RedisKeyType.DEVICE_LOG_KEY.key, maxCount);
|
if (keys == null || keys.isEmpty()) {
|
return;
|
}
|
List<Object> values = redisUtil.multiGet(keys);
|
List<DeviceDataLog> list = new ArrayList<>();
|
for (Object object : values) {
|
if (object instanceof DeviceDataLog) {
|
list.add((DeviceDataLog) object);
|
}
|
}
|
if (!list.isEmpty()) {
|
if (deviceDataLogService.saveBatch(list)) {
|
redisUtil.del(keys.toArray(new String[0]));
|
}
|
}
|
}
|
|
}
|