Browse Source

推送交易给java

lcl 2 months ago
parent
commit
9be9bd5d0e
4 changed files with 394 additions and 0 deletions
  1. 1 0
      Startup.cs
  2. 3 0
      Util/HaoDa/WeChatPayBackService.cs
  3. 308 0
      Util/KxsRedisDbconn.cs
  4. 82 0
      Util/PosPushDataNewHelper.cs

+ 1 - 0
Startup.cs

@@ -90,6 +90,7 @@ namespace MySystem
 
             MySystemLib.SystemPublicFuction.appcheck = "success";
             RedisDbconn.csredis = new CSRedis.CSRedisClient(Configuration["Setting:RedisConnStr"]);
+            KxsRedisDbconn.csredis = new CSRedis.CSRedisClient(Configuration["Setting:KxsRedisConnStr"]);
         }
 
         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

+ 3 - 0
Util/HaoDa/WeChatPayBackService.cs

@@ -147,6 +147,9 @@ namespace MySystem
                     //给商户单独推送订单消息
                     string orderData = "{\"order_no\":\"" + order.OrderNo + "\",\"order_status\":\"" + order.Status + "\",\"order_time\":\"" + order.UpdateDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + "\"}";
                     RedisDbconn.Instance.AddList("OrderPushQueue", orderData);
+
+                    //推送交易给java
+                    PosPushDataNewHelper.Trade(order, addinfo);
                 }
                 else
                 {

+ 308 - 0
Util/KxsRedisDbconn.cs

@@ -0,0 +1,308 @@
+using System.Collections.Generic;
+using System.Threading;
+using System.Threading.Tasks;
+using Library;
+
+namespace MySystem
+{
+    public class KxsRedisDbconn
+    {
+        public readonly static KxsRedisDbconn Instance = new KxsRedisDbconn();
+        public static CSRedis.CSRedisClient csredis;
+        private KxsRedisDbconn()
+        {
+        }
+
+        #region 设置单个字段
+        public bool Set(string key, object value)
+        {
+            return RedisHelper.Set(key, value);
+            // return false;
+        }
+        #endregion
+
+        #region 整数累加
+        public long AddInt(string key, long value = 1)
+        {
+            return RedisHelper.IncrBy(key, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 数字累加
+        public decimal AddNumber(string key, decimal value = 1)
+        {
+            return RedisHelper.IncrByFloat(key, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 获取单个字段
+        public T Get<T>(string key)
+        {
+            return RedisHelper.Get<T>(key);
+        }
+        #endregion
+
+        #region 设置散列字段
+        public bool HSet(string key, string field, object value)
+        {
+            return RedisHelper.HSet(key, field, value);
+            // return false;
+        }
+        #endregion
+
+        #region 散列整数累加
+        public long HAddInt(string key, string field, long value = 1)
+        {
+            return RedisHelper.HIncrBy(key, field, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 散列数字累加
+        public decimal HAddNumber(string key, string field, decimal value = 1)
+        {
+            return RedisHelper.HIncrByFloat(key, field, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 获取散列元素
+        public T HGet<T>(string key, string field)
+        {
+            return RedisHelper.HGet<T>(key, field);
+        }
+        #endregion
+
+        #region 获取散列所有元素
+        public Dictionary<string, T> HGetAll<T>(string key)
+        {
+            return RedisHelper.HGetAll<T>(key);
+        }
+        #endregion
+
+        #region 添加列表对象
+        public long AddList(string key, object value)
+        {
+            return RedisHelper.LPush(key, value);
+            // return 0;
+        }
+        public long AddList(string key, object[] value)
+        {
+            return RedisHelper.LPush(key, value);
+            // return 0;
+        }
+        public T RPop<T>(string key)
+        {
+            return RedisHelper.RPop<T>(key);
+        }
+        #endregion
+
+        #region 添加集合对象
+        public long SAdd(string key, object value)
+        {
+            return RedisHelper.SAdd(key, value);
+            // return 0;
+        }
+        public long SAdd(string key, object[] value)
+        {
+            return RedisHelper.SAdd(key, value);
+            // return 0;
+        }
+        #endregion
+
+        #region 获取集合对象
+        public T[] SGetList<T>(string key)
+        {
+            return RedisHelper.SMembers<T>(key);
+        }
+        #endregion
+
+        #region 修改列表对象
+        public bool SetList(string key, int index, object value)
+        {
+            long itemindex = RedisHelper.LLen(key) - index - 1;
+            return RedisHelper.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;
+            string[] list = RedisHelper.LRange(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
+
+        #region 移除列表
+        public long RemoveFromList(string key, object value, int count = 1)
+        {            
+            return RedisHelper.LRem(key, count, value);
+        }
+        #endregion
+
+        #region 添加排序列表对象
+        public long AddSort(string key, object value, decimal score)
+        {
+            return RedisHelper.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 = RedisHelper.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 = RedisHelper.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 RedisHelper.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 RedisHelper.LLen(key);
+        }
+
+        public void Clear(string pattern)
+        {
+            string[] keys = RedisHelper.Keys(pattern);
+            RedisHelper.Del(keys);
+        }
+
+        public string[] GetKeys(string pattern)
+        {
+            string[] keys = RedisHelper.Keys(pattern);
+            return keys;
+        }
+
+        public void SetExpire(string key, int expire)
+        {
+            RedisHelper.Expire(key, expire); //秒为单位
+        }
+
+        public bool Exists(string key)
+        {
+            return RedisHelper.Exists(key);
+        }
+
+        public long AddRightList(string key, object value)
+        {
+            return RedisHelper.RPush(key, value);
+            // return 0;
+        }
+        /// <summary>
+        /// 锁key
+        /// </summary>
+        private readonly string lockKey = "RedisLock";
+
+        /// <summary>
+        /// 锁的过期秒数
+        /// </summary>
+        private readonly int lockTime = 20;
+
+        /// <summary>
+        /// 续命线程取消令牌
+        /// </summary>
+        private CancellationTokenSource tokenSource = new CancellationTokenSource();
+
+        /// <summary>
+        /// 获取锁
+        /// </summary>
+        /// <param name="requestId">请求id保证释放锁时的客户端和加锁的客户端一致</param>
+        /// <returns></returns>
+        public bool GetLock(string requestId)
+        {
+            //设置key 设置过期时间20s
+            // while (true)
+            // {
+            //     //设置key Redis2.6.12以上版本,可以用set获取锁。set可以实现setnx和expire,这个是原子操作
+            //     if (RedisHelper.Set(lockKey, requestId, lockTime, RedisHelper.RedisExistence.Nx))
+            //     {
+            //         //设置成功后开启子线程为key续命
+            //         CreateThredXm();
+            //         return true;
+            //     }
+            // }
+            return true;
+        }
+
+        /// <summary>
+        /// 为锁续命(防止业务操作时间大于锁自动释放时间,锁被自动释放掉)
+        /// </summary>
+        void CreateThredXm()
+        {
+            Task.Run(() =>
+            {
+                while (true)
+                {
+                    Thread.Sleep(10);
+                    //外部取消 退出子线程
+                    if (tokenSource.IsCancellationRequested)
+                    {
+                        return;
+                    }
+                    //查询key还有多少秒释放
+                    var Seconds = RedisHelper.PTtl(lockKey) / 1000;
+                    //key还剩1/3秒时重设过期时间
+                    if (Seconds < (lockTime / 3))
+                    {
+                        //小于5秒则自动 重设过期时间
+                        RedisHelper.Expire(lockKey, lockTime);
+                    }
+                }
+            }, tokenSource.Token);
+        }
+        
+        /// <summary>
+        /// 释放锁操作
+        /// </summary>
+        /// <param name="requestId">请求id保证释放锁时的客户端和加锁的客户端一致</param>
+        public void ReleaseLock(string requestId)
+        {
+            //这里使用Lua脚本保证原子性操作
+            // string script = "if  redis.call('get', KEYS[1]) == ARGV[1] then " +
+            //         "return redis.call('del', KEYS[1]) " +
+            //         "else return 0 end";
+            // RedisHelper.Eval(script, lockKey, requestId);
+            // //取消续命线程
+            // tokenSource.Cancel();
+        }
+    }
+}

+ 82 - 0
Util/PosPushDataNewHelper.cs

@@ -0,0 +1,82 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using Library;
+using MySystem.Models.Main;
+
+namespace MySystem
+{
+    public class PosPushDataNewHelper
+    {
+        public readonly static PosPushDataNewHelper Instance = new PosPushDataNewHelper();
+        private PosPushDataNewHelper()
+        { }
+
+        //绑定数据
+        // public static void Bind(MerchantAddInfo mer, PosMachinesTwo pos)
+        // {
+        //     try
+        //     {
+        //         Dictionary<string, object> dataContent = new Dictionary<string, object>();
+        //         dataContent.Add("pos_sn", pos.PosSn); //机具sn
+        //         dataContent.Add("mer_no", mer.MchtNo); //商户号
+        //         dataContent.Add("id_card", mer.IdCardNumber); //商户身份证
+        //         dataContent.Add("mer_name", mer.CertMerchantName); //商户姓名
+        //         dataContent.Add("mer_mobile", mer.MobilePhone); //商户手机号
+        //         dataContent.Add("request_id", getRequestId(mer.Id, "bind")); //流水号
+        //         dataContent.Add("bind_time", mer.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间
+        //         dataContent.Add("brand", 101); //品牌
+        //         Push("bind", dataContent);
+        //     }
+        //     catch (Exception ex)
+        //     {
+        //         function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送绑定数据异常");
+        //     }
+        // }
+
+        //交易数据
+        public static void Trade(ConsumerOrders trade, MerchantAddInfo mer)
+        {
+            try
+            {
+                int QrPayFlag = 0;
+                int BankCardType = 1;
+                List<string> BrandIds = new List<string>();
+                decimal TradeAmount = trade.PayMoney;
+                Dictionary<string, object> dataContent = new Dictionary<string, object>();
+                dataContent.Add("pos_sn", trade.SnNo); //机具sn
+                dataContent.Add("mer_no", mer.MchtNo); //商户号
+                dataContent.Add("request_id", trade.OrderNo); //流水号
+                dataContent.Add("trade_amount", TradeAmount); //交易金额
+                dataContent.Add("trade_time", trade.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //交易时间
+                dataContent.Add("brand", 101); //品牌
+                dataContent.Add("card_type", BankCardType); //卡类型
+                dataContent.Add("qr_pay_flag", QrPayFlag); //云闪付标记
+                dataContent.Add("fee_rate", trade.IsAct == 1 ? 0.6M : 0.63M);
+                dataContent.Add("fee_amt", trade.IsAct == 1 ? 0 : 3);
+                Push("trade", dataContent);
+            }
+            catch (Exception ex)
+            {
+                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送交易数据异常");
+            }
+        }
+
+        public static void Push(string dataType, Dictionary<string, object> dataContent)
+        {
+            Dictionary<string, object> data = new Dictionary<string, object>();
+            data.Add("data_type", dataType);
+            data.Add("data_content", dataContent);
+            KxsRedisDbconn.Instance.AddList("KxsPosDataQueue", Newtonsoft.Json.JsonConvert.SerializeObject(data));
+        }
+
+        public static string getRequestId(int id, string type)
+        {
+            if(id > 0)
+            {
+                return function.MD5_32(id.ToString() + type);
+            }
+            return DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
+        }
+    }
+}