package com.miyzh.rabbitMQ;

import com.miyzh.entity.UploadExceptionLog;
import com.miyzh.service.UploadExceptionLogService;
import com.miyzh.service.UploadService;
import com.miyzh.utils.DesUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Slf4j
@Component
public class MsgReceiver {

    @Autowired
    private UploadService uploadService;
    @Autowired
    private UploadExceptionLogService uploadExceptionLogService;


//    @RabbitListener(queues = "preSettleMq")
    public void preSettleMq(String content, Message message, Channel channel) throws IOException {
        try {
//            int a = 1 / 0;
            if (null != content && content.length() > 0) {
                Document doc = DocumentHelper.parseText(DesUtil.decrypt(new String(message.getBody(), "utf-8")));
//                String str = new String(message.getBody(), "utf-8");
                log.info("消费消息：" + doc);
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
                UploadExceptionLog uploadExceptionLog = new UploadExceptionLog();
                uploadExceptionLog.setUploadInfo(DesUtil.decrypt(content));
                new Thread(() -> {
                    uploadService.upload(uploadExceptionLog);
                }).start();
                log.info("消费消息确认" + message.getMessageProperties().getConsumerQueue() + "，接收到了回调方法");
                try{
                    uploadExceptionLog.setTradeCode(doc.getRootElement().element("TradeCode").getTextTrim());
                    uploadExceptionLog.setClinicNo(doc.getRootElement().element("YLJGDM").getTextTrim());
                    uploadExceptionLog.setSerialNumber(doc.getRootElement().element("SERIAL_NUMBER").getTextTrim());
                } catch (Exception e) {
                    uploadExceptionLog.setTradeCode("");
                    uploadExceptionLog.setClinicNo("");
                    uploadExceptionLog.setSerialNumber("");
                }
                uploadExceptionLogService.saveUploadPatBillInfoReq(uploadExceptionLog);
            }
        } catch (Exception e) {
            //重新回到队列
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            log.error("尝试重发：" + message.getMessageProperties().getConsumerQueue());
            //requeue =true 重回队列，false 丢弃
//            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            // TODO 该消息已经导致异常，重发无意义，自己实现补偿机制


        }
    }

//    @RabbitListener(queues = "confirmSettleMq")
    public void confirmSettleMq(String content, Message message, Channel channel) throws IOException {
        try {
            if (null != content && content.length() > 0) {
                Document doc = DocumentHelper.parseText(DesUtil.decrypt(content));
                log.info("消费消息：" + doc);
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
                UploadExceptionLog uploadExceptionLog = new UploadExceptionLog();
                uploadExceptionLog.setUploadInfo(DesUtil.decrypt(content));
//                uploadService.upload(uploadExceptionLog);
//                log.info("消费消息确认" + message.getMessageProperties().getConsumerQueue() + "，接收到了回调方法");
                try{
                    uploadExceptionLog.setTradeCode(doc.getRootElement().element("TradeCode").getTextTrim());
                    uploadExceptionLog.setClinicNo(doc.getRootElement().element("YLJGDM").getTextTrim());
                    uploadExceptionLog.setSerialNumber(doc.getRootElement().element("SERIAL_NUMBER").getTextTrim());
                    uploadExceptionLog.setStatus(Integer.parseInt(doc.getRootElement().element("STATUS").getTextTrim()));
                } catch (Exception e) {
                    uploadExceptionLog.setTradeCode("");
                    uploadExceptionLog.setClinicNo("");
                    uploadExceptionLog.setSerialNumber("");
                    uploadExceptionLog.setStatus(3);
                }
                uploadExceptionLogService.saveUploadDocStatusReq(uploadExceptionLog);
            }
        } catch (Exception e) {
            //重新回到队列
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            log.info("尝试重发：" + message.getMessageProperties().getConsumerQueue());
            //requeue =true 重回队列，false 丢弃
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
            // TODO 该消息已经导致异常，重发无意义，自己实现补偿机制


        }
    }

//    @RabbitListener(queues = "test")
    public void testMq(String content, Message message, Channel channel) throws IOException {
        try {
            if (null != content && content.length() > 0) {
                System.out.println(content);
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            }
        } catch (Exception e) {
            //重新回到队列
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            log.info("尝试重发：" + message.getMessageProperties().getConsumerQueue());
            //requeue =true 重回队列，false 丢弃
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
            // TODO 该消息已经导致异常，重发无意义，自己实现补偿机制


        }
    }


}
