RecommandKingHelper.cs 21 KB

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