Browse Source

添加码牌交易队列

lcl 1 year ago
parent
commit
2d1a12ed60

+ 235 - 0
AppStart/MpRedisDbconn.cs

@@ -0,0 +1,235 @@
+using System.Collections.Generic;
+using Library;
+using System.Linq;
+
+namespace MySystem
+{
+    public class MpRedisDbconn
+    {
+        public readonly static MpRedisDbconn Instance = new MpRedisDbconn();
+        public static CSRedis.CSRedisClient csredis;
+        private MpRedisDbconn()
+        {
+        }
+
+        #region 设置单个字段
+        public bool Set(string key, object value)
+        {
+            return csredis.Set(key, value);
+            // return false;
+        }
+        #endregion
+
+        #region 整数累加
+        public long AddInt(string key, long value = 1)
+        {
+            return csredis.IncrBy(key, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 数字累加
+        public decimal AddNumber(string key, decimal value = 1)
+        {
+            return csredis.IncrByFloat(key, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 获取单个字段
+        public T Get<T>(string key)
+        {
+            return csredis.Get<T>(key);
+        }
+        #endregion
+
+        #region 设置散列字段
+        public bool HSet(string key, string field, object value)
+        {
+            return csredis.HSet(key, field, value);
+            // return false;
+        }
+        #endregion
+
+        #region 散列整数累加
+        public long HAddInt(string key, string field, long value = 1)
+        {
+            return csredis.HIncrBy(key, field, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 散列数字累加
+        public decimal HAddNumber(string key, string field, decimal value = 1)
+        {
+            return csredis.HIncrByFloat(key, field, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 获取散列元素
+        public T HGet<T>(string key, string field)
+        {
+            return csredis.HGet<T>(key, field);
+        }
+        #endregion
+
+        #region 获取散列所有元素
+        public Dictionary<string, T> HGetAll<T>(string key)
+        {
+            return csredis.HGetAll<T>(key);
+        }
+        #endregion
+
+        #region 添加列表对象
+        public long AddList(string key, object value)
+        {
+            return csredis.LPush(key, value);
+            // return 0;
+        }
+        public long AddRightList(string key, object value)
+        {
+            return csredis.RPush(key, value);
+            // return 0;
+        }
+        public long AddList(string key, object[] value)
+        {
+            return csredis.LPush(key, value);
+            // return 0;
+        }
+        public T RPop<T>(string key)
+        {
+            return csredis.RPop<T>(key);
+        }
+        #endregion
+
+        #region 添加集合对象
+        public long SAdd(string key, object value)
+        {
+            return csredis.SAdd(key, value);
+            // return 0;
+        }
+        public long SAdd(string key, object[] value)
+        {
+            return csredis.SAdd(key, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 获取集合对象
+        public T[] SGetList<T>(string key)
+        {
+            return csredis.SMembers<T>(key);
+        }
+        #endregion
+
+        #region 修改列表对象
+        public bool SetList(string key, int index, object value)
+        {
+            long itemindex = csredis.LLen(key) - index - 1;
+            return csredis.LSet(key, itemindex, value);
+            // return false;
+        }
+        #endregion
+
+        #region 获取列表
+        public List<T> GetList<T>(string key, int pageNum = 1, int pageSize = 10)
+        {
+            int start = (pageNum - 1) * pageSize;
+            int end = start + pageSize - 1;
+            T[] list = csredis.LRange<T>(key, start, end);
+            return list.ToList();
+        }
+        #endregion
+
+        #region 删除列表元素
+        public void DelList<T>(string key, T item)
+        {
+            List<T> oldlist = GetList<T>(key, 1, 10000000);
+            oldlist.Remove(item);
+            Clear(key);
+            AddList(key, oldlist.ToArray());
+        }
+        #endregion
+
+        #region 添加排序列表对象
+        public long AddSort(string key, object value, decimal score)
+        {
+            return csredis.ZAdd(key, (score, value));
+            // return 0;
+        }
+        #endregion
+
+        #region 获取排序列表
+        public List<T> GetSort<T>(string key, int pageNum = 1, int pageSize = 10)
+        {
+            int start = (pageNum - 1) * pageSize;
+            int end = start + pageSize;
+            string[] list = csredis.ZRangeByScore(key, start, end);
+            List<T> lists = new List<T>();
+            foreach (string record in list)
+            { 
+                lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
+            }
+            return lists;
+        }
+        public List<T> GetSortDesc<T>(string key, int pageNum = 1, int pageSize = 10)
+        {
+            int start = (pageNum - 1) * pageSize;
+            int end = start + pageSize;
+            string[] list = csredis.ZRevRangeByScore(key, start, end);
+            List<T> lists = new List<T>();
+            foreach (string record in list)
+            { 
+                lists.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<T>(record));
+            }
+            return lists;
+        }
+        #endregion
+
+        public bool Remove(string key, long start, long end)
+        {
+            return csredis.LTrim(key, start, end);
+        }
+
+        public bool RemoveTop(string key, long count)
+        {
+            return RedisDbconn.Instance.Remove(key, count, RedisDbconn.Instance.Count(key) - 1);;
+        }
+
+        public long Count(string key)
+        {
+            return csredis.LLen(key);
+        }
+
+        public void Clear(string pattern)
+        {
+            string[] keys = csredis.Keys(pattern);
+            csredis.Del(keys);
+        }
+
+        public string[] GetKeys(string pattern)
+        { 
+            string[] keys = csredis.Keys(pattern);
+            return keys;
+        }
+
+        public void SetExpire(string key, int expire)
+        { 
+            csredis.Expire(key, expire); //秒为单位
+        }
+
+        #region 判断Key是否存在
+
+        public bool CheckKey(string key)
+        {
+            if (csredis.Exists(key))
+            {
+                return true;
+            }
+            return false;
+        }
+
+        #endregion
+    }
+}

+ 25 - 1
AppStart/RabbitMQClient.cs

@@ -21,7 +21,7 @@ namespace MySystem
         }
 
         #region 单对单发送
-        public void SendMsg(string content, string QueueName = "")
+        public void SendMsg(string content, string QueueName)
         {
             // 创建连接对象工厂
             var factory = new ConnectionFactory()
@@ -45,6 +45,30 @@ namespace MySystem
             channel.Dispose();
             conn.Dispose();
         }
+        public void SendMsg(string QueueName)
+        {
+            // 创建连接对象工厂
+            var factory = new ConnectionFactory()
+            {
+                UserName = UserName,
+                Password = Password,
+                AutomaticRecoveryEnabled = true,  //如果connection挂掉是否重新连接
+                TopologyRecoveryEnabled = true  //连接恢复后,连接的交换机,队列等是否一同恢复
+            };
+            List<AmqpTcpEndpoint> p = new List<AmqpTcpEndpoint>();
+            string[] HostNames = HostName.Split(',');
+            foreach (string subHostName in HostNames)
+            {
+                string[] subHostNameData = subHostName.Split(':');
+                p.Add(new AmqpTcpEndpoint(subHostNameData[0], int.Parse(subHostNameData[1])));
+            }
+            var conn = factory.CreateConnection(p);
+            var channel = conn.CreateModel();
+            channel.QueueDeclare(QueueName, true, false, false);
+            MpOrderService.Instance.dosomething(channel, QueueName);
+            channel.Dispose();
+            conn.Dispose();
+        }
         #endregion
 
         #region 单对单发送

+ 52 - 0
AppStart/Service/MpOrderService.cs

@@ -0,0 +1,52 @@
+using System;
+using System.Collections.Generic;
+using Library;
+using System.Linq;
+using System.Threading;
+using RabbitMQ.Client;
+using System.Text;
+
+namespace MySystem
+{
+    public class MpOrderService
+    {
+        public readonly static MpOrderService Instance = new MpOrderService();
+        private MpOrderService()
+        { }
+
+        public void Start()
+        {
+            Thread th = new Thread(SendMsg);
+            th.IsBackground = true;
+            th.Start();
+        }
+
+        public void SendMsg()
+        {
+            RabbitMQClient.Instance.SendMsg("QUEUE_LKB_TRADE_DIVISION");
+        }
+
+        public void dosomething(IModel channel, string QueueName)
+        {
+            while (true)
+            {
+                string content = MpRedisDbconn.Instance.RPop<string>("MpOrderQueue");
+                if (!string.IsNullOrEmpty(content))
+                {
+                    try
+                    {
+                        channel.BasicPublish("", QueueName, null, Encoding.Default.GetBytes(content));
+                    }
+                    catch (Exception ex)
+                    {                        
+                        function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "设置激活奖励异常");
+                    }
+                }
+                else
+                {
+                    Thread.Sleep(500);
+                }
+            }
+        }
+    }
+}

+ 2 - 0
Startup.cs

@@ -96,6 +96,7 @@ namespace MySystem
             MySystemLib.SystemPublicFuction.checkurl = checkurl;
             MySystemLib.SystemPublicFuction.appcheck = "success";
             RedisDbconn.csredis = new CSRedis.CSRedisClient(Configuration["Setting:RedisConnStr"]);
+            MpRedisDbconn.csredis = new CSRedis.CSRedisClient(Configuration["Setting:MpRedisConnStr"]);
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -134,6 +135,7 @@ namespace MySystem
 
             //必须打开的
             RabbitMQClient.Instance.StartReceive("QUEUE_GD_ACT_DIVISION");
+            MpOrderService.Instance.Start();
         }
     }
 }

+ 1 - 0
appsettings.Development.json

@@ -27,6 +27,7 @@
     "MpSqlConnStr2": "server=47.109.31.237;port=3306;user=QrCodePlateMainServer;password=ll4DFaALMu9YIooM;database=QrCodePlateMainServer2;charset=utf8;",
     "RdsStatSqlConnStr": "server=rm-2vcu953x4yx7z454who.rwlb.cn-chengdu.rds.aliyuncs.com;user=KxsProfitServer;password=FrW8ZfxlcaVdm1r0;port=3306;database=KxsStatServer;charset=utf8;",
     "RedisConnStr": "47.108.62.166:6379,password=klm@redis,DefaultDatabase=1,poolsize=500,preheat=50,asyncPipeline=true",
+    "MpRedisConnStr": "8.137.99.48:6379,password=kxsmp@2023,DefaultDatabase=0,poolsize=500,preheat=50,asyncPipeline=true",
     "IOSAppVersion": "1.0.0",
     "AndroidAppVersion": "1.0.0",
     "OSSKey": "iL9dWgBunZRwGbHQ",