package com.zhongshu.card.server.core.service.pay;

import com.github.microservice.core.util.JsonUtil;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.mqtt.SendMessageModel;
import com.zhongshu.card.client.type.payAccount.RechargeOrderStatus;
import com.zhongshu.card.server.core.service.mqtt.MqttServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class PayMqttService {

    @Autowired
    MqttServiceImpl mqttService;

    public ResultContent send(String userId, String orderNo) {
        if (StringUtils.isBlank(userId)){
            return ResultContent.buildFail("userId不能为空");
        }
        SendMessageModel sendMessageModel = new SendMessageModel();
        sendMessageModel.setMessage(JsonUtil.toJson(Map.of("status", RechargeOrderStatus.Success.name(), "orderNo", orderNo)));
        String topic = String.format("/wechat/pay/%s", userId);
        sendMessageModel.setTopic(topic);
        sendMessageModel.setUserId(userId);
        log.info("mq topic : {}", topic);
        return mqttService.sendMessage(sendMessageModel);
    }
}
