//package com.fuyuanshen.global.mqtt.receiver;
//
//import cn.hutool.core.lang.Dict;
//import com.baomidou.lock.LockTemplate;
//import com.fuyuanshen.common.core.constant.GlobalConstants;
//import com.fuyuanshen.common.core.utils.StringUtils;
//import com.fuyuanshen.common.json.utils.JsonUtils;
//import com.fuyuanshen.common.redis.utils.RedisUtils;
//import com.fuyuanshen.global.mqtt.base.NewMqttRuleContext;
//import com.fuyuanshen.global.mqtt.base.NewMqttRuleEngine;
//import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
//import com.fuyuanshen.global.queue.MqttMessageQueueConstants;
//import lombok.extern.slf4j.Slf4j;
//import org.redisson.api.RLock;
//import org.redisson.api.RedissonClient;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.messaging.Message;
//import org.springframework.messaging.MessageHandler;
//import org.springframework.messaging.MessageHeaders;
//import org.springframework.messaging.MessagingException;
//import org.springframework.stereotype.Service;
//
//import java.time.Duration;
//import java.util.Objects;
//import java.util.concurrent.TimeUnit;
//
//import static com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants.DEVICE_KEY_PREFIX;
//
//@Service
//@Slf4j
//public class NewReceiverMessageHandler implements MessageHandler {
//
//    @Autowired
//    private NewMqttRuleEngine newRuleEngine;
//
//    @Override
//    public void handleMessage(Message<?> message) throws MessagingException {
//
//        Object payload = message.getPayload();
//        MessageHeaders headers = message.getHeaders();
//        String receivedTopic = Objects.requireNonNull(headers.get("mqtt_receivedTopic")).toString();
//        String receivedQos = Objects.requireNonNull(headers.get("mqtt_receivedQos")).toString();
//        String timestamp = Objects.requireNonNull(headers.get("timestamp")).toString();
//
//        log.info("MQTT2 payload= {} \n receivedTopic = {} \n receivedQos = {} \n timestamp = {}",
//                payload, receivedTopic, receivedQos, timestamp);
//
//        Dict payloadDict = JsonUtils.parseMap(payload.toString());
//        if (receivedTopic == null || payloadDict == null) {
//            return;
//        }
//        String imei = payloadDict.getStr("imei");
//        String funcType = payloadDict.getStr("funcType");
//        // 执行业务逻辑
//        if(StringUtils.isNotBlank(imei)){
//            String queueKey = MqttMessageQueueConstants.MQTT_MESSAGE_QUEUE_KEY;
//            String dedupKey = MqttMessageQueueConstants.MQTT_MESSAGE_DEDUP_KEY;
//            RedisUtils.offerDeduplicated(queueKey,dedupKey,imei, Duration.ofSeconds(900));
//            //在线状态
//            String deviceOnlineStatusRedisKey = GlobalConstants.GLOBAL_REDIS_KEY+ DEVICE_KEY_PREFIX+ imei + DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX ;
//            RedisUtils.setCacheObject(deviceOnlineStatusRedisKey, "1", Duration.ofSeconds(360));
//        }
//
//        String[] topicArr = receivedTopic.split("/");
//
//        NewMqttRuleContext context = new NewMqttRuleContext();
//        context.setCommandType(topicArr[2]+"_"+funcType);
//        context.setDeviceImei(imei);
//        context.setPayloadDict(payloadDict);
//
//        boolean ruleExecuted = newRuleEngine.executeRule(context);
//
//        if (!ruleExecuted) {
//            log.warn("未找到匹配的规则来处理命令类型: {}", topicArr[2] + " : " +funcType);
//        }
////        final LockInfo lockInfo = lockTemplate.lock(GlobalConstants.GLOBAL_REDIS_KEY + lockKey + imei, 100L, 3000L, RedissonLockExecutor.class);
////        if (null == lockInfo) {
////            log.info("MQTT3业务处理中,请稍后再试:funcType=>{},imei=>{}",funcType,imei);
////            return;
////        }
//////         获取锁成功，处理业务
////        try {
////            if(StringUtils.isNotBlank(imei)){
////                String queueKey = MqttMessageQueueConstants.MQTT_MESSAGE_QUEUE_KEY;
////                String dedupKey = MqttMessageQueueConstants.MQTT_MESSAGE_DEDUP_KEY;
////                RedisUtils.offerDeduplicated(queueKey,dedupKey,imei, Duration.ofSeconds(900));
////                //在线状态
////                String deviceOnlineStatusRedisKey = GlobalConstants.GLOBAL_REDIS_KEY+ DEVICE_KEY_PREFIX+ imei + DeviceRedisKeyConstants.DEVICE_ONLINE_STATUS_KEY_PREFIX ;
////                RedisUtils.setCacheObject(deviceOnlineStatusRedisKey, "1", Duration.ofSeconds(360));
////            }
////
////            String[] topicArr = receivedTopic.split("/");
////
////            NewMqttRuleContext context = new NewMqttRuleContext();
////            context.setCommandType(topicArr[2]+"_"+funcType);
////            context.setDeviceImei(imei);
////            context.setPayloadDict(payloadDict);
////
////            boolean ruleExecuted = newRuleEngine.executeRule(context);
////
////            if (!ruleExecuted) {
////                log.warn("未找到匹配的规则来处理命令类型: {}", topicArr[2] + " : " +funcType);
////            }
////        } finally {
////            //释放锁
////            lockTemplate.releaseLock(lockInfo);
////        }
//
//
//        /* ===== 追加：根据报文内容识别格式并统一解析 ===== */
////        int intType = MqttXinghanCommandType.computeVirtualCommandType(payloadDict);
////        if (intType > 0) {
////            MqttRuleContext newCtx = new MqttRuleContext();
////            String commandType = "Light_"+intType;
////            newCtx.setCommandType(commandType);
////            newCtx.setDeviceImei(imei);
////            newCtx.setPayloadDict(payloadDict);
////
////            boolean ok = ruleEngine.executeRule(newCtx);
////            if (!ok) {
////                log.warn("新规则引擎未命中, imei={}", imei);
////            }
////        }
//    }
//}
