RecommandKingHelper.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 > 19 && DateTime.Now.Hour < 23)
  28. {
  29. string today = DateTime.Now.ToString("yyyy-MM-dd");
  30. string checkFlag = function.ReadInstance("/RecommandKing/" + today + ".txt");
  31. if(string.IsNullOrEmpty(checkFlag))
  32. {
  33. function.WritePage("/RecommandKing/", today + ".txt", DateTime.Now.ToString());
  34. if(DateTime.Now.Day == 2)
  35. {
  36. Recommend(DateTime.Now.AddMonths(-1).ToString("yyyyMM"), 1);
  37. Recommend(DateTime.Now.AddMonths(-1).ToString("yyyyMM"), 2);
  38. }
  39. Recommend(DateTime.Now.ToString("yyyyMM"), 1);
  40. Recommend(DateTime.Now.ToString("yyyyMM"), 2);
  41. }
  42. }
  43. Thread.Sleep(600000);
  44. }
  45. }
  46. public void Recommend(string TradeMonth, int Kind)
  47. {
  48. int CheckCount = 0;
  49. if(Kind == 1) CheckCount = 40;
  50. if(Kind == 2) CheckCount = 50;
  51. List<int> ReduceUserId = new List<int>();
  52. WebCMSEntities db = new WebCMSEntities();
  53. List<RecommendDirectUser> list = new List<RecommendDirectUser>();
  54. List<int> Historys = new List<int>();
  55. if(Kind == 1)
  56. {
  57. Historys = db.RecommendDirectUser.Where(m => m.QueryCount >= CheckCount && m.TradeMonth != TradeMonth && m.Kind == Kind).ToList().Select(m => m.UserId).ToList();
  58. Historys.Add(565);
  59. Historys.Add(139473);
  60. Historys.Add(173790);
  61. Historys.Add(174506);
  62. Historys.Add(127023);
  63. Historys.Add(173790);
  64. Historys.Add(174506);
  65. Historys.Add(139473);
  66. Historys.Add(565);
  67. Historys.Add(127023);
  68. Historys.Add(176218);
  69. Historys.Add(195205);
  70. Historys.Add(193048);
  71. Historys.Add(182658);
  72. Historys.Add(173793);
  73. Historys.Add(146503);
  74. Historys.Add(168578);
  75. Historys.Add(71648);
  76. Historys.Add(730);
  77. Historys.Add(1421);
  78. Historys.Add(133571);
  79. Historys.Add(588);
  80. Historys.Add(609);
  81. Historys.Add(582);
  82. Historys.Add(196795);
  83. Historys.Add(195002);
  84. Historys.Add(205459);
  85. Historys.Add(198621);
  86. Historys.Add(197509);
  87. Historys.Add(202230);
  88. Historys.Add(205490);
  89. Historys.Add(193837);
  90. Historys.Add(195612);
  91. Historys.Add(193350);
  92. Historys.Add(164983);
  93. Historys.Add(176444);
  94. Historys.Add(141621);
  95. Historys.Add(10618);
  96. Historys.Add(134503);
  97. Historys.Add(168546);
  98. Historys.Add(149335);
  99. Historys.Add(415);
  100. Historys.Add(218);
  101. Historys.Add(180152);
  102. Historys.Add(198178);
  103. Historys.Add(190609);
  104. Historys.Add(145176);
  105. Historys.Add(134929);
  106. Historys.Add(4858);
  107. Historys.Add(391);
  108. Historys.Add(193891);
  109. Historys.Add(206512);
  110. Historys.Add(197367);
  111. Historys.Add(174434);
  112. Historys.Add(210112);
  113. Historys.Add(209078);
  114. Historys.Add(175907);
  115. Historys.Add(146169);
  116. Historys.Add(160481);
  117. Historys.Add(181934);
  118. Historys.Add(39041);
  119. Historys.Add(206658);
  120. Historys.Add(223045);
  121. Historys.Add(212269);
  122. Historys.Add(170063);
  123. Historys.Add(179072);
  124. Historys.Add(150694);
  125. Historys.Add(8573);
  126. Historys.Add(32534);
  127. Historys.Add(232054);
  128. Historys.Add(225212);
  129. Historys.Add(219510);
  130. Historys.Add(222996);
  131. Historys.Add(216389);
  132. Historys.Add(216513);
  133. Historys.Add(200374);
  134. Historys.Add(193740);
  135. Historys.Add(229240);
  136. Historys.Add(150696);
  137. }
  138. DateTime check = DateTime.Parse("2023-10-01 00:00:00");
  139. DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
  140. DateTime end = start.AddMonths(1);
  141. CustomerSqlConn.op("delete from RecommendDirectUser where TradeMonth='" + TradeMonth + "' and Kind=" + Kind, MysqlConn.SqlConnStr);
  142. List<int> ProductIds = new List<int>();
  143. ProductIds.Add(10);
  144. ProductIds.Add(11);
  145. ProductIds.Add(77);
  146. ProductIds.Add(78);
  147. ProductIds.Add(79);
  148. ProductIds.Add(27);
  149. ProductIds.Add(28);
  150. ProductIds.Add(39);
  151. ProductIds.Add(40);
  152. List<RecommandKingItem> users = new List<RecommandKingItem>();
  153. ReadModels.WebCMSEntities readdb = new ReadModels.WebCMSEntities();
  154. MpMainModels.WebCMSEntities mpdb = new MpMainModels.WebCMSEntities();
  155. MpMainModels2.WebCMSEntities mpdb2 = new MpMainModels2.WebCMSEntities();
  156. //统计当月下单名单
  157. // List<int> checkIds = readdb.Users.Where(m => (m.ParentNav.Contains(",609,") || m.Id == 609) && m.AuthFlag == 1).ToList().Select(m => m.Id).ToList();
  158. var orders = readdb.Orders.Where(m => m.PayDate >= check && m.Status > 0 && m.Sort == 0 && m.UserId > 1 && ProductIds.Contains(m.ProductId)).ToList();
  159. function.WriteLog(orders.Count.ToString(), "推荐王创客入库");
  160. List<int> uids = orders.Select(m => m.UserId).Distinct().ToList();
  161. foreach(int uid in uids)
  162. {
  163. ReadModels.Users user = readdb.Users.FirstOrDefault(m => m.Id == uid);
  164. if(user != null)
  165. {
  166. if(!users.Any(m => m.UserId == user.Id))
  167. {
  168. users.Add(new RecommandKingItem()
  169. {
  170. UserId = user.Id,
  171. ParentUserId = user.ParentUserId,
  172. ParentNav = user.ParentNav,
  173. // PreAmount = PreMonthAmt.ContainsKey(user.Id) ? PreMonthAmt[user.Id] : 0,
  174. Level = user.ParentNav.Replace(",,", ",").Trim(',').Split(',').Length + 1,
  175. UserLevel = user.UserLevel,
  176. Kind = 1,
  177. Op = false,
  178. });
  179. function.WriteLog(user.Id.ToString(), "推荐王创客入库");
  180. }
  181. }
  182. }
  183. foreach(RecommandKingItem user in users)
  184. {
  185. int uid = user.UserId;
  186. int ActCount = 0;
  187. if(ActCount == 0)
  188. {
  189. if(readdb.UserTradeMonthSummary.Any(m => m.UserId == uid && m.TradeMonth == TradeMonth && m.SeoTitle == "self"))
  190. {
  191. ActCount += readdb.UserTradeMonthSummary.Where(m => m.UserId == uid && m.TradeMonth == TradeMonth && m.SeoTitle == "self").Sum(m => m.ActiveBuddyMerStatus);
  192. // if(readdb.UserSimActSummary.Any(m => m.UserId == uid && m.Kind == 1))
  193. // {
  194. // ActCount -= readdb.UserSimActSummary.Where(m => m.UserId == uid && m.Kind == 1).Sum(m => m.ActCount); //减去注销的用户
  195. // }
  196. }
  197. }
  198. if(ActCount == 0)
  199. {
  200. ActCount += mpdb.MerchantDepositOrder.Count(m => m.UserId == uid && m.CreateDate >= start && m.CreateDate < end && m.Status > 0 && m.UserId == uid);
  201. }
  202. if(ActCount == 0)
  203. {
  204. ActCount += mpdb2.MerchantDepositOrder.Count(m => m.UserId == uid && m.CreateDate >= start && m.CreateDate < end && m.Status > 0 && m.UserId == uid);
  205. }
  206. if(ActCount > 0)
  207. {
  208. user.Op = true;
  209. function.WriteLog(user.UserId.ToString(), "推荐王达标创客名单");
  210. }
  211. }
  212. foreach(int uid in uids)
  213. {
  214. ReadModels.Users user = readdb.Users.FirstOrDefault(m => m.Id == uid);
  215. if(user != null)
  216. {
  217. string[] ParentIds = user.ParentNav.Replace(",,", ",").Trim(',').Split(',');
  218. foreach(string ParentId in ParentIds)
  219. {
  220. int ParentUserId = int.Parse(ParentId);
  221. ReadModels.Users puser = readdb.Users.FirstOrDefault(m => m.Id == ParentUserId && m.Id > 1);
  222. if(puser != null)
  223. {
  224. if(!users.Any(m => m.UserId == puser.Id))
  225. {
  226. users.Add(new RecommandKingItem()
  227. {
  228. UserId = puser.Id,
  229. ParentUserId = puser.ParentUserId,
  230. ParentNav = puser.ParentNav,
  231. // PreAmount = PreMonthAmt.ContainsKey(puser.Id) ? PreMonthAmt[puser.Id] : 0,
  232. Level = puser.ParentNav.Replace(",,", ",").Trim(',').Split(',').Length + 1,
  233. UserLevel = puser.UserLevel,
  234. Kind = 2,
  235. Op = false,
  236. });
  237. function.WriteLog(puser.Id.ToString() + "-p", "推荐王创客入库");
  238. }
  239. }
  240. }
  241. }
  242. }
  243. users = users.OrderByDescending(m => m.Level).ToList();
  244. foreach(RecommandKingItem user in users)
  245. {
  246. string UserIdStr = "," + user.UserId + ",";
  247. int Count = users.Count(m => m.ParentNav.Contains(UserIdStr) && m.Op);
  248. user.ActCount = Count;
  249. user.ChkActCount = Count;
  250. function.WriteLog(user.UserId + ":" + Count, "推荐王总的达标创客数量");
  251. // RedisDbconn.Instance.AddList("userstmp2024", user);
  252. }
  253. // users = RedisDbconn.Instance.GetList<RecommandKingItem>("userstmp2024", 1, 999999).OrderByDescending(m => m.Level).ToList();
  254. users = users.OrderByDescending(m => m.Level).ToList();
  255. foreach(RecommandKingItem user in users)
  256. {
  257. function.WriteLog(user.UserId.ToString() + "-" + user.UserLevel, "推荐王最终的达标创客数量分析");
  258. int ActCount = 0;
  259. List<RecommandKingItem> subs = users.Where(m => m.ParentUserId == user.UserId).ToList();
  260. foreach(RecommandKingItem sub in subs)
  261. {
  262. function.WriteLog(sub.UserId + ":" + sub.ActCount, "推荐王最终的达标创客数量分析");
  263. if(sub.ActCount < CheckCount && sub.UserLevel < 5 && !Historys.Contains(sub.UserId))
  264. {
  265. ActCount += sub.ActCount;
  266. if(sub.Op) ActCount += 1;
  267. }
  268. }
  269. user.ActCount = ActCount;
  270. function.WriteLog("\n\n\n", "推荐王最终的达标创客数量分析");
  271. }
  272. int num = 0;
  273. foreach(RecommandKingItem user in users)
  274. {
  275. if(!Historys.Contains(user.UserId))
  276. {
  277. num += 1;
  278. RecommendDirectUser item = db.RecommendDirectUser.FirstOrDefault(m => m.UserId == user.UserId && m.TradeMonth == TradeMonth && m.Kind == Kind);
  279. if(item == null)
  280. {
  281. function.WriteLog(user.UserId.ToString(), "推荐王达标最终入库");
  282. item = db.RecommendDirectUser.Add(new RecommendDirectUser()
  283. {
  284. UserId = user.UserId,
  285. TradeMonth = TradeMonth,
  286. QueryCount = user.ActCount > CheckCount ? CheckCount : user.ActCount,
  287. Kind = Kind,
  288. }).Entity;
  289. if(num % 200 == 0)
  290. {
  291. db.SaveChanges();
  292. }
  293. }
  294. }
  295. }
  296. db.SaveChanges();
  297. db.Dispose();
  298. readdb.Dispose();
  299. mpdb.Dispose();
  300. mpdb2.Dispose();
  301. }
  302. public decimal GetTradeAmt(int UserId, string TradeMonth)
  303. {
  304. ReadModels.WebCMSEntities rdb = new ReadModels.WebCMSEntities();
  305. MpMainModels.WebCMSEntities mpmaindb = new MpMainModels.WebCMSEntities();
  306. MpMainModels2.WebCMSEntities mpmaindb2 = new MpMainModels2.WebCMSEntities();
  307. decimal TradeAmount = 0;
  308. //创客团队交易额
  309. bool check = rdb.TradeDaySummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  310. if (check)
  311. {
  312. 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);
  313. }
  314. check = rdb.TradeDaySummary2.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  315. if (check)
  316. {
  317. TradeAmount += rdb.TradeDaySummary2.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team").Sum(m => m.ProfitTradeAmt + m.ProfitDebitTradeAmt);
  318. }
  319. //码牌团队交易额(直联)
  320. check = mpmaindb.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  321. if (check)
  322. {
  323. TradeAmount += mpmaindb.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  324. }
  325. //码牌团队交易额(银联)
  326. check = mpmaindb2.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  327. if (check)
  328. {
  329. TradeAmount += mpmaindb2.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  330. }
  331. //广电卡扶持期按1万/张计入职级
  332. check = rdb.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14);
  333. if (check)
  334. {
  335. TradeAmount += rdb.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14).Sum(m => m.ActiveBuddyMerStatus) * 10000;
  336. }
  337. return TradeAmount;
  338. }
  339. public Dictionary<int, decimal> GetTradeAmtList(string TradeMonth)
  340. {
  341. Dictionary<int, decimal> dic = new Dictionary<int, decimal>();
  342. ReadModels.WebCMSEntities rdb = new ReadModels.WebCMSEntities();
  343. MpMainModels.WebCMSEntities mpmaindb = new MpMainModels.WebCMSEntities();
  344. MpMainModels2.WebCMSEntities mpmaindb2 = new MpMainModels2.WebCMSEntities();
  345. //创客团队交易额
  346. bool check = rdb.TradeDaySummary.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  347. if (check)
  348. {
  349. var list = rdb.TradeDaySummary.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team").ToList().GroupBy(m => m.UserId);
  350. foreach(var item in list)
  351. {
  352. int UserId = item.Key;
  353. decimal Amt = item.Sum(m => m.HelpNonDirectTradeAmt + m.HelpNonDirectDebitTradeAmt + m.NotHelpNonDirectTradeAmt + m.NotHelpNonDirectDebitTradeAmt + m.ProfitNonDirectTradeAmt + m.ProfitNonDirectDebitTradeAmt);
  354. if(!dic.ContainsKey(UserId))
  355. {
  356. dic.Add(UserId, Amt);
  357. }
  358. else
  359. {
  360. dic[UserId] += Amt;
  361. }
  362. }
  363. }
  364. check = rdb.TradeDaySummary2.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  365. if (check)
  366. {
  367. var list = rdb.TradeDaySummary2.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team").ToList().GroupBy(m => m.UserId);
  368. foreach(var item in list)
  369. {
  370. int UserId = item.Key;
  371. decimal Amt = item.Sum(m => m.ProfitTradeAmt + m.ProfitDebitTradeAmt);
  372. if(!dic.ContainsKey(UserId))
  373. {
  374. dic.Add(UserId, Amt);
  375. }
  376. else
  377. {
  378. dic[UserId] += Amt;
  379. }
  380. }
  381. }
  382. //码牌团队交易额(直联)
  383. check = mpmaindb.UserAmountSummary.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  384. if (check)
  385. {
  386. var list = mpmaindb.UserAmountSummary.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).ToList().GroupBy(m => m.UserId);
  387. foreach(var item in list)
  388. {
  389. int UserId = item.Key;
  390. decimal Amt = item.Sum(m => m.TotalAmount) * 4;
  391. if(!dic.ContainsKey(UserId))
  392. {
  393. dic.Add(UserId, Amt);
  394. }
  395. else
  396. {
  397. dic[UserId] += Amt;
  398. }
  399. }
  400. }
  401. //码牌团队交易额(银联)
  402. check = mpmaindb2.UserAmountSummary.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  403. if (check)
  404. {
  405. var list = mpmaindb2.UserAmountSummary.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).ToList().GroupBy(m => m.UserId);
  406. foreach(var item in list)
  407. {
  408. int UserId = item.Key;
  409. decimal Amt = item.Sum(m => m.TotalAmount) * 4;
  410. if(!dic.ContainsKey(UserId))
  411. {
  412. dic.Add(UserId, Amt);
  413. }
  414. else
  415. {
  416. dic[UserId] += Amt;
  417. }
  418. }
  419. }
  420. //广电卡扶持期按1万/张计入职级
  421. check = rdb.UserTradeMonthSummary.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14);
  422. if (check)
  423. {
  424. var list = rdb.UserTradeMonthSummary.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14).ToList().GroupBy(m => m.UserId);
  425. foreach(var item in list)
  426. {
  427. int UserId = item.Key;
  428. decimal Amt = item.Sum(m => m.ActiveBuddyMerStatus) * 10000;
  429. if(!dic.ContainsKey(UserId))
  430. {
  431. dic.Add(UserId, Amt);
  432. }
  433. else
  434. {
  435. dic[UserId] += Amt;
  436. }
  437. }
  438. }
  439. return dic;
  440. }
  441. public void StartTest()
  442. {
  443. Thread th = new Thread(StartTestFor);
  444. th.IsBackground = true;
  445. th.Start();
  446. }
  447. public void StartTestFor()
  448. {
  449. Recommend("202410", 1);
  450. Recommend("202410", 2);
  451. }
  452. }
  453. }