RecommandKingHelper.cs 17 KB

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