Forráskód Böngészése

返现队列调整

lcl 1 éve
szülő
commit
6c62970f08

+ 36 - 34
AppStart/Helper/ConsumerOrdersReturnDoService.cs

@@ -26,60 +26,62 @@ namespace MySystem
             bool op = true;
             while (op)
             {
-                RefundItem item = RedisDbconn.Instance.RPop<RefundItem>("ConsumerOrdersStat");
-                if (item != null)
+                DateTime start = DateTime.Now.AddDays(-1);
+                WebCMSEntities db = new WebCMSEntities();
+                var list = db.ConsumerProfit.Select(m => new { m.Id, m.Status, m.CreateDate }).Where(m => m.CreateDate >= start && m.Status == 0).Take(20).ToList();
+                foreach(var sub in list)
                 {
                     try
                     {
-                        bool success = false;
-                        WebCMSEntities db = new WebCMSEntities();
-                        if (item.PayMode == 1)
+                        ConsumerProfit item = db.ConsumerProfit.FirstOrDefault(m => m.Id == sub.Id);
+                        if(item != null)
                         {
-                            string resultString = new AlipayFunction().Refund(item.TradeNo, item.RefundMoney.ToString("f2"), "202202BB4fc8c2c03c914f2f87b6c678a77d7C03");
-                            JsonData jsonObj = JsonMapper.ToObject(resultString);
-                            if (jsonObj["alipay_trade_refund_response"]["code"].ToString() == "10000")
+                            bool success = false;
+                            if (item.QueryCount == 1)
                             {
-                                success = true;
+                                string resultString = new AlipayFunction().Refund(item.SeoTitle, item.GetMoney.ToString("f2"), "202202BB4fc8c2c03c914f2f87b6c678a77d7C03");
+                                JsonData jsonObj = JsonMapper.ToObject(resultString);
+                                if (jsonObj["alipay_trade_refund_response"]["code"].ToString() == "10000")
+                                {
+                                    success = true;
+                                }
                             }
-                        }
-                        else if (item.PayMode == 2)
-                        {
-                            string AppId = "wxe2c051b3e46c0f6f";
-                            string MchId = "1611167423";
-                            MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == item.MerchantId) ?? new MerchantAddInfo();
-                            MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == item.MerchantId) ?? new MerchantInfo();
-                            string SubMchId = merchantAdd.SubMchid;
-                            string Description = merchant.Name;
-                            string RefundNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
-                            Dictionary<string, string> dic = new WeChatFunction().Refund(AppId, MchId, SubMchId, item.RefundMoney, item.TotalMoney, item.TradeNo, RefundNo);
-                            if (dic.ContainsKey("status"))
+                            else if (item.QueryCount == 2)
                             {
-                                if (dic["status"] == "SUCCESS")
+                                string AppId = "wxe2c051b3e46c0f6f";
+                                string MchId = "1611167423";
+                                MerchantAddInfo merchantAdd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == item.MerchantId) ?? new MerchantAddInfo();
+                                MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == item.MerchantId) ?? new MerchantInfo();
+                                string SubMchId = merchantAdd.SubMchid;
+                                string Description = merchant.Name;
+                                string RefundNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
+                                Dictionary<string, string> dic = new WeChatFunction().Refund(AppId, MchId, SubMchId, item.GetMoney, decimal.Parse(function.CheckNum(item.SeoKeyword)), item.SeoTitle, RefundNo);
+                                if (dic.ContainsKey("status"))
                                 {
-                                    success = true;
+                                    if (dic["status"] == "SUCCESS")
+                                    {
+                                        success = true;
+                                    }
                                 }
                             }
-                        }
-                        if (success)
-                        {
-                            ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == item.ProfitId);
-                            if (order != null)
+                            if (success)
                             {
-                                order.Status = 2;
+                                item.Status = 1;
+                                ConsumerOrders order = db.ConsumerOrders.FirstOrDefault(m => m.Id == item.OrderId);
+                                if (order != null)
+                                {
+                                    order.Status = 2;
+                                }
                                 db.SaveChanges();
                             }
                         }
-                        db.Dispose();
                     }
                     catch (Exception ex)
                     {
                         function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "退款返现队列异常");
                     }
                 }
-                else
-                {
-                    op = false;
-                }
+                db.Dispose();
             }
         }
     }

+ 109 - 115
AppStart/Helper/Profit/ProfitHelper.cs

@@ -158,7 +158,7 @@ namespace MySystem
                                                     GetMoney = DiviMoney;
                                                 }
                                                 else
-                                                {                                                     
+                                                {
                                                     DiffMoney = DiviMoney - GetMoney;
                                                 }
                                                 if (GetMoney > minProfit)
@@ -175,17 +175,11 @@ namespace MySystem
                                                         MerchantId = suborder.MerchantId,
                                                         OrderId = OrderId,
                                                         GetMoney = GetMoney,
+                                                        QueryCount = PayMode,
+                                                        SeoTitle = order.OrderNo,
+                                                        SeoKeyword = order.PayMoney.ToString(),
                                                     }).Entity;
                                                     db.SaveChanges();
-                                                    RedisDbconn.Instance.AddList("ConsumerOrdersStat", new RefundItem()
-                                                    {
-                                                        ProfitId = cprofit.Id,
-                                                        TradeNo = order.OrderNo,
-                                                        RefundMoney = GetMoney,
-                                                        TotalMoney = PayMoney,
-                                                        PayMode = order.PayMode,
-                                                        MerchantId = order.MerchantId,
-                                                    });
                                                     if (suborder.CurDivi < suborder.MaxDivi)
                                                     {
                                                         RedisDbconn.Instance.AddList("ConsumerOrders:Divi:" + MerchantId, suborder);
@@ -215,18 +209,11 @@ namespace MySystem
                                     TotalActual = PayMoney * GetPercent + DoCount * DiviMoney + OtherMoney;
                                     function.WriteLog("返现金额:" + DiviMoney + ";返现人数:" + DiviPersons + ";返现商户:" + TotalActual + ";返现溢出:" + OtherMoney + ";商户应得金额:" + MerchantGetMoney + ";返现差额:" + DoMoney + ";", "消费者返现日志");
 
-                                    //创客分润
-                                    DoProfit(db, order, true);
 
                                     //代理商分润
                                     // AgentProfit(db, order);
                                 }
                             }
-                            else
-                            {
-                                // TotalActual = PayMoney * (1 - cusumerFeePercent);
-                                DoProfit(db, order);
-                            }
 
                             //统计商户数据
                             // string MonthString = order.UpdateDate.Value.ToString("yyyyMM");
@@ -291,127 +278,135 @@ namespace MySystem
             db.Dispose();
         }
 
-        //分润算法
-        public void DoProfit(WebCMSEntities dbnew, ConsumerOrders trade_record, bool IsActive = false)
+        public void StartListenProfit()
         {
-            MerchantInfo merchant = dbnew.MerchantInfo.FirstOrDefault(m => m.Id == trade_record.MerchantId) ?? new MerchantInfo();
-            List<ProfitUsers> users = new List<ProfitUsers>();
-            int TopUserId = 0; //顶级创客Id
-            Users us = dbnew.Users.FirstOrDefault(a => a.Id == merchant.UserId);
-            if (us != null)
+            Thread th = new Thread(StartListenProfitDo);
+            th.IsBackground = true;
+            th.Start();
+        }
+
+        public void StartListenProfitDo()
+        {
+            while(true)
             {
-                string uidstring = us.Id.ToString();
-                if (!string.IsNullOrEmpty(us.ParentNav))
+                if(DateTime.Now.Day == 1 && DateTime.Now.Hour > 0)
                 {
-                    uidstring = us.ParentNav.Trim(',').Replace(",,", ",") + "," + uidstring;
-                    TopUserId = int.Parse(function.CheckInt(us.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]));
+                    DoProfit();
+                    Thread.Sleep(600000);
                 }
-                string[] uidlist = uidstring.Split(',');
-                Array.Reverse(uidlist);
-                foreach (string uidstr in uidlist)
+                else
                 {
-                    int puid = int.Parse(function.CheckInt(uidstr));
-                    Users pus = dbnew.Users.FirstOrDefault(a => a.Id == puid);
-                    if (pus != null)
-                    {
-                        users.Add(new ProfitUsers()
-                        {
-                            UserId = pus.Id,
-                            UserNav = pus.ParentNav,
-                            UserLevel = pus.UserLevel,
-                            CreateDate = pus.CreateDate.Value,
-                        });
-                    }
+                    Thread.Sleep(3600000);
                 }
             }
-            string TradeMonth = trade_record.CreateDate.Value.ToString("yyyyMM");
-            List<ProfitResult> list = new List<ProfitResult>();
-            list = StartProft(dbnew, trade_record, 1, users, IsActive);
-            using (var tran = dbnew.Database.BeginTransaction())
+        }
+
+        //分润算法
+        public void DoProfit()
+        {
+            string TradeMonth = DateTime.Now.ToString("yyyyMM");
+            WebCMSEntities dbnew = new WebCMSEntities();
+            DataTable dt = CustomerSqlConn.dtable("select IsAct,UserId,sum(TotalAmount) from UserAmountSummary where TradeMonth='" + TradeMonth + "' and SeoTitle='self' group by IsAct,UserId", MysqlConn.SqlConnStr);
+            foreach(DataRow dr in dt.Rows)
             {
-                try
+                int UserId = int.Parse(dr["UserId"].ToString());
+                bool IsActive = dr["IsAct"].ToString() == "1";
+                decimal TotalAmount = decimal.Parse(dr[2].ToString());
+                List<ProfitUsers> users = new List<ProfitUsers>();
+                int TopUserId = 0; //顶级创客Id
+                Users us = dbnew.Users.FirstOrDefault(a => a.Id == UserId);
+                if (us != null)
                 {
-                    foreach (ProfitResult sub in list)
+                    string uidstring = us.Id.ToString();
+                    if (!string.IsNullOrEmpty(us.ParentNav))
                     {
-                        if (sub.Message == "分红奖励")
-                        { 
-                            UserAccountRecord accountRecord = dbnew.UserAccountRecord.Add(new UserAccountRecord()
+                        uidstring = us.ParentNav.Trim(',').Replace(",,", ",") + "," + uidstring;
+                        TopUserId = int.Parse(function.CheckInt(us.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]));
+                    }
+                    string[] uidlist = uidstring.Split(',');
+                    Array.Reverse(uidlist);
+                    foreach (string uidstr in uidlist)
+                    {
+                        int puid = int.Parse(function.CheckInt(uidstr));
+                        Users pus = dbnew.Users.FirstOrDefault(a => a.Id == puid);
+                        if (pus != null)
+                        {
+                            users.Add(new ProfitUsers()
                             {
-                                CreateDate = DateTime.Now,
-                                UpdateDate = DateTime.Now,
-                                UserId = sub.UserId, //创客
-                                ChangeType = 11, //变动类型
-                                ProductType = 1, //产品类型
-                                ChangeAmount = sub.Money, //变更金额
-                                Status = -1,
-                            }).Entity;
-                            dbnew.SaveChanges();
+                                UserId = pus.Id,
+                                UserNav = pus.ParentNav,
+                                UserLevel = pus.UserLevel,
+                                CreateDate = pus.CreateDate.Value,
+                            });
                         }
-                        else
+                    }
+                }
+                List<ProfitResult> list = new List<ProfitResult>();
+                list = StartProft(dbnew, TotalAmount, 1, users, IsActive);
+                using (var tran = dbnew.Database.BeginTransaction())
+                {
+                    try
+                    {
+                        foreach (ProfitResult sub in list)
                         {
-                            int ProfitType = sub.UserId == merchant.UserId ? 0 : 1;
-                            ProfitRewardRecord editprofitrecord = dbnew.ProfitRewardRecord.FirstOrDefault(m => m.UserId == sub.UserId && m.MerchantId == trade_record.MerchantId && m.BrandId == 1 && m.ProfitType == ProfitType && m.TradeMonth == TradeMonth);
-                            if (editprofitrecord == null)
-                            {
-                                editprofitrecord = dbnew.ProfitRewardRecord.Add(new ProfitRewardRecord()
+                            if (sub.Message == "分红奖励")
+                            { 
+                                UserAccountRecord accountRecord = dbnew.UserAccountRecord.Add(new UserAccountRecord()
                                 {
                                     CreateDate = DateTime.Now,
+                                    UpdateDate = DateTime.Now,
                                     UserId = sub.UserId, //创客
-                                    MerchantId = trade_record.MerchantId, //商户
-                                    BrandId = 1, //品牌
-                                    ProfitType = ProfitType, //创客分润类型
-                                    TradeMonth = TradeMonth, //交易月
-                                    TopUserId = TopUserId, //顶级创客
+                                    ChangeType = 11, //变动类型
+                                    ProductType = 1, //产品类型
+                                    ChangeAmount = sub.Money, //变更金额
+                                    Status = -1,
                                 }).Entity;
                                 dbnew.SaveChanges();
-                                string RecordNo = "KPM";
-                                int RecordId = editprofitrecord.Id;
-                                string RecordIdString = RecordId.ToString();
-                                for (int i = 0; i < 14 - RecordId.ToString().Length; i++)
+                            }
+                            else
+                            {
+                                int ProfitType = sub.UserId == UserId ? 0 : 1;
+                                ProfitRewardRecord editprofitrecord = dbnew.ProfitRewardRecord.FirstOrDefault(m => m.UserId == sub.UserId && m.BrandId == 1 && m.ProfitType == ProfitType && m.TradeMonth == TradeMonth);
+                                if (editprofitrecord == null)
                                 {
-                                    RecordIdString = "0" + RecordIdString;
+                                    editprofitrecord = dbnew.ProfitRewardRecord.Add(new ProfitRewardRecord()
+                                    {
+                                        CreateDate = DateTime.Now,
+                                        UserId = sub.UserId, //创客
+                                        BrandId = 1, //品牌
+                                        ProfitType = ProfitType, //创客分润类型
+                                        TradeMonth = TradeMonth, //交易月
+                                        TopUserId = TopUserId, //顶级创客
+                                    }).Entity;
+                                    dbnew.SaveChanges();
+                                    string RecordNo = "KPM";
+                                    int RecordId = editprofitrecord.Id;
+                                    string RecordIdString = RecordId.ToString();
+                                    for (int i = 0; i < 14 - RecordId.ToString().Length; i++)
+                                    {
+                                        RecordIdString = "0" + RecordIdString;
+                                    }
+                                    RecordNo += RecordIdString;
+                                    editprofitrecord.RecordNo = RecordNo;
+                                    dbnew.SaveChanges();
                                 }
-                                RecordNo += RecordIdString;
-                                editprofitrecord.RecordNo = RecordNo;
+                                editprofitrecord.TradeAmt += TotalAmount; //商户交易总额                            
+                                editprofitrecord.DebitCapTradeNum += 1; //商户借记卡封顶交易总笔数
+                                decimal ProfitMoney = sub.Money;
                                 dbnew.SaveChanges();
                             }
-                            decimal TradeAmount = trade_record.PayMoney;
-                            editprofitrecord.TradeAmt += TradeAmount; //商户交易总额                            
-                            editprofitrecord.DebitCapTradeNum += 1; //商户借记卡封顶交易总笔数
-                            decimal ProfitMoney = sub.Money;
-                            dbnew.SaveChanges();
-
-                            dbnew.ProfitRecord.Add(new ProfitRecord()
-                            {
-                                CreateDate = DateTime.Now,
-                                UpdateDate = DateTime.Now,
-                                UserId = sub.UserId, //创客
-                                MerchantId = trade_record.MerchantId, //商户
-                                SnNo = trade_record.SnNo, //SN号
-                                TradeDate = trade_record.CreateDate, //交易日期
-                                ProfitAmount = sub.Money, //分润金额
-                                BrandId = 1, //品牌
-                                ParentNav = us.ParentNav, //上级创客集
-                                TradeId = trade_record.Id, //交易记录Id
-                            });
-                            dbnew.SaveChanges();
                         }
+                        dbnew.SaveChanges();
+                        tran.Commit();
+                    }
+                    catch (Exception ex)
+                    { 
+                        tran.Rollback();
+                        function.WriteLog(ex.ToString(), "分润异常");
                     }
-                    dbnew.SaveChanges();
-                    tran.Commit();
-                }
-                catch (Exception ex)
-                { 
-                    tran.Rollback();
-                    ErrorMsg msg = new ErrorMsg()
-                    {
-                        Time = DateTime.Now,
-                        ErrorContent = ex.ToString(),
-                        Obj = Newtonsoft.Json.JsonConvert.SerializeObject(trade_record),
-                    };
-                    function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "MainServerProfitError");
                 }
             }
+            dbnew.Dispose();
         }
 
         public void DoActives(WebCMSEntities dbnew, TradeRecord trade_record)
@@ -715,10 +710,9 @@ namespace MySystem
 
 
         #region 分润算法
-        public List<ProfitResult> StartProft(WebCMSEntities dbnew, ConsumerOrders trade, int LevelKindId, List<ProfitUsers> users, bool IsActive = true)
+        public List<ProfitResult> StartProft(WebCMSEntities dbnew, decimal TotalAmount, int LevelKindId, List<ProfitUsers> users, bool IsActive = true)
         {
             int BrandId = 1;
-            decimal TradeMoney = trade.PayMoney;
             List<UserLevelSet> levels = RedisDbconn.Instance.GetList<UserLevelSet>("pobjlevel");
             List<ProfitResult> result = new List<ProfitResult>();
             ProfitObjects obj = RedisDbconn.Instance.Get<ProfitObjects>("pobj" + BrandId);
@@ -759,7 +753,7 @@ namespace MySystem
                             decimal profitPercent = IsActive ? objlevel.DebitPercents : objlevel.Percents;
                             if (profitPercent > 0)
                             {
-                                getLevelProfit += TradeMoney * profitPercent;
+                                getLevelProfit += TotalAmount * profitPercent;
                             }
                             decimal money = getLevelProfit;
                             getLevelProfit -= diffLevelProfit;