RecommandKingHelper.cs 17 KB

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