package com.ruoyi.rocketmq.handler;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.enums.MsgEnum;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.rocketmq.domain.*;
import com.ruoyi.rocketmq.domain.vo.CollectDataVo;
import com.ruoyi.rocketmq.domain.vo.RegisterMsgVo;
import com.ruoyi.rocketmq.domain.vo.RspResultVo;
import com.ruoyi.rocketmq.domain.vo.RspVo;
import com.ruoyi.rocketmq.enums.AlarmTypeEnum;
import com.ruoyi.rocketmq.enums.PushMethodEnum;
import com.ruoyi.rocketmq.service.*;
import com.ruoyi.rocketmq.utils.DataProcessUtil;
import com.ruoyi.rocketmq.utils.JsonDataParser;
import com.ruoyi.rocketmq.utils.WeChatWorkBot;
import com.ruoyi.system.api.RemoteRevenueConfigService;
import com.ruoyi.system.api.domain.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 设备消息处理器
 */
@Slf4j
@Component
@EnableScheduling
public class MessageHandler {

    private final Map<String, Consumer<DeviceMessage>> messageHandlers = new HashMap<>();
    // 心跳状态
    private static final String HEARTBEAT_STATUS_PREFIX = "heartbeat:status:";
    // 心跳时间
    private static final String HEARTBEAT_TIME_PREFIX = "heartbeat:time:";
    // 心跳告警
    private static final String HEARTBEAT_ALERT_PREFIX = "heartbeat:alert:";
    String HEARTBEAT_RECOVERY_COUNT_PREFIX = "heartbeat:recovery:count:";

    String HEARTBEAT_COUNT_PREFIX = "heartbeat:count:";
    private static final long HEARTBEAT_TIMEOUT = 30000; // 3分钟超时


    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private  IInitialBandwidthTrafficService initialBandwidthTrafficService;
    @Autowired
    private  RemoteRevenueConfigService remoteRevenueConfigService;
    @Autowired
    private IInitialDockerInfoService initialDockerInfoService;
    @Autowired
    private IInitialCpuInfoService initialCpuInfoService;
    @Autowired
    private IInitialDiskInfoService initialDiskInfoService;
    @Autowired
    private IInitialMemoryInfoService initialMemoryInfoService;
    @Autowired
    private IInitialMountPointInfoService initialMountPointInfoService;
    @Autowired
    private IInitialHeartbeatListenLogService initialHeartbeatListenLog;
    @Autowired
    private IInitialSystemOtherCollectDataService iInitialSystemOtherCollectDataService;
    @Autowired
    private IRmResourceRemoteService rmResourceRemoteService;
    @Autowired
    private IRmAgentManagementService rmAgentManagementService;
    @Autowired
    private DataProcessUtil dataProcessUtil;
    @Autowired
    private IRmNetworkInterfaceService rmNetworkInterfaceService;
    @Autowired
    private IRmMonitorPolicyService rmMonitorPolicyService;
    @Autowired
    private IRmDeploymentPolicyService rmDeploymentPolicyService;
    @Autowired
    private IInitialBandwidthTrafficTempService initialBandwidthTrafficTempService;
    @Autowired
    private IRmAlarmLogService rmAlarmLogService;
    @Autowired
    private IRmAlarmPushConfigService rmAlarmPushConfigService;


    /**
     * 初始化处理器映射
     */
    @PostConstruct
    public void init() {
        registerHandler(MsgEnum.执行脚本策略应答.getValue(), this::handleScriptRspMessage);
        registerHandler(MsgEnum.Agent版本更新应答.getValue(), this::handleAgentUpdateRspMessage);

        // 其他类型消息可以单独注册处理器
        registerHandler(MsgEnum.注册.getValue(), this::handleRegisterMessage);
        registerHandler(MsgEnum.获取最新策略.getValue(), this::handleNewPolicyMessage);
        registerHandler(MsgEnum.CPU上报.getValue(), this::handleCpuMessage);
        registerHandler(MsgEnum.磁盘上报.getValue(), this::handleDiskMessage);
        registerHandler(MsgEnum.容器上报.getValue(), this::handleDockerMessage);
        registerHandler(MsgEnum.内存上报.getValue(), this::handleMemoryMessage);
        registerHandler(MsgEnum.网络上报.getValue(), this::handleNetMessage);
        registerHandler(MsgEnum.挂载上报.getValue(), this::handleMountPointMessage);
        registerHandler(MsgEnum.系统其他上报.getValue(), this::handleOtherSystemMessage);
        registerHandler(MsgEnum.心跳上报.getValue(), this::handleHeartbeatMessage);
        registerHandler(MsgEnum.多公网IP探测.getValue(), this::handleNetWorkDelectMessage);
    }

    /**
     * 保存网卡信息
     * @param message
     */
    private void handleNetWorkDelectMessage(DeviceMessage message) {
        List<RegisterMsgVo> interfaces = JsonDataParser.parseJsonData(message.getData(), RegisterMsgVo.class);
        if(!interfaces.isEmpty()) {
            String clientId = message.getClientId();
            RegisterMsgVo registerMsg = interfaces.get(0);
            // 时间戳转换
            long timestamp = registerMsg.getTimestamp();
            long millis = timestamp * 1000;
            Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
            List<NetworkInfo> networkInfoList = registerMsg.getNetworkInfo();
            if(!networkInfoList.isEmpty()){
                if(networkInfoList.size()==1){
                    NetworkInfo networkInfo = networkInfoList.get(0);
                    // 查询该网卡信息是否存在
                    RmNetworkInterface queryParam = new RmNetworkInterface();
                    queryParam.setClientId(clientId);
                    queryParam.setMacAddress(networkInfo.getMac());
                    queryParam.setNewFlag(1);
                    List<RmNetworkInterface> exits = rmNetworkInterfaceService.selectRmNetworkInterfaceList(queryParam);
                    if(exits.isEmpty()){
                        // 保存网卡信息
                        RmNetworkInterface insertData = new RmNetworkInterface();
                        // 业务ip和管理网ip
                        insertData.setBindIp("3");
                        insertData.setClientId(clientId);
                        insertData.setIsp(networkInfo.getCarrier());
                        insertData.setCity(networkInfo.getCity());
                        insertData.setGateway(networkInfo.getGateway());
                        insertData.setInterfaceName(networkInfo.getName());
                        insertData.setIpv4Address(networkInfo.getIpv4());
                        insertData.setMacAddress(networkInfo.getMac());
                        insertData.setProvince(networkInfo.getProvince());
                        insertData.setPublicIp(networkInfo.getPublicIp());
                        insertData.setInterfaceType(networkInfo.getType());
                        insertData.setCreateTime(createTime);
                        rmNetworkInterfaceService.insertRmNetworkInterface(insertData);
                    }else{
                        RmNetworkInterface oldInterfaceMsg = exits.get(0);
                        if(!StringUtils.equals(networkInfo.getName(),oldInterfaceMsg.getInterfaceName())
                                || !StringUtils.equals(networkInfo.getGateway(),oldInterfaceMsg.getGateway())){
                            // 查询该网卡信息是否旧数据
                            RmNetworkInterface query = new RmNetworkInterface();
                            query.setMacAddress(oldInterfaceMsg.getMacAddress());
                            query.setClientId(clientId);
                            query.setNewFlag(999);
                            List<RmNetworkInterface> oldExits = rmNetworkInterfaceService.selectRmNetworkInterfaceList(query);
                            if(!oldExits.isEmpty()){
                                // 先删除旧数据
                                oldExits.forEach(oldMsg ->{
                                    rmNetworkInterfaceService.deleteRmNetworkInterfaceById(oldMsg.getId());
                                });
                            }
                            // 先将已存在的改为旧数据
                            RmNetworkInterface oldData = new RmNetworkInterface();
                            oldData.setNewFlag(0);
                            oldData.setClientId(clientId);
                            oldData.setMacAddress(oldInterfaceMsg.getMacAddress());
                            rmNetworkInterfaceService.updateRmNetworkInterfaceByMac(oldData);
                            RmNetworkInterface insertData = new RmNetworkInterface();
                            insertData.setClientId(clientId);
                            insertData.setIsp(networkInfo.getCarrier());
                            insertData.setCity(networkInfo.getCity());
                            insertData.setGateway(networkInfo.getGateway());
                            insertData.setInterfaceName(networkInfo.getName());
                            insertData.setIpv4Address(networkInfo.getIpv4());
                            insertData.setMacAddress(networkInfo.getMac());
                            insertData.setProvince(networkInfo.getProvince());
                            insertData.setPublicIp(networkInfo.getPublicIp());
                            insertData.setInterfaceType(networkInfo.getType());
                            rmNetworkInterfaceService.insertRmNetworkInterface(insertData);
                        }else {
                            RmNetworkInterface updateData = new RmNetworkInterface();
                            // 检查其他字段是否需要更新
                            boolean needUpdate = false;
                            // 逐个字段比较是否需要更新
                            if (!StringUtils.equals(networkInfo.getCity(), oldInterfaceMsg.getCity())) {
                                updateData.setCity(networkInfo.getCity());
                                needUpdate = true;
                            }
                            if (!StringUtils.equals(networkInfo.getIpv4(), oldInterfaceMsg.getIpv4Address())) {
                                updateData.setIpv4Address(networkInfo.getIpv4());
                                needUpdate = true;
                            }
                            if (!StringUtils.equals(networkInfo.getProvince(), oldInterfaceMsg.getProvince())) {
                                updateData.setProvince(networkInfo.getProvince());
                                needUpdate = true;
                            }
                            if (!StringUtils.equals(networkInfo.getPublicIp(), oldInterfaceMsg.getPublicIp())) {
                                updateData.setPublicIp(networkInfo.getPublicIp());
                                needUpdate = true;
                            }
                            if (!StringUtils.equals(networkInfo.getCarrier(), oldInterfaceMsg.getIsp())) {
                                updateData.setIsp(networkInfo.getCarrier());
                                needUpdate = true;
                            }
                            if (!StringUtils.equals(networkInfo.getType(), oldInterfaceMsg.getInterfaceType())) {
                                updateData.setInterfaceType(networkInfo.getType());
                                needUpdate = true;
                            }
                            // 只有有字段变化时才执行更新
                            if (needUpdate) {
                                updateData.setClientId(clientId);
                                updateData.setMacAddress(oldInterfaceMsg.getMacAddress());
                                rmNetworkInterfaceService.updateRmNetworkInterfaceByMac(updateData);
                            }
                        }
                    }
                }else{
                    for (NetworkInfo networkInfo : networkInfoList) {
                        // 查询该网卡信息是否存在
                        RmNetworkInterface queryParam = new RmNetworkInterface();
                        queryParam.setClientId(clientId);
                        queryParam.setMacAddress(networkInfo.getMac());
                        List<RmNetworkInterface> exits = rmNetworkInterfaceService.selectRmNetworkInterfaceList(queryParam);
                        if(exits.isEmpty()){
                            // 保存网卡信息
                            RmNetworkInterface insertData = new RmNetworkInterface();
                            // 业务ip和管理网ip
                            insertData.setClientId(clientId);
                            insertData.setIsp(networkInfo.getCarrier());
                            insertData.setCity(networkInfo.getCity());
                            insertData.setGateway(networkInfo.getGateway());
                            insertData.setInterfaceName(networkInfo.getName());
                            insertData.setIpv4Address(networkInfo.getIpv4());
                            insertData.setMacAddress(networkInfo.getMac());
                            insertData.setProvince(networkInfo.getProvince());
                            insertData.setPublicIp(networkInfo.getPublicIp());
                            insertData.setInterfaceType(networkInfo.getType());
                            insertData.setCreateTime(createTime);
                            rmNetworkInterfaceService.insertRmNetworkInterface(insertData);
                        }else{
                            RmNetworkInterface oldInterfaceMsg = exits.get(0);
                            if(!StringUtils.equals(networkInfo.getName(),oldInterfaceMsg.getInterfaceName())
                                    || !StringUtils.equals(networkInfo.getGateway(),oldInterfaceMsg.getGateway())){
                                // 查询该网卡信息是否旧数据
                                RmNetworkInterface query = new RmNetworkInterface();
                                query.setClientId(clientId);
                                query.setMacAddress(networkInfo.getMac());
                                query.setNewFlag(999);
                                List<RmNetworkInterface> oldExits = rmNetworkInterfaceService.selectRmNetworkInterfaceList(query);
                                if(!oldExits.isEmpty()){
                                    // 先删除旧数据
                                    oldExits.forEach(oldMsg ->{
                                        rmNetworkInterfaceService.deleteRmNetworkInterfaceById(oldMsg.getId());
                                    });
                                }
                                // 先将已存在的改为旧数据
                                RmNetworkInterface oldData = new RmNetworkInterface();
                                oldData.setNewFlag(0);
                                oldData.setClientId(clientId);
                                oldData.setMacAddress(oldInterfaceMsg.getMacAddress());
                                rmNetworkInterfaceService.updateRmNetworkInterfaceByMac(oldData);
                                RmNetworkInterface insertData = new RmNetworkInterface();
                                insertData.setClientId(clientId);
                                insertData.setIsp(networkInfo.getCarrier());
                                insertData.setCity(networkInfo.getCity());
                                insertData.setGateway(networkInfo.getGateway());
                                insertData.setInterfaceName(networkInfo.getName());
                                insertData.setIpv4Address(networkInfo.getIpv4());
                                insertData.setMacAddress(networkInfo.getMac());
                                insertData.setProvince(networkInfo.getProvince());
                                insertData.setPublicIp(networkInfo.getPublicIp());
                                insertData.setInterfaceType(networkInfo.getType());
                                rmNetworkInterfaceService.insertRmNetworkInterface(insertData);
                            }else {
                                RmNetworkInterface updateData = new RmNetworkInterface();
                                // 检查其他字段是否需要更新
                                boolean needUpdate = false;
                                // 逐个字段比较是否需要更新
                                if (!StringUtils.equals(networkInfo.getCity(), oldInterfaceMsg.getCity())) {
                                    updateData.setCity(networkInfo.getCity());
                                    needUpdate = true;
                                }
                                if (!StringUtils.equals(networkInfo.getIpv4(), oldInterfaceMsg.getIpv4Address())) {
                                    updateData.setIpv4Address(networkInfo.getIpv4());
                                    needUpdate = true;
                                }
                                if (!StringUtils.equals(networkInfo.getProvince(), oldInterfaceMsg.getProvince())) {
                                    updateData.setProvince(networkInfo.getProvince());
                                    needUpdate = true;
                                }
                                if (!StringUtils.equals(networkInfo.getPublicIp(), oldInterfaceMsg.getPublicIp())) {
                                    updateData.setPublicIp(networkInfo.getPublicIp());
                                    needUpdate = true;
                                    updateData.setBindIp("0");
                                    // 修改绑定公网ip状态
                                    RmResourceRegistrationRemote updateParam = new RmResourceRegistrationRemote();
                                    updateParam.setClientId(clientId);
                                    updateParam.setMultiPublicIpStatus("0");
                                    remoteRevenueConfigService.updateStatusByResource(updateParam, SecurityConstants.INNER);
                                }
                                if (!StringUtils.equals(networkInfo.getCarrier(), oldInterfaceMsg.getIsp())) {
                                    updateData.setIsp(networkInfo.getCarrier());
                                    needUpdate = true;
                                }
                                if (!StringUtils.equals(networkInfo.getType(), oldInterfaceMsg.getInterfaceType())) {
                                    updateData.setInterfaceType(networkInfo.getType());
                                    needUpdate = true;
                                }
                                // 只有有字段变化时才执行更新
                                if (needUpdate) {
                                    updateData.setClientId(clientId);
                                    updateData.setMacAddress(oldInterfaceMsg.getMacAddress());
                                    rmNetworkInterfaceService.updateRmNetworkInterfaceByMac(updateData);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 获取最新策略
     * @param deviceMessage
     */
    private void handleNewPolicyMessage(DeviceMessage deviceMessage) {
        List<RegisterMsgVo> interfaces = JsonDataParser.parseJsonData(deviceMessage.getData(), RegisterMsgVo.class);
        if(!interfaces.isEmpty()) {
            RegisterMsgVo registerMsgVo = interfaces.get(0);
            String clientId = registerMsgVo.getClientId();
            // 如果未下发监控策略，下发
            rmMonitorPolicyService.issuePolicyMsgByClientId(clientId);
            // 如果未下发服务器脚本策略，下发
            rmDeploymentPolicyService.issueDeployPolicyMsgByClientId(clientId);
            // 如果路由有变化，更新路由信息
            rmNetworkInterfaceService.updateRouteMsg(clientId);
        }
    }

    /**
     * 服务器注册
     * @param message
     */
    private void handleRegisterMessage(DeviceMessage message) {
        List<RegisterMsgVo> interfaces = JsonDataParser.parseJsonData(message.getData(), RegisterMsgVo.class);
        if(!interfaces.isEmpty()) {
            String clientId = message.getClientId();
            RegisterMsgVo registerMsg = interfaces.get(0);
            // 自动注册服务器信息
            RmRegisterMsgRemote rmRegisterMsgRemote = new RmRegisterMsgRemote();
            BeanUtils.copyProperties(registerMsg, rmRegisterMsgRemote);
            int rows = remoteRevenueConfigService.innerAddRegist(rmRegisterMsgRemote, SecurityConstants.INNER).getData();
            if(rows == 2){
                // 注册成功，下发优先级为0的策略
                rmMonitorPolicyService.issueDefaultPolicyByClientId(message.getClientId());
                // agent更新表插入数据
                // 存储更新结果
                // agent更新结果存储
                RmAgentManagement query = new RmAgentManagement();
                query.setClientId(clientId);
                List<RmAgentManagement> agentManagements = rmAgentManagementService.selectRmAgentManagementList(query);
                if(agentManagements == null || agentManagements.isEmpty()) {
                    RmAgentManagement insertAgentUpdateData = new RmAgentManagement();
                    insertAgentUpdateData.setClientId(clientId);
                    rmAgentManagementService.insertRmAgentManagement(insertAgentUpdateData);
                }
            }
            // 时间戳转换
            long timestamp = registerMsg.getTimestamp();
            long millis = timestamp * 1000;
            Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
            String timeStr = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", createTime);
            List<NetworkInfo> networkInfoList = registerMsg.getNetworkInfo();
            if(!networkInfoList.isEmpty()){
                if(networkInfoList.size()==1){
                    NetworkInfo networkInfo = networkInfoList.get(0);
                    // 查询该网卡信息是否存在
                    RmNetworkInterface queryParam = new RmNetworkInterface();
                    queryParam.setClientId(clientId);
                    queryParam.setMacAddress(networkInfo.getMac());
                    queryParam.setNewFlag(1);
                    List<RmNetworkInterface> exits = rmNetworkInterfaceService.selectRmNetworkInterfaceList(queryParam);
                    if(exits.isEmpty()){
                        // 保存网卡信息
                        RmNetworkInterface insertData = new RmNetworkInterface();
                        // 业务ip和管理网ip
                        insertData.setBindIp("3");
                        insertData.setClientId(clientId);
                        insertData.setIsp(networkInfo.getCarrier());
                        insertData.setCity(networkInfo.getCity());
                        insertData.setGateway(networkInfo.getGateway());
                        insertData.setInterfaceName(networkInfo.getName());
                        insertData.setIpv4Address(networkInfo.getIpv4());
                        insertData.setMacAddress(networkInfo.getMac());
                        insertData.setProvince(networkInfo.getProvince());
                        insertData.setPublicIp(networkInfo.getPublicIp());
                        insertData.setInterfaceType(networkInfo.getType());
                        insertData.setCreateTime(createTime);
                        rmNetworkInterfaceService.insertRmNetworkInterface(insertData);
                    }else{
                        RmNetworkInterface oldInterfaceMsg = exits.get(0);
                        if(!StringUtils.equals(networkInfo.getName(),oldInterfaceMsg.getInterfaceName())
                        || !StringUtils.equals(networkInfo.getGateway(),oldInterfaceMsg.getGateway())){
                            // 查询该网卡信息是否旧数据
                            RmNetworkInterface query = new RmNetworkInterface();
                            query.setClientId(clientId);
                            query.setMacAddress(oldInterfaceMsg.getMacAddress());
                            query.setNewFlag(999);
                            List<RmNetworkInterface> oldExits = rmNetworkInterfaceService.selectRmNetworkInterfaceList(query);
                            if(!oldExits.isEmpty()){
                                // 先删除旧数据
                                oldExits.forEach(oldMsg ->{
                                    rmNetworkInterfaceService.deleteRmNetworkInterfaceById(oldMsg.getId());
                                });
                            }
                            // 先将已存在的改为旧数据
                            RmNetworkInterface oldData = new RmNetworkInterface();
                            oldData.setNewFlag(0);
                            oldData.setMacAddress(oldInterfaceMsg.getMacAddress());
                            oldData.setClientId(clientId);
                            rmNetworkInterfaceService.updateRmNetworkInterfaceByMac(oldData);
                            RmNetworkInterface insertData = new RmNetworkInterface();
                            insertData.setClientId(clientId);
                            insertData.setIsp(networkInfo.getCarrier());
                            insertData.setCity(networkInfo.getCity());
                            insertData.setGateway(networkInfo.getGateway());
                            insertData.setInterfaceName(networkInfo.getName());
                            insertData.setIpv4Address(networkInfo.getIpv4());
                            insertData.setMacAddress(networkInfo.getMac());
                            insertData.setProvince(networkInfo.getProvince());
                            insertData.setPublicIp(networkInfo.getPublicIp());
                            insertData.setInterfaceType(networkInfo.getType());
                            rmNetworkInterfaceService.insertRmNetworkInterface(insertData);
                        }else {
                            RmNetworkInterface updateData = new RmNetworkInterface();
                            // 检查其他字段是否需要更新
                            boolean needUpdate = false;
                            // 逐个字段比较是否需要更新
                            if (!StringUtils.equals(networkInfo.getCity(), oldInterfaceMsg.getCity())) {
                                updateData.setCity(networkInfo.getCity());
                                needUpdate = true;
                            }
                            if (!StringUtils.equals(networkInfo.getIpv4(), oldInterfaceMsg.getIpv4Address())) {
                                updateData.setIpv4Address(networkInfo.getIpv4());
                                needUpdate = true;
                            }
                            if (!StringUtils.equals(networkInfo.getProvince(), oldInterfaceMsg.getProvince())) {
                                updateData.setProvince(networkInfo.getProvince());
                                needUpdate = true;
                            }
                            if (!StringUtils.equals(networkInfo.getPublicIp(), oldInterfaceMsg.getPublicIp())) {
                                updateData.setPublicIp(networkInfo.getPublicIp());
                                needUpdate = true;
                            }
                            if (!StringUtils.equals(networkInfo.getCarrier(), oldInterfaceMsg.getIsp())) {
                                updateData.setIsp(networkInfo.getCarrier());
                                needUpdate = true;
                            }
                            if (!StringUtils.equals(networkInfo.getType(), oldInterfaceMsg.getInterfaceType())) {
                                updateData.setInterfaceType(networkInfo.getType());
                                needUpdate = true;
                            }
                            // 只有有字段变化时才执行更新
                            if (needUpdate) {
                                updateData.setClientId(clientId);
                                updateData.setMacAddress(oldInterfaceMsg.getMacAddress());
                                rmNetworkInterfaceService.updateRmNetworkInterfaceByMac(updateData);
                            }
                        }
                    }
                }else{
                    for (NetworkInfo networkInfo : networkInfoList) {
                        // 查询该网卡信息是否存在
                        RmNetworkInterface queryParam = new RmNetworkInterface();
                        queryParam.setMacAddress(networkInfo.getMac());
                        queryParam.setClientId(clientId);
                        List<RmNetworkInterface> exits = rmNetworkInterfaceService.selectRmNetworkInterfaceList(queryParam);
                        if(exits.isEmpty()){
                            // 保存网卡信息
                            RmNetworkInterface insertData = new RmNetworkInterface();
                            // 业务ip和管理网ip
                            insertData.setClientId(clientId);
                            insertData.setIsp(networkInfo.getCarrier());
                            insertData.setCity(networkInfo.getCity());
                            insertData.setGateway(networkInfo.getGateway());
                            insertData.setInterfaceName(networkInfo.getName());
                            insertData.setIpv4Address(networkInfo.getIpv4());
                            insertData.setMacAddress(networkInfo.getMac());
                            insertData.setProvince(networkInfo.getProvince());
                            insertData.setPublicIp(networkInfo.getPublicIp());
                            insertData.setInterfaceType(networkInfo.getType());
                            insertData.setCreateTime(createTime);
                            rmNetworkInterfaceService.insertRmNetworkInterface(insertData);
                        }else{
                            RmNetworkInterface oldInterfaceMsg = exits.get(0);
                            if(!StringUtils.equals(networkInfo.getName(),oldInterfaceMsg.getInterfaceName())
                                    || !StringUtils.equals(networkInfo.getGateway(),oldInterfaceMsg.getGateway())){
                                // 查询该网卡信息是否旧数据
                                RmNetworkInterface query = new RmNetworkInterface();
                                query.setClientId(clientId);
                                query.setMacAddress(oldInterfaceMsg.getMacAddress());
                                query.setNewFlag(999);
                                List<RmNetworkInterface> oldExits = rmNetworkInterfaceService.selectRmNetworkInterfaceList(query);
                                if(!oldExits.isEmpty()){
                                    // 先删除旧数据
                                    oldExits.forEach(oldMsg ->{
                                        rmNetworkInterfaceService.deleteRmNetworkInterfaceById(oldMsg.getId());
                                    });
                                }
                                // 先将已存在的改为旧数据
                                RmNetworkInterface oldData = new RmNetworkInterface();
                                oldData.setNewFlag(0);
                                oldData.setClientId(clientId);
                                oldData.setMacAddress(oldInterfaceMsg.getMacAddress());
                                rmNetworkInterfaceService.updateRmNetworkInterfaceByMac(oldData);
                                RmNetworkInterface insertData = new RmNetworkInterface();
                                insertData.setClientId(clientId);
                                insertData.setIsp(networkInfo.getCarrier());
                                insertData.setCity(networkInfo.getCity());
                                insertData.setGateway(networkInfo.getGateway());
                                insertData.setInterfaceName(networkInfo.getName());
                                insertData.setIpv4Address(networkInfo.getIpv4());
                                insertData.setMacAddress(networkInfo.getMac());
                                insertData.setProvince(networkInfo.getProvince());
                                insertData.setPublicIp(networkInfo.getPublicIp());
                                insertData.setInterfaceType(networkInfo.getType());
                                rmNetworkInterfaceService.insertRmNetworkInterface(insertData);
                            }else {
                                RmNetworkInterface updateData = new RmNetworkInterface();
                                // 检查其他字段是否需要更新
                                boolean needUpdate = false;
                                // 逐个字段比较是否需要更新
                                if (!StringUtils.equals(networkInfo.getCity(), oldInterfaceMsg.getCity())) {
                                    updateData.setCity(networkInfo.getCity());
                                    needUpdate = true;
                                }
                                if (!StringUtils.equals(networkInfo.getIpv4(), oldInterfaceMsg.getIpv4Address())) {
                                    updateData.setIpv4Address(networkInfo.getIpv4());
                                    needUpdate = true;
                                }
                                if (!StringUtils.equals(networkInfo.getProvince(), oldInterfaceMsg.getProvince())) {
                                    updateData.setProvince(networkInfo.getProvince());
                                    needUpdate = true;
                                }
                                if (!StringUtils.equals(networkInfo.getPublicIp(), oldInterfaceMsg.getPublicIp())) {
                                    updateData.setPublicIp(networkInfo.getPublicIp());
                                    needUpdate = true;
                                    updateData.setBindIp("0");
                                    // 修改绑定公网ip状态
                                    RmResourceRegistrationRemote updateParam = new RmResourceRegistrationRemote();
                                    updateParam.setClientId(clientId);
                                    updateParam.setMultiPublicIpStatus("0");
                                    remoteRevenueConfigService.updateStatusByResource(updateParam, SecurityConstants.INNER);
                                }
                                if (!StringUtils.equals(networkInfo.getCarrier(), oldInterfaceMsg.getIsp())) {
                                    updateData.setIsp(networkInfo.getCarrier());
                                    needUpdate = true;
                                }
                                if (!StringUtils.equals(networkInfo.getType(), oldInterfaceMsg.getInterfaceType())) {
                                    updateData.setInterfaceType(networkInfo.getType());
                                    needUpdate = true;
                                }
                                // 只有有字段变化时才执行更新
                                if (needUpdate) {
                                    updateData.setClientId(clientId);
                                    updateData.setMacAddress(oldInterfaceMsg.getMacAddress());
                                    rmNetworkInterfaceService.updateRmNetworkInterfaceByMac(updateData);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * agent更新响应
     * @param message
     */
    private void handleAgentUpdateRspMessage(DeviceMessage message) {
        List<RspVo> rspVoList = JsonDataParser.parseJsonData(message.getData(), RspVo.class);
        if (!rspVoList.isEmpty()) {
            RspVo rsp = rspVoList.get(0);
            if(rsp.getResCode() == 1){
                RmAgentManagement rmAgentManagement = new RmAgentManagement();
                rmAgentManagement.setClientId(message.getClientId());
                rmAgentManagement.setLastUpdateResult("1");
                rmAgentManagement.setLastUpdateTime(DateUtils.getNowDate());
                rmAgentManagementService.updateRmAgentManagementByHardwareSn(rmAgentManagement);
            }else{
                RmAgentManagement rmAgentManagement = new RmAgentManagement();
                rmAgentManagement.setClientId(message.getClientId());
                rmAgentManagement.setLastUpdateResult("0");
                rmAgentManagement.setLastUpdateTime(DateUtils.getNowDate());
                rmAgentManagementService.updateRmAgentManagementByHardwareSn(rmAgentManagement);
            }
        }
    }

    /**
     * 注册消息处理器
     */
    private void registerHandler(String dataType, Consumer<DeviceMessage> handler) {
        messageHandlers.put(dataType, handler);
    }

    /**
     * 处理设备消息（对外暴露的主方法）
     */
    public void handleMessage(DeviceMessage message) {
        String dataType = message.getDataType();
        Consumer<DeviceMessage> handler = messageHandlers.get(dataType);

        if (handler != null) {
            handler.accept(message);
        } else {
            log.warn("未知数据类型：{}", dataType);
        }
    }

    // ========== 具体的消息处理方法 ==========

    /**
     * 网络流量数据入库
     * @param message
     */
    private void handleNetMessage(DeviceMessage message) {
        List<InitialBandwidthTraffic> interfaces = JsonDataParser.parseJsonData(message.getData(), InitialBandwidthTraffic.class);
        if(!interfaces.isEmpty()){
            String clientId = message.getClientId();
            // 时间戳转换
            long timestamp = interfaces.get(0).getTimestamp();
            long millis = timestamp * 1000;
            Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
            String timeStr = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",createTime);
            // 创建比timestamp少5分钟的时间
            long fiveMinutesEarlier = millis - (5 * 60 * 1000); // 减去5分钟的毫秒数
            Date fiveMinutesEarlierDate = new Date(fiveMinutesEarlier / 1000 * 1000); // 同样去除毫秒
            // 查询临时表信息，计算实际流量值
            InitialBandwidthTrafficTemp temp = new InitialBandwidthTrafficTemp();
            temp.setCreateTime(fiveMinutesEarlierDate);
            temp.setClientId(clientId);
            List<InitialBandwidthTrafficTemp> tempList = initialBandwidthTrafficTempService.selectInitialBandwidthTrafficTempList(temp);
            if(!tempList.isEmpty()){
                // 1. 构建快速查找的Map
                Map<String, InitialBandwidthTrafficTemp> tempMap = tempList.stream()
                        .collect(Collectors.toMap(
                                InitialBandwidthTrafficTemp::getMac,
                                Function.identity(),
                                (existing, replacement) -> existing
                        ));

                // 2. 预计算除数（避免重复创建对象）
                BigDecimal divisor = new BigDecimal(300);

                interfaces.forEach(iface -> {
                    iface.setClientId(clientId);
                    iface.setCreateTime(createTime);
                    // 发送流量
                    iface.setTotalOutSpeed(dataProcessUtil.bytesToBits(iface.getOutSpeed()));
                    // 接收流量
                    iface.setTotalInSpeed(dataProcessUtil.bytesToBits(iface.getInSpeed()));
                    InitialBandwidthTrafficTemp tempInfo = tempMap.get(iface.getMac());
                    if (tempInfo != null) {
                        // 计算inSpeed
                        if (iface.getTotalInSpeed() != null && tempInfo.getTotalInSpeed() != null) {
                            BigDecimal nowInSpeed = new BigDecimal(iface.getTotalInSpeed());
                            BigDecimal tempInSpeed = new BigDecimal(tempInfo.getTotalInSpeed());
                            BigDecimal inDiff = nowInSpeed.subtract(tempInSpeed);

                            // 检查相减结果是否为非负数
                            if (inDiff.compareTo(BigDecimal.ZERO) >= 0) {
                                iface.setInSpeed(inDiff.divide(divisor, 0, RoundingMode.HALF_UP).toString());
                            }else{
                                iface.setInSpeed(null);
                            }
                        }
                        // 计算outSpeed
                        if (iface.getTotalOutSpeed() != null && tempInfo.getTotalOutSpeed() != null) {
                            BigDecimal nowOutSpeed = new BigDecimal(iface.getTotalOutSpeed());
                            BigDecimal tempOutSpeed = new BigDecimal(tempInfo.getTotalOutSpeed());
                            BigDecimal outDiff = nowOutSpeed.subtract(tempOutSpeed);

                            // 检查相减结果是否为非负数
                            if (outDiff.compareTo(BigDecimal.ZERO) >= 0) {
                                iface.setOutSpeed(outDiff.divide(divisor, 0, RoundingMode.HALF_UP).toString());
                            }else{
                                iface.setOutSpeed(null);
                            }
                        }
                    }
                });
                // 清空临时表对应server信息
                initialBandwidthTrafficTempService.deleteTempMsgByClientId(clientId);
            }else{
                interfaces.forEach(iface -> {
                    iface.setClientId(clientId);
                    iface.setCreateTime(createTime);
                    // 总发送流量
                    iface.setTotalOutSpeed(dataProcessUtil.bytesToBits(iface.getOutSpeed()));
                    // 总接收流量
                    iface.setTotalInSpeed(dataProcessUtil.bytesToBits(iface.getInSpeed()));
                    iface.setInSpeed(null);
                    iface.setOutSpeed(null);
                });
            }
            InitialBandwidthTraffic data = new InitialBandwidthTraffic();
            // 批量入库集合
            data.setList(interfaces);
            // 临时表 用来计算inSpeed outSeppd
            initialBandwidthTrafficTempService.batchInsertServerTemp(interfaces);
            // 初始流量数据入库
            initialBandwidthTrafficService.batchInsert(data);
            EpsInitialTrafficDataRemote epsInitialTrafficDataRemote = new EpsInitialTrafficDataRemote();
            epsInitialTrafficDataRemote.setStartTime(timeStr);
            epsInitialTrafficDataRemote.setEndTime(timeStr);
            // 复制到业务初始库
            remoteRevenueConfigService.autoSaveServiceTrafficData(epsInitialTrafficDataRemote, SecurityConstants.INNER);
        }else{
            throw new RuntimeException("NET流量data数据为空");
        }
    }
    /**
     * docker数据入库
     * @param message
     */
    private void handleDockerMessage(DeviceMessage message) {
        List<InitialDockerInfo> dockers = JsonDataParser.parseJsonData(message.getData(), InitialDockerInfo.class);
        if(!dockers.isEmpty()){
            // 时间戳转换
            long timestamp = dockers.get(0).getTimestamp();
            long millis = timestamp * 1000;
            Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
            dockers.forEach(iface -> {
                iface.setClientId(message.getClientId());
                iface.setCreateTime(createTime);
            });
            // 初始容器数据入库
            initialDockerInfoService.batchInsertInitialDockerInfo(dockers);
        }else{
            throw new RuntimeException("DOCKER容器data数据为空");
        }
    }
    /**
     * cpu数据入库
     * @param message
     */
    private void handleCpuMessage(DeviceMessage message) {
        List<InitialCpuInfo> cpus = JsonDataParser.parseJsonData(message.getData(),InitialCpuInfo.class);
        // 时间戳转换
        long timestamp = cpus.get(0).getTimestamp();
        long millis = timestamp * 1000;
        Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
        if(!cpus.isEmpty()){
            cpus.forEach(iface -> {
                iface.setClientId(message.getClientId());
                iface.setCreateTime(createTime);
            });
            // 初始CPU数据入库
            initialCpuInfoService.batchInsertInitialCpuInfo(cpus);
        }else{
            throw new RuntimeException("CPUdata数据为空");
        }
    }
    /**
     * 磁盘数据入库
     * @param message
     */
    private void handleDiskMessage(DeviceMessage message) {
        List<InitialDiskInfo> disks = JsonDataParser.parseJsonData(message.getData(), InitialDiskInfo.class);
        // 时间戳转换
        long timestamp = disks.get(0).getTimestamp();
        long millis = timestamp * 1000;
        Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
        if(!disks.isEmpty()){
            disks.forEach(iface -> {
                iface.setClientId(message.getClientId());
                iface.setCreateTime(createTime);
            });
            // 初始磁盘数据入库
            initialDiskInfoService.batchInsertInitialDiskInfo(disks);
        }else{
            throw new RuntimeException("磁盘data数据为空");
        }
    }
    /**
     * 内存数据入库
     * @param message
     */
    private void handleMemoryMessage(DeviceMessage message) {
        List<InitialMemoryInfo> memorys = JsonDataParser.parseJsonData(message.getData(), InitialMemoryInfo.class);
        if(!memorys.isEmpty()){
            // 时间戳转换
            long timestamp = memorys.get(0).getTimestamp();
            long millis = timestamp * 1000;
            Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
            memorys.forEach(iface -> {
                iface.setClientId(message.getClientId());
                iface.setCreateTime(createTime);
            });
            // 初始内存数据入库
            initialMemoryInfoService.batchInsertInitialMemoryInfo(memorys);
        }else{
            throw new RuntimeException("内存data数据为空");
        }
    }
    /**
     * 挂载点数据入库
     * @param message
     */
    private void handleMountPointMessage(DeviceMessage message) {
        List<InitialMountPointInfo> mountPointInfos = JsonDataParser.parseJsonData(message.getData(), InitialMountPointInfo.class);
        if(!mountPointInfos.isEmpty()){
            // 时间戳转换
            long timestamp = mountPointInfos.get(0).getTimestamp();
            long millis = timestamp * 1000;
            Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
            mountPointInfos.forEach(iface -> {
                iface.setClientId(message.getClientId());
                iface.setCreateTime(createTime);
            });
            // 初始挂载点数据入库
            initialMountPointInfoService.batchInsertInitialMountPointInfo(mountPointInfos);
        }else{
            throw new RuntimeException("挂载点data数据为空");
        }
    }




    /**
     * 系统其他信息
     * @param message
     */
    private void handleOtherSystemMessage(DeviceMessage message) {
        List<CollectDataVo> otherData = JsonDataParser.parseJsonData(message.getData(), CollectDataVo.class);
        if(!otherData.isEmpty()){
            CollectDataVo systemDataVo = otherData.get(0);
            if (systemDataVo != null){
                try {
                    InitialSystemOtherCollectData insertData = new InitialSystemOtherCollectData();

                    // 时间戳转换
                    long timestamp = systemDataVo.getTimestamp();
                    long millis = timestamp * 1000;
                    Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒

                    insertData.setClientId(message.getClientId());
                    insertData.setCreateTime(createTime);
                    String collectType = systemDataVo.getType();
                    String collectValue = systemDataVo.getValue();
                    insertData.setCollectType(collectType);
                    insertData.setCollectValue(collectValue);
                    iInitialSystemOtherCollectDataService.insertInitialSystemOtherCollectData(insertData);

                } catch (Exception e) {
                    log.error("解析JSON数据失败: {}, value: {}", e.getMessage(), systemDataVo.getValue());
                    // 可以选择保存原始数据或进行其他错误处理
                }
            }
        }
    }
    /**
     * 监听心跳
     * @param message
     */
    private void handleHeartbeatMessage(DeviceMessage message) {
        List<InitialHeartbeatListen> heartbeats = JsonDataParser.parseJsonData(message.getData(), InitialHeartbeatListen.class);
        if(!heartbeats.isEmpty()){
            InitialHeartbeatListen heartbeat = heartbeats.get(0);
            String clientId = message.getClientId();
            String version = heartbeat.getVersion();
            String name = heartbeat.getName();
            log.debug("处理心跳消息，客户端ID: {}, 时间: {}", clientId, heartbeat.getTimestamp());

            // 使用Redis存储状态
            String statusKey = HEARTBEAT_STATUS_PREFIX + clientId;
            String timeKey = HEARTBEAT_TIME_PREFIX + clientId;
            String recoveryCountKey = HEARTBEAT_RECOVERY_COUNT_PREFIX + clientId;
            String heartbeatCountKey = HEARTBEAT_COUNT_PREFIX + clientId;
            String alertKey = HEARTBEAT_ALERT_PREFIX + clientId;

            try {
                // 记录处理前状态（调试用）
                String prevStatus = redisTemplate.opsForValue().get(statusKey);
                String prevTime = redisTemplate.opsForValue().get(timeKey);
                String prevHeartbeatCount = redisTemplate.opsForValue().get(heartbeatCountKey);
                Boolean prevAlertStatus = redisTemplate.hasKey(alertKey);
                log.debug("客户端ID: {} 处理前状态 - status: {}, time: {}, heartbeatCount: {}, hasAlert: {}",
                        clientId, prevStatus, prevTime, prevHeartbeatCount, prevAlertStatus);

                // 原子递增心跳计数（线程安全）
                Long newHeartbeatCount = redisTemplate.opsForValue().increment(heartbeatCountKey);

                // 使用事务更新状态和时间
                redisTemplate.execute(new SessionCallback<Object>() {
                    @Override
                    public Object execute(RedisOperations operations) throws DataAccessException {
                        operations.multi();
                        // 重置丢失计数为0，设置最后心跳时间
                        operations.opsForValue().set(statusKey, "0");
                        operations.opsForValue().set(timeKey, String.valueOf(System.currentTimeMillis()));
                        return operations.exec();
                    }
                });

                log.debug("客户端ID: {} 心跳处理完成，当前心跳次数: {}", clientId, newHeartbeatCount);

                // 检查是否之前有告警状态（心跳恢复检测）
                if (Boolean.TRUE.equals(redisTemplate.hasKey(alertKey))) {
                    log.info("客户端ID: {} 检测到心跳恢复", clientId);

                    // 原子递增恢复计数
                    Long recoveryCount = redisTemplate.opsForValue().increment(recoveryCountKey);

                    log.debug("客户端ID: {} 恢复计数: {}", clientId, recoveryCount);

                    if (recoveryCount >= 2) {
                        // 达到2次恢复，清除告警状态
                        log.warn("客户端ID: {} 心跳恢复达到{}次，清除告警状态", clientId, recoveryCount);
                        insertHeartbeatLog(clientId, "2", "心跳恢复,设备在线状态改为在线");

                        // 清理告警相关key
                        redisTemplate.delete(alertKey);
                        redisTemplate.delete(recoveryCountKey);

                        // 修改资源状态为在线
                        updateResourceStatus(clientId, "1");

                        log.info("客户端ID: {} 告警状态已清除", clientId);
                    } else {
                        // 未达到2次，只记录恢复次数
                        log.info("客户端ID: {} 心跳恢复第{}次", clientId, recoveryCount);
                    }
                }

                // 只有达到3次心跳才执行数据库操作
                if (newHeartbeatCount >= 3) {
                    log.info("客户端ID: {} 达到{}次心跳，开始执行数据库操作", clientId, newHeartbeatCount);

                    // 添加逻辑节点标识
                    RmResourceRegistrationRemote updateData = new RmResourceRegistrationRemote();
                    updateData.setClientId(message.getClientId());
                    updateData.setLogicalNodeId(heartbeat.getLogicalNode());
                    updateData.setOnlineStatus("1");
                    updateData.setAgentVersion(version);
                    updateData.setOnboardTime(DateUtils.getNowDate());
                    remoteRevenueConfigService.innerUpdateRegist(updateData, SecurityConstants.INNER);

                    // agent更新结果存储
                    RmAgentManagement query = new RmAgentManagement();
                    query.setClientId(clientId);
                    List<RmAgentManagement> agentManagements = rmAgentManagementService.selectRmAgentManagementList(query);
                    if(!agentManagements.isEmpty()){
                        RmAgentManagement rmAgentManagement = agentManagements.get(0);
                        if(!StringUtils.equals(rmAgentManagement.getAgentVersion(), version)){
                            // 存储更新结果
                            RmAgentManagement updateResultData = new RmAgentManagement();
                            updateResultData.setId(rmAgentManagement.getId());
                            updateResultData.setAgentVersion(version);
                            updateResultData.setLastUpdateTime(DateUtils.getNowDate());
                            updateResultData.setLastUpdateResult("1");
                            rmAgentManagementService.updateRmAgentManagementByHardwareSn(updateResultData);
                        }
                    }

                    // 交换机在线状态改为在线（只在第3次心跳时执行）
                    if(newHeartbeatCount == 3){
                        log.info("客户端ID: {} 第3次心跳，更新交换机状态为在线", clientId);
                        RmSwitchManagementRemote rmSwitchManagementRemote = new RmSwitchManagementRemote();
                        rmSwitchManagementRemote.setClientId(clientId);
                        R<List<RmSwitchManagementRemote>> rmSwitchManagementRemoteListR = remoteRevenueConfigService.getSwitchNameByClientId(rmSwitchManagementRemote, SecurityConstants.INNER);
                        if(rmSwitchManagementRemoteListR != null &&
                                rmSwitchManagementRemoteListR.getData()!=null &&
                                !rmSwitchManagementRemoteListR.getData().isEmpty()){
                            RmSwitchManagementRemote switchUpdate = new RmSwitchManagementRemote();
                            switchUpdate.setClientId(clientId);
                            switchUpdate.setOnlineStatus("1");
                            remoteRevenueConfigService.updateSwitchMsgByClientId(switchUpdate, SecurityConstants.INNER);
                            log.info("客户端ID: {} 交换机状态更新完成", clientId);
                        } else {
                            log.debug("客户端ID: {} 未找到对应的交换机信息", clientId);
                        }
                    }
                } else {
                    log.debug("客户端ID: {} 当前心跳次数: {}，未达到3次，跳过数据库操作", clientId, newHeartbeatCount);
                }

                // 记录处理后状态（调试用）
                String currentStatus = redisTemplate.opsForValue().get(statusKey);
                String currentTime = redisTemplate.opsForValue().get(timeKey);
                String currentHeartbeatCount = redisTemplate.opsForValue().get(heartbeatCountKey);
                Boolean currentAlertStatus = redisTemplate.hasKey(alertKey);
                log.debug("客户端ID: {} 处理后状态 - status: {}, time: {}, heartbeatCount: {}, hasAlert: {}",
                        clientId, currentStatus, currentTime, currentHeartbeatCount, currentAlertStatus);

            } catch (Exception e) {
                log.error("处理心跳消息异常, clientId: {}", clientId, e);
            }
        }
    }

    // 添加一个定时任务方法，定期检查心跳状态
    @Scheduled(fixedRate = 60000) // 每60s检查一次
    public void checkHeartbeatStatus() {
        long currentTime = System.currentTimeMillis();
        log.debug("开始心跳状态检查，当前时间: {}", currentTime);

        // 获取所有客户端时间键
        Set<String> timeKeys = redisTemplate.keys(HEARTBEAT_TIME_PREFIX + "*");
        if (timeKeys == null) {
            log.debug("未找到任何心跳时间键");
            return;
        }

        log.debug("找到 {} 个客户端需要检查", timeKeys.size());

        for (String timeKey : timeKeys) {
            String clientId = timeKey.substring(HEARTBEAT_TIME_PREFIX.length());
            String statusKey = HEARTBEAT_STATUS_PREFIX + clientId;
            String alertKey = HEARTBEAT_ALERT_PREFIX + clientId;

            try {
                // 检查是否已经存在告警
                String existingAlert = redisTemplate.opsForValue().get(alertKey);
                if ("1".equals(existingAlert)) {
                    log.debug("客户端ID: {} 已有告警，跳过检查", clientId);
                    continue; // 如果已有告警，跳过处理
                }

                String lastTimeStr = redisTemplate.opsForValue().get(timeKey);
                if (lastTimeStr == null) {
                    log.debug("客户端ID: {} 时间键为空，跳过", clientId);
                    continue;
                }

                long lastHeartbeatTime = Long.parseLong(lastTimeStr);
                long timeDiff = currentTime - lastHeartbeatTime;

                log.debug("客户端ID: {} 最后心跳: {}, 时间差: {}ms, 超时阈值: {}ms",
                        clientId, lastHeartbeatTime, timeDiff, HEARTBEAT_TIMEOUT);

                if (timeDiff > HEARTBEAT_TIMEOUT) {
                    // 心跳超时处理 - 使用原子操作增加计数
                    Long lostCount = redisTemplate.opsForValue().increment(statusKey);
                    if (lostCount == 1) {
                        // 确保第一次增加时值为1
                        redisTemplate.opsForValue().set(statusKey, "1");
                        lostCount = 1L;
                    }

                    log.warn("客户端ID: {} 心跳超时，连续次数: {}, 时间差: {}ms",
                            clientId, lostCount, timeDiff);

                    if (lostCount >= 3) {
                        log.warn("客户端ID: {} 连续三次心跳丢失，触发告警", clientId);
                        insertHeartbeatLog(clientId, "3", "连续三次心跳丢失");
                        // 告警
                        insertAlarmRecords(clientId);
                        redisTemplate.opsForValue().set(HEARTBEAT_ALERT_PREFIX + clientId, "1");
                        // 设置告警后删除timeKey和statusKey
                        redisTemplate.delete(timeKey);
                        redisTemplate.delete(statusKey);

                        log.info("客户端ID: {} 已设置告警并清理心跳记录", clientId);
                        // 修改资源状态
                        updateResourceStatus(clientId, "0");
                    }
                } else {
                    // 如果心跳正常，重置丢失次数
                    String currentStatus = redisTemplate.opsForValue().get(statusKey);
                    if (!"0".equals(currentStatus)) {
                        redisTemplate.opsForValue().set(statusKey, "0");
                        log.debug("客户端ID: {} 心跳正常，重置丢失次数从 {} 到 0", clientId, currentStatus);
                    } else {
                        log.debug("客户端ID: {} 心跳正常，状态已是0", clientId);
                    }
                }
            } catch (Exception e) {
                log.error("检查心跳状态异常, clientId: {}", clientId, e);
            }
        }

        log.debug("心跳状态检查完成");
    }

    // 更新资源状态的公共方法
    private void updateResourceStatus(String clientId, String status) {
        log.info("开启更新资源状态========");
        RmResourceRegistrationRemote query = new RmResourceRegistrationRemote();
        query.setClientId(clientId);
        R<RmResourceRegistrationRemote> registerMsgR = remoteRevenueConfigService.getListByHardwareSn(query, SecurityConstants.INNER);
        if(registerMsgR != null && registerMsgR.getData() != null){
            RmResourceRegistrationRemote rmResourceRegistrationRemote = new RmResourceRegistrationRemote();
            rmResourceRegistrationRemote.setOnlineStatus(status);
            rmResourceRegistrationRemote.setClientId(clientId);
            remoteRevenueConfigService.updateStatusByResource(rmResourceRegistrationRemote, SecurityConstants.INNER);
        }
        RmSwitchManagementRemote rmSwitchManagementRemote = new RmSwitchManagementRemote();
        rmSwitchManagementRemote.setClientId(clientId);
        R<List<RmSwitchManagementRemote>> rmSwitchManagementRemoteListR = remoteRevenueConfigService.getSwitchNameByClientId(rmSwitchManagementRemote, SecurityConstants.INNER);
        if(rmSwitchManagementRemoteListR != null &&
                rmSwitchManagementRemoteListR.getData()!=null &&
                !rmSwitchManagementRemoteListR.getData().isEmpty()){
            RmSwitchManagementRemote switchUpdate = new RmSwitchManagementRemote();
            switchUpdate.setClientId(clientId);
            switchUpdate.setOnlineStatus(status);
            remoteRevenueConfigService.updateSwitchMsgByClientId(switchUpdate, SecurityConstants.INNER);
        }
    }
    // 插入心跳日志到数据库
    private void insertHeartbeatLog(String machineId, String status, String remark) {
        try {
            InitialHeartbeatListenLog listenLog = new InitialHeartbeatListenLog();
            listenLog.setClientId(machineId);
            listenLog.setStatus(status); // 0-离线 1-在线 2-恢复 3-三次丢失
            listenLog.setRemark(remark);
            listenLog.setCreateTime(new Date());

            // 调用DAO或Service插入日志
            initialHeartbeatListenLog.insertInitialHeartbeatListenLog(listenLog);
            log.info("已记录心跳日志，客户端ID: {}, 状态: {}", machineId, status);
        } catch (Exception e) {
            log.error("插入心跳日志失败", e);
        }
    }

    /**
     * 添加告警日志并推送消息到企业微信
     * @param clientId
     */
    private void insertAlarmRecords(String clientId) {
        // 查询clientId是否为交换机唯一标识
        boolean isSwitch = false;
        String switchName = "";
        RmSwitchManagementRemote rmSwitchManagementRemote = new RmSwitchManagementRemote();
        rmSwitchManagementRemote.setClientId(clientId);
        R<List<RmSwitchManagementRemote>> rmSwitchManagementRemoteListR = remoteRevenueConfigService.getSwitchNameByClientId(rmSwitchManagementRemote, SecurityConstants.INNER);
        if (rmSwitchManagementRemoteListR != null &&
                rmSwitchManagementRemoteListR.getData() != null &&
                !rmSwitchManagementRemoteListR.getData().isEmpty()) {
            isSwitch = true;
            switchName = rmSwitchManagementRemoteListR.getData().get(0).getSwitchName();
        }

        // 创建告警日志记录
        RmAlarmLog rmAlarmLog = createAlarmLog(clientId, isSwitch, switchName);

        // 插入告警日志
        rmAlarmLogService.insertRmAlarmLog(rmAlarmLog);

        // 发送告警推送
        sendAlarmPush(rmAlarmLog, isSwitch);
    }

    /**
     * 创建告警日志记录
     */
    private RmAlarmLog createAlarmLog(String clientId, boolean isSwitch, String switchName) {
        RmAlarmLog rmAlarmLog = new RmAlarmLog();
        String alarmContent = clientId + "下线";

        if (isSwitch) {
            rmAlarmLog.setClientId(switchName);
            rmAlarmLog.setAlarmType("2");
        } else {
            // 查询管理网公网ip
            RmNetworkInterface rmNetworkInterface = new RmNetworkInterface();
            rmNetworkInterface.setClientId(clientId);
            rmNetworkInterface.setNewFlag(1);
            List<RmNetworkInterface> interfaceList = rmNetworkInterfaceService.selectRmNetworkInterfaceList(rmNetworkInterface);
            if (interfaceList != null && !interfaceList.isEmpty()) {
                interfaceList.stream()
                        .filter(info -> "2".equals(info.getBindIp()) || "3".equals(info.getBindIp()))
                        .findFirst()
                        .ifPresent(networkInterface -> {
                            rmAlarmLog.setMgmPublicIp(networkInterface.getPublicIp());
                        });
            }
            rmAlarmLog.setClientId(clientId);
            rmAlarmLog.setAlarmType("1");
        }

        rmAlarmLog.setAlarmContent(alarmContent);
        rmAlarmLog.setAlarmTime(DateUtils.getNowDate());
        return rmAlarmLog;
    }

    /**
     * 发送告警推送
     */
    private void sendAlarmPush(RmAlarmLog rmAlarmLog, boolean isSwitch) {
        String alarmTypeCode = isSwitch ? AlarmTypeEnum.交换机下线.getCode() : AlarmTypeEnum.服务器下线.getCode();
        String alarmTypeMsg = isSwitch ? AlarmTypeEnum.交换机下线.getMsg() : AlarmTypeEnum.服务器下线.getMsg();

        RmAlarmPushConfig rmAlarmPushConfig = new RmAlarmPushConfig();
        rmAlarmPushConfig.setPushMethod(PushMethodEnum.企业微信.getCode());
        rmAlarmPushConfig.setPushAlarmTypes(alarmTypeCode);
        List<RmAlarmPushConfig> alarmConfigList = rmAlarmPushConfigService.selectRmAlarmPushConfigList(rmAlarmPushConfig);

        if (alarmConfigList != null && !alarmConfigList.isEmpty()) {
            for (RmAlarmPushConfig alarmPushConfig : alarmConfigList) {
                String contentTemplate = alarmPushConfig.getMessageContent();
                String webhookUrl = alarmPushConfig.getPushAddress();
                Map<String, Object> alarmMap = new HashMap<>();
                alarmMap.put("告警时间", rmAlarmLog.getAlarmTime());
                alarmMap.put("管理网-公网IP", rmAlarmLog.getMgmPublicIp());
                alarmMap.put("告警类型", alarmTypeMsg);
                alarmMap.put("告警设备", rmAlarmLog.getClientId());
                alarmMap.put("告警内容", rmAlarmLog.getAlarmContent());

                if (alarmPushConfig.getContactPhones() != null) {
                    String[] phones = alarmPushConfig.getContactPhones().split(",");
                    WeChatWorkBot.sendTemplateMessage(webhookUrl, contentTemplate, alarmMap, rmAlarmLog.getAlarmContent(), phones, false);
                } else {
                    WeChatWorkBot.sendTemplateMessage(webhookUrl, contentTemplate, alarmMap);
                }
            }
        }
    }

    /**
     * 应答信息
     * @param message
     */
    private RspVo handleResponseMessage(DeviceMessage message) {
        List<RspVo> rspVoList = JsonDataParser.parseJsonData(message.getData(), RspVo.class);
        if (!rspVoList.isEmpty()) {
            RspVo rsp = rspVoList.get(0);
            log.info("应答信息：{}",rsp);
            return rsp;
        }
        return null;
    }
    /**
     * 脚本策略应答信息
     * @param message
     */
    private void handleScriptRspMessage(DeviceMessage message) {
        List<RspVo> rspVoList = JsonDataParser.parseJsonData(message.getData(), RspVo.class);
        if (!rspVoList.isEmpty()) {
            RspVo rsp = rspVoList.get(0);
            // 时间戳转换
            long timestamp = rsp.getTimestamp();
            long millis = timestamp * 1000;
            Date createTime = new Date(millis / 1000 * 1000); // 去除毫秒
            if(rsp.getResCode() == 1){
                if(rsp.getResult() != null){
                    List<RspResultVo> resultVos = JsonDataParser.parseJsonData(rsp.getResult(), RspResultVo.class);
                    RspResultVo rspResultVo = resultVos.get(0);
                    JSONObject jsonObject = JSONObject.parseObject(rsp.getResult());
                    String resOut = jsonObject.getString("resOut");
                    // 查询执行的脚本id
                    RmDeploymentPolicy deploymentPolicy = rmDeploymentPolicyService.selectRmDeploymentPolicyById(Long.valueOf(rspResultVo.getScriptId()));
                    Long businessScriptId = deploymentPolicy.getScriptId();
                    // 判断失败返回结果是否包含脚本执行失败关键字
                    R<EpsBusinessScriptRemote> businessScriptMsgR = remoteRevenueConfigService.getBusinessScriptMsgByScriptId(businessScriptId, SecurityConstants.INNER);
                    // 构建脚本执行结果实体类
                    RmResourceRemote insertData = new RmResourceRemote();
                    insertData.setClientId(message.getClientId());
                    insertData.setScriptId(Long.valueOf(rspResultVo.getScriptId()));
                    if(businessScriptMsgR != null && businessScriptMsgR.getData() != null){
                        EpsBusinessScriptRemote businessScriptMsg = businessScriptMsgR.getData();
                        String failedKeywords = businessScriptMsg.getFailedKeywords();
                        if(resOut.contains(failedKeywords)){
                            insertData.setResultFlag(0);
                        }else {
                            insertData.setResultFlag(1);
                        }
                    }else{
                        insertData.setResultFlag(1);
                    }
                    insertData.setDescription(rsp.getResult());
                    insertData.setCreateTime(createTime);
                    // 执行插入sql
                    rmResourceRemoteService.insertRmResourceRemote(insertData);
                    log.info("脚本执行结果入库成功：{}",rsp);
                }
            }else{
                // 构建脚本执行结果实体类
                RmResourceRemote insertData = new RmResourceRemote();
                insertData.setClientId(message.getClientId());
                insertData.setResultFlag(0);
                if(rsp.getResult() != null){
                    List<RspResultVo> resultVos = JsonDataParser.parseJsonData(rsp.getResult(), RspResultVo.class);
                    RspResultVo rspResultVo = resultVos.get(0);
                    insertData.setDescription(rsp.getResult());
                    insertData.setScriptId(Long.valueOf(rspResultVo.getScriptId()));
                }
                insertData.setCreateTime(createTime);
                // 执行插入sql
                rmResourceRemoteService.insertRmResourceRemote(insertData);
                log.error("脚本执行失败:{}", rsp);
            }
        }
    }
}