package com.dxhy.core.rabbitmq;

import javax.annotation.Resource;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * @author user
 */
@Component
@Slf4j

public class Sender implements RabbitTemplate.ConfirmCallback {
    @Resource
    private AmqpTemplate rabbitTemplate;
    @Value("${TO_EXCEL_APPLY:TO_EXCEL_APPLY}")
    private String toExcelApply;
    @Value("${TO_MSG_EXCEL_APPLY:TO_MSG_EXCEL_APPLY}")
    private String toMsgExcelApply;
    @Value("${TO_INVOICE_STATUS:TO_INVOICE_STATUS}")
    private String toInvoiceStatus;

    /**
     * 导出excel
     */
    public boolean sendToExcel(String msg) {
        // 执行保存
        log.debug("申请导出  -   " + msg);
        try {
            rabbitTemplate.convertAndSend("exchange", toExcelApply, msg);
            log.debug("申请导出 消息发送成功");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean sendToMsg(String msg) {
        log.debug("导出成功发送消息  --    " + msg);
        try {
            rabbitTemplate.convertAndSend("exchange", toMsgExcelApply, msg);
            log.debug("导出成功发送消息成功" + msg);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean sendToStatus(String msg) {
        log.debug("推送发票状态发送消息  --    " + msg);
        try {
            rabbitTemplate.convertAndSend("exchange", toInvoiceStatus, msg);
            log.debug("推送发票状态发送消息成功" + msg);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void confirm(CorrelationData correlationData, boolean b, String s) {
        if (b) {
            log.debug(correlationData + "发送成功！");
        } else {
            log.info(correlationData + "消息发送确认失败:" + s);
        }
    }
}
