TradeStatTimer2.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 TradeStatTimer2
  12. {
  13. public readonly static TradeStatTimer2 Instance = new TradeStatTimer2();
  14. private TradeStatTimer2()
  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>("TradeStatQueueTest");
  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. if (UserId == 9)
  34. {
  35. string ss = "123";
  36. }
  37. int BrandId = int.Parse(jsonObj["BrandId"].ToString()); //品牌
  38. int BankCardType = int.Parse(jsonObj["BankCardType"].ToString()); //卡类型
  39. int MerchantId = int.Parse(jsonObj["MerchantId"].ToString()); //商户Id
  40. int QrPayFlag = int.Parse(jsonObj["QrPayFlag"].ToString()); //云闪付
  41. decimal TradeAmount = decimal.Parse(jsonObj["TradeAmount"].ToString()); //当日交易额
  42. string TradeMonth = DateTime.Parse(DateString).ToString("yyyyMM");
  43. string TradeDate = DateTime.Parse(DateString).ToString("yyyyMMdd");
  44. WebCMSEntities db = new WebCMSEntities();
  45. var tran = db.Database.BeginTransaction();
  46. try
  47. {
  48. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == MerchantId) ?? new PosMerchantInfo();
  49. MachineForMerNo machineFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.KqMerNo) ?? new MachineForMerNo();
  50. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineFor.SnId) ?? new PosMachinesTwo();
  51. int MerchantTypeUserId = int.Parse(function.CheckInt(pos.SeoTitle));
  52. if (MerchantTypeUserId > 0)
  53. {
  54. Users merchantUser = db.Users.FirstOrDefault(m => m.Id == MerchantTypeUserId && m.MerchantType == 1);
  55. if (merchantUser != null)
  56. {
  57. UserId = MerchantTypeUserId;
  58. }
  59. }
  60. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  61. if (user != null)
  62. {
  63. if (user.AuthFlag == 1)
  64. {
  65. 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");
  66. if (selfStat == null)
  67. {
  68. selfStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  69. {
  70. UserId = UserId,
  71. TradeMonth = TradeMonth,
  72. TradeDate = TradeDate,
  73. BrandId = BrandId,
  74. QueryCount = QrPayFlag,
  75. Sort = MerchantId,
  76. SeoTitle = "self",
  77. }).Entity;
  78. db.SaveChanges();
  79. }
  80. if (BankCardType == 0)
  81. {
  82. selfStat.DirectDebitTradeAmt += TradeAmount;
  83. }
  84. else if (BankCardType == 1)
  85. {
  86. selfStat.DirectTradeAmt += TradeAmount;
  87. }
  88. db.SaveChanges();
  89. string ParentNav = user.ParentNav;
  90. if (UserId != pos.UserId)
  91. {
  92. user = db.Users.FirstOrDefault(m => m.Id == pos.UserId);
  93. if (user != null)
  94. {
  95. ParentNav = user.ParentNav;
  96. UserId = pos.UserId;
  97. }
  98. }
  99. if (!string.IsNullOrEmpty(ParentNav))
  100. {
  101. ParentNav += "," + UserId + ",";
  102. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  103. foreach (string UserIdString in ParentNavList)
  104. {
  105. int ParentUserId = int.Parse(UserIdString);
  106. 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");
  107. if (teamStat == null)
  108. {
  109. teamStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  110. {
  111. UserId = ParentUserId,
  112. TradeMonth = TradeMonth,
  113. TradeDate = TradeDate,
  114. BrandId = BrandId,
  115. QueryCount = QrPayFlag,
  116. Sort = MerchantId,
  117. SeoTitle = "team",
  118. }).Entity;
  119. db.SaveChanges();
  120. }
  121. if (BankCardType == 0)
  122. {
  123. teamStat.NonDirectDebitTradeAmt += TradeAmount;
  124. }
  125. else if (BankCardType == 1)
  126. {
  127. teamStat.NonDirectTradeAmt += TradeAmount;
  128. }
  129. db.SaveChanges();
  130. }
  131. }
  132. }
  133. }
  134. else
  135. {
  136. int ParentUserId = 1;
  137. 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");
  138. if (teamStat == null)
  139. {
  140. teamStat = db.UserTradeDaySummary.Add(new UserTradeDaySummary()
  141. {
  142. UserId = ParentUserId,
  143. TradeMonth = TradeMonth,
  144. TradeDate = TradeDate,
  145. BrandId = BrandId,
  146. QueryCount = QrPayFlag,
  147. Sort = MerchantId,
  148. SeoTitle = "team",
  149. }).Entity;
  150. db.SaveChanges();
  151. }
  152. if (BankCardType == 0)
  153. {
  154. teamStat.NonDirectDebitTradeAmt += TradeAmount;
  155. }
  156. else if (BankCardType == 1)
  157. {
  158. teamStat.NonDirectTradeAmt += TradeAmount;
  159. }
  160. db.SaveChanges();
  161. }
  162. tran.Commit();
  163. }
  164. catch (Exception ex)
  165. {
  166. function.WriteLog(DateTime.Now.ToString() + "\n" + content + "\n" + ex.ToString(), "统计交易额异常2");
  167. tran.Rollback();
  168. }
  169. tran.Dispose();
  170. db.Dispose();
  171. }
  172. else
  173. {
  174. Thread.Sleep(5000);
  175. }
  176. }
  177. }
  178. }