package com.dxhy.common.rabbitmq;

import javax.annotation.Resource;

import com.dxhy.common.constant.RabbitMqConstant;
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 jiaohongyang
 */
@Component
@Slf4j
public class RabbitMqSender implements RabbitTemplate.ConfirmCallback {
    @Resource
    private RabbitTemplate rabbitTemplate;
    private String tomessagelog = RabbitMqConstant.TO_MESSAGE_LOG;
    private String toinvoicelog = RabbitMqConstant.TO_INVOICE_LOG;
    private String tocustomslog = RabbitMqConstant.TO_CUSTOMS_LOG;
    private String toCustomsOcr = RabbitMqConstant.TO_CUSTOMS_OCR;

    private String toExcelApply = RabbitMqConstant.TO_EXCEL_APPLY;
    private String toMsgExcelApply = RabbitMqConstant.TO_MSG_EXCEL_APPLY;
    private String toInvoiceStatus = RabbitMqConstant.TO_INVOICE_STATUS;

    //乐企初始化数据下载队列
    private String toLqcshsjxz = RabbitMqConstant.LQ_CSHSJ_XZ;


    /**
     * 日志存储推送 mq
     */
    public boolean sendSaveLogs(String logs) {
        log.debug(" 日志推送开始  --   " + logs + " 队列名：" + tomessagelog);
        try {
            this.rabbitTemplate.convertAndSend("exchange", tomessagelog, logs);
            log.debug(" 日志推送结束 ");
            return true;
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return false;
        }
    }

    /**
     * 发票操作日志推送 mq
     */
    public boolean sendInvoiceLogs(String logs) {
        log.debug(" 发票操作日志推送开始  --   " + logs + " 队列名：" + toinvoicelog);
        try {
            this.rabbitTemplate.convertAndSend("exchange", toinvoicelog, logs);
            log.debug(" 发票操作日志推送结束 ");
            return true;
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return false;
        }
    }

    /**
     * 缴款书操作日志推送 mq
     */
    public boolean sendCustomsLogs(String logs) {
        log.debug(" 缴款书操作日志推送开始  --   " + logs + " 队列名：" + tocustomslog);
        try {
            this.rabbitTemplate.convertAndSend("exchange", tocustomslog, logs);
            log.debug(" 缴款书操作日志推送结束 ");
            return true;
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return false;
        }
    }

    /**
     * 缴款书ocr识别
     *
     * @param sendInfo
     *            图片信息
     * @return 返回结果
     */
    public boolean toCustomsOcr(String sendInfo) {
        log.debug(" 缴款书ocr识别推送开始  --   " + sendInfo + " 队列名：" + toCustomsOcr);
        try {
            this.rabbitTemplate.convertAndSend("exchange", toCustomsOcr, sendInfo);
            log.debug(" 缴款书ocr识别推送结束 ");
            return true;
        } catch (Exception e) {
            log.error("缴款书ocr识别队列消息发送异常:{}",e);
            return false;
        }
    }

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

    }

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

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


    public boolean lqcshsjxzSend(String msg) {
        log.debug("乐企初始化数据下载队列发送消息  --    " + msg);
        try {
            rabbitTemplate.convertAndSend("exchange", toLqcshsjxz, msg);
            log.debug("乐企初始化数据下载队列发送消息成功" + msg);
            return true;
        } catch (Exception e) {
            log.error("乐企初始化数据下载队列消息发送异常:{}",e);
            return false;
        }
    }

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