RecommandKingHelper.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using MySystem.PxcModels;
  6. using Library;
  7. using LitJson;
  8. using System.Threading;
  9. namespace MySystem
  10. {
  11. public class RecommandKingHelper
  12. {
  13. public readonly static RecommandKingHelper Instance = new RecommandKingHelper();
  14. private RecommandKingHelper()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(StartFor);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. public void StartFor()
  23. {
  24. while (true)
  25. {
  26. Recommend();
  27. if(DateTime.Now.Hour > 0 && DateTime.Now.Hour < 9)
  28. {
  29. Thread.Sleep(3600000);
  30. }
  31. else
  32. {
  33. Thread.Sleep(600000);
  34. }
  35. }
  36. }
  37. public void Recommend()
  38. {
  39. DateTime check = DateTime.Parse("2023-10-01 00:00:00");
  40. DateTime start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
  41. string TradeMonth = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  42. string PreTradeMonth = DateTime.Now.AddMonths(-2).ToString("yyyyMM");
  43. List<int> ProductIds = new List<int>();
  44. ProductIds.Add(10);
  45. ProductIds.Add(11);
  46. ProductIds.Add(77);
  47. ProductIds.Add(78);
  48. ProductIds.Add(79);
  49. ProductIds.Add(27);
  50. ProductIds.Add(28);
  51. ProductIds.Add(39);
  52. ProductIds.Add(40);
  53. WebCMSEntities db = new WebCMSEntities();
  54. ReadModels.WebCMSEntities readdb = new ReadModels.WebCMSEntities();
  55. //统计当月下单名单
  56. List<int> uids = readdb.Orders.Where(m => m.PayDate >= check && m.Status > 0 && m.Sort == 0 && ProductIds.Contains(m.ProductId)).ToList().Select(m => m.UserId).Distinct().ToList();
  57. foreach(int uid in uids)
  58. {
  59. int ActCount = 0;
  60. if(ActCount == 0)
  61. {
  62. if(readdb.UserTradeMonthSummary.Any(m => m.UserId == uid && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId != 14))
  63. {
  64. ActCount += readdb.UserTradeMonthSummary.Where(m => m.UserId == uid && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId != 14).Sum(m => m.ActiveBuddyMerStatus);
  65. }
  66. }
  67. if(ActCount == 0)
  68. {
  69. MpMainModels.WebCMSEntities mpdb = new MpMainModels.WebCMSEntities();
  70. ActCount += mpdb.MerchantDepositOrder.Count(m => m.UserId == uid && m.CreateDate >= start && m.Status > 0 && m.UserId == uid);
  71. mpdb.Dispose();
  72. }
  73. if(ActCount == 0)
  74. {
  75. MpMainModels2.WebCMSEntities mpdb = new MpMainModels2.WebCMSEntities();
  76. ActCount += mpdb.MerchantDepositOrder.Count(m => m.UserId == uid && m.CreateDate >= start && m.Status > 0 && m.UserId == uid);
  77. mpdb.Dispose();
  78. }
  79. if(ActCount > 0)
  80. {
  81. Users user = db.Users.FirstOrDefault(m => m.Id == uid);
  82. if(user != null)
  83. {
  84. string ParentNav = user.ParentNav;
  85. if(!string.IsNullOrEmpty(ParentNav))
  86. {
  87. string[] ParentNavList = ParentNav.Replace(",,", ",").Trim(',').Split(',');
  88. Array.Reverse(ParentNavList);
  89. foreach(string UserIdString in ParentNavList)
  90. {
  91. int UserId = int.Parse(UserIdString);
  92. bool PassFlag = true;
  93. //判断是否历史达标创客
  94. if(PassFlag)
  95. {
  96. if(db.RecommendDirectUser.Any(m => m.UserId == UserId && m.TradeMonth != TradeMonth))
  97. {
  98. PassFlag = false;
  99. }
  100. }
  101. //判断上月交易额是否满3000W
  102. if(PassFlag)
  103. {
  104. if(GetTradeAmt(UserId, PreTradeMonth) >= 30000000)
  105. {
  106. PassFlag = false;
  107. }
  108. }
  109. if(PassFlag)
  110. {
  111. RecommendDirectUser item = db.RecommendDirectUser.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth);
  112. if(item == null)
  113. {
  114. item = db.RecommendDirectUser.Add(new RecommendDirectUser()
  115. {
  116. UserId = UserId,
  117. TradeMonth = TradeMonth,
  118. }).Entity;
  119. db.SaveChanges();
  120. }
  121. item.QueryCount += 1;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. db.Dispose();
  129. readdb.Dispose();
  130. }
  131. public decimal GetTradeAmt(int UserId, string TradeMonth)
  132. {
  133. ReadModels.WebCMSEntities rdb = new ReadModels.WebCMSEntities();
  134. MpMainModels.WebCMSEntities mpmaindb = new MpMainModels.WebCMSEntities();
  135. MpMainModels2.WebCMSEntities mpmaindb2 = new MpMainModels2.WebCMSEntities();
  136. decimal TradeAmount = 0;
  137. //创客团队交易额
  138. bool check = rdb.TradeDaySummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  139. if (check)
  140. {
  141. TradeAmount += rdb.TradeDaySummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team").Sum(m => m.HelpNonDirectTradeAmt + m.HelpNonDirectDebitTradeAmt + m.NotHelpNonDirectTradeAmt + m.NotHelpNonDirectDebitTradeAmt + m.ProfitNonDirectTradeAmt + m.ProfitNonDirectDebitTradeAmt);
  142. }
  143. check = rdb.TradeDaySummary2.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  144. if (check)
  145. {
  146. TradeAmount += rdb.TradeDaySummary2.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team").Sum(m => m.ProfitTradeAmt + m.ProfitDebitTradeAmt);
  147. }
  148. //码牌团队交易额(直联)
  149. check = mpmaindb.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  150. if (check)
  151. {
  152. TradeAmount += mpmaindb.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  153. }
  154. //码牌团队交易额(银联)
  155. check = mpmaindb2.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  156. if (check)
  157. {
  158. TradeAmount += mpmaindb2.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  159. }
  160. //广电卡扶持期按1万/张计入职级
  161. check = rdb.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14);
  162. if (check)
  163. {
  164. TradeAmount += rdb.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14).Sum(m => m.ActiveBuddyMerStatus) * 10000;
  165. }
  166. return TradeAmount;
  167. }
  168. }
  169. }