RecommandKingHelper.cs 22 KB

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