using System; using System.Collections.Generic; using Library; using LitJson; using System.Linq; using System.Data; using System.Threading; using MySystem.PxcModels; namespace MySystem { public class StatNewService { public readonly static StatNewService Instance = new StatNewService(); private StatNewService() { } // 统计交易额V2 public void StartStat() { Thread th = new Thread(StartStatDo); th.IsBackground = true; th.Start(); } public void StartStatDo() { while (true) { string content = RedisDbconn.Instance.RPop("StatTradeAmountQueue"); if(!string.IsNullOrEmpty(content)) { StatTradeAmount(content, 1); StatTradeAmount(content, 2); } Thread.Sleep(10000); } } public void StatTradeAmount(string content, int Kind) { WebCMSEntities db = new WebCMSEntities(); try { JsonData selfDr = JsonMapper.ToObject(content); int UserId = int.Parse(selfDr["UserId"].ToString()); Users user = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users(); string ParentNav = user.ParentNav; int BrandId = int.Parse(selfDr["BrandId"].ToString()); int BankCardType = int.Parse(selfDr["BankCardType"].ToString()); int QrPayFlag = int.Parse(selfDr["QrPayFlag"].ToString()); int MerHelpFlag = int.Parse(selfDr["MerHelpFlag"].ToString()); int Version = int.Parse(selfDr["Version"].ToString()); int CapFlag = int.Parse(selfDr["CapFlag"].ToString()); int VipFlag = int.Parse(selfDr["VipFlag"].ToString()); int PayType = int.Parse(selfDr["PayType"].ToString()); string TradeDate = selfDr["TradeDate"].ToString(); decimal TradeAmount = decimal.Parse(selfDr["TradeAmount"].ToString()); int TradeCount = int.Parse(selfDr["TradeCount"].ToString()); if(Kind == 2) { TradeDate = TradeDate.Substring(0, 6); } if(string.IsNullOrEmpty(RedisDbconn.Instance.Get("TradeDaySummary" + TradeDate))) { CustomerSqlConn.op(AppConfig.Base.CreateTradeDaySummary.Replace("#DateTime#", TradeDate), AppConfig.Base.StatSqlConn); RedisDbconn.Instance.Set("TradeDaySummary" + TradeDate, "1"); int sec = 3600 * 13; if(Kind == 2) { sec = 3600 * 12 * 35; } RedisDbconn.Instance.SetExpire("TradeDaySummary" + TradeDate, sec); Thread.Sleep(2000); } string Id = "0"; DataTable check = CustomerSqlConn.dtable("select Id from TradeDaySummary" + TradeDate + " where UserId=" + UserId + " and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='self'", AppConfig.Base.StatSqlConn); if(check.Rows.Count < 1) { check = CustomerSqlConn.dtable("insert into TradeDaySummary" + TradeDate + " (UserId,BrandId,QueryCount,VipFlag,PayType,SeoTitle) values (" + UserId + "," + BrandId + "," + QrPayFlag + "," + VipFlag + "," + PayType + ",'self');select @@IDENTITY", AppConfig.Base.StatSqlConn); if(check.Rows.Count > 0) { Id = check.Rows[0][0].ToString(); } } else { Id = check.Rows[0][0].ToString(); } string selfStat = ""; if (BankCardType == 0) { if (Version == 1) { selfStat += "ProfitDirectDebitTradeAmt = ProfitDirectDebitTradeAmt+" + TradeAmount + ","; if (CapFlag == 1) { selfStat += "ProfitDirectDebitCapTradeAmt = ProfitDirectDebitCapTradeAmt+" + TradeAmount + ","; selfStat += "ProfitDirectDebitCapNum = ProfitDirectDebitCapNum+" + TradeCount + ","; } } else if (MerHelpFlag == 1) { selfStat += "HelpDirectDebitTradeAmt = HelpDirectDebitTradeAmt+" + TradeAmount + ","; if (CapFlag == 1) { selfStat += "HelpDirectDebitCapTradeAmt = HelpDirectDebitCapTradeAmt+" + TradeAmount + ","; selfStat += "HelpDirectDebitCapNum = HelpDirectDebitCapNum+" + TradeCount + ","; } } else { selfStat += "NotHelpDirectDebitTradeAmt = NotHelpDirectDebitTradeAmt+" + TradeAmount + ","; if (CapFlag == 1) { selfStat += "NotHelpDirectDebitCapTradeAmt = NotHelpDirectDebitCapTradeAmt+" + TradeAmount + ","; selfStat += "NotHelpDirectDebitCapNum = NotHelpDirectDebitCapNum+" + TradeCount + ","; } } } else if (BankCardType != 0) { if (Version == 1) { selfStat += "ProfitDirectTradeAmt = ProfitDirectTradeAmt+" + TradeAmount + ","; } else if (MerHelpFlag == 1) { selfStat += "HelpDirectTradeAmt = HelpDirectTradeAmt+" + TradeAmount + ","; } else { selfStat += "NotHelpDirectTradeAmt = NotHelpDirectTradeAmt+" + TradeAmount + ","; } } CustomerSqlConn.op("update TradeDaySummary" + TradeDate + " set " + selfStat.TrimEnd(',') + " where Id=" + Id, AppConfig.Base.StatSqlConn); ParentNav += "," + UserId + ","; if (!string.IsNullOrEmpty(ParentNav)) { string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(','); foreach (string NavUserIdString in ParentNavList) { int NavUserId = int.Parse(NavUserIdString); string PId = "0"; DataTable pcheck = CustomerSqlConn.dtable("select Id from TradeDaySummary" + TradeDate + " where UserId=" + NavUserId + " and BrandId=" + BrandId + " and QueryCount=" + QrPayFlag + " and VipFlag=" + VipFlag + " and PayType=" + PayType + " and SeoTitle='team'", AppConfig.Base.StatSqlConn); if(pcheck.Rows.Count < 1) { pcheck = CustomerSqlConn.dtable("insert into TradeDaySummary" + TradeDate + " (UserId,BrandId,QueryCount,VipFlag,PayType,SeoTitle) values (" + NavUserId + "," + BrandId + "," + QrPayFlag + "," + VipFlag + "," + PayType + ",'team');select @@IDENTITY", AppConfig.Base.StatSqlConn); if(pcheck.Rows.Count > 0) { PId = pcheck.Rows[0][0].ToString(); } } else { PId = pcheck.Rows[0][0].ToString(); } string teamStat = ""; if (BankCardType == 0) { if (Version == 1) { teamStat += "ProfitNonDirectDebitTradeAmt = ProfitNonDirectDebitTradeAmt+" + TradeAmount + ","; if (CapFlag == 1) { teamStat += "ProfitDirectDebitCapTradeAmt = ProfitDirectDebitCapTradeAmt+" + TradeAmount + ","; teamStat += "ProfitDirectDebitCapNum = ProfitDirectDebitCapNum+" + TradeCount + ","; } } else if (MerHelpFlag == 1) { teamStat += "HelpNonDirectDebitTradeAmt = HelpNonDirectDebitTradeAmt+" + TradeAmount + ","; if (CapFlag == 1) { teamStat += "HelpDirectDebitCapTradeAmt = HelpDirectDebitCapTradeAmt+" + TradeAmount + ","; teamStat += "HelpDirectDebitCapNum = HelpDirectDebitCapNum+" + TradeCount + ","; } } else { teamStat += "NotHelpNonDirectDebitTradeAmt = NotHelpNonDirectDebitTradeAmt+" + TradeAmount + ","; if (CapFlag == 1) { teamStat += "NotHelpDirectDebitCapTradeAmt = NotHelpDirectDebitCapTradeAmt+" + TradeAmount + ","; teamStat += "NotHelpDirectDebitCapNum = NotHelpDirectDebitCapNum+" + TradeCount + ","; } } } else if (BankCardType != 0) { if (Version == 1) { teamStat += "ProfitNonDirectTradeAmt = ProfitNonDirectTradeAmt+" + TradeAmount + ","; } else if (MerHelpFlag == 1) { teamStat += "HelpNonDirectTradeAmt = HelpNonDirectTradeAmt+" + TradeAmount + ","; } else { teamStat += "NotHelpNonDirectTradeAmt = NotHelpNonDirectTradeAmt+" + TradeAmount + ","; } } CustomerSqlConn.op("update TradeDaySummary" + TradeDate + " set " + teamStat.TrimEnd(',') + " where Id=" + PId, AppConfig.Base.StatSqlConn); } } db.SaveChanges(); } catch (Exception ex) { function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "实时统计交易额日志RDS异常"); } db.Dispose(); function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "实时统计交易额日志RDS"); } } }