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.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.UUID;

/**
 * @Auther: Stormeye
 * @Date: 2019-01-31
 * @Description: RocketMQ消息生产者
 */
@Service
@Profile(MQConfig.Impl.ROCKET_MQ)
public class RocketMqMessageProducer implements MqBaseProducer {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Value("${spring.rocketmq.nameServer}")
    private String namesrvAddr;

    private static final DefaultMQProducer producer = new DefaultMQProducer("DefaultProducer");

    @Autowired
    private WxMsgRecordMapper wxMsgRecordMapper;

    @Autowired
    private WxMsgRecordService wxMsgRecordService;

    @PostConstruct
    public void start(){
        try {
            producer.setNamesrvAddr(namesrvAddr);
            producer.start();
            log.info("Message Producer Start...");
        }catch (Exception e){
            log.error("Message Producer Start Error!!",e);
        }
    }

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

            byte[] messageBody = JsonUtil.obj2Json(data).getBytes(RemotingHelper.DEFAULT_CHARSET);

            Message mqMsg = new Message(topic, tags, keys, messageBody);
            if(data.getDelayTimeLevel() > 0){
                mqMsg.setDelayTimeLevel(data.getDelayTimeLevel());
            }

            producer.send(mqMsg, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    log.info("Message Producer: Send Message Success {}", sendResult);
                    data.setMsgStatus(EnumMsgRecordStatus.SEND_SUCC.getCode());
                    wxMsgRecordMapper.updateByBaseMsg(data);
                }

                @Override
                public void onException(Throwable throwable) {
                    log.error("Message Producer: Send Message Error ", throwable);
                }
            });
        } catch (Exception e) {
            log.error("Message Producer: Send Message Error ", e);
        }

    }

}
