RecommandKingHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. }
  93. }
  94. foreach(int uid in uids)
  95. {
  96. Users user = db.Users.FirstOrDefault(m => m.Id == uid);
  97. if(user != null)
  98. {
  99. string[] ParentIds = user.ParentNav.Replace(",,", ",").Trim(',').Split(',');
  100. foreach(string ParentId in ParentIds)
  101. {
  102. int ParentUserId = int.Parse(ParentId);
  103. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId && m.Id > 1);
  104. if(puser != null)
  105. {
  106. if(!users.Any(m => m.UserId == puser.Id))
  107. {
  108. users.Add(new RecommandKingItem()
  109. {
  110. UserId = puser.Id,
  111. ParentUserId = puser.ParentUserId,
  112. ParentNav = puser.ParentNav,
  113. PreAmount = PreMonthAmt.ContainsKey(puser.Id) ? PreMonthAmt[puser.Id] : 0,
  114. Level = puser.ParentNav.Replace(",,", ",").Trim(',').Split(',').Length + 1,
  115. Kind = 2,
  116. Op = false,
  117. });
  118. }
  119. }
  120. }
  121. }
  122. }
  123. foreach(RecommandKingItem user in users)
  124. {
  125. if(user.Kind == 1)
  126. {
  127. int uid = user.UserId;
  128. int ActCount = 0;
  129. if(ActCount == 0)
  130. {
  131. if(readdb.UserTradeMonthSummary.Any(m => m.UserId == uid && m.TradeMonth == TradeMonth && m.SeoTitle == "self"))
  132. {
  133. ActCount += readdb.UserTradeMonthSummary.Where(m => m.UserId == uid && m.TradeMonth == TradeMonth && m.SeoTitle == "self").Sum(m => m.ActiveBuddyMerStatus);
  134. if(readdb.UserSimActSummary.Any(m => m.UserId == uid && m.Kind == 1))
  135. {
  136. ActCount -= readdb.UserSimActSummary.Where(m => m.UserId == uid && m.Kind == 1).Sum(m => m.ActCount); //减去注销的用户
  137. }
  138. }
  139. }
  140. if(ActCount == 0)
  141. {
  142. ActCount += mpdb.MerchantDepositOrder.Count(m => m.UserId == uid && m.CreateDate >= start && m.CreateDate < end && m.Status > 0 && m.UserId == uid);
  143. }
  144. if(ActCount == 0)
  145. {
  146. ActCount += mpdb2.MerchantDepositOrder.Count(m => m.UserId == uid && m.CreateDate >= start && m.CreateDate < end && m.Status > 0 && m.UserId == uid);
  147. }
  148. if(ActCount > 0)
  149. {
  150. user.Op = true;
  151. function.WriteLog(user.UserId.ToString(), "推荐王达标创客名单");
  152. }
  153. }
  154. }
  155. // users = RedisDbconn.Instance.GetList<RecommandKingItem>("userstmp2024", 1, 99999999);
  156. users = users.OrderBy(m => m.Level).ToList();
  157. foreach(RecommandKingItem user in users)
  158. {
  159. string UserIdStr = "," + user.UserId + ",";
  160. int Count = users.Count(m => m.ParentNav.Contains(UserIdStr) && m.Op);
  161. user.ActCount = Count;
  162. user.ChkActCount = Count;
  163. function.WriteLog(user.UserId + ":" + Count, "推荐王总的达标创客数量");
  164. }
  165. foreach(RecommandKingItem user in users)
  166. {
  167. string UserIdStr = "," + user.UserId + ",";
  168. function.WriteLog(user.UserId.ToString(), "推荐王最终的达标创客数量分析");
  169. if(users.Any(m => m.ParentNav.Contains(UserIdStr) && (m.ChkActCount >= 50 || m.PreAmount >= 30000000 || Historys.Contains(m.UserId))))
  170. {
  171. List<RecommandKingItem> subs = users.Where(m => m.ParentUserId == user.UserId).ToList();
  172. foreach(RecommandKingItem sub in subs)
  173. {
  174. function.WriteLog(sub.UserId + ":" + sub.ActCount, "推荐王最终的达标创客数量分析");
  175. string subUserIdStr = "," + sub.UserId + ",";
  176. if(sub.ChkActCount >= 50 || sub.PreAmount >= 30000000 || Historys.Contains(sub.UserId))
  177. {
  178. user.ActCount -= sub.ChkActCount;
  179. function.WriteLog("减去" + sub.ChkActCount, "推荐王最终的达标创客数量分析");
  180. }
  181. else if(users.Any(m => m.ParentNav.Contains(subUserIdStr) && (m.ChkActCount >= 50 || m.PreAmount >= 30000000 || Historys.Contains(sub.UserId))))
  182. {
  183. RecommandKingItem tar = users.Where(m => m.ParentNav.Contains(subUserIdStr) && (m.ChkActCount >= 50 || m.PreAmount >= 30000000)).OrderBy(m => m.Level).FirstOrDefault();
  184. user.ActCount -= tar.ChkActCount;
  185. function.WriteLog("减去" + tar.UserId + ":" + sub.ChkActCount, "推荐王最终的达标创客数量分析");
  186. }
  187. }
  188. }
  189. function.WriteLog("\n\n\n", "推荐王最终的达标创客数量分析");
  190. }
  191. int num = 0;
  192. foreach(RecommandKingItem user in users)
  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. db.SaveChanges();
  211. db.Dispose();
  212. readdb.Dispose();
  213. mpdb.Dispose();
  214. mpdb2.Dispose();
  215. }
  216. public decimal GetTradeAmt(int UserId, string TradeMonth)
  217. {
  218. ReadModels.WebCMSEntities rdb = new ReadModels.WebCMSEntities();
  219. MpMainModels.WebCMSEntities mpmaindb = new MpMainModels.WebCMSEntities();
  220. MpMainModels2.WebCMSEntities mpmaindb2 = new MpMainModels2.WebCMSEntities();
  221. decimal TradeAmount = 0;
  222. //创客团队交易额
  223. bool check = rdb.TradeDaySummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  224. if (check)
  225. {
  226. 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);
  227. }
  228. check = rdb.TradeDaySummary2.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  229. if (check)
  230. {
  231. TradeAmount += rdb.TradeDaySummary2.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team").Sum(m => m.ProfitTradeAmt + m.ProfitDebitTradeAmt);
  232. }
  233. //码牌团队交易额(直联)
  234. check = mpmaindb.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  235. if (check)
  236. {
  237. TradeAmount += mpmaindb.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  238. }
  239. //码牌团队交易额(银联)
  240. check = mpmaindb2.UserAmountSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  241. if (check)
  242. {
  243. TradeAmount += mpmaindb2.UserAmountSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).Sum(m => m.TotalAmount) * 4;
  244. }
  245. //广电卡扶持期按1万/张计入职级
  246. check = rdb.UserTradeMonthSummary.Any(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14);
  247. if (check)
  248. {
  249. TradeAmount += rdb.UserTradeMonthSummary.Where(m => m.UserId == UserId && m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14).Sum(m => m.ActiveBuddyMerStatus) * 10000;
  250. }
  251. return TradeAmount;
  252. }
  253. public Dictionary<int, decimal> GetTradeAmtList(string TradeMonth)
  254. {
  255. Dictionary<int, decimal> dic = new Dictionary<int, decimal>();
  256. ReadModels.WebCMSEntities rdb = new ReadModels.WebCMSEntities();
  257. MpMainModels.WebCMSEntities mpmaindb = new MpMainModels.WebCMSEntities();
  258. MpMainModels2.WebCMSEntities mpmaindb2 = new MpMainModels2.WebCMSEntities();
  259. //创客团队交易额
  260. bool check = rdb.TradeDaySummary.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  261. if (check)
  262. {
  263. var list = rdb.TradeDaySummary.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team").ToList().GroupBy(m => m.UserId);
  264. foreach(var item in list)
  265. {
  266. int UserId = item.Key;
  267. decimal Amt = item.Sum(m => m.HelpNonDirectTradeAmt + m.HelpNonDirectDebitTradeAmt + m.NotHelpNonDirectTradeAmt + m.NotHelpNonDirectDebitTradeAmt + m.ProfitNonDirectTradeAmt + m.ProfitNonDirectDebitTradeAmt);
  268. if(!dic.ContainsKey(UserId))
  269. {
  270. dic.Add(UserId, Amt);
  271. }
  272. else
  273. {
  274. dic[UserId] += Amt;
  275. }
  276. }
  277. }
  278. check = rdb.TradeDaySummary2.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  279. if (check)
  280. {
  281. var list = rdb.TradeDaySummary2.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team").ToList().GroupBy(m => m.UserId);
  282. foreach(var item in list)
  283. {
  284. int UserId = item.Key;
  285. decimal Amt = item.Sum(m => m.ProfitTradeAmt + m.ProfitDebitTradeAmt);
  286. if(!dic.ContainsKey(UserId))
  287. {
  288. dic.Add(UserId, Amt);
  289. }
  290. else
  291. {
  292. dic[UserId] += Amt;
  293. }
  294. }
  295. }
  296. //码牌团队交易额(直联)
  297. check = mpmaindb.UserAmountSummary.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  298. if (check)
  299. {
  300. var list = mpmaindb.UserAmountSummary.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).ToList().GroupBy(m => m.UserId);
  301. foreach(var item in list)
  302. {
  303. int UserId = item.Key;
  304. decimal Amt = item.Sum(m => m.TotalAmount) * 4;
  305. if(!dic.ContainsKey(UserId))
  306. {
  307. dic.Add(UserId, Amt);
  308. }
  309. else
  310. {
  311. dic[UserId] += Amt;
  312. }
  313. }
  314. }
  315. //码牌团队交易额(银联)
  316. check = mpmaindb2.UserAmountSummary.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team");
  317. if (check)
  318. {
  319. var list = mpmaindb2.UserAmountSummary.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.IsAct == 1).ToList().GroupBy(m => m.UserId);
  320. foreach(var item in list)
  321. {
  322. int UserId = item.Key;
  323. decimal Amt = item.Sum(m => m.TotalAmount) * 4;
  324. if(!dic.ContainsKey(UserId))
  325. {
  326. dic.Add(UserId, Amt);
  327. }
  328. else
  329. {
  330. dic[UserId] += Amt;
  331. }
  332. }
  333. }
  334. //广电卡扶持期按1万/张计入职级
  335. check = rdb.UserTradeMonthSummary.Any(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14);
  336. if (check)
  337. {
  338. var list = rdb.UserTradeMonthSummary.Where(m => m.TradeMonth == TradeMonth && m.SeoTitle == "team" && m.BrandId == 14).ToList().GroupBy(m => m.UserId);
  339. foreach(var item in list)
  340. {
  341. int UserId = item.Key;
  342. decimal Amt = item.Sum(m => m.ActiveBuddyMerStatus) * 10000;
  343. if(!dic.ContainsKey(UserId))
  344. {
  345. dic.Add(UserId, Amt);
  346. }
  347. else
  348. {
  349. dic[UserId] += Amt;
  350. }
  351. }
  352. }
  353. return dic;
  354. }
  355. }
  356. }