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;

/**
 * services消息回复
 *
 * @author Sdy
 * @since 1.0.0 2026/4/24
 */
@Slf4j
@Component
public class ServicesReplyHandler implements MessageHandler {

    @Override
    public void handleMessage(String topic, String payload, String gateway) {
        log.debug("services 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("services reply --> payload解析失败，解析后为null");
        }
    }
}
