private RabbitMQDeclareSettings GetSettings(RabbitMQDeclareSettings settings)
        {
            settings ??= new RabbitMQDeclareSettings();

            settings.Exchange ??= new ExchangeDeclareSettings();

            settings.Queue ??= new QueueDeclareSettings();

            var exchange = settings.Exchange;

            if (exchange.Name.IsNull())
            {
                switch (exchange.Type)
                {
                case ExchangeType.Fanout:
                    exchange.Name = DefaultExchange.Fanout;
                    break;

                case ExchangeType.Topic:
                    exchange.Name = DefaultExchange.Topic;
                    break;

                case ExchangeType.Headers:
                    exchange.Name = DefaultExchange.Headers;
                    break;

                default:
                    exchange.Name = DefaultExchange.Direct;
                    break;
                }
            }

            return(settings);
        }
        /// <summary>
        /// 发送单条消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queue">队列名</param>
        /// <param name="routingKey">路由建</param>
        /// <param name="message">消息体</param>
        /// <param name="settings">配置</param>
        public void Send <T>(string queue, T message, string routingKey = "", RabbitMQDeclareSettings settings = null)
        {
            Check.NotNull(queue, nameof(queue), "queue is null");

            queue = GetQueueName(queue);

            if (routingKey.IsNull())
            {
                routingKey = queue;
            }

            using var channel = _sendConnection.CreateModel();

            settings = GetSettings(settings);
            var exchange = settings.Exchange;

            channel.ExchangeDeclare(exchange.Name, exchange.Type, exchange.Durable, exchange.AutoDelete, exchange.Arguments);
            channel.QueueDeclare(queue, settings.Queue.Durable, settings.Queue.Exclusive, settings.Queue.AutoDelete, settings.Queue.Arguments);
            channel.QueueBind(queue, exchange.Name, routingKey);

            var body       = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
            var properties = channel.CreateBasicProperties();

            properties.Persistent = true;

            channel.BasicPublish(exchange.Name, routingKey, properties, body);
        }
        /// <summary>
        /// 使用事件接收消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queue">队列名称</param>
        /// <param name="func">回调函数</param>
        /// <param name="settings">定义设置</param>
        public Consumer Receive <T>(string queue, Func <T, bool> func, RabbitMQDeclareSettings settings = null)
        {
            Check.NotNull(queue, nameof(queue), "queue is null");
            Check.NotNull(func, nameof(func), "func is null");

            queue = GetQueueName(queue);

            var channel = _receiveConnection.CreateModel();

            channel.BasicQos(0, 1, false);
            settings = GetSettings(settings);
            channel.QueueDeclare(queue, settings.Queue.Durable, settings.Queue.Exclusive, settings.Queue.AutoDelete, settings.Queue.Arguments);

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (sender, eventArgs) =>
            {
                var message = JsonConvert.DeserializeObject <T>(Encoding.UTF8.GetString(eventArgs.Body.ToArray()));
                if (func(message))
                {
                    channel.BasicAck(eventArgs.DeliveryTag, false);
                }
                else
                {
                    channel.BasicNack(eventArgs.DeliveryTag, false, true);
                }
            };

            var tag = channel.BasicConsume(queue, false, consumer);

            return(new Consumer
            {
                Channel = channel,
                Tag = tag
            });
        }