Pārlūkot izejas kodu

增加补分账队列

lcl 1 gadu atpakaļ
vecāks
revīzija
7398f8dac4

+ 71 - 1
AppStart/Helper/AlipayPayBackService.cs

@@ -148,7 +148,9 @@ namespace MySystem
                                         Account = AlipayFunction.Instance.Pid,
                                         Amount = fee.ToString("f2"),
                                     });
-                                    AlipayFunction.Instance.Settle(OrderNo, TradeNo, Parameters, merchantAdd.AlipayAuthToken);
+                                    string ProfitShareResult = AlipayFunction.Instance.Settle(OrderNo, TradeNo, Parameters, merchantAdd.AlipayAuthToken);
+                                    order.DivideLog = "请求分账日志:" + ProfitShareResult;
+                                    db.SaveChanges();
 
                                     //开始监听分账状态
                                     Dictionary<string, object> req = new Dictionary<string, object>();
@@ -175,5 +177,73 @@ namespace MySystem
                 Thread.Sleep(2000);
             }
         }
+
+
+
+        //触发分账接口
+        public void StartDivi()
+        {
+            Thread th = new Thread(ListenDivi);
+            th.IsBackground = true;
+            th.Start();
+        }
+
+        public void ListenDivi()
+        {
+            bool op = true;
+            while (op)
+            {
+                string content = RedisDbconn.Instance.RPop<string>("AddAlipayDiviQueue");
+                if (!string.IsNullOrEmpty(content))
+                {
+                    try
+                    {
+                        int id = int.Parse(function.CheckInt(content));
+                        WebCMSEntities db = new WebCMSEntities();
+                        ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == id);
+                        if (order != null)
+                        {
+                            MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
+                            MerchantParamSet set = db.MerchantParamSet.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantParamSet();
+                            if (order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
+                            {
+                                //发起分账
+                                decimal fee = order.PayMoney * 0.01M;
+                                if(fee >= 0.01M)
+                                {
+                                    string TradeNo = order.SeoTitle;
+                                    string OrderNo = order.OrderNo;
+                                    List<RoyaltyParameters> Parameters = new List<RoyaltyParameters>();
+                                    Parameters.Add(new RoyaltyParameters()
+                                    {
+                                        Account = AlipayFunction.Instance.Pid,
+                                        Amount = fee.ToString("f2"),
+                                    });
+                                    string ProfitShareResult = AlipayFunction.Instance.Settle(OrderNo, TradeNo, Parameters, merchantAdd.AlipayAuthToken);
+                                    order.DivideLog = "请求分账日志:" + ProfitShareResult;
+                                    db.SaveChanges();
+
+                                    //开始监听分账状态
+                                    Dictionary<string, object> req = new Dictionary<string, object>();
+                                    req.Add("TradeNo", TradeNo); //支付宝订单号
+                                    req.Add("OrderNo", OrderNo); //商户订单号
+                                    req.Add("AuthToken", merchantAdd.AlipayAuthToken);
+                                    RedisDbconn.Instance.AddList("AlipayShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
+                                }
+                            }
+                        }
+                        db.Dispose();
+                    }
+                    catch (Exception ex)
+                    {
+                        LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "补微信分账异常");
+                    }
+                }
+                else
+                {
+                    Thread.Sleep(2000);
+                }
+            }
+        }
     }
 }

+ 2 - 0
AppStart/Helper/AlipayShareService.cs

@@ -57,6 +57,8 @@ namespace MySystem
                         ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == OrderNo && m.Status == 2);
                         if (order != null)
                         {
+                            order.DivideLog = "分账审核结果:" + result;
+                            db.SaveChanges();
                             RedisDbconn.Instance.AddList("ConsumerOrders:Divi:" + order.PayMode + ":List", order.Id.ToString());
                             // RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:" + order.PayMode + ":" + order.MerchantId, order);
                         }

+ 2 - 0
AppStart/Helper/ProfitShareService.cs

@@ -57,6 +57,8 @@ namespace MySystem
                         ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == OrderNo && m.Status == 2);
                         if (order != null)
                         {
+                            order.DivideLog = "分账审核结果:" + result;
+                            db.SaveChanges();
                             RedisDbconn.Instance.AddList("ConsumerOrders:Divi:" + order.PayMode + ":List", order.Id.ToString());
                             // RedisDbconn.Instance.AddRightList("ConsumerOrders:Divi:" + order.PayMode + ":" + order.MerchantId, order);
                         }

+ 75 - 0
AppStart/Helper/WeChatPayBackService.cs

@@ -193,6 +193,8 @@ namespace MySystem
                                     LogHelper.Instance.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(Receivers), "微信分账队列监听");
                                     string ProfitShareResult = WeChatFunction.Instance.ProfitShare(merchantAdd.SubMchid, TradeNo, OrderNo, Receivers);
                                     LogHelper.Instance.WriteLog("分账结果:" + ProfitShareResult, "微信分账队列监听");
+                                    order.DivideLog = "请求分账日志:" + ProfitShareResult;
+                                    db.SaveChanges();
 
                                     //开始监听分账状态
                                     Dictionary<string, object> req = new Dictionary<string, object>();
@@ -220,5 +222,78 @@ namespace MySystem
                 Thread.Sleep(2000);
             }
         }
+
+
+        //触发分账接口
+        public void StartDivi()
+        {
+            Thread th = new Thread(ListenDivi);
+            th.IsBackground = true;
+            th.Start();
+        }
+
+        public void ListenDivi()
+        {
+            bool op = true;
+            while (op)
+            {
+                string content = RedisDbconn.Instance.RPop<string>("AddWeChatDiviQueue");
+                if (!string.IsNullOrEmpty(content))
+                {
+                    try
+                    {
+                        int id = int.Parse(function.CheckInt(content));
+                        WebCMSEntities db = new WebCMSEntities();
+                        ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == id);
+                        if (order != null)
+                        {
+                            MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
+                            MerchantParamSet set = db.MerchantParamSet.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantParamSet();
+                            if (order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
+                            {
+                                //发起分账
+                                decimal fee = order.PayMoney; //单位:分
+                                if(fee >= 1)
+                                {
+                                    string TradeNo = order.SeoTitle;
+                                    string OrderNo = order.OrderNo;
+                                    List<ReceiverList> Receivers = new List<ReceiverList>();
+                                    Receivers.Add(new ReceiverList()
+                                    {
+                                        type = "MERCHANT_ID",  //分账接收方类型
+                                        account = WeChatFunction.Instance.MchId,  //分账接收方账号
+                                        amount = int.Parse(fee.ToString("f0")),  //分账金额
+                                        description = "服务费",  //分账描述
+                                    });
+                                    LogHelper.Instance.WriteLog("TradeNo:" + TradeNo, "微信分账队列监听");
+                                    LogHelper.Instance.WriteLog("OrderNo:" + OrderNo, "微信分账队列监听");
+                                    LogHelper.Instance.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(Receivers), "微信分账队列监听");
+                                    string ProfitShareResult = WeChatFunction.Instance.ProfitShare(merchantAdd.SubMchid, TradeNo, OrderNo, Receivers);
+                                    LogHelper.Instance.WriteLog("分账结果:" + ProfitShareResult, "微信分账队列监听");
+                                    order.DivideLog = "请求分账日志:" + ProfitShareResult;
+                                    db.SaveChanges();
+
+                                    //开始监听分账状态
+                                    Dictionary<string, object> req = new Dictionary<string, object>();
+                                    req.Add("SubMchid", merchantAdd.SubMchid); //子商户号
+                                    req.Add("TradeNo", TradeNo); //微信订单号
+                                    req.Add("OrderNo", OrderNo); //商户订单号
+                                    RedisDbconn.Instance.AddList("ProfitShareQueue", Newtonsoft.Json.JsonConvert.SerializeObject(req));
+                                }
+                            }
+                        }
+                        db.Dispose();
+                    }
+                    catch (Exception ex)
+                    {
+                        LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "补微信分账异常");
+                    }
+                }
+                else
+                {
+                    Thread.Sleep(2000);
+                }
+            }
+        }
     }
 }

+ 3 - 0
Models/ConsumerOrders.cs

@@ -29,5 +29,8 @@ namespace MySystem.Models
         public ulong IsAct { get; set; }
         public int UserId { get; set; }
         public decimal MerchantActualAmount { get; set; }
+        public string DivideLog { get; set; }
+        public int DivideFlag { get; set; }
+        public DateTime? DivideDate { get; set; }
     }
 }

+ 14 - 0
Models/WebCMSEntities.cs

@@ -2257,6 +2257,20 @@ namespace MySystem.Models
 
                 entity.Property(e => e.CurDivi).HasColumnType("decimal(18,2)");
 
+                entity.Property(e => e.DivideDate)
+                    .HasColumnType("datetime")
+                    .HasComment("提交分账时间");
+
+                entity.Property(e => e.DivideFlag)
+                    .HasColumnType("int(11)")
+                    .HasComment("分账标记");
+
+                entity.Property(e => e.DivideLog)
+                    .HasColumnType("mediumtext")
+                    .HasComment("分账结果日志")
+                    .HasCharSet("utf8")
+                    .HasCollation("utf8_general_ci");
+
                 entity.Property(e => e.IsAct)
                     .HasColumnType("bit(1)")
                     .HasDefaultValueSql("b'0'");

+ 2 - 0
Startup.cs

@@ -190,6 +190,8 @@ namespace MySystem
                 ProfitHelper.Instance.StartListenWxTrade(); //微信返现队列
                 ProfitShareService.Instance.Start(); //分账状态监控队列,分账完成则提交返现(微信)
                 AlipayShareService.Instance.Start(); //分账状态监控队列,分账完成则提交返现(支付宝)
+                WeChatPayBackService.Instance.StartDivi(); //补分账
+                AlipayPayBackService.Instance.StartDivi(); //补分账
                 ProfitHelper.Instance.StartListenProfit(); //每月分润
 
                 AlipayPayBackFeeService.Instance.Start();