package com.ruoyi.dj.mqtt.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.dj.mqtt.common.CommonFields;
import com.ruoyi.dj.mqtt.common.CommonFieldsVo;
import com.ruoyi.dj.mqtt.domain.DrcVo;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CommonFieldsVoUtil {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 从 JSON 消息中提取 CommonFields 对象
     * @param message JSON 消息
     * @return 提取出的 CommonFields 对象
     */
    public static CommonFieldsVo extractCommonFields(String message) {
        try {
            JsonNode jsonNode = objectMapper.readTree(message);
            String tid = jsonNode.get("tid").asText();
            String bid = jsonNode.get("bid").asText();
            String method = jsonNode.get("method").asText();
            return new CommonFieldsVo(bid, tid, method);
        } catch (Exception e) {
            log.error("提取 CommonFieldsVo 时发生错误: ", e);
            return null;
        }
    }

    public static DrcVo extractDrcVo(String message) {
        try {
            JsonNode jsonNode = objectMapper.readTree(message);
            DrcVo drcVo = new DrcVo();
            drcVo.setMethod(jsonNode.get("method").asText());
            return drcVo;
        } catch (Exception e) {
            log.error("提取 CommonFieldsVo 时发生错误: ", e);
            return null;
        }
    }
}
