RecommandKingHelper.cs 16 KB

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