RecommandKingHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if(DateTime.Now.Hour > 0 && DateTime.Now.Hour < 22)
  27. {
  28. Recommend();
  29. }
  30. Thread.Sleep(600000);
  31. }
  32. }
  33. public void Recommend()
  34. {
  35. string today = DateTime.Now.ToString("yyyy-MM-dd");
  36. string checkFlag = function.ReadInstance("/RecommandKing/" + today + ".txt");
  37. if(!string.IsNullOrEmpty(checkFlag))
  38. {
  39. return;
  40. }
  41. function.WritePage("/RecommandKing/", today + ".txt", DateTime.Now.ToString());
  42. List<RecommendDirectUser> list = new List<RecommendDirectUser>();
  43. List<int> Historys = new List<int>();
  44. Historys.Add(565);
  45. Historys.Add(139473);
  46. Historys.Add(173790);
  47. Historys.Add(174506);
  48. string TradeMonth = DateTime.Now.AddMonths(-0).ToString("yyyyMM");
  49. DateTime check = DateTime.Parse("2023-10-01 00:00:00");
  50. DateTime start = DateTime.Parse(DateTime.Now.AddMonths(-0).ToString("yyyy-MM") + "-01 00:00:00");
  51. DateTime end = start.AddMonths(1);
  52. CustomerSqlConn.op("delete from RecommendDirectUser where TradeMonth='" + TradeMonth + "'", MysqlConn.SqlConnStr);
  53. string PreTradeMonth = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  54. List<int> ProductIds = new List<int>();
  55. ProductIds.Add(10);
  56. ProductIds.Add(11);
  57. ProductIds.Add(77);
  58. ProductIds.Add(78);
  59. ProductIds.Add(79);
  60. ProductIds.Add(27);
  61. ProductIds.Add(28);
  62. ProductIds.Add(39);
  63. ProductIds.Add(40);
  64. WebCMSEntities db = new WebCMSEntities();
  65. ReadModels.WebCMSEntities readdb = new ReadModels.WebCMSEntities();
  66. //统计当月下单名单
  67. 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();
  68. uids = readdb.Users.Where(m => uids.Contains(m.Id)).OrderByDescending(m => m.ParentNav).ToList().Select(m => m.Id).ToList();
  69. foreach(int uid in uids)
  70. {
  71. int ActCount = 0;
  72. if(ActCount == 0)
  73. {
  74. if(readdb.UserTradeMonthSummary.Any(m => m.UserId == uid && m.TradeMonth == TradeMonth && m.SeoTitle == "team"))
  75. {
  76. ActCount += readdb.UserTradeMonthSummary.Where(m => m.UserId == uid && m.TradeMonth == TradeMonth && m.SeoTitle == "team").Sum(m => m.ActiveBuddyMerStatus);
  77. ActCount -= readdb.SimposMerchantInfo.Count(m => m.UserId == uid && m.Status < 0); //减去注销的用户
  78. }
  79. }
  80. if(ActCount == 0)
  81. {
  82. MpMainModels.WebCMSEntities mpdb = new MpMainModels.WebCMSEntities();
  83. ActCount += mpdb.MerchantDepositOrder.Count(m => m.UserId == uid && m.CreateDate >= start && m.CreateDate < end && m.Status > 0 && m.UserId == uid);
  84. mpdb.Dispose();
  85. }
  86. if(ActCount == 0)
  87. {
  88. MpMainModels2.WebCMSEntities mpdb = new MpMainModels2.WebCMSEntities();
  89. ActCount += mpdb.MerchantDepositOrder.Count(m => m.UserId == uid && m.CreateDate >= start && m.CreateDate < end && m.Status > 0 && m.UserId == uid);
  90. mpdb.Dispose();
  91. }
  92. if(ActCount > 0)
  93. {
  94. Users user = db.Users.FirstOrDefault(m => m.Id == uid);
  95. if(user != null)
  96. {
  97. string ParentNav = user.ParentNav;
  98. if(!string.IsNullOrEmpty(ParentNav))
  99. {
  100. string[] ParentNavList = ParentNav.Replace(",,", ",").Trim(',').Split(',');
  101. Array.Reverse(ParentNavList);
  102. bool PassFlag = true;
  103. bool UpLevelClear = false;
  104. foreach(string UserIdString in ParentNavList)
  105. {
  106. int UserId = int.Parse(UserIdString);
  107. if(UpLevelClear)
  108. {
  109. RecommendDirectUser item = list.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth);
  110. if(item == null)
  111. {
  112. item = new RecommendDirectUser()
  113. {
  114. UserId = UserId,
  115. TradeMonth = TradeMonth,
  116. };
  117. list.Add(item);
  118. }
  119. item.QueryCount -= 49;
  120. }
  121. //判断是否历史达标创客
  122. if(PassFlag)
  123. {
  124. if(list.Any(m => m.UserId == UserId && m.QueryCount >= 50) || Historys.Contains(UserId))
  125. {
  126. PassFlag = false;
  127. }
  128. }
  129. if(PassFlag)
  130. {
  131. RecommendDirectUser item = list.FirstOrDefault(m => m.UserId == UserId && m.TradeMonth == TradeMonth);
  132. if(item == null)
  133. {
  134. item = new RecommendDirectUser()
  135. {
  136. UserId = UserId,
  137. TradeMonth = TradeMonth,
  138. };
  139. list.Add(item);
  140. }
  141. item.QueryCount += 1;
  142. if(item.QueryCount >= 50)
  143. {
  144. PassFlag = false;
  145. UpLevelClear = true;
  146. }
  147. }
  148. //判断上月交易额是否满3000W
  149. if(PassFlag)
  150. {
  151. if(GetTradeAmt(UserId, PreTradeMonth) >= 30000000)
  152. {
  153. PassFlag = false;
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }
  161. foreach(RecommendDirectUser sub in list)
  162. {
  163. db.RecommendDirectUser.Add(sub);
  164. }
  165. db.SaveChanges();
  166. db.Dispose();
  167. readdb.Dispose();
  168. }
  169. public decimal GetTradeAmt(int UserId, string TradeMonth)
  170. {
  171. ReadModels.WebCMSEntities rdb = new ReadModels.WebCMSEntities();
  172. MpMainModels.WebCMSEntities mpmaindb = new MpMainModels.WebCMSEntities();
  173. MpMainModels2.WebCMSEntities mpmaindb2 = new MpMainModels2.WebCMSEntities();
  174. decimal TradeAmount = 0;
  175. //创客团队交易额
  176. bool check = rdb.TradeDaySummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  177. if (check)
  178. {
  179. 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);
  180. }
  181. check = rdb.TradeDaySummary2.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  182. if (check)
  183. {
  184. TradeAmount += rdb.TradeDaySummary2.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team").Sum(m => m.ProfitTradeAmt + m.ProfitDebitTradeAmt);
  185. }
  186. //码牌团队交易额(直联)
  187. check = mpmaindb.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  188. if (check)
  189. {
  190. TradeAmount += mpmaindb.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  191. }
  192. //码牌团队交易额(银联)
  193. check = mpmaindb2.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  194. if (check)
  195. {
  196. TradeAmount += mpmaindb2.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  197. }
  198. //广电卡扶持期按1万/张计入职级
  199. check = rdb.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14);
  200. if (check)
  201. {
  202. TradeAmount += rdb.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14).Sum(m => m.ActiveBuddyMerStatus) * 10000;
  203. }
  204. return TradeAmount;
  205. }
  206. }
  207. }