package com.iformall.mq.impl;

import com.iformall.domain.po.msg.BaseMsg;
import com.iformall.enums.EnumMsgRecordStatus;
import com.iformall.mapper.WxMsgRecordMapper;
import com.iformall.mq.MQConfig;
import com.iformall.mq.MqBaseProducer;
import com.iformall.service.WxMsgRecordService;
import com.iformall.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Auther: Stormeye
 * @Date: 2019-01-31
 * @Description: RocketMQ消息生产者
 */
@Service
@Profile(MQConfig.Impl.RABBIT_MQ)
public class RabbitMqMessageProducer implements MqBaseProducer {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private AmqpTemplate rabbitTemplate;
    @Autowired
    private WxMsgRecordMapper wxMsgRecordMapper;
    @Autowired
    private WxMsgRecordService wxMsgRecordService;


    @Override
    public void sendMessage(BaseMsg data, String topic, String tags, String keys) {
        try {
            data.setUuid(UUID.randomUUID().toString());
            wxMsgRecordService.save(data);

            rabbitTemplate.convertAndSend(topic, JsonUtil.obj2Json(data));

            data.setMsgStatus(EnumMsgRecordStatus.SEND_SUCC.getCode());
            wxMsgRecordMapper.update(data);
        } catch (Exception e) {
            log.error("RabbitMqMessageProducer: Send Message Error ", e);
        }
    }

}
