RecommandKingHelper.cs 18 KB

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