TradeStatTimer.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using System.Linq;
  6. using Microsoft.Extensions.Hosting;
  7. using MySystem;
  8. using MySystem.PxcModels;
  9. using LitJson;
  10. using Library;
  11. public class TradeStatTimer
  12. {
  13. public readonly static TradeStatTimer Instance = new TradeStatTimer();
  14. private TradeStatTimer()
  15. {
  16. }
  17. public void Start()
  18. {
  19. Thread th = new Thread(DoWorks);
  20. th.IsBackground = true;
  21. th.Start();
  22. }
  23. private void DoWorks()
  24. {
  25. while (true)
  26. {
  27. string content = RedisDbconn.Instance.RPop<string>("TradeStatQueue");
  28. if (!string.IsNullOrEmpty(content))
  29. {
  30. JsonData jsonObj = JsonMapper.ToObject(content);
  31. string DateString = jsonObj["DateString"].ToString();
  32. int UserId = int.Parse(jsonObj["UserId"].ToString()); //创客ID
  33. int BrandId = int.Parse(jsonObj["BrandId"].ToString()); //品牌
  34. int BankCardType = int.Parse(jsonObj["BankCardType"].ToString()); //卡类型
  35. int MerchantId = int.Parse(jsonObj["MerchantId"].ToString()); //商户Id
  36. int QrPayFlag = int.Parse(jsonObj["QrPayFlag"].ToString()); //云闪付
  37. decimal TradeAmount = decimal.Parse(jsonObj["TradeAmount"].ToString()); //当日交易额
  38. string TradeMonth = DateTime.Parse(DateString).ToString("yyyyMM");
  39. string TradeDate = DateTime.Parse(DateString).ToString("yyyyMMdd");
  40. WebCMSEntities db = new WebCMSEntities();
  41. var tran = db.Database.BeginTransaction();
  42. try
  43. {
  44. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == MerchantId) ?? new PosMerchantInfo();
  45. MachineForMerNo machineFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.KqMerNo) ?? new MachineForMerNo();
  46. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineFor.SnId) ?? new PosMachinesTwo();
  47. int MerchantTypeUserId = int.Parse(function.CheckInt(pos.SeoTitle));
  48. if (MerchantTypeUserId > 0)
  49. {
  50. Users merchantUser = db.Users.FirstOrDefault(m => m.Id == MerchantTypeUserId && m.MerchantType == 1);
  51. if (merchantUser != null)
  52. {
  53. UserId = MerchantTypeUserId;
  54. }
  55. }
  56. RedisDbconn.Instance.AddNumber("MerTotalAmount:" + MerchantId, TradeAmount);
  57. RedisDbconn.Instance.AddNumber("MerTotalAmount:" + MerchantId + ":" + TradeMonth, TradeAmount); //商户当月交易
  58. RedisDbconn.Instance.AddNumber("MerTotalAmount:" + MerchantId + ":" + TradeDate, TradeAmount); //商户当日交易
  59. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  60. if (user != null)
  61. {
  62. if (user.AuthFlag == 1)
  63. {
  64. // UserTradeDaySummary selfStat = db.UserTradeDaySummary.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.Sort == MerchantId && m.SeoTitle == "self");
  65. // if (selfStat == null)
  66. // {
  67. // selfStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  68. // {
  69. // UserId = UserId,
  70. // TradeMonth = TradeMonth,
  71. // TradeDate = TradeDate,
  72. // BrandId = BrandId,
  73. // QueryCount = QrPayFlag,
  74. // Sort = MerchantId,
  75. // SeoTitle = "self",
  76. // }).Entity;
  77. // db.SaveChanges();
  78. // }
  79. // if (BankCardType == 0)
  80. // {
  81. // selfStat.DirectDebitTradeAmt += TradeAmount;
  82. // }
  83. // else if (BankCardType == 1)
  84. // {
  85. // selfStat.DirectTradeAmt += TradeAmount;
  86. // }
  87. // db.SaveChanges();
  88. RedisDbconn.Instance.AddNumber("TotalAmount:" + UserId, TradeAmount); //总交易
  89. RedisDbconn.Instance.AddNumber("TotalAmount:" + UserId + ":" + TradeDate, TradeAmount); //总交易
  90. RedisDbconn.Instance.AddNumber("TotalAmount:" + UserId + ":" + TradeMonth, TradeAmount); //总交易
  91. RedisDbconn.Instance.AddNumber("TotalAmount:" + UserId + ":" + BrandId + ":" + TradeDate, TradeAmount); //总交易
  92. RedisDbconn.Instance.AddNumber("TotalAmount:" + UserId + ":" + BrandId + ":" + TradeMonth, TradeAmount); //总交易
  93. if (QrPayFlag == 1)
  94. {
  95. RedisDbconn.Instance.AddNumber("TotalCloudPayAmount:" + UserId + ":" + TradeDate, TradeAmount); //云闪付小额交易额
  96. RedisDbconn.Instance.AddNumber("TotalCloudPayAmount:" + UserId + ":" + TradeMonth, TradeAmount); //云闪付小额交易额
  97. }
  98. else
  99. {
  100. RedisDbconn.Instance.AddNumber("TotalPosAmount:" + UserId + ":" + TradeDate, TradeAmount); //POS机刷卡交易额
  101. RedisDbconn.Instance.AddNumber("TotalPosAmount:" + UserId + ":" + TradeMonth, TradeAmount); //POS机刷卡交易额
  102. }
  103. string ParentNav = user.ParentNav;
  104. if (UserId != pos.UserId)
  105. {
  106. user = db.Users.FirstOrDefault(m => m.Id == pos.UserId);
  107. if (user != null)
  108. {
  109. ParentNav = user.ParentNav;
  110. UserId = pos.UserId;
  111. }
  112. }
  113. if (!string.IsNullOrEmpty(ParentNav))
  114. {
  115. ParentNav += "," + UserId + ",";
  116. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  117. foreach (string UserIdString in ParentNavList)
  118. {
  119. int ParentUserId = int.Parse(UserIdString);
  120. // UserTradeDaySummary teamStat = db.UserTradeDaySummary.FirstOrDefault(m => m.UserId == ParentUserId && m.TradeMonth == TradeMonth && m.TradeDate == TradeDate && m.BrandId == BrandId && m.QueryCount == QrPayFlag && m.Sort == MerchantId && m.SeoTitle == "team");
  121. // if (teamStat == null)
  122. // {
  123. // teamStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  124. // {
  125. // UserId = ParentUserId,
  126. // TradeMonth = TradeMonth,
  127. // TradeDate = TradeDate,
  128. // BrandId = BrandId,
  129. // QueryCount = QrPayFlag,
  130. // Sort = MerchantId,
  131. // SeoTitle = "team",
  132. // }).Entity;
  133. // db.SaveChanges();
  134. // }
  135. // if (BankCardType == 0)
  136. // {
  137. // teamStat.NonDirectDebitTradeAmt += TradeAmount;
  138. // }
  139. // else if (BankCardType == 1)
  140. // {
  141. // teamStat.NonDirectTradeAmt += TradeAmount;
  142. // }
  143. // db.SaveChanges();
  144. RedisDbconn.Instance.AddNumber("TeamTotalAmount:" + ParentUserId, TradeAmount); //总交易
  145. RedisDbconn.Instance.AddNumber("TeamTotalAmount:" + ParentUserId + ":" + TradeDate, TradeAmount); //总交易
  146. RedisDbconn.Instance.AddNumber("TeamTotalAmount:" + ParentUserId + ":" + TradeMonth, TradeAmount); //总交易
  147. RedisDbconn.Instance.AddNumber("TeamTotalAmount:" + ParentUserId + ":" + BrandId + ":" + TradeDate, TradeAmount); //总交易
  148. RedisDbconn.Instance.AddNumber("TeamTotalAmount:" + ParentUserId + ":" + BrandId + ":" + TradeMonth, TradeAmount); //总交易
  149. if (QrPayFlag == 1)
  150. {
  151. RedisDbconn.Instance.AddNumber("TeamTotalCloudPayAmount:" + ParentUserId + ":" + TradeDate, TradeAmount); //云闪付小额交易额
  152. RedisDbconn.Instance.AddNumber("TeamTotalCloudPayAmount:" + ParentUserId + ":" + TradeMonth, TradeAmount); //云闪付小额交易额
  153. }
  154. else
  155. {
  156. RedisDbconn.Instance.AddNumber("TeamTotalPosAmount:" + ParentUserId + ":" + TradeDate, TradeAmount); //POS机刷卡交易额
  157. RedisDbconn.Instance.AddNumber("TeamTotalPosAmount:" + ParentUserId + ":" + TradeMonth, TradeAmount); //POS机刷卡交易额
  158. }
  159. }
  160. }
  161. }
  162. }
  163. tran.Commit();
  164. }
  165. catch (Exception ex)
  166. {
  167. function.WriteLog(DateTime.Now.ToString() + "\n" + content + "\n" + ex.ToString(), "统计交易额异常");
  168. tran.Rollback();
  169. }
  170. tran.Dispose();
  171. db.Dispose();
  172. }
  173. else
  174. {
  175. Thread.Sleep(900000);
  176. }
  177. }
  178. }
  179. }