using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Text;

namespace Edu.Common.Message
{
    /// <summary>
    /// 消息发送
    /// </summary>
    public class MessageHelper
    {
        /// <summary>
        /// 获取连接
        /// </summary>
        /// <param name="rabbitConfig">连接配置实体</param>
        /// <returns></returns>
        private static ConnectionFactory GetConnectionFactory(RabbitConfig rabbitConfig)
        {
            ConnectionFactory factory = new ConnectionFactory
            {
                HostName = rabbitConfig.HostName,
                //默认端口
                Port = rabbitConfig.Port,
                UserName = rabbitConfig.UserName,
                Password = rabbitConfig.Password,
            };
            return factory;
        }

        /// <summary>
        /// 发送信息
        /// </summary>
        /// <param name="message"></param>
        public static void SendMessage(PushMessageModel message)
        {
            Int32.TryParse(Common.Config.ReadConfigKey("RabbitMqConfig", subKey: "Port"), out int Port);
            RabbitConfig rabbitConfig = new RabbitConfig()
            {
                HostName = Common.Config.ReadConfigKey("RabbitMqConfig", subKey: "HostName"),
                Password = Common.Config.ReadConfigKey("RabbitMqConfig", subKey: "Password"),
                Port = Port,
                UserName = Common.Config.ReadConfigKey("RabbitMqConfig", subKey: "UserName"),
                QueenName = Common.Config.ReadConfigKey("RabbitMqConfig", subKey: "QueenName"),
            };
            var obj = new
            {
                Id = 0,// 主键
                message.Title,
                message.Content,
                message.CoverImg,
                message.CategoryId,
                message.Platform,
                message.SendType,
                message.SendTime,
                message.ReceiveId,
                message.JumpUrl,
                message.CreateByName,
                message.MsgSign,
                message.MsgTemplateCode,
                Status = 0,
                CreateTime = DateTime.Now,
                AppId = Common.Config.ReadConfigKey("PushAppId"),
            };
            using (IConnection conn = GetConnectionFactory(rabbitConfig).CreateConnection())
            {
                using (IModel channel = conn.CreateModel())
                {
                    //在MQ上定义一个持久化队列，如果名称相同不会重复创建
                    channel.QueueDeclare(rabbitConfig.QueenName, true, false, false, null);
                    byte[] buffer = Encoding.UTF8.GetBytes(Common.Plugin.JsonHelper.Serialize(obj));
                    IBasicProperties properties = channel.CreateBasicProperties();
                    properties.DeliveryMode = 2;
                    channel.BasicPublish("", rabbitConfig.QueenName, properties, buffer);
                }
            }
        }

        /// <summary>
        /// 批量推送
        /// </summary>
        /// <param name="msgList"></param>
        public static void SendMessage(List<PushMessageModel> msgList)
        {
            foreach (var item in msgList)
            {
                SendMessage(item);
            }
        }
    }
}
