package com.multictrl.modules.business.handler;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.multictrl.common.constant.BusinessConstant;
import com.multictrl.common.utils.CacheUtils;
import com.multictrl.common.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 属性设置上报
 *
 * @author Sdy
 * @since 1.0.0 2026/6/17
 */
@Slf4j
@Component
public class SetReplyHandle implements MessageHandler {

    @Override
    public void handleMessage(String topic, String payload, String gateway) {
        log.debug("set property reply --> topic: {}, payload: {}", topic, payload);
        JSONObject message = JsonUtils.parseObject(payload, JSONObject.class);
        if (message != null) {
            String bid = message.getStr("bid");
            String tid = message.getStr("tid");
            if (StrUtil.isNotBlank(bid) && StrUtil.isNotBlank(tid)) {
                JSONObject data = message.getJSONObject(BusinessConstant.DATA);
                if (data == null) {
                    data = new JSONObject();
                    data.set("result", 0);
                }
                CacheUtils.set(bid + "_" + tid, data);
            }
        } else {
            log.debug("set property reply --> payload解析失败，解析后为null");
        }
    }
}
