using System.Text;
using System.Text.Json;
using CommonEntity;
using Confluent.Kafka;
using MessagePack;
using MongoDB.Bson;
using MongoDB.Driver;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //MongoDBMMMM();
            WriteLog().Wait();
            //MongoDBAdd();
            //MongoDB();
            Console.ReadKey();
        }

        public static void MongoDBMMMM()
        {
            var connectionString = "mongodb://blv-rd-admin:A*%26(y*DG%26v(AS%26Di7gct@10.8.8.210:27017/";
            var client = new MongoClient(connectionString);

            var collection = client.GetDatabase("udppackage").GetCollection<StepInfo_db>("zeroe_stepmonitor");
            StepInfo_db us = new StepInfo_db();
            us.MessageId = "test";
            us.Step = 11.11;
            us.StepDescription = "测试使用";
            us.Content = new byte[] { };
            us.TriggerTime = DateTime.Now;
            us.EveryMessageId = 1;
            us.Monitor_0E_01 = 1;
            us.HostNumber = "8888";
            us.HotelCode = "8888";
            collection.InsertOne(us);
        }

        public static void MongoDBAdd()
        {

            var connectionString = "mongodb://localhost:27017/";
            if (connectionString == null)
            {
                Environment.Exit(0);
            }
            var client = new MongoClient(connectionString);
            var collection = client.GetDatabase("blwlog").GetCollection<LogBase>("udppackage");

            List<LogBase> list1 = new List<LogBase>();
            Random r111 = new Random();

            for (int i = 0; i < 100000; i++)
            {
                LogBase r = new LogBase();
                DataContext ddd = new DataContext();
                var iiu = r111.Next(1, 300);

                ddd.Age = iiu;
                r.Context = ddd;
                r.CreateDateTime = DateTime.Now;
                r.HotelCode = "1001";
                list1.Add(r);
                Task.Delay(10);
            }
            collection.InsertMany(list1);
        }

        public static void MongoDB()
        {

            var connectionString = "mongodb://localhost:27017/";
            if (connectionString == null)
            {
                Environment.Exit(0);
            }
            var client = new MongoClient(connectionString);
            var collection = client.GetDatabase("blwlog").GetCollection<BsonDocument>("udppackage");

            //构建查询条件
            var filter = Builders<BsonDocument>.Filter.Gt("Context.Age", 20);



            // //执行查询
            var results = collection.Find(filter).ToList();

            // //输出结果
            foreach (var doc in results)
            {
                Console.WriteLine(doc.ToJson());
            }
        }
        public class UUU
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }




        public static async Task WriteLog()
        {

            var config = new ProducerConfig
            {
                //BootstrapServers = "43.138.217.154:9092",
                BootstrapServers = "172.16.4.132:9092",
                SecurityProtocol = SecurityProtocol.SaslPlaintext,
                SaslMechanism = SaslMechanism.Plain,
                SaslUsername = "blwmomo",
                SaslPassword = "blwmomo"
            };
            using (var p = new ProducerBuilder<string, string>(config).Build())
            {
                try
                {
                    //UDPPackage u = new UDPPackage();
                    //u.CommandType = "aaa";
                    //u.FenLei = new Dictionary<string, long>();
                    //u.FenLei.Add("111", 222);
                    //u.RemoveTime = DateTime.Now.ToString();
                    //byte[] bbb = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(u));
                    //byte[] bytes = MessagePackSerializer.Serialize(u);

                    var topic = "blwlog-rcu-udppackage-topic";
                    //for (int i = 0; i < 3; i++)
                    //{
                    //    var dr = await p.ProduceAsync(topic, new Message<string, string> { Key = "testtest", Value = "hello" });
                    //    Console.WriteLine($"Delivered '{dr.Value}' to '{dr.TopicPartitionOffset}'");
                    //}
                    for (int i = 0; i < 30; i++)
                    {
                        var partition = new Partition(0); // 指定分区号
                        var topicPartition = new TopicPartition(topic, partition);
                        var dr = await p.ProduceAsync(topicPartition, new Message<string, string> { Key = "testtest", Value = "hello0"});
                        Console.WriteLine($"Delivered '{dr.Value}' to '{dr.TopicPartitionOffset}'");
                    }
                    for (int i = 0; i < 550; i++)
                    {
                        var partition = new Partition(1); // 指定分区号
                        var topicPartition = new TopicPartition(topic, partition);
                        var dr = await p.ProduceAsync(topicPartition, new Message<string, string> { Key = "testtest", Value = "hello1" });
                        Console.WriteLine($"Delivered '{dr.Value}' to '{dr.TopicPartitionOffset}'");
                    }
                    for (int i = 0; i < 770; i++)
                    {
                        var partition = new Partition(2); // 指定分区号
                        var topicPartition = new TopicPartition(topic, partition);
                        var dr = await p.ProduceAsync(topicPartition, new Message<string, string> { Key = "testtest", Value = "hello2"});
                        Console.WriteLine($"Delivered '{dr.Value}' to '{dr.TopicPartitionOffset}'");
                    }
                }
                catch (ProduceException<string, string> e)
                {
                    Console.WriteLine($"Delivery failed: {e.Error.Reason}");
                }
            }
            await Task.CompletedTask;
        }
    }
}
