RecommendActStatService.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using System.Data;
  7. using System.Threading;
  8. using MySystem.PxcModels;
  9. namespace MySystem
  10. {
  11. public class RecommendActStatService
  12. {
  13. public readonly static RecommendActStatService Instance = new RecommendActStatService();
  14. private RecommendActStatService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(dosomething);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. // 统计数据条件
  23. // 1.下单成功统计当前直推并未开机的创客表数据
  24. // 2.推荐创客认证成功,统计直推创客表数据
  25. // 3.机具激活,统计激活机具交易表
  26. // 4.交易,统计激活机具交易表
  27. public void dosomething()
  28. {
  29. while (true)
  30. {
  31. string data = RedisDbconn.Instance.RPop<string>("RecommendActStatQueue");
  32. if (!string.IsNullOrEmpty(data) && DateTime.Now < DateTime.Parse("2023-10-01 00:00:00"))
  33. {
  34. try
  35. {
  36. JsonData jsonObj = JsonMapper.ToObject(data);
  37. string Kind = jsonObj["Kind"].ToString(); //数据类型:1-购买推荐王订单,2-用户认证,3-机具激活,4-机具交易
  38. JsonData jsonData = jsonObj["Data"];
  39. WebCMSEntities db = new WebCMSEntities();
  40. if(Kind == "1")
  41. {
  42. int UserId = int.Parse(jsonData["UserId"].ToString());
  43. string TradeMonth = jsonData["TradeMonth"].ToString();
  44. DateTime StartTime = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
  45. DateTime EndTime = StartTime.AddMonths(1);
  46. List<Users> users = db.Users.Where(m => m.ParentUserId == UserId && m.AuthFlag == 1).ToList();
  47. foreach(Users user in users)
  48. {
  49. bool posCheck = db.PosMachinesTwo.Any(m => m.BuyUserId == user.Id && m.ActivationState == 1 && m.ActivationTime < StartTime);
  50. if(!posCheck)
  51. {
  52. bool check = db.RecommendDirectUser.Any(m => m.UserId == UserId && m.DirectUserId == user.Id && m.TradeMonth == TradeMonth);
  53. if(!check)
  54. {
  55. db.RecommendDirectUser.Add(new RecommendDirectUser()
  56. {
  57. CreateDate = DateTime.Now,
  58. UserId = UserId,
  59. DirectUserId = user.Id,
  60. TradeMonth = TradeMonth,
  61. });
  62. db.SaveChanges();
  63. }
  64. var poslist = db.PosMachinesTwo.Select(m => new { m.BuyUserId, m.BindMerchantId, m.PosSn, m.ActivationState, m.ActivationTime }).Where(m => m.BuyUserId == user.Id && m.ActivationState == 1 && m.ActivationTime >= StartTime && m.ActivationTime < EndTime).ToList();
  65. foreach(var pos in poslist)
  66. {
  67. bool chk = db.RecommendTradeSummary.Any(m => m.UserId == user.Id && m.TradeMonth == TradeMonth && m.PosSn == pos.PosSn);
  68. if(!chk)
  69. {
  70. PosMerchantTradeSummay tradeSummay = db.PosMerchantTradeSummay.FirstOrDefault(m => m.MerchantId == pos.BindMerchantId && m.TradeMonth == TradeMonth) ?? new PosMerchantTradeSummay();
  71. db.RecommendTradeSummary.Add(new RecommendTradeSummary()
  72. {
  73. CreateDate = DateTime.Now,
  74. UserId = user.Id,
  75. TradeMonth = TradeMonth,
  76. PosSn = pos.PosSn,
  77. TradeAmount = tradeSummay.TradeAmount,
  78. });
  79. db.SaveChanges();
  80. }
  81. }
  82. }
  83. }
  84. }
  85. else if(Kind == "2")
  86. {
  87. int UserId = int.Parse(jsonData["UserId"].ToString());
  88. int DirectUserId = int.Parse(jsonData["DirectUserId"].ToString());
  89. string TradeMonth = jsonData["TradeMonth"].ToString();
  90. bool check = db.RecommendDirectUser.Any(m => m.UserId == UserId && m.DirectUserId == DirectUserId && m.TradeMonth == TradeMonth);
  91. if(!check)
  92. {
  93. db.RecommendDirectUser.Add(new RecommendDirectUser()
  94. {
  95. CreateDate = DateTime.Now,
  96. UserId = UserId,
  97. DirectUserId = DirectUserId,
  98. TradeMonth = TradeMonth,
  99. });
  100. db.SaveChanges();
  101. }
  102. }
  103. else if(Kind == "3")
  104. {
  105. int UserId = int.Parse(jsonData["UserId"].ToString());
  106. int PosId = int.Parse(jsonData["PosId"].ToString());
  107. string TradeMonth = jsonData["TradeMonth"].ToString();
  108. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == PosId);
  109. if(pos != null)
  110. {
  111. bool chk = db.RecommendTradeSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.PosSn == pos.PosSn);
  112. if(!chk)
  113. {
  114. PosMerchantTradeSummay tradeSummay = db.PosMerchantTradeSummay.FirstOrDefault(m => m.MerchantId == pos.BindMerchantId && m.TradeMonth == TradeMonth) ?? new PosMerchantTradeSummay();
  115. db.RecommendTradeSummary.Add(new RecommendTradeSummary()
  116. {
  117. CreateDate = DateTime.Now,
  118. UserId = UserId,
  119. TradeMonth = TradeMonth,
  120. PosSn = pos.PosSn,
  121. TradeAmount = tradeSummay.TradeAmount,
  122. });
  123. db.SaveChanges();
  124. }
  125. }
  126. }
  127. else if(Kind == "4")
  128. {
  129. int PosId = int.Parse(jsonData["PosId"].ToString());
  130. string TradeMonth = jsonData["TradeMonth"].ToString();
  131. decimal TradeAmount = decimal.Parse(jsonData["TradeAmount"].ToString());
  132. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == PosId);
  133. if(pos != null)
  134. {
  135. RecommendTradeSummary tradeSummay = db.RecommendTradeSummary.FirstOrDefault(m => m.PosSn == pos.PosSn && m.TradeMonth == TradeMonth);
  136. if(tradeSummay != null)
  137. {
  138. tradeSummay.TradeAmount += TradeAmount;
  139. db.SaveChanges();
  140. }
  141. }
  142. }
  143. db.Dispose();
  144. }
  145. catch (Exception ex)
  146. {
  147. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "推荐王数据统计异常");
  148. }
  149. Thread.Sleep(100);
  150. }
  151. else
  152. {
  153. Thread.Sleep(600);
  154. }
  155. }
  156. }
  157. }
  158. }