Browse Source

调整分账金额计算方式

lcl 1 year ago
parent
commit
be98a6804c

+ 11 - 2
Controllers/HomeController.cs

@@ -26,7 +26,7 @@ namespace MySystem.Controllers
             return View();
         }
 
-        public string test(decimal a)
+        public string test(decimal a, decimal b, decimal g)
         {
             // List<int> MerchantIds = new List<int>();
             // MerchantIds.Add(5);
@@ -87,11 +87,20 @@ namespace MySystem.Controllers
 
             // db.Dispose();
 
-            decimal hdfee = 0.0038M * a * (1 - 0.9M - 0.01M);
+            decimal settleAmount = b / 100M;
+            decimal divideAmt = (1 - 0.0038M) * a;
+            divideAmt = decimal.Parse(divideAmt.ToString("f2"));
+            decimal hdfee = 0.0038M * a * (1 - g / 100 - 0.01M);
             hdfee = decimal.Parse(hdfee.ToString("f2"));
             decimal servicefee = 0.01M * a - hdfee;
             servicefee = decimal.Parse(servicefee.ToString("f2"));
             decimal amount = a*(1-0.0038M) - servicefee;
+            amount = decimal.Parse(amount.ToString("f2"));
+            decimal otherAmt = settleAmount - servicefee - amount;
+            if(otherAmt != 0)
+            {
+                amount += otherAmt;
+            }
 
             amount = amount * 100; //金额(分)
             servicefee = servicefee * 100;

+ 1 - 0
Models/Main/ConsumerOrders.cs

@@ -34,5 +34,6 @@ namespace MySystem.Models.Main
         public string DivideLog { get; set; }
         public string TradePayNo { get; set; }
         public int SetRecordId { get; set; }
+        public int SettleAmount { get; set; }
     }
 }

+ 4 - 0
Models/Main/WebCMSEntities.cs

@@ -2316,6 +2316,10 @@ namespace MySystem.Models.Main
                     .HasColumnType("int(11)")
                     .HasComment("活动记录Id");
 
+                entity.Property(e => e.SettleAmount)
+                    .HasColumnType("int(11)")
+                    .HasComment("结算金额");
+
                 entity.Property(e => e.SnNo)
                     .HasColumnType("varchar(50)")
                     .HasCharSet("utf8")

+ 1 - 0
Startup.cs

@@ -155,6 +155,7 @@ namespace MySystem
                 HaoDaAuthQueryHelper.Instance.Start(); //查询实名认证状态
                 WeChatPayBackService.Instance.Start(); //支付回调
                 WeChatPayBackService.Instance.StartProfitShare(); //分账队列
+                WeChatPayBackService.Instance.StartSettleOrder(); //接收分账结算金额
                 WeChatPayBackService.Instance.StartDivi(); //补分账
                 ProfitHelper.Instance.StartListenTrade(); //返现队列-支付宝
                 ProfitHelper.Instance.StartListenWxTrade(); //返现队列-微信

+ 64 - 12
Util/HaoDa/TestHaoDaService.cs

@@ -38,17 +38,16 @@ namespace MySystem
             // ConfiemMer(808);//新建商户
             // ChangeMerchantCard(1);//商户修改银行卡
             // AddOpenDivideAccounts(832); //添加分账方
-            // QueryAuthStatus(5);//查询实名认证状态
+            // QueryAuthStatus(1600);//查询实名认证状态
             // WeChatFunctionForHD.Instance.QueryAuthMerchant("600955391");
             // AlipayFunctionForHD.Instance.AuthOrderAuthQuery("2088820718243322");
             // StartProfitShareListen();
             // HaoDaHelper.Instance.BindWeChatAppId("1490230", "21622558", AppConfig.Haoda.SubAppId);
 
             // HaoDaHelper.Instance.OrderDivideAccountsQuery("FZ2023110315225748140470708", "1492500"); //查询分账结果
-            // WebCMSEntities db = new WebCMSEntities();
+            WebCMSEntities db = new WebCMSEntities();
             
             // List<int> ids = new List<int>();
-            // ids.Add(959);
             // foreach(int id in ids)
             // {
             //     ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == id) ?? new ConsumerOrders();
@@ -59,15 +58,68 @@ namespace MySystem
             //     string acctNo = AppConfig.Haoda.AcctNo; //账户号,分账接收方账户号
             //     string sacctNo = merchantAdd.MchtNo; //账户号,收款商户号
             //     string acctType = "0"; //账户类型(0:商户收款账户(大B),1:分账接收方账户(小B))
-            //     decimal hdfee = 0.0038M * order.PayMoney;
-            //     hdfee = decimal.Parse(hdfee.ToString("f2"));
-            //     decimal servicefee = 0.01M * order.PayMoney;
-            //     servicefee = decimal.Parse(servicefee.ToString("f2"));
-            //     decimal amount = order.PayMoney - hdfee - servicefee;
-            //     amount = amount * 100; //金额(分)
-            //     servicefee = servicefee * 100;
-            //     string seviceAmount = servicefee.ToString("f0"); //服务费
-            //     string result = HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, sacctNo, acctType, "1479", "0"));
+            //     // decimal hdfee = 0.0038M * order.PayMoney;
+            //     // hdfee = decimal.Parse(hdfee.ToString("f2"));
+            //     // decimal servicefee = 0.01M * order.PayMoney;
+            //     // servicefee = decimal.Parse(servicefee.ToString("f2"));
+            //     // decimal amount = order.PayMoney - hdfee - servicefee;
+            //     // amount = amount * 100; //金额(分)
+            //     // servicefee = servicefee * 100;
+            //     // string seviceAmount = servicefee.ToString("f0"); //服务费
+            //     string result = HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, sacctNo, acctType, "1", "0"));
+            // }
+
+            // string content = function.ReadInstance("content.txt");
+            // string[] list = content.Split('\n');
+            // foreach(string sub in list)
+            // {
+            //     JsonData jsonObj = JsonMapper.ToObject(sub);
+            //     //{"applyNo":"FZ2023112405301626900369427","mchtNo":"1492094","orderNo":"G2231123C02179899212","detail":[{"acctNo":"230817000000000278","acctType":"1","amount":"247"},{"acctNo":"030510106937306","acctType":"0","amount":"29440"}]}
+            //     string orderNo = jsonObj["orderNo"].ToString();
+
+            //     JsonData detail = jsonObj["detail"];
+            //     string me = detail[0]["amount"].ToString();
+            //     string mer = detail[1]["amount"].ToString();
+            //     ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.SeoTitle == orderNo) ?? new ConsumerOrders();
+            //     if(order.SettleAmount > 0)
+            //     {
+            //         MerchantParamSet set = Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantParamSet>(order.SeoDescription);
+            //         MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
+            //         string applyNo = "FZ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8); //批次号
+            //         string mchtNo = merchantAdd.OutMchtNo; //慧掌柜商户号
+            //         string acctNo = AppConfig.Haoda.AcctNo; //账户号,分账接收方账户号
+            //         string sacctNo = merchantAdd.MchtNo; //账户号,收款商户号
+
+            //         decimal settleAmount = (decimal)order.SettleAmount / 100M;
+            //         decimal divideAmt = (1 - 0.0038M) * order.PayMoney;
+            //         divideAmt = decimal.Parse(divideAmt.ToString("f2"));
+            //         decimal hdfee = 0.0038M * order.PayMoney * (1 - set.GetPercent / 100 - 0.01M);
+            //         hdfee = decimal.Parse(hdfee.ToString("f2"));
+            //         decimal servicefee = 0.01M * order.PayMoney - hdfee;
+            //         servicefee = decimal.Parse(servicefee.ToString("f2"));
+            //         decimal amount = order.PayMoney*(1-0.0038M) - servicefee;
+            //         amount = decimal.Parse(amount.ToString("f2"));
+            //         decimal otherAmt = settleAmount - servicefee - amount;
+            //         if(otherAmt != 0)
+            //         {
+            //             amount += otherAmt;
+            //         }
+
+            //         amount -= decimal.Parse(mer) / 100M;
+            //         servicefee -= decimal.Parse(me) / 100M;
+            //         if(amount > 0)
+            //         {
+            //             if(servicefee < 0) servicefee = 0;
+
+            //             amount = amount * 100 - 1; //金额(分)
+            //             servicefee = servicefee * 100;
+
+            //             string seviceAmount = servicefee.ToString("f0"); //服务费
+            //             string amountAmount = amount.ToString("f0");
+
+            //             string result = HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, sacctNo, "0", amountAmount, seviceAmount));
+            //         }
+            //     }
             // }
 
             // db.Dispose();

+ 71 - 11
Util/HaoDa/WeChatPayBackService.cs

@@ -131,7 +131,7 @@ namespace MySystem
                 {
                     WebCMSEntities db = new WebCMSEntities();
                     DateTime checkDate = DateTime.Now.AddMinutes(-2);
-                    var list = db.ConsumerOrders.Select(m => new { m.Id, m.UpdateDate, m.Status, m.IsAct, m.PayMode }).Where(m => m.Status == 1 && m.IsAct == 1 && m.UpdateDate < checkDate).OrderBy(m => m.Id).Take(10).ToList();
+                    var list = db.ConsumerOrders.Select(m => new { m.Id, m.SettleAmount, m.Status, m.IsAct, m.PayMode }).Where(m => m.Status == 1 && m.IsAct == 1 && m.SettleAmount > 0).OrderBy(m => m.Id).Take(10).ToList();
                     foreach (var sub in list)
                     {
                         ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == sub.Id);
@@ -154,17 +154,20 @@ namespace MySystem
                                     string sacctNo = merchantAdd.MchtNo; //账户号,收款商户号
                                     string acctType = "0"; //账户类型(0:商户收款账户(大B),1:分账接收方账户(小B))
                                     
+                                    decimal settleAmount = (decimal)order.SettleAmount / 100M;
+                                    decimal divideAmt = (1 - 0.0038M) * order.PayMoney;
+                                    divideAmt = decimal.Parse(divideAmt.ToString("f2"));
                                     decimal hdfee = 0.0038M * order.PayMoney * (1 - set.GetPercent / 100 - 0.01M);
                                     hdfee = decimal.Parse(hdfee.ToString("f2"));
                                     decimal servicefee = 0.01M * order.PayMoney - hdfee;
                                     servicefee = decimal.Parse(servicefee.ToString("f2"));
                                     decimal amount = order.PayMoney*(1-0.0038M) - servicefee;
-
-                                    // decimal hdfee = 0.0038M * order.PayMoney;
-                                    // hdfee = decimal.Parse(hdfee.ToString("f2"));
-                                    // decimal servicefee = 0.01M * order.PayMoney;
-                                    // servicefee = decimal.Parse(servicefee.ToString("f2"));
-                                    // decimal amount = order.PayMoney - hdfee - servicefee;
+                                    amount = decimal.Parse(amount.ToString("f2"));
+                                    decimal otherAmt = settleAmount - servicefee - amount;
+                                    if(otherAmt != 0)
+                                    {
+                                        amount += otherAmt;
+                                    }
 
                                     amount = amount * 100; //金额(分)
                                     servicefee = servicefee * 100;
@@ -251,15 +254,26 @@ namespace MySystem
                                 string acctNo = AppConfig.Haoda.AcctNo; //账户号,分账接收方账户号
                                 string sacctNo = merchantAdd.MchtNo; //账户号,收款商户号
                                 string acctType = "0"; //账户类型(0:商户收款账户(大B),1:分账接收方账户(小B))
-                                decimal hdfee = 0.0038M * order.PayMoney;
+                                decimal settleAmount = (decimal)order.SettleAmount / 100M;
+                                decimal divideAmt = (1 - 0.0038M) * order.SettleAmount;
+                                divideAmt = decimal.Parse(divideAmt.ToString("f2"));
+                                decimal hdfee = 0.0038M * order.SettleAmount * (1 - set.GetPercent / 100 - 0.01M);
                                 hdfee = decimal.Parse(hdfee.ToString("f2"));
-                                decimal servicefee = 0.01M * order.PayMoney;
+                                decimal servicefee = 0.01M * order.SettleAmount - hdfee;
                                 servicefee = decimal.Parse(servicefee.ToString("f2"));
-                                decimal amount = order.PayMoney - hdfee - servicefee;
+                                decimal amount = order.SettleAmount*(1-0.0038M) - servicefee;
+                                amount = decimal.Parse(amount.ToString("f2"));
+                                decimal otherAmt = divideAmt - servicefee - amount;
+                                if(otherAmt != 0)
+                                {
+                                    amount += otherAmt;
+                                }
+
                                 amount = amount * 100; //金额(分)
                                 servicefee = servicefee * 100;
                                 string seviceAmount = servicefee.ToString("f0"); //服务费
-                                string result = HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, sacctNo, acctType, amount.ToString().Split('.')[0], seviceAmount));
+                                string amountAmount = amount.ToString("f0");
+                                string result = HaoDaHelper.Instance.OrderDivideAccounts(OrderDivideAccountsUtil.AddValue(applyNo, mchtNo, orderNo, acctNo, sacctNo, acctType, amountAmount, seviceAmount));
                                 order.DivideLog = "请求分账日志:" + result;
                                 db.SaveChanges();
                             }
@@ -277,5 +291,51 @@ namespace MySystem
                 }
             }
         }
+
+        //监听支付结算订单
+        public void StartSettleOrder()
+        {
+            Thread th = new Thread(SettleOrderListen);
+            th.IsBackground = true;
+            th.Start();
+        }
+
+        public void SettleOrderListen()
+        {
+            bool op = true;
+            while (op)
+            {
+                string content = RedisDbconn.Instance.RPop<string>("PartOrderQueue");
+                if (!string.IsNullOrEmpty(content))
+                {
+                    try
+                    {
+                        //{"id":137403,"mchtNo":"1494637","tradeMchtNo":"030500107115593","channelMchtNo":"030500107115593","orderNo":"G2231123C02179895853","channelOrderNo":"50231123B00032642694","txSubCode":31,"paymentType":2,"status":0,"partStatus":0,"partAmount":0,"payAmount":1000,"payServiceFee":4,"settleAmount":996,"totalPerkAmount":0,"payTime":"2023-11-23 23:36:05","algoDate":"2023-11-23 00:00:00","bizDate":"2023-11-23 00:00:00","storeNo":"21627867","deviceSn":" ","cupsNo":"ylzf","cardType":0,"notPartAmount":996,"realAmount":996}
+                        if(content.Contains("\"settleAmount\""))
+                        {
+                            JsonData jsonObj = JsonMapper.ToObject(content);
+                            string orderNo = jsonObj["orderNo"].ToString();
+                            int settleAmount = int.Parse(function.CheckInt(jsonObj["settleAmount"].ToString()));
+                            WebCMSEntities db = new WebCMSEntities();
+                            ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.SeoTitle == orderNo);
+                            if(order != null)
+                            {
+                                order.SettleAmount = settleAmount;
+                                db.SaveChanges();
+                            }
+                            db.Dispose();
+                        }
+                    }
+                    catch (Exception ex)
+                    {
+                        LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "监听支付结算订单异常");
+                    }
+                }
+                else
+                {
+                    Thread.Sleep(2000);
+                }
+            }
+        }
     }
 }