RecommandKingHelper.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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"));
  37. // }
  38. // Recommend(DateTime.Now.ToString("yyyyMM"));
  39. // }
  40. // }
  41. // Thread.Sleep(600000);
  42. // }
  43. Recommend("202311", 50);
  44. Recommend("202312", 50);
  45. Recommend("202401", 50);
  46. Recommend("202402", 50);
  47. Recommend("202403", 50);
  48. Recommend("202404", 50);
  49. Recommend("202405", 50);
  50. Recommend("202406", 50);
  51. Recommend("202407", 50);
  52. Recommend("202408", 50);
  53. Recommend("202311", 45);
  54. Recommend("202312", 45);
  55. Recommend("202401", 45);
  56. Recommend("202402", 45);
  57. Recommend("202403", 45);
  58. Recommend("202404", 45);
  59. Recommend("202405", 45);
  60. Recommend("202406", 45);
  61. Recommend("202407", 45);
  62. Recommend("202408", 45);
  63. Recommend("202311", 40);
  64. Recommend("202312", 40);
  65. Recommend("202401", 40);
  66. Recommend("202402", 40);
  67. Recommend("202403", 40);
  68. Recommend("202404", 40);
  69. Recommend("202405", 40);
  70. Recommend("202406", 40);
  71. Recommend("202407", 40);
  72. Recommend("202408", 40);
  73. }
  74. public void Recommend(string TradeMonth, int CheckActCount)
  75. {
  76. List<int> ReduceUserId = new List<int>();
  77. WebCMSEntities db = new WebCMSEntities();
  78. List<RecommendDirectUser> list = new List<RecommendDirectUser>();
  79. List<int> Historys = new List<int>();
  80. // List<int> Historys = db.RecommendDirectUser.Where(m => m.QueryCount >= 50 && m.TradeMonth != TradeMonth).ToList().Select(m => m.UserId).ToList();
  81. // Historys.Add(565);
  82. // Historys.Add(139473);
  83. // Historys.Add(173790);
  84. // Historys.Add(174506);
  85. // Historys.Add(127023);
  86. DateTime check = DateTime.Parse("2023-10-01 00:00:00");
  87. DateTime start = DateTime.Parse(TradeMonth.Substring(0, 4) + "-" + TradeMonth.Substring(4, 2) + "-01 00:00:00");
  88. DateTime end = start.AddMonths(1);
  89. CustomerSqlConn.op("delete from RecommendDirectUser", MysqlConn.SqlConnStr);
  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. ReadModels.WebCMSEntities readdb = new ReadModels.WebCMSEntities();
  102. MpMainModels.WebCMSEntities mpdb = new MpMainModels.WebCMSEntities();
  103. MpMainModels2.WebCMSEntities mpdb2 = new MpMainModels2.WebCMSEntities();
  104. //统计当月下单名单
  105. // List<int> checkIds = readdb.Users.Where(m => (m.ParentNav.Contains(",609,") || m.Id == 609) && m.AuthFlag == 1).ToList().Select(m => m.Id).ToList();
  106. var orders = readdb.Orders.Where(m => m.PayDate >= check && m.Status > 0 && m.Sort == 0 && m.UserId > 1 && ProductIds.Contains(m.ProductId)).ToList();
  107. function.WriteLog(orders.Count.ToString(), "推荐王创客入库");
  108. List<int> uids = orders.Select(m => m.UserId).Distinct().ToList();
  109. foreach(int uid in uids)
  110. {
  111. ReadModels.Users user = readdb.Users.FirstOrDefault(m => m.Id == uid);
  112. if(user != null)
  113. {
  114. if(!users.Any(m => m.UserId == user.Id))
  115. {
  116. users.Add(new RecommandKingItem()
  117. {
  118. UserId = user.Id,
  119. ParentUserId = user.ParentUserId,
  120. ParentNav = user.ParentNav,
  121. // PreAmount = PreMonthAmt.ContainsKey(user.Id) ? PreMonthAmt[user.Id] : 0,
  122. Level = user.ParentNav.Replace(",,", ",").Trim(',').Split(',').Length + 1,
  123. UserLevel = user.UserLevel,
  124. Kind = 1,
  125. Op = false,
  126. });
  127. function.WriteLog(user.Id.ToString(), "推荐王创客入库");
  128. }
  129. }
  130. }
  131. foreach(RecommandKingItem user in users)
  132. {
  133. int uid = user.UserId;
  134. int ActCount = 0;
  135. if(ActCount == 0)
  136. {
  137. if(readdb.UserTradeMonthSummary.Any(m => m.UserId == uid && m.TradeMonth == TradeMonth && m.SeoTitle == "self"))
  138. {
  139. ActCount += readdb.UserTradeMonthSummary.Where(m => m.UserId == uid && m.TradeMonth == TradeMonth && m.SeoTitle == "self").Sum(m => m.ActiveBuddyMerStatus);
  140. // if(readdb.UserSimActSummary.Any(m => m.UserId == uid && m.Kind == 1))
  141. // {
  142. // ActCount -= readdb.UserSimActSummary.Where(m => m.UserId == uid && m.Kind == 1).Sum(m => m.ActCount); //减去注销的用户
  143. // }
  144. }
  145. }
  146. if(ActCount == 0)
  147. {
  148. ActCount += mpdb.MerchantDepositOrder.Count(m => m.UserId == uid && m.CreateDate >= start && m.CreateDate < end && m.Status > 0 && m.UserId == uid);
  149. }
  150. if(ActCount == 0)
  151. {
  152. ActCount += mpdb2.MerchantDepositOrder.Count(m => m.UserId == uid && m.CreateDate >= start && m.CreateDate < end && m.Status > 0 && m.UserId == uid);
  153. }
  154. if(ActCount > 0)
  155. {
  156. user.Op = true;
  157. function.WriteLog(user.UserId.ToString(), "推荐王达标创客名单");
  158. }
  159. }
  160. foreach(int uid in uids)
  161. {
  162. ReadModels.Users user = readdb.Users.FirstOrDefault(m => m.Id == uid);
  163. if(user != null)
  164. {
  165. string[] ParentIds = user.ParentNav.Replace(",,", ",").Trim(',').Split(',');
  166. foreach(string ParentId in ParentIds)
  167. {
  168. int ParentUserId = int.Parse(ParentId);
  169. ReadModels.Users puser = readdb.Users.FirstOrDefault(m => m.Id == ParentUserId && m.Id > 1);
  170. if(puser != null)
  171. {
  172. if(!users.Any(m => m.UserId == puser.Id))
  173. {
  174. users.Add(new RecommandKingItem()
  175. {
  176. UserId = puser.Id,
  177. ParentUserId = puser.ParentUserId,
  178. ParentNav = puser.ParentNav,
  179. // PreAmount = PreMonthAmt.ContainsKey(puser.Id) ? PreMonthAmt[puser.Id] : 0,
  180. Level = puser.ParentNav.Replace(",,", ",").Trim(',').Split(',').Length + 1,
  181. UserLevel = puser.UserLevel,
  182. Kind = 2,
  183. Op = false,
  184. });
  185. function.WriteLog(puser.Id.ToString() + "-p", "推荐王创客入库");
  186. }
  187. }
  188. }
  189. }
  190. }
  191. users = users.OrderByDescending(m => m.Level).ToList();
  192. foreach(RecommandKingItem user in users)
  193. {
  194. string UserIdStr = "," + user.UserId + ",";
  195. int Count = users.Count(m => m.ParentNav.Contains(UserIdStr) && m.Op);
  196. user.ActCount = Count;
  197. user.ChkActCount = Count;
  198. function.WriteLog(user.UserId + ":" + Count, "推荐王总的达标创客数量");
  199. // RedisDbconn.Instance.AddList("userstmp2024", user);
  200. }
  201. // users = RedisDbconn.Instance.GetList<RecommandKingItem>("userstmp2024", 1, 999999).OrderByDescending(m => m.Level).ToList();
  202. users = users.OrderByDescending(m => m.Level).ToList();
  203. foreach(RecommandKingItem user in users)
  204. {
  205. function.WriteLog(user.UserId.ToString() + "-" + user.UserLevel, "推荐王最终的达标创客数量分析");
  206. int ActCount = 0;
  207. List<RecommandKingItem> subs = users.Where(m => m.ParentUserId == user.UserId).ToList();
  208. foreach(RecommandKingItem sub in subs)
  209. {
  210. function.WriteLog(sub.UserId + ":" + sub.ActCount, "推荐王最终的达标创客数量分析");
  211. if(sub.ActCount < CheckActCount && sub.UserLevel < 5 && !Historys.Contains(sub.UserId))
  212. {
  213. ActCount += sub.ActCount;
  214. if(sub.Op) ActCount += 1;
  215. }
  216. }
  217. user.ActCount = ActCount;
  218. function.WriteLog("\n\n\n", "推荐王最终的达标创客数量分析");
  219. }
  220. int num = 0;
  221. foreach(RecommandKingItem user in users)
  222. {
  223. if(!Historys.Contains(user.UserId))
  224. {
  225. num += 1;
  226. RecommendDirectUser item = db.RecommendDirectUser.FirstOrDefault(m => m.UserId == user.UserId && m.TradeMonth == TradeMonth);
  227. if(item == null)
  228. {
  229. item = db.RecommendDirectUser.Add(new RecommendDirectUser()
  230. {
  231. UserId = user.UserId,
  232. TradeMonth = TradeMonth,
  233. QueryCount = user.ActCount > CheckActCount ? CheckActCount : user.ActCount,
  234. }).Entity;
  235. if(num % 200 == 0)
  236. {
  237. db.SaveChanges();
  238. }
  239. }
  240. }
  241. }
  242. db.SaveChanges();
  243. db.Dispose();
  244. readdb.Dispose();
  245. mpdb.Dispose();
  246. mpdb2.Dispose();
  247. CustomerSqlConn.op("CREATE TABLE RecommendDirectUser" + TradeMonth + "_" + CheckActCount + " LIKE RecommendDirectUser", MysqlConn.SqlConnStr);
  248. CustomerSqlConn.op("INSERT INTO RecommendDirectUser" + TradeMonth + "_" + CheckActCount + " SELECT * FROM RecommendDirectUser", MysqlConn.SqlConnStr);
  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. }