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

统计交易加事务,避免重复统计

lcl 1 éve
szülő
commit
58ca9e49ea
2 módosított fájl, 248 hozzáadás és 238 törlés
  1. 124 119
      AppStart/Helper/StatService.cs
  2. 124 119
      AppStart/Helper/StatService2.cs

+ 124 - 119
AppStart/Helper/StatService.cs

@@ -32,7 +32,7 @@ namespace MySystem
             while (true)
             {
                 StatTradeAmount();
-                Thread.Sleep(30000);
+                Thread.Sleep(10000);
             }
         }
         public void StatTradeAmount()
@@ -41,149 +41,154 @@ namespace MySystem
             function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "统计商户和创客交易额日志");
             WebCMSEntities db = new WebCMSEntities();
             KxsMainModels.WebCMSEntities maindb = new KxsMainModels.WebCMSEntities();
-            try
+            using (var tran = db.Database.BeginTransaction())
             {
-                string start = DateTime.Now.AddDays(-100).ToString("yyyy-MM-dd HH:mm:ss");
-                DataTable idsDt = OtherMySqlConn.dtable("select Id from ConsumerOrders where CreateDate>='" + start + "' and Status>0 and Version=0 order by Id limit 50");
-                if(idsDt.Rows.Count > 0)
+                try
                 {
-                    string ids = "";
-                    foreach (DataRow idsDr in idsDt.Rows)
+                    string start = DateTime.Now.AddDays(-100).ToString("yyyy-MM-dd HH:mm:ss");
+                    DataTable idsDt = OtherMySqlConn.dtable("select Id from ConsumerOrders where CreateDate>='" + start + "' and Status>0 and Version=0 order by Id limit 20");
+                    if(idsDt.Rows.Count > 0)
                     {
-                        ids += idsDr["Id"].ToString() + ",";
-                    }
-                    DataTable selfDt = OtherMySqlConn.dtable("select UserId,MerchantId,SnNo,PayMode,IsAct,DATE_FORMAT(CreateDate,'%Y%m%d'),sum(PayMoney),count(Id),sum(MaxDivi),sum(MerchantActualAmount) from ConsumerOrders where Id in (" + ids.TrimEnd(',') + ") group by UserId,MerchantId,SnNo,PayMode,IsAct,DATE_FORMAT(CreateDate,'%Y%m%d')");
-                    if (selfDt.Rows.Count > 0)
-                    {
-                        function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "统计商户和创客交易额日志");
-                        foreach (DataRow selfDr in selfDt.Rows)
+                        string ids = "";
+                        foreach (DataRow idsDr in idsDt.Rows)
                         {
-                            int UserId = int.Parse(selfDr["UserId"].ToString());
-                            KxsMainModels.Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId) ?? new KxsMainModels.Users();
-                            string ParentNav = user.ParentNav;
-                            int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
-                            int PayMode = int.Parse(selfDr["PayMode"].ToString());
-                            int IsAct = int.Parse(selfDr["IsAct"].ToString());
-                            string SnNo = selfDr["SnNo"].ToString();
-                            string TradeDate = selfDr[5].ToString();
-                            decimal TradeAmount = decimal.Parse(selfDr[6].ToString());
-                            int TradeCount = int.Parse(selfDr[7].ToString());
-                            decimal MaxDivi = decimal.Parse(selfDr[8].ToString());
-                            decimal ActualAmount = decimal.Parse(selfDr[9].ToString());
-                            string TradeMonth = TradeDate.Substring(0, 6);
-
-                            //创客交易额
-                            UserAmountSummary selfStat = db.UserAmountSummary.FirstOrDefault(m => m.UserId == UserId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode && m.SeoTitle == "self");
-                            if (selfStat == null)
+                            ids += idsDr["Id"].ToString() + ",";
+                        }
+                        DataTable selfDt = OtherMySqlConn.dtable("select UserId,MerchantId,SnNo,PayMode,IsAct,DATE_FORMAT(CreateDate,'%Y%m%d'),sum(PayMoney),count(Id),sum(MaxDivi),sum(MerchantActualAmount) from ConsumerOrders where Id in (" + ids.TrimEnd(',') + ") group by UserId,MerchantId,SnNo,PayMode,IsAct,DATE_FORMAT(CreateDate,'%Y%m%d')");
+                        if (selfDt.Rows.Count > 0)
+                        {
+                            function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "统计商户和创客交易额日志");
+                            foreach (DataRow selfDr in selfDt.Rows)
                             {
-                                selfStat = db.UserAmountSummary.Add(new UserAmountSummary()
+                                int UserId = int.Parse(selfDr["UserId"].ToString());
+                                KxsMainModels.Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId) ?? new KxsMainModels.Users();
+                                string ParentNav = user.ParentNav;
+                                int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
+                                int PayMode = int.Parse(selfDr["PayMode"].ToString());
+                                int IsAct = int.Parse(selfDr["IsAct"].ToString());
+                                string SnNo = selfDr["SnNo"].ToString();
+                                string TradeDate = selfDr[5].ToString();
+                                decimal TradeAmount = decimal.Parse(selfDr[6].ToString());
+                                int TradeCount = int.Parse(selfDr[7].ToString());
+                                decimal MaxDivi = decimal.Parse(selfDr[8].ToString());
+                                decimal ActualAmount = decimal.Parse(selfDr[9].ToString());
+                                string TradeMonth = TradeDate.Substring(0, 6);
+
+                                //创客交易额
+                                UserAmountSummary selfStat = db.UserAmountSummary.FirstOrDefault(m => m.UserId == UserId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode && m.SeoTitle == "self");
+                                if (selfStat == null)
                                 {
-                                    UserId = UserId,
-                                    TradeMonth = TradeMonth,
-                                    TradeDate = TradeDate,
-                                    PayMode = PayMode,
-                                    IsAct = IsAct,
-                                    SeoTitle = "self",
-                                }).Entity;
-                                db.SaveChanges();
-                            }
-                            selfStat.TotalAmount += TradeAmount;
-                            selfStat.TradeCount += TradeCount;
-                            ParentNav += "," + UserId + ",";
-                            if (!string.IsNullOrEmpty(ParentNav))
-                            {
-                                string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
-                                foreach (string NavUserIdString in ParentNavList)
+                                    selfStat = db.UserAmountSummary.Add(new UserAmountSummary()
+                                    {
+                                        UserId = UserId,
+                                        TradeMonth = TradeMonth,
+                                        TradeDate = TradeDate,
+                                        PayMode = PayMode,
+                                        IsAct = IsAct,
+                                        SeoTitle = "self",
+                                    }).Entity;
+                                    db.SaveChanges();
+                                }
+                                selfStat.TotalAmount += TradeAmount;
+                                selfStat.TradeCount += TradeCount;
+                                ParentNav += "," + UserId + ",";
+                                if (!string.IsNullOrEmpty(ParentNav))
                                 {
-                                    int NavUserId = int.Parse(NavUserIdString);
-                                    UserAmountSummary teamStat = db.UserAmountSummary.FirstOrDefault(m => m.UserId == NavUserId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode && m.SeoTitle == "team");
-                                    if (teamStat == null)
+                                    string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
+                                    foreach (string NavUserIdString in ParentNavList)
                                     {
-                                        teamStat = db.UserAmountSummary.Add(new UserAmountSummary()
+                                        int NavUserId = int.Parse(NavUserIdString);
+                                        UserAmountSummary teamStat = db.UserAmountSummary.FirstOrDefault(m => m.UserId == NavUserId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode && m.SeoTitle == "team");
+                                        if (teamStat == null)
                                         {
-                                            UserId = NavUserId,
-                                            TradeMonth = TradeMonth,
-                                            TradeDate = TradeDate,
-                                            PayMode = PayMode,
-                                            IsAct = IsAct,
-                                            SeoTitle = "team",
-                                        }).Entity;
-                                        db.SaveChanges();
+                                            teamStat = db.UserAmountSummary.Add(new UserAmountSummary()
+                                            {
+                                                UserId = NavUserId,
+                                                TradeMonth = TradeMonth,
+                                                TradeDate = TradeDate,
+                                                PayMode = PayMode,
+                                                IsAct = IsAct,
+                                                SeoTitle = "team",
+                                            }).Entity;
+                                            db.SaveChanges();
+                                        }
+                                        teamStat.TotalAmount += TradeAmount;
+                                        teamStat.TradeCount += TradeCount;
                                     }
-                                    teamStat.TotalAmount += TradeAmount;
-                                    teamStat.TradeCount += TradeCount;
                                 }
-                            }
-                            db.SaveChanges();
+                                db.SaveChanges();
 
-                            //商户交易额
-                            MerchantAmountSummay merchantStat = db.MerchantAmountSummay.FirstOrDefault(m => m.MerchantId == MerchantId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode);
-                            if (merchantStat == null)
-                            {
-                                merchantStat = db.MerchantAmountSummay.Add(new MerchantAmountSummay()
+                                //商户交易额
+                                MerchantAmountSummay merchantStat = db.MerchantAmountSummay.FirstOrDefault(m => m.MerchantId == MerchantId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode);
+                                if (merchantStat == null)
                                 {
-                                    MerchantId = MerchantId,
-                                    TradeMonth = TradeMonth,
-                                    TradeDate = TradeDate,
-                                    PayMode = PayMode,
-                                    IsAct = IsAct,
-                                }).Entity;
-                                db.SaveChanges();
-                            }
-                            merchantStat.TradeAmount += TradeAmount;
-                            merchantStat.TradeCount += TradeCount;
-                            merchantStat.TotalActual += ActualAmount;
+                                    merchantStat = db.MerchantAmountSummay.Add(new MerchantAmountSummay()
+                                    {
+                                        MerchantId = MerchantId,
+                                        TradeMonth = TradeMonth,
+                                        TradeDate = TradeDate,
+                                        PayMode = PayMode,
+                                        IsAct = IsAct,
+                                    }).Entity;
+                                    db.SaveChanges();
+                                }
+                                merchantStat.TradeAmount += TradeAmount;
+                                merchantStat.TradeCount += TradeCount;
+                                merchantStat.TotalActual += ActualAmount;
 
-                            MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
-                            if(merchant != null)
-                            {
-                                merchant.TotalAmount += TradeAmount;
-                                merchant.TotalConsumeCount += TradeCount;
-                                merchant.TotalActual += ActualAmount;
-                                merchant.ActMaxAmount += MaxDivi; //活动应返金额
-                                if(IsAct == 1)
+                                MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
+                                if(merchant != null)
                                 {
-                                    merchant.TotalActAmount += TradeAmount;
-                                    merchant.TotalActActual += ActualAmount;
-                                    if(merchant.ActivationStatus == 0 && merchant.TotalActAmount >= 10000M)
+                                    merchant.TotalAmount += TradeAmount;
+                                    merchant.TotalConsumeCount += TradeCount;
+                                    merchant.TotalActual += ActualAmount;
+                                    merchant.ActMaxAmount += MaxDivi; //活动应返金额
+                                    if(IsAct == 1)
                                     {
-                                        // RedisDbconn.Instance.AddList("ActProfitQueue", MerchantId.ToString());
-                                        merchant.ActivationStatus = 1;
-                                        merchant.ActivationDate = DateTime.Now;
+                                        merchant.TotalActAmount += TradeAmount;
+                                        merchant.TotalActActual += ActualAmount;
+                                        if(merchant.ActivationStatus == 0 && merchant.TotalActAmount >= 10000M)
+                                        {
+                                            // RedisDbconn.Instance.AddList("ActProfitQueue", MerchantId.ToString());
+                                            merchant.ActivationStatus = 1;
+                                            merchant.ActivationDate = DateTime.Now;
+                                        }
                                     }
                                 }
-                            }
-                            db.SaveChanges();
+                                db.SaveChanges();
 
-                            //码牌交易额
-                            QrCodeAmountSummay qrcode = db.QrCodeAmountSummay.FirstOrDefault(m => m.SnNo == SnNo && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode);
-                            if (qrcode == null)
-                            {
-                                qrcode = db.QrCodeAmountSummay.Add(new QrCodeAmountSummay()
+                                //码牌交易额
+                                QrCodeAmountSummay qrcode = db.QrCodeAmountSummay.FirstOrDefault(m => m.SnNo == SnNo && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode);
+                                if (qrcode == null)
                                 {
-                                    SnNo = SnNo,
-                                    TradeMonth = TradeMonth,
-                                    TradeDate = TradeDate,
-                                    PayMode = PayMode,
-                                    IsAct = IsAct,
-                                }).Entity;
+                                    qrcode = db.QrCodeAmountSummay.Add(new QrCodeAmountSummay()
+                                    {
+                                        SnNo = SnNo,
+                                        TradeMonth = TradeMonth,
+                                        TradeDate = TradeDate,
+                                        PayMode = PayMode,
+                                        IsAct = IsAct,
+                                    }).Entity;
+                                    db.SaveChanges();
+                                }
+                                qrcode.TradeAmount += TradeAmount;
+                                qrcode.TradeCount += TradeCount;
+                                qrcode.TotalActual += ActualAmount;
                                 db.SaveChanges();
-                            }
-                            qrcode.TradeAmount += TradeAmount;
-                            qrcode.TradeCount += TradeCount;
-                            qrcode.TotalActual += ActualAmount;
-                            db.SaveChanges();
 
-                            string jsonString = "{\"TradeDate\":\"" + TradeDate + "\",\"PayMoney\":\"" + TradeAmount + "\",\"TradeCount\":\"" + TradeCount + "\",\"MerchantActualAmount\":\"" + ActualAmount + "\",\"MerchantId\":\"" + MerchantId + "\",\"PayMode\":\"" + PayMode + "\",\"IsAct\":\"" + IsAct + "\"}";
-                            RedisDbconn.Instance.AddList("StatMerchantTradeSummaryQueue", jsonString);
+                                string jsonString = "{\"TradeDate\":\"" + TradeDate + "\",\"PayMoney\":\"" + TradeAmount + "\",\"TradeCount\":\"" + TradeCount + "\",\"MerchantActualAmount\":\"" + ActualAmount + "\",\"MerchantId\":\"" + MerchantId + "\",\"PayMode\":\"" + PayMode + "\",\"IsAct\":\"" + IsAct + "\"}";
+                                RedisDbconn.Instance.AddList("StatMerchantTradeSummaryQueue", jsonString);
+                            }
+                            OtherMySqlConn.op("update ConsumerOrders set Version=1 where Id in (" + ids.TrimEnd(',') + ")");
                         }
-                        OtherMySqlConn.op("update ConsumerOrders set Version=1 where Id in (" + ids.TrimEnd(',') + ")");
                     }
+                    tran.Commit();
+                }
+                catch (Exception ex)
+                {
+                    tran.Rollback();
+                    function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计商户和创客交易额异常");
                 }
-            }
-            catch (Exception ex)
-            {
-                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计商户和创客交易额异常");
             }
             db.Dispose();
             maindb.Dispose();

+ 124 - 119
AppStart/Helper/StatService2.cs

@@ -32,7 +32,7 @@ namespace MySystem
             while (true)
             {
                 StatTradeAmount();
-                Thread.Sleep(30000);
+                Thread.Sleep(10000);
             }
         }
         public void StatTradeAmount()
@@ -40,149 +40,154 @@ namespace MySystem
             function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "统计商户和创客交易额日志");
             WebCMSEntities db = new WebCMSEntities();
             KxsMainModels.WebCMSEntities maindb = new KxsMainModels.WebCMSEntities();
-            try
+            using (var tran = db.Database.BeginTransaction())
             {
-                string start = DateTime.Now.AddDays(-100).ToString("yyyy-MM-dd HH:mm:ss");
-                DataTable idsDt = CustomerSqlConn.dtable("select Id from ConsumerOrders where CreateDate>='" + start + "' and Status>0 and Version=0 order by Id limit 50", AppConfig.Base.SqlConn2);
-                if(idsDt.Rows.Count > 0)
+                try
                 {
-                    string ids = "";
-                    foreach (DataRow idsDr in idsDt.Rows)
+                    string start = DateTime.Now.AddDays(-100).ToString("yyyy-MM-dd HH:mm:ss");
+                    DataTable idsDt = CustomerSqlConn.dtable("select Id from ConsumerOrders where CreateDate>='" + start + "' and Status>0 and Version=0 order by Id limit 20", AppConfig.Base.SqlConn2);
+                    if(idsDt.Rows.Count > 0)
                     {
-                        ids += idsDr["Id"].ToString() + ",";
-                    }
-                    DataTable selfDt = CustomerSqlConn.dtable("select UserId,MerchantId,SnNo,PayMode,IsAct,DATE_FORMAT(CreateDate,'%Y%m%d'),sum(PayMoney),count(Id),sum(MaxDivi),sum(MerchantActualAmount) from ConsumerOrders where Id in (" + ids.TrimEnd(',') + ") group by UserId,MerchantId,SnNo,PayMode,IsAct,DATE_FORMAT(CreateDate,'%Y%m%d')", AppConfig.Base.SqlConn2);
-                    if (selfDt.Rows.Count > 0)
-                    {
-                        function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "统计商户和创客交易额日志");
-                        foreach (DataRow selfDr in selfDt.Rows)
+                        string ids = "";
+                        foreach (DataRow idsDr in idsDt.Rows)
                         {
-                            int UserId = int.Parse(selfDr["UserId"].ToString());
-                            KxsMainModels.Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId) ?? new KxsMainModels.Users();
-                            string ParentNav = user.ParentNav;
-                            int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
-                            int PayMode = int.Parse(selfDr["PayMode"].ToString());
-                            int IsAct = int.Parse(selfDr["IsAct"].ToString());
-                            string SnNo = selfDr["SnNo"].ToString();
-                            string TradeDate = selfDr[5].ToString();
-                            decimal TradeAmount = decimal.Parse(selfDr[6].ToString());
-                            int TradeCount = int.Parse(selfDr[7].ToString());
-                            decimal MaxDivi = decimal.Parse(selfDr[8].ToString());
-                            decimal ActualAmount = decimal.Parse(selfDr[9].ToString());
-                            string TradeMonth = TradeDate.Substring(0, 6);
-
-                            //创客交易额
-                            UserAmountSummary selfStat = db.UserAmountSummary.FirstOrDefault(m => m.UserId == UserId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode && m.SeoTitle == "self");
-                            if (selfStat == null)
+                            ids += idsDr["Id"].ToString() + ",";
+                        }
+                        DataTable selfDt = CustomerSqlConn.dtable("select UserId,MerchantId,SnNo,PayMode,IsAct,DATE_FORMAT(CreateDate,'%Y%m%d'),sum(PayMoney),count(Id),sum(MaxDivi),sum(MerchantActualAmount) from ConsumerOrders where Id in (" + ids.TrimEnd(',') + ") group by UserId,MerchantId,SnNo,PayMode,IsAct,DATE_FORMAT(CreateDate,'%Y%m%d')", AppConfig.Base.SqlConn2);
+                        if (selfDt.Rows.Count > 0)
+                        {
+                            function.WriteLog("统计人数:" + selfDt.Rows.Count + "\n\n", "统计商户和创客交易额日志");
+                            foreach (DataRow selfDr in selfDt.Rows)
                             {
-                                selfStat = db.UserAmountSummary.Add(new UserAmountSummary()
+                                int UserId = int.Parse(selfDr["UserId"].ToString());
+                                KxsMainModels.Users user = maindb.Users.FirstOrDefault(m => m.Id == UserId) ?? new KxsMainModels.Users();
+                                string ParentNav = user.ParentNav;
+                                int MerchantId = int.Parse(selfDr["MerchantId"].ToString());
+                                int PayMode = int.Parse(selfDr["PayMode"].ToString());
+                                int IsAct = int.Parse(selfDr["IsAct"].ToString());
+                                string SnNo = selfDr["SnNo"].ToString();
+                                string TradeDate = selfDr[5].ToString();
+                                decimal TradeAmount = decimal.Parse(selfDr[6].ToString());
+                                int TradeCount = int.Parse(selfDr[7].ToString());
+                                decimal MaxDivi = decimal.Parse(selfDr[8].ToString());
+                                decimal ActualAmount = decimal.Parse(selfDr[9].ToString());
+                                string TradeMonth = TradeDate.Substring(0, 6);
+
+                                //创客交易额
+                                UserAmountSummary selfStat = db.UserAmountSummary.FirstOrDefault(m => m.UserId == UserId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode && m.SeoTitle == "self");
+                                if (selfStat == null)
                                 {
-                                    UserId = UserId,
-                                    TradeMonth = TradeMonth,
-                                    TradeDate = TradeDate,
-                                    PayMode = PayMode,
-                                    IsAct = IsAct,
-                                    SeoTitle = "self",
-                                }).Entity;
-                                db.SaveChanges();
-                            }
-                            selfStat.TotalAmount += TradeAmount;
-                            selfStat.TradeCount += TradeCount;
-                            ParentNav += "," + UserId + ",";
-                            if (!string.IsNullOrEmpty(ParentNav))
-                            {
-                                string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
-                                foreach (string NavUserIdString in ParentNavList)
+                                    selfStat = db.UserAmountSummary.Add(new UserAmountSummary()
+                                    {
+                                        UserId = UserId,
+                                        TradeMonth = TradeMonth,
+                                        TradeDate = TradeDate,
+                                        PayMode = PayMode,
+                                        IsAct = IsAct,
+                                        SeoTitle = "self",
+                                    }).Entity;
+                                    db.SaveChanges();
+                                }
+                                selfStat.TotalAmount += TradeAmount;
+                                selfStat.TradeCount += TradeCount;
+                                ParentNav += "," + UserId + ",";
+                                if (!string.IsNullOrEmpty(ParentNav))
                                 {
-                                    int NavUserId = int.Parse(NavUserIdString);
-                                    UserAmountSummary teamStat = db.UserAmountSummary.FirstOrDefault(m => m.UserId == NavUserId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode && m.SeoTitle == "team");
-                                    if (teamStat == null)
+                                    string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
+                                    foreach (string NavUserIdString in ParentNavList)
                                     {
-                                        teamStat = db.UserAmountSummary.Add(new UserAmountSummary()
+                                        int NavUserId = int.Parse(NavUserIdString);
+                                        UserAmountSummary teamStat = db.UserAmountSummary.FirstOrDefault(m => m.UserId == NavUserId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode && m.SeoTitle == "team");
+                                        if (teamStat == null)
                                         {
-                                            UserId = NavUserId,
-                                            TradeMonth = TradeMonth,
-                                            TradeDate = TradeDate,
-                                            PayMode = PayMode,
-                                            IsAct = IsAct,
-                                            SeoTitle = "team",
-                                        }).Entity;
-                                        db.SaveChanges();
+                                            teamStat = db.UserAmountSummary.Add(new UserAmountSummary()
+                                            {
+                                                UserId = NavUserId,
+                                                TradeMonth = TradeMonth,
+                                                TradeDate = TradeDate,
+                                                PayMode = PayMode,
+                                                IsAct = IsAct,
+                                                SeoTitle = "team",
+                                            }).Entity;
+                                            db.SaveChanges();
+                                        }
+                                        teamStat.TotalAmount += TradeAmount;
+                                        teamStat.TradeCount += TradeCount;
                                     }
-                                    teamStat.TotalAmount += TradeAmount;
-                                    teamStat.TradeCount += TradeCount;
                                 }
-                            }
-                            db.SaveChanges();
+                                db.SaveChanges();
 
-                            //商户交易额
-                            MerchantAmountSummay merchantStat = db.MerchantAmountSummay.FirstOrDefault(m => m.MerchantId == MerchantId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode);
-                            if (merchantStat == null)
-                            {
-                                merchantStat = db.MerchantAmountSummay.Add(new MerchantAmountSummay()
+                                //商户交易额
+                                MerchantAmountSummay merchantStat = db.MerchantAmountSummay.FirstOrDefault(m => m.MerchantId == MerchantId && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode);
+                                if (merchantStat == null)
                                 {
-                                    MerchantId = MerchantId,
-                                    TradeMonth = TradeMonth,
-                                    TradeDate = TradeDate,
-                                    PayMode = PayMode,
-                                    IsAct = IsAct,
-                                }).Entity;
-                                db.SaveChanges();
-                            }
-                            merchantStat.TradeAmount += TradeAmount;
-                            merchantStat.TradeCount += TradeCount;
-                            merchantStat.TotalActual += ActualAmount;
+                                    merchantStat = db.MerchantAmountSummay.Add(new MerchantAmountSummay()
+                                    {
+                                        MerchantId = MerchantId,
+                                        TradeMonth = TradeMonth,
+                                        TradeDate = TradeDate,
+                                        PayMode = PayMode,
+                                        IsAct = IsAct,
+                                    }).Entity;
+                                    db.SaveChanges();
+                                }
+                                merchantStat.TradeAmount += TradeAmount;
+                                merchantStat.TradeCount += TradeCount;
+                                merchantStat.TotalActual += ActualAmount;
 
-                            MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
-                            if(merchant != null)
-                            {
-                                merchant.TotalAmount += TradeAmount;
-                                merchant.TotalConsumeCount += TradeCount;
-                                merchant.TotalActual += ActualAmount;
-                                merchant.ActMaxAmount += MaxDivi; //活动应返金额
-                                if(IsAct == 1)
+                                MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
+                                if(merchant != null)
                                 {
-                                    merchant.TotalActAmount += TradeAmount;
-                                    merchant.TotalActActual += ActualAmount;
-                                    if(merchant.ActivationStatus == 0 && merchant.TotalActAmount >= 10000M)
+                                    merchant.TotalAmount += TradeAmount;
+                                    merchant.TotalConsumeCount += TradeCount;
+                                    merchant.TotalActual += ActualAmount;
+                                    merchant.ActMaxAmount += MaxDivi; //活动应返金额
+                                    if(IsAct == 1)
                                     {
-                                        // RedisDbconn.Instance.AddList("ActProfitQueue", MerchantId.ToString());
-                                        merchant.ActivationStatus = 1;
-                                        merchant.ActivationDate = DateTime.Now;
+                                        merchant.TotalActAmount += TradeAmount;
+                                        merchant.TotalActActual += ActualAmount;
+                                        if(merchant.ActivationStatus == 0 && merchant.TotalActAmount >= 10000M)
+                                        {
+                                            // RedisDbconn.Instance.AddList("ActProfitQueue", MerchantId.ToString());
+                                            merchant.ActivationStatus = 1;
+                                            merchant.ActivationDate = DateTime.Now;
+                                        }
                                     }
                                 }
-                            }
-                            db.SaveChanges();
+                                db.SaveChanges();
 
-                            //码牌交易额
-                            QrCodeAmountSummay qrcode = db.QrCodeAmountSummay.FirstOrDefault(m => m.SnNo == SnNo && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode);
-                            if (qrcode == null)
-                            {
-                                qrcode = db.QrCodeAmountSummay.Add(new QrCodeAmountSummay()
+                                //码牌交易额
+                                QrCodeAmountSummay qrcode = db.QrCodeAmountSummay.FirstOrDefault(m => m.SnNo == SnNo && m.IsAct == IsAct && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.PayMode == PayMode);
+                                if (qrcode == null)
                                 {
-                                    SnNo = SnNo,
-                                    TradeMonth = TradeMonth,
-                                    TradeDate = TradeDate,
-                                    PayMode = PayMode,
-                                    IsAct = IsAct,
-                                }).Entity;
+                                    qrcode = db.QrCodeAmountSummay.Add(new QrCodeAmountSummay()
+                                    {
+                                        SnNo = SnNo,
+                                        TradeMonth = TradeMonth,
+                                        TradeDate = TradeDate,
+                                        PayMode = PayMode,
+                                        IsAct = IsAct,
+                                    }).Entity;
+                                    db.SaveChanges();
+                                }
+                                qrcode.TradeAmount += TradeAmount;
+                                qrcode.TradeCount += TradeCount;
+                                qrcode.TotalActual += ActualAmount;
                                 db.SaveChanges();
-                            }
-                            qrcode.TradeAmount += TradeAmount;
-                            qrcode.TradeCount += TradeCount;
-                            qrcode.TotalActual += ActualAmount;
-                            db.SaveChanges();
 
-                            string jsonString = "{\"TradeDate\":\"" + TradeDate + "\",\"PayMoney\":\"" + TradeAmount + "\",\"TradeCount\":\"" + TradeCount + "\",\"MerchantActualAmount\":\"" + ActualAmount + "\",\"MerchantId\":\"" + MerchantId + "\",\"PayMode\":\"" + PayMode + "\",\"IsAct\":\"" + IsAct + "\"}";
-                            RedisDbconn.Instance.AddList("StatMerchantTradeSummaryQueue2", jsonString);
+                                string jsonString = "{\"TradeDate\":\"" + TradeDate + "\",\"PayMoney\":\"" + TradeAmount + "\",\"TradeCount\":\"" + TradeCount + "\",\"MerchantActualAmount\":\"" + ActualAmount + "\",\"MerchantId\":\"" + MerchantId + "\",\"PayMode\":\"" + PayMode + "\",\"IsAct\":\"" + IsAct + "\"}";
+                                RedisDbconn.Instance.AddList("StatMerchantTradeSummaryQueue2", jsonString);
+                            }
+                            CustomerSqlConn.op("update ConsumerOrders set Version=1 where Id in (" + ids.TrimEnd(',') + ")", AppConfig.Base.SqlConn2);
                         }
-                        CustomerSqlConn.op("update ConsumerOrders set Version=1 where Id in (" + ids.TrimEnd(',') + ")", AppConfig.Base.SqlConn2);
                     }
+                    tran.Commit();
+                }
+                catch (Exception ex)
+                {
+                    tran.Rollback();
+                    function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计商户和创客交易额异常");
                 }
-            }
-            catch (Exception ex)
-            {
-                function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "统计商户和创客交易额异常");
             }
             db.Dispose();
             maindb.Dispose();