package com.isu.gaswellwatch.modbus.data;
import cn.hutool.core.map.MapUtil;
import com.isu.gaswellwatch.entity.Response;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author ็ไป้พ
* 2024/11/23 11:55
*/
@Slf4j
@Component
@SuppressWarnings("all")
public class Redis2DBPersistenceService {
private static final String EXISTS_TABLE_SQL = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA"
+ " IN ('gaswellwatch', 'gas_well_watch') AND TABLE_NAME='$TableName$'";
private static final String DEVICE_INFO_SQL = "SELECT * from device where id = ";
@Resource
private JdbcTemplate jdbcTemplate;
@Resource(name = "stringRedisTemplate")
private RedisTemplate redisTemplate;
@Resource
private Map persistenceHandlerMap;
private RestTemplate restTemplate = new RestTemplate();
@Scheduled(cron = "20/30 * * * * ? ")
public void write() {
Map idGatewayMappingMap = getOnlineGateway();
HashOperations operations = this.redisTemplate.opsForHash();
try (Cursor cursor = this.redisTemplate.scan(ScanOptions.scanOptions()
.match(PersistenceHandler.DEVICE_DATA_CACHE + "*").build())) {
Long deviceId;
String cacheKey, tableName;
Map deviceMap;
PersistenceHandler persistenceHandler;
List