RecommandKingHelper.cs 17 KB

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