RecommandKingHelper.cs 18 KB

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