Browse Source

补分账

lcl 6 months ago
parent
commit
45bac10999
2 changed files with 90 additions and 247 deletions
  1. 62 247
      Controllers/HomeController.cs
  2. 28 0
      Util/HaoDa/HaoDaHelper.cs

+ 62 - 247
Controllers/HomeController.cs

@@ -37,266 +37,81 @@ namespace MySystem.Controllers
 
         public string test()
         {
-            // List<int> MerchantIds = new List<int>();
-            // MerchantIds.Add(5);
-            // foreach(int MerchantId in MerchantIds)
-            // {
-            //     RedisDbconn.Instance.AddList("MerchantConfirmHdQueue", "{\"MerchantId\":\"" + MerchantId + "\"}");
-            // }
-            // TianYuVoiceHelper.Instance.doSomething(DateTime.Now.ToString("yyyyMMddHHmmssfff"),"2023100701","100");
-            // MqLinksHelper.Instance.doSomething("LKB01230600250000585","100");
-
+            List<string> hdorders = new List<string>();
             WebCMSEntities db = new WebCMSEntities();
-            DateTime start = DateTime.Parse("2024-03-10 00:00:00");
-            List<ConsumerOrders> orders = db.ConsumerOrders.Where(m => m.Status > 0 && m.IsAct == 1 && m.CreateDate > start && m.CurDivi < m.MaxDivi && m.SettleAmount > 0).OrderBy(m => m.Id).ToList();
-            foreach(ConsumerOrders order in orders)
+            var list = db.ConsumerOrders.Select(m => new { m.Id, m.SettleAmount, m.IsAct, m.SeoTitle }).Where(m => hdorders.Contains(m.SeoTitle) && m.IsAct == 1 && m.SettleAmount == 0).OrderBy(m => m.Id).ToList();
+            foreach (var sub in list)
             {
-                bool check = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, 1, 10000).Any(m => m.Id == order.Id);
-                bool check1 = db.ConsumerProfit.Any(m => m.OrderId == order.Id);
-                if(!check && !check1)
-                {
-                    RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":List", order.Id.ToString());
-                    ConsumerOrders edit = db.ConsumerOrders.FirstOrDefault(m => m.Id == order.Id);
-                    if(edit != null)
-                    {
-                        edit.ReturnFlag = 1;
-                        db.SaveChanges();
-                    }
-                }
-                else
+                ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == sub.Id);
+                if (order != null)
                 {
-                    if(!check)
-                    {
-                        RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, order);
-                        ConsumerOrders edit = db.ConsumerOrders.FirstOrDefault(m => m.Id == order.Id);
-                        if(edit != null)
-                        {
-                            edit.ReturnFlag = 1;
-                            db.SaveChanges();
-                        }
-                    }
-                    else if(!check1)
+                    int Status = 2;
+                    MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
+                    MerchantParamSet set = Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantParamSet>(order.SeoDescription);
+                    if (order.IsAct == 1 && order.PayMoney >= set.MinPayMoney)
                     {
-                        ConsumerOrders edit = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, 1, 10000).FirstOrDefault(m => m.Id == order.Id);
-                        long count = RedisDbconn.Instance.DelFromList("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId,edit);
-                        if(count > 0)
+                        MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantAddInfo();
+                        
+                        //获取结算金额
+                        string settleString = HaoDaHelper.Instance.QueryOrderDivideDetail(merchantAdd.OutMchtNo, order.SeoTitle);
+                        JsonData jsonData = JsonMapper.ToObject(settleString);
+                        if(jsonData["resultCode"].ToString() == "1")
                         {
-                            RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":List", order.Id.ToString());
+                            string settleAmountString = jsonData["data"]["settleAmount"].ToString();
+                            order.SettleAmount = int.Parse(settleAmountString);
+
+                            //发起分账
+                            decimal fee = order.PayMoney;
+                            if (fee >= 1)
+                            {
+                                string applyNo = "FZ" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8); //批次号
+                                string mchtNo = merchantAdd.OutMchtNo; //慧掌柜商户号
+                                string orderNo = order.SeoTitle; //交易订单号(好哒平台订单号,G开头)
+                                string acctNo = AppConfig.Haoda.AcctNo; //账户号,分账接收方账户号
+                                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;
+                                amount = decimal.Parse(amount.ToString("f2"));
+                                decimal otherAmt = settleAmount - servicefee - amount;
+                                if (otherAmt != 0)
+                                {
+                                    amount += otherAmt;
+                                }
+
+                                amount = amount * 100; //金额(分)
+                                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, acctType, amountAmount, seviceAmount), merchantAdd.BrandId);
+                                order.DivideLog = "请求分账日志:" + result;
+                                order.TradePayNo = applyNo;
+                                if (result.Contains("\"resultCode\":\"0\"") && (result.Contains("分账金额不足") || result.Contains("不存在")))
+                                {
+                                    Status = 1;
+                                }
+                            }
                         }
                     }
-                }
-                if(order.ReturnFlag == 0 && check && check1)
-                {
-                    ConsumerOrders edit = db.ConsumerOrders.FirstOrDefault(m => m.Id == order.Id);
-                    if(edit != null)
+                    order.Status = Status;
+                    if (Status == 2)
                     {
-                        edit.ReturnFlag = 1;
-                        db.SaveChanges();
+                        order.DivideFlag = 1;
+                        order.DivideDate = DateTime.Now;
                     }
                 }
             }
-
-            // Dictionary<string, decimal> ids = new Dictionary<string, decimal>();
-            // ids.Add("2023123120272680842405573", 30.08M);
-            // foreach(string orderno in ids.Keys)
-            // {
-            //     decimal cur = ids[orderno];
-            //     ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.OrderNo == orderno) ?? new ConsumerOrders();
-            //     order.CurDivi = cur;
-            //     order.SeoDescription = Regex.Replace(function.CheckNull(order.SeoDescription), "\"UpdateDate\":\".*?\"", "\"UpdateDate\":\"" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + "\"");
-            //     order.SeoDescription = Regex.Replace(function.CheckNull(order.SeoDescription), "\"ProfitDays\":.*?,", "\"ProfitDays\":90,");
-            //     RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, order);
-            // }
-
-            // string content = function.ReadInstance("/222.log");
-            // string[] contents = content.Split('\n');
-            // foreach(string sub in contents)
-            // {
-            //     // RedisDbconn.Instance.AddList("WeChatPayBackHd", sub);
-            //     RedisDbconn.Instance.AddList("PartOrderQueue", sub);
-            // }
-
-            // DataTable dt = CustomerSqlConn.dtable("select tb.Id,tb.PayMode,p.Id pId from (select Id,PayMode from ConsumerOrders where CreateDate>='2024-01-18 00:00:00' and `Status`>0) tb left join ConsumerProfit p on tb.Id=p.OrderId", AppConfig.Base.SqlConnStr);
-            // foreach(DataRow dr in dt.Rows)
-            // {
-            //     string Id = dr["Id"].ToString();
-            //     string PayMode = dr["PayMode"].ToString();
-            //     string pId = dr["pId"].ToString();
-            //     if(string.IsNullOrEmpty(pId))
-            //     {
-            //         RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + PayMode + ":List", Id);
-            //     }
-            // }
-
-            string str = "";
-            // str += "<table>";
-            // str += "<tr><td>订单号</td><td>下单时间</td><td>商家分红天数</td><td>当前分红金额</td><td>最大分红</td></tr>";
-            // List<int> orders = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrdersHd:Divi:2:1940", 1, 10000).Select(m => m.Id).ToList();
-            // DataTable dt = CustomerSqlConn.dtable("select OrderId,sum(GetMoney) money from ConsumerProfit where MerchantId=1940 and CreateDate>='2023-12-01 00:00:00' group by OrderId order by OrderId", AppConfig.Base.SqlConnStr);
-            // foreach(DataRow dr in dt.Rows)
-            // {
-            //     decimal cur = decimal.Parse(dr["money"].ToString());
-            //     int OrderId = int.Parse(dr["OrderId"].ToString());
-            //     if(!orders.Contains(OrderId))
-            //     {
-            //         // string orderno = dr["OrderNo"].ToString();
-            //         ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == OrderId) ?? new ConsumerOrders();
-            //         if(order.MaxDivi - cur >= 1)
-            //         {
-            //             order.CurDivi = cur;
-            //             MerchantParamSet set = Newtonsoft.Json.JsonConvert.DeserializeObject<MerchantParamSet>(order.SeoDescription);
-            //             order.SeoDescription = Regex.Replace(function.CheckNull(order.SeoDescription), "\"UpdateDate\":\".*?\"", "\"UpdateDate\":\"" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + "\"");
-            //             // order.SeoDescription = Regex.Replace(function.CheckNull(order.SeoDescription), "\"ProfitDays\":.*?,", "\"ProfitDays\":365,");
-            //             str += "<tr><td>" + order.OrderNo + "</td><td>" + order.UpdateDate.Value.ToString() + "</td><td>" + set.ProfitDays + "</td><td>" + cur + "</td><td>" + order.MaxDivi + "</td></tr>";
-            //             // RedisDbconn.Instance.AddList("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, order);
-            //             // var list = db.ConsumerProfit.Where(m => m.OrderId == order.Id).OrderBy(m => m.Id).ToList();
-            //             // foreach(var sub in list)
-            //             // {
-            //             //     if(cur >= sub.GetMoney)
-            //             //     {
-            //             //         cur -= sub.GetMoney;
-            //             //     }
-            //             //     else
-            //             //     {
-            //             //         if(cur > 0)
-            //             //         {
-            //             //             var edit = db.ConsumerProfit.FirstOrDefault(m => m.OrderId == order.Id);
-            //             //             if(edit != null)
-            //             //             {
-            //             //                 edit.GetMoney = cur;
-            //             //                 db.SaveChanges();
-            //             //             }
-            //             //         }
-            //             //         else
-            //             //         {
-            //             //             var edit = db.ConsumerProfit.FirstOrDefault(m => m.OrderId == order.Id);
-            //             //             if(edit != null)
-            //             //             {
-            //             //                 db.ConsumerProfit.Remove(edit);
-            //             //                 db.SaveChanges();
-            //             //             }
-            //             //         }
-            //             //     }
-            //             // }
-            //         }
-            //     }
-            // }
-            // str += "</table>";
-
-
-
-            // List<int> ids = new List<int>();
-            // ids.Add(4058);
-            // List<ConsumerOrders> orders = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrdersHd:Divi:2:1271");
-            // foreach(int oid in ids)
-            // {
-            //     function.WriteLog("oid:" + oid, "返现id");
-            //     decimal curDivi = 0;
-            //     ConsumerOrders order = orders.FirstOrDefault(m => m.Id == oid);
-            //     if(order == null)
-            //     {
-            //         order = db.ConsumerOrders.FirstOrDefault(m => m.Id == oid);
-            //         curDivi = order.MaxDivi;
-            //         function.WriteLog("出局", "返现id");
-            //     }
-            //     else
-            //     {
-            //         function.WriteLog("未出局", "返现id");
-            //         curDivi = order.CurDivi;
-            //     }
-            //     if(order != null)
-            //     {
-            //         List<ConsumerProfit> profits = db.ConsumerProfit.Where(m => m.OrderId == oid).ToList();
-            //         foreach(ConsumerProfit profit in profits)
-            //         {
-            //             if(curDivi >= profit.GetMoney)
-            //             {
-            //                 curDivi -= profit.GetMoney;
-            //             }
-            //             else
-            //             {
-            //                 function.WriteLog(profit.Id.ToString(), "返现id");
-            //             }
-            //         }
-            //     }
-            //     function.WriteLog("\n\n", "返现id");
-            // }
-
+            db.SaveChanges();
             db.Dispose();
 
-            // RedisDbconn.Instance.AddRightList("testlist", "1");
-            // RedisDbconn.Instance.AddRightList("testlist", "2");
-            // RedisDbconn.Instance.AddRightList("testlist", "3");
-            // RedisDbconn.Instance.AddRightList("testlist", "4");
-            // RedisDbconn.Instance.AddRightList("testlist", "5");
-            // RedisDbconn.Instance.AddRightList("testlist", "6");
-            // RedisDbconn.Instance.AddRightList("testlist", "7");
-            // RedisDbconn.Instance.AddRightList("testlist", "8");
-            // RedisDbconn.Instance.AddRightList("testlist", "9");
-
-            // str = RedisDbconn.Instance.RPopLPush<string>("testlist", "testlist"); 
-
-            // 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;
-            // string seviceAmount = servicefee.ToString("f0"); //服务费
-            // string amountAmount = amount.ToString("f0");
-
-            // return amountAmount + ":" + seviceAmount;
-
-            // List<int> ids = new List<int>();
-            // ids.Add(191);
-            // foreach(int id in ids)
-            // {
-            //     RedisDbconn.Instance.AddList("MerchantConfirmHdQueue", "{\"MerchantId\":\"" + id.ToString() + "\"}");
-            // }
-
-            // List<ConsumerOrders> orders = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrdersHd:Divi:2:1940", 1, 10000);
-            // int index = orders.Count;
-            // foreach(ConsumerOrders order in orders)
-            // {
-            //     index -= 1;
-            //     // decimal persent = order.CurDivi / order.MaxDivi;
-            //     // if(order.CurDivi > order.MaxDivi)
-            //     // {
-            //         // RedisDbconn.Instance.DelFromList("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, order);
-            //         order.MaxDivi = order.PayMoney * 0.8M;
-            //         RedisDbconn.Instance.SetList("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, index, order);
-            //     // }
-            // }
-
-            // DataTable dt = CustomerSqlConn.dtable("select DISTINCT MerchantId,PayMode from ConsumerOrders where IsAct=1 and `Status`>0 and MerchantId!=1940 order by MerchantId,PayMode", AppConfig.Base.SqlConnStr);
-            // foreach(DataRow dr in dt.Rows)
-            // {
-            //     string PayMode = dr["PayMode"].ToString();
-            //     string MerchantId = dr["MerchantId"].ToString();
-            //     List<ConsumerOrders> orders = RedisDbconn.Instance.GetList<ConsumerOrders>("ConsumerOrdersHd:Divi:" + PayMode + ":" + MerchantId, 1, 10000);
-            //     foreach(ConsumerOrders order in orders)
-            //     {
-            //         decimal check = order.MaxDivi - order.CurDivi;
-            //         if(check < 0.01M)
-            //         {
-            //             function.WriteLog("MerchantId:" + order.MerchantId);
-            //             RedisDbconn.Instance.DelFromList("ConsumerOrdersHd:Divi:" + order.PayMode + ":" + order.MerchantId, order);
-            //         }
-            //     }
-            // }
-
-            return str;
+            return "ok";
         }
 
         public string bindapp(int mid)

+ 28 - 0
Util/HaoDa/HaoDaHelper.cs

@@ -398,6 +398,34 @@ namespace MySystem
             return result;
         }
 
+        /// <summary>
+        /// 分账交易详情查询
+        /// </summary>
+        /// <param name="mchtNo">慧掌柜商户号</param>
+        /// <param name="orderNo">交易订单号	</param>
+        /// <returns></returns>
+        public string QueryOrderDivideDetail(string mchtNo, string orderNo, int BrandId = 0)
+        {
+            var BoxRequestUrl = HaodaUnionParam.BoxRequestUrl;
+            if (BrandId == 1) BoxRequestUrl = HaodaParam.BoxRequestUrl;
+
+            LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "好哒分账交易详情查询");
+            Dictionary<string, object> reqdic = new Dictionary<string, object>();
+            reqdic.Add("mchtNo", mchtNo); // 慧掌柜商户号
+            reqdic.Add("orderNo", orderNo); // 交易订单号	
+            string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
+            LogHelper.Instance.WriteLog(req, "好哒分账交易详情查询");
+            Dictionary<string, string> headdic = GetHeader(req);
+            string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
+            LogHelper.Instance.WriteLog("请求头\n" + head, "好哒分账交易详情查询");
+            LogHelper.Instance.WriteLog("请求参数\n" + req, "好哒分账交易详情查询");
+            LogHelper.Instance.WriteLog("请求地址:" + BoxRequestUrl + "/api/v3/hzg/part/trade/clear/get", "好哒分账交易详情查询");
+            string result = PostWebRequest(BoxRequestUrl + "/api/v3/hzg/part/trade/clear/get", req, headdic);
+            LogHelper.Instance.WriteLog("返回\n" + result + "\n\n", "好哒分账交易详情查询");
+
+            return result;
+        }
+
         /// <summary>
         /// 订单分账申请
         /// </summary>