RecommendActStatService.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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))
  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 check = db.RecommendDirectUser.Any(m => m.UserId == UserId && m.DirectUserId == user.Id && m.TradeMonth == TradeMonth);
  50. if(!check)
  51. {
  52. db.RecommendDirectUser.Add(new RecommendDirectUser()
  53. {
  54. CreateDate = DateTime.Now,
  55. UserId = UserId,
  56. DirectUserId = user.Id,
  57. TradeMonth = TradeMonth,
  58. });
  59. db.SaveChanges();
  60. }
  61. 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();
  62. foreach(var pos in poslist)
  63. {
  64. bool chk = db.RecommendTradeSummary.Any(m => m.UserId == user.Id && m.TradeMonth == TradeMonth && m.PosSn == pos.PosSn);
  65. if(!chk)
  66. {
  67. PosMerchantTradeSummay tradeSummay = db.PosMerchantTradeSummay.FirstOrDefault(m => m.MerchantId == pos.BindMerchantId && m.TradeMonth == TradeMonth) ?? new PosMerchantTradeSummay();
  68. db.RecommendTradeSummary.Add(new RecommendTradeSummary()
  69. {
  70. CreateDate = DateTime.Now,
  71. UserId = user.Id,
  72. TradeMonth = TradeMonth,
  73. PosSn = pos.PosSn,
  74. TradeAmount = tradeSummay.TradeAmount,
  75. });
  76. db.SaveChanges();
  77. }
  78. }
  79. }
  80. }
  81. else if(Kind == "2")
  82. {
  83. int UserId = int.Parse(jsonData["UserId"].ToString());
  84. int DirectUserId = int.Parse(jsonData["DirectUserId"].ToString());
  85. string TradeMonth = jsonData["TradeMonth"].ToString();
  86. bool check = db.RecommendDirectUser.Any(m => m.UserId == UserId && m.DirectUserId == DirectUserId && m.TradeMonth == TradeMonth);
  87. if(!check)
  88. {
  89. db.RecommendDirectUser.Add(new RecommendDirectUser()
  90. {
  91. CreateDate = DateTime.Now,
  92. UserId = UserId,
  93. DirectUserId = DirectUserId,
  94. TradeMonth = TradeMonth,
  95. });
  96. db.SaveChanges();
  97. }
  98. }
  99. else if(Kind == "3")
  100. {
  101. int UserId = int.Parse(jsonData["UserId"].ToString());
  102. int PosId = int.Parse(jsonData["PosId"].ToString());
  103. string TradeMonth = jsonData["TradeMonth"].ToString();
  104. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == PosId);
  105. if(pos != null)
  106. {
  107. bool chk = db.RecommendTradeSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.PosSn == pos.PosSn);
  108. if(!chk)
  109. {
  110. PosMerchantTradeSummay tradeSummay = db.PosMerchantTradeSummay.FirstOrDefault(m => m.MerchantId == pos.BindMerchantId && m.TradeMonth == TradeMonth) ?? new PosMerchantTradeSummay();
  111. db.RecommendTradeSummary.Add(new RecommendTradeSummary()
  112. {
  113. CreateDate = DateTime.Now,
  114. UserId = UserId,
  115. TradeMonth = TradeMonth,
  116. PosSn = pos.PosSn,
  117. TradeAmount = tradeSummay.TradeAmount,
  118. });
  119. db.SaveChanges();
  120. }
  121. }
  122. }
  123. else if(Kind == "4")
  124. {
  125. int PosId = int.Parse(jsonData["PosId"].ToString());
  126. string TradeMonth = jsonData["TradeMonth"].ToString();
  127. decimal TradeAmount = decimal.Parse(jsonData["TradeAmount"].ToString());
  128. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == PosId);
  129. if(pos != null)
  130. {
  131. PosMerchantTradeSummay tradeSummay = db.PosMerchantTradeSummay.FirstOrDefault(m => m.MerchantId == pos.BindMerchantId && m.TradeMonth == TradeMonth);
  132. if(tradeSummay != null)
  133. {
  134. tradeSummay.TradeAmount = TradeAmount;
  135. db.SaveChanges();
  136. }
  137. }
  138. }
  139. db.Dispose();
  140. }
  141. catch (Exception ex)
  142. {
  143. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "推荐王数据统计异常");
  144. }
  145. Thread.Sleep(100);
  146. }
  147. else
  148. {
  149. Thread.Sleep(60000);
  150. }
  151. }
  152. }
  153. }
  154. }