package com.jkcredit.invoice.hub.puducer;

import com.jkcredit.invoice.hub.constant.CommonConstant;
import com.jkcredit.invoice.hub.model.dto.userBalance.UserBalanceDto;
import com.jkcredit.invoice.hub.model.dto.userBalance.UserBalanceInvoiceDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: xusonglin
 * @create: 2019-06-04 13:47
 * @version: V1.0
 **/
@Slf4j
@Component
public class ChargeProducer implements RabbitTemplate.ReturnCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send(UserBalanceInvoiceDto userBalanceInvoiceDto) {
        this.rabbitTemplate.setReturnCallback(this);
        this.rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (!ack) {
                log.info("消息发送失败:{},{}", cause, correlationData.toString());
            }
        });
        this.rabbitTemplate.convertAndSend(CommonConstant.CHARGE_EXCHANGE, "charge.*", userBalanceInvoiceDto);
    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
//        System.out.println("sender return success" + message.toString() + "===" + replyText + "===" + exchange + "===" + routingKey);
    }
}
