package cn.estsh.i3plus.core.apiservice.mq;
import cn.estsh.i3plus.icloud.wms.sdk.IWmsDemoCloud;
import cn.estsh.i3plus.pojo.base.bean.BaseResultBean;
import cn.estsh.i3plus.pojo.platform.bean.SysMessage;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 * @Description : 获取队列信息后处理
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2018-09-26 10:40
 * @Modify:
 **/
@Component
public class I3CoreQueueReceiver {
    private static final Logger LOGGER = LoggerFactory.getLogger(I3CoreQueueReceiver.class);

    @Autowired
    IWmsDemoCloud iWmsDemoCloud;

    /**
     * 系统邮件处理队列
     * @param msg
     * @param channel
     * @param message
     * 发送：rabbitTemplate.convertAndSend(I3CoreQueueConfig.IMPP_MESSAGE_QUEUE, new SysMessage(....));
     */
    @RabbitListener(queues = I3CoreQueueConfig.IMPP_MESSAGE_QUEUE)
    public void processImppMessage(SysMessage msg, Channel channel, Message message) {
        try {
            LOGGER.info("【MQ-IMPP_MESSAGE_QUEUE】数据接收成功：{}",msg);

            //信息已处理
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            LOGGER.error("【MQ-IMPP_MESSAGE_QUEUE】处理出错：{}",e.getMessage(),e);
            //丢弃这条消息
            try {
                // 未成功处理，重新发送
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    /**
     * 微服处理队列
     * @param msg
     * @param channel
     * @param message
     * 发送：rabbitTemplate.convertAndSend(I3CoreQueueConfig.IMPP_MESSAGE_QUEUE, new SysMessage(....));
     */
    @RabbitListener(queues = I3CoreQueueConfig.DEMO_CLOUD_WMS)
    public void processCloud(String msg, Channel channel, Message message) {
        try {
            LOGGER.info("【MQ-DEMO_CLOUD_WMS】数据接收成功：{}",msg);
            BaseResultBean result = iWmsDemoCloud.testWms(msg);
            LOGGER.info("微服返回结果：{}",result);

            //信息已处理
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            LOGGER.error("【MQ-DEMO_CLOUD_WMS】处理出错：{}",e.getMessage(),e);
            //丢弃这条消息
            try {
                // 未成功处理，重新发送
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }


    /*********************  消息队列处理demo *******************/

    /*@RabbitListener(queues = I3CoreQueueConfig.DEMO_STR_QUEUE)
    public void getObjQueue(String data) {
        System.out.println("接收数据  : " + data.toString());
    }

    @RabbitListener(queues = I3CoreQueueConfig.DEMO_OBJ_QUEUE)
    public void getStrQueue(MachineFactory data) {
        System.out.println("强转对象：" + data);
    }*/

    /*@RabbitListener(queues = I3CoreQueueConfig.DEMO_RETURN_QUEUE)
    public Message processReturn(String data, Channel channel, Message message) {
        try {
            System.out.println("【client】数据接收成功：" + data);
            Thread.sleep(1000);

            //告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发
            //消息的标识，false只确认当前一个消息收到，true确认所有consumer获得的消息

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            System.out.println("出现异常：" + e.getMessage());
            // 错误反馈
            // val1 = 是否批量.true:将一次性拒绝所有小于deliveryTag的消息。
            // val2 = 被拒绝的是否重新列入队列
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,true);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            //拒绝，同basicNack，只是没有批量这个属性
            //channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
        Message returnMsg = null;
        try {
            returnMsg = new Message("---返回数据---".getBytes("UTF-8"), message.getMessageProperties());
        }catch(Exception e){
            e.printStackTrace();
        }
        return message;
    }

    @RabbitListener(queues = I3CoreQueueConfig.DEMO_ACK_QUEUE)
    public void processAck(String data, Channel channel, Message message) {
        try {
            System.out.println("【client】数据接收成功：" + data);

            *//*if(data.startsWith("ack")){
                System.out.println("【client】数据抛出异常");
                throw new RuntimeException("【队列抛出异常】" + data);
            }*//*
            //在队列删掉 不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
            //丢弃这条消息
            // channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
            // System.out.println("receiver fail");
            try {
                //
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }*/
}
