package com.foreverwin.mesnac.common.util;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Description TODO
 * @Author zhaojiawei
 * @Since 2021-07-21
 */
@Component
public class ActiveMQUtil {

    @Autowired(required = false)
    @Qualifier("mesJmsTemplate")
    private JmsMessagingTemplate mesJmsTemplate;

    private static Logger logger = LoggerFactory.getLogger(ActiveMQUtil.class);

    public void wechatSendMessage(String user, String text) {
            String queue = "send.weChat.notice";
            JSONObject jsonObject = new JSONObject();
            try{
                jsonObject.put("TO_USER", user);
                jsonObject.put("CONTENT",text);
                mesJmsTemplate.convertAndSend(queue, jsonObject.toString());
            }catch (Exception ignored){
                LocalDateTime now = LocalDateTime.now();
                logger.info("消息发送失败。日期:" + now + ",消息内容:"+text+",错误信息:"+ignored.getMessage());
            }
    }
}
