RecommandKingHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. using GraphQL;
  10. namespace MySystem
  11. {
  12. public class RecommandKingHelper
  13. {
  14. public readonly static RecommandKingHelper Instance = new RecommandKingHelper();
  15. private RecommandKingHelper()
  16. { }
  17. public void Start()
  18. {
  19. Thread th = new Thread(StartFor);
  20. th.IsBackground = true;
  21. th.Start();
  22. }
  23. public void StartFor()
  24. {
  25. while (true)
  26. {
  27. if(DateTime.Now.Hour > 0 && DateTime.Now.Hour < 23)
  28. {
  29. Recommend();
  30. }
  31. Thread.Sleep(600000);
  32. }
  33. }
  34. public void Recommend()
  35. {
  36. string today = DateTime.Now.ToString("yyyy-MM-dd");
  37. // string checkFlag = function.ReadInstance("/RecommandKing/" + today + ".txt");
  38. // if(!string.IsNullOrEmpty(checkFlag))
  39. // {
  40. // return;
  41. // }
  42. // function.WritePage("/RecommandKing/", today + ".txt", DateTime.Now.ToString());
  43. List<int> Historys = new List<int>();
  44. Historys.Add(565);
  45. Historys.Add(139473);
  46. Historys.Add(173790);
  47. Historys.Add(174506);
  48. Historys.Add(127023);
  49. string TradeMonth = DateTime.Now.AddMonths(-0).ToString("yyyyMM");
  50. DateTime check = DateTime.Parse("2023-10-01 00:00:00");
  51. DateTime start = DateTime.Parse(DateTime.Now.AddMonths(-0).ToString("yyyy-MM") + "-01 00:00:00");
  52. DateTime end = start.AddMonths(1);
  53. CustomerSqlConn.op("delete from RecommendDirectUser where TradeMonth='" + TradeMonth + "'", MysqlConn.SqlConnStr);
  54. string PreTradeMonth = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  55. List<int> ProductIds = new List<int>();
  56. ProductIds.Add(10);
  57. ProductIds.Add(11);
  58. ProductIds.Add(77);
  59. ProductIds.Add(78);
  60. ProductIds.Add(79);
  61. ProductIds.Add(27);
  62. ProductIds.Add(28);
  63. ProductIds.Add(39);
  64. ProductIds.Add(40);
  65. List<RecommandKingItem> users = new List<RecommandKingItem>();
  66. Dictionary<int, decimal> PreMonthAmt = GetTradeAmtList(PreTradeMonth);
  67. WebCMSEntities db = new WebCMSEntities();
  68. ReadModels.WebCMSEntities readdb = new ReadModels.WebCMSEntities();
  69. MpMainModels.WebCMSEntities mpdb = new MpMainModels.WebCMSEntities();
  70. MpMainModels2.WebCMSEntities mpdb2 = new MpMainModels2.WebCMSEntities();
  71. //统计当月下单名单
  72. List<int> checkIds = readdb.Users.Where(m => (m.ParentNav.Contains(",609,") || m.Id == 609) && m.AuthFlag == 1).ToList().Select(m => m.Id).ToList();
  73. List<int> uids = readdb.Orders.Where(m => m.PayDate >= check && m.Status > 0 && m.Sort == 0 && m.UserId > 1 && checkIds.Contains(m.UserId) && ProductIds.Contains(m.ProductId)).ToList().Select(m => m.UserId).Distinct().ToList();
  74. foreach(int uid in uids)
  75. {
  76. Users user = db.Users.FirstOrDefault(m => m.Id == uid);
  77. if(user != null)
  78. {
  79. if(!users.Any(m => m.UserId == user.Id))
  80. {
  81. users.Add(new RecommandKingItem()
  82. {
  83. UserId = user.Id,
  84. ParentUserId = user.ParentUserId,
  85. ParentNav = user.ParentNav,
  86. PreAmount = PreMonthAmt.ContainsKey(user.Id) ? PreMonthAmt[user.Id] : 0,
  87. Level = user.ParentNav.Replace(",,", ",").Trim(',').Split(',').Length + 1,
  88. Kind = 1,
  89. Op = false,
  90. });
  91. }
  92. }
  93. }
  94. foreach(int uid in uids)
  95. {
  96. Users user = db.Users.FirstOrDefault(m => m.Id == uid);
  97. if(user != null)
  98. {
  99. string[] ParentIds = user.ParentNav.Replace(",,", ",").Trim(',').Split(',');
  100. foreach(string ParentId in ParentIds)
  101. {
  102. int ParentUserId = int.Parse(ParentId);
  103. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId && m.Id > 1);
  104. if(puser != null)
  105. {
  106. if(!users.Any(m => m.UserId == puser.Id))
  107. {
  108. users.Add(new RecommandKingItem()
  109. {
  110. UserId = puser.Id,
  111. ParentUserId = puser.ParentUserId,
  112. ParentNav = puser.ParentNav,
  113. PreAmount = PreMonthAmt.ContainsKey(puser.Id) ? PreMonthAmt[puser.Id] : 0,
  114. Level = puser.ParentNav.Replace(",,", ",").Trim(',').Split(',').Length + 1,
  115. Kind = 2,
  116. Op = false,
  117. });
  118. }
  119. }
  120. }
  121. }
  122. }
  123. foreach(RecommandKingItem user in users)
  124. {
  125. if(user.Kind == 1)
  126. {
  127. int uid = user.UserId;
  128. if(uid == 193523)
  129. {
  130. string ss = "123123";
  131. }
  132. int ActCount = 0;
  133. if(ActCount == 0)
  134. {
  135. if(readdb.UserTradeMonthSummary.Any(m => m.UserId == uid && m.TradeMonth == TradeMonth && m.SeoTitle == "self"))
  136. {
  137. ActCount += readdb.UserTradeMonthSummary.Where(m => m.UserId == uid && m.TradeMonth == TradeMonth && m.SeoTitle == "self").Sum(m => m.ActiveBuddyMerStatus);
  138. if(readdb.UserSimActSummary.Any(m => m.UserId == uid && m.Kind == 1))
  139. {
  140. ActCount -= readdb.UserSimActSummary.Where(m => m.UserId == uid && m.Kind == 1).Sum(m => m.ActCount); //减去注销的用户
  141. }
  142. }
  143. }
  144. if(ActCount == 0)
  145. {
  146. ActCount += mpdb.MerchantDepositOrder.Count(m => m.UserId == uid && m.CreateDate >= start && m.CreateDate < end && m.Status > 0 && m.UserId == uid);
  147. }
  148. if(ActCount == 0)
  149. {
  150. ActCount += mpdb2.MerchantDepositOrder.Count(m => m.UserId == uid && m.CreateDate >= start && m.CreateDate < end && m.Status > 0 && m.UserId == uid);
  151. }
  152. if(ActCount > 0)
  153. {
  154. user.Op = true;
  155. }
  156. }
  157. RedisDbconn.Instance.AddList("userstmp2024", user);
  158. }
  159. // users = RedisDbconn.Instance.GetList<RecommandKingItem>("userstmp2024", 1, 99999999);
  160. users = users.OrderBy(m => m.Level).ToList();
  161. foreach(RecommandKingItem user in users)
  162. {
  163. string UserIdStr = "," + user.UserId + ",";
  164. int Count = users.Count(m => m.ParentNav.Contains(UserIdStr) && m.Op);
  165. user.ActCount = Count;
  166. user.ChkActCount = Count;
  167. if(user.UserId == 184916)
  168. {
  169. var test = users.Where(m => m.ParentNav.Contains(UserIdStr) && m.Op).ToList();
  170. }
  171. }
  172. foreach(RecommandKingItem user in users)
  173. {
  174. string UserIdStr = "," + user.UserId + ",";
  175. if(users.Any(m => m.ParentNav.Contains(UserIdStr) && (m.ChkActCount >= 50 || m.PreAmount >= 30000000)))
  176. {
  177. List<RecommandKingItem> subs = users.Where(m => m.ParentUserId == user.UserId).ToList();
  178. foreach(RecommandKingItem sub in subs)
  179. {
  180. string subUserIdStr = "," + sub.UserId + ",";
  181. if(sub.ChkActCount >= 50 || sub.PreAmount >= 30000000 || Historys.Contains(sub.UserId))
  182. {
  183. user.ActCount -= sub.ChkActCount;
  184. }
  185. else if(users.Any(m => m.ParentNav.Contains(subUserIdStr) && (m.ChkActCount >= 50 || m.PreAmount >= 30000000)))
  186. {
  187. RecommandKingItem tar = users.Where(m => m.ParentNav.Contains(subUserIdStr) && (m.ChkActCount >= 50 || m.PreAmount >= 30000000)).OrderBy(m => m.Level).FirstOrDefault();
  188. user.ActCount -= tar.ChkActCount;
  189. }
  190. }
  191. }
  192. }
  193. int num = 0;
  194. foreach(RecommandKingItem user in users)
  195. {
  196. num += 1;
  197. RecommendDirectUser item = db.RecommendDirectUser.FirstOrDefault(m => m.UserId == user.UserId && m.TradeMonth == TradeMonth);
  198. if(item == null)
  199. {
  200. item = db.RecommendDirectUser.Add(new RecommendDirectUser()
  201. {
  202. UserId = user.UserId,
  203. TradeMonth = TradeMonth,
  204. QueryCount = user.ActCount > 50 ? 50 : user.ActCount,
  205. }).Entity;
  206. if(num % 200 == 0)
  207. {
  208. db.SaveChanges();
  209. }
  210. }
  211. }
  212. db.SaveChanges();
  213. db.Dispose();
  214. readdb.Dispose();
  215. mpdb.Dispose();
  216. mpdb2.Dispose();
  217. }
  218. public decimal GetTradeAmt(int UserId, string TradeMonth)
  219. {
  220. ReadModels.WebCMSEntities rdb = new ReadModels.WebCMSEntities();
  221. MpMainModels.WebCMSEntities mpmaindb = new MpMainModels.WebCMSEntities();
  222. MpMainModels2.WebCMSEntities mpmaindb2 = new MpMainModels2.WebCMSEntities();
  223. decimal TradeAmount = 0;
  224. //创客团队交易额
  225. bool check = rdb.TradeDaySummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  226. if (check)
  227. {
  228. 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);
  229. }
  230. check = rdb.TradeDaySummary2.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  231. if (check)
  232. {
  233. TradeAmount += rdb.TradeDaySummary2.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team").Sum(m => m.ProfitTradeAmt + m.ProfitDebitTradeAmt);
  234. }
  235. //码牌团队交易额(直联)
  236. check = mpmaindb.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  237. if (check)
  238. {
  239. TradeAmount += mpmaindb.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  240. }
  241. //码牌团队交易额(银联)
  242. check = mpmaindb2.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  243. if (check)
  244. {
  245. TradeAmount += mpmaindb2.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  246. }
  247. //广电卡扶持期按1万/张计入职级
  248. check = rdb.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14);
  249. if (check)
  250. {
  251. TradeAmount += rdb.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14).Sum(m => m.ActiveBuddyMerStatus) * 10000;
  252. }
  253. return TradeAmount;
  254. }
  255. public Dictionary<int, decimal> GetTradeAmtList(string TradeMonth)
  256. {
  257. Dictionary<int, decimal> dic = new Dictionary<int, decimal>();
  258. ReadModels.WebCMSEntities rdb = new ReadModels.WebCMSEntities();
  259. MpMainModels.WebCMSEntities mpmaindb = new MpMainModels.WebCMSEntities();
  260. MpMainModels2.WebCMSEntities mpmaindb2 = new MpMainModels2.WebCMSEntities();
  261. //创客团队交易额
  262. bool check = rdb.TradeDaySummary.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  263. if (check)
  264. {
  265. var list = rdb.TradeDaySummary.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team").ToList().GroupBy(m => m.UserId);
  266. foreach(var item in list)
  267. {
  268. int UserId = item.Key;
  269. decimal Amt = item.Sum(m => m.HelpNonDirectTradeAmt + m.HelpNonDirectDebitTradeAmt + m.NotHelpNonDirectTradeAmt + m.NotHelpNonDirectDebitTradeAmt + m.ProfitNonDirectTradeAmt + m.ProfitNonDirectDebitTradeAmt);
  270. if(!dic.ContainsKey(UserId))
  271. {
  272. dic.Add(UserId, Amt);
  273. }
  274. else
  275. {
  276. dic[UserId] += Amt;
  277. }
  278. }
  279. }
  280. check = rdb.TradeDaySummary2.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  281. if (check)
  282. {
  283. var list = rdb.TradeDaySummary2.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team").ToList().GroupBy(m => m.UserId);
  284. foreach(var item in list)
  285. {
  286. int UserId = item.Key;
  287. decimal Amt = item.Sum(m => m.ProfitTradeAmt + m.ProfitDebitTradeAmt);
  288. if(!dic.ContainsKey(UserId))
  289. {
  290. dic.Add(UserId, Amt);
  291. }
  292. else
  293. {
  294. dic[UserId] += Amt;
  295. }
  296. }
  297. }
  298. //码牌团队交易额(直联)
  299. check = mpmaindb.UserAmountSummary.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  300. if (check)
  301. {
  302. var list = mpmaindb.UserAmountSummary.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).ToList().GroupBy(m => m.UserId);
  303. foreach(var item in list)
  304. {
  305. int UserId = item.Key;
  306. decimal Amt = item.Sum(m => m.TotalAmount) * 4;
  307. if(!dic.ContainsKey(UserId))
  308. {
  309. dic.Add(UserId, Amt);
  310. }
  311. else
  312. {
  313. dic[UserId] += Amt;
  314. }
  315. }
  316. }
  317. //码牌团队交易额(银联)
  318. check = mpmaindb2.UserAmountSummary.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  319. if (check)
  320. {
  321. var list = mpmaindb2.UserAmountSummary.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).ToList().GroupBy(m => m.UserId);
  322. foreach(var item in list)
  323. {
  324. int UserId = item.Key;
  325. decimal Amt = item.Sum(m => m.TotalAmount) * 4;
  326. if(!dic.ContainsKey(UserId))
  327. {
  328. dic.Add(UserId, Amt);
  329. }
  330. else
  331. {
  332. dic[UserId] += Amt;
  333. }
  334. }
  335. }
  336. //广电卡扶持期按1万/张计入职级
  337. check = rdb.UserTradeMonthSummary.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14);
  338. if (check)
  339. {
  340. var list = rdb.UserTradeMonthSummary.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14).ToList().GroupBy(m => m.UserId);
  341. foreach(var item in list)
  342. {
  343. int UserId = item.Key;
  344. decimal Amt = item.Sum(m => m.ActiveBuddyMerStatus) * 10000;
  345. if(!dic.ContainsKey(UserId))
  346. {
  347. dic.Add(UserId, Amt);
  348. }
  349. else
  350. {
  351. dic[UserId] += Amt;
  352. }
  353. }
  354. }
  355. return dic;
  356. }
  357. }
  358. }