LeaderCompPrizeHelper.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. using System;
  2. using System.Threading;
  3. using System.Linq;
  4. using System.Data;
  5. using Library;
  6. using LitJson;
  7. using System.Collections.Generic;
  8. using MySystem.PxcModels;
  9. namespace MySystem
  10. {
  11. public class LeaderCompPrizeHelper
  12. {
  13. public readonly static LeaderCompPrizeHelper Instance = new LeaderCompPrizeHelper();
  14. private LeaderCompPrizeHelper()
  15. {
  16. }
  17. public void Start()//启动
  18. {
  19. Thread thread = new Thread(Listen);
  20. thread.IsBackground = true;
  21. thread.Start();
  22. }
  23. public void Listen()//启动
  24. {
  25. while(true)
  26. {
  27. if(DateTime.Now.Hour > 0 && DateTime.Now.Hour < 3)
  28. {
  29. if(DateTime.Now.Month == 1)
  30. {
  31. CustomerSqlConn.op("insert into LeaderCompTradeStatBak select * from LeaderCompTradeStat;insert into LeaderCompTmpBak select * from LeaderCompTmp;insert into LeaderCompPrizeBak select * from LeaderCompPrize;insert into LeaderCompAddTradeBak select * from LeaderCompAddTrade;", MysqlConn.SqlConnStr);
  32. }
  33. Ready(DateTime.Now.ToString("yyyy-MM"));
  34. }
  35. Thread.Sleep(60000);
  36. }
  37. // doSomething("202306", "202305");
  38. // doSomething("202306", "202206");
  39. // doSomething("202305", "202206");
  40. // SendPrize("202306", "202305");
  41. // SendPrize("202306", "202206");
  42. // SendPrize("202305", "202206");
  43. }
  44. public void Ready(string curMonth)
  45. {
  46. CustomerSqlConn.op("delete from LeaderCompTradeStat;delete from LeaderCompTmp;delete from LeaderCompPrize;delete from LeaderCompAddTrade;", MysqlConn.SqlConnStr);
  47. Thread.Sleep(10000);
  48. DateTime Start = DateTime.Parse("2023-06-01 00:00:00");
  49. DateTime now = DateTime.Parse(curMonth + "-01 00:00:00");
  50. while(now > Start)
  51. {
  52. string month = now.ToString("yyyyMM");
  53. string checkMonth = Start.ToString("yyyyMM");
  54. doSomething(month, checkMonth);
  55. now = now.AddMonths(-1);
  56. }
  57. now = DateTime.Parse(curMonth + "-01 00:00:00");
  58. while(now > Start)
  59. {
  60. string month = now.ToString("yyyyMM");
  61. string checkMonth = Start.ToString("yyyyMM");
  62. SendPrize(month, checkMonth);
  63. now = now.AddMonths(-1);
  64. }
  65. }
  66. public void doSomething(string month, string checkMonth)
  67. {
  68. try
  69. {
  70. WebCMSEntities db = new WebCMSEntities();
  71. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  72. string MonthString = month + "-" + checkMonth;
  73. //统计交易增量
  74. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  75. DataTable dt = CustomerSqlConn.dtable("select CONCAT('insert into LeaderCompTradeStat (CreateDate,UserId,StatMonth,TradeAmount,SelfTradeAmount,CheckTradeAmount,CheckSelfTradeAmount) values (now(),', UserId,',\\\'" + month + "-" + checkMonth + "\\\',', amt" + month + ",',', samt" + month + ",',', amt" + checkMonth + ",',', samt" + checkMonth + ",');') from (select UserId,(case when amt" + month + " is null then 0 else amt" + month + " end) amt" + month + ",(case when samt" + month + " is null then 0 else samt" + month + " end) samt" + month + ",(case when amt" + checkMonth + " is null then 0 else amt" + checkMonth + " end) amt" + checkMonth + ",(case when samt" + checkMonth + " is null then 0 else samt" + checkMonth + " end) samt" + checkMonth + " from (select UserId,sum(HelpNonDirectTradeAmt + NotHelpNonDirectTradeAmt + ProfitNonDirectTradeAmt + HelpNonDirectDebitTradeAmt + NotHelpNonDirectDebitTradeAmt + ProfitNonDirectDebitTradeAmt) amt" + month + ",(select sum(HelpNonDirectTradeAmt + NotHelpNonDirectTradeAmt + ProfitNonDirectTradeAmt + HelpNonDirectDebitTradeAmt + NotHelpNonDirectDebitTradeAmt + ProfitNonDirectDebitTradeAmt) from TradeDaySummary" + checkMonth + " where SeoTitle='team' and UserId=main.UserId) amt" + checkMonth + ",(select sum(HelpDirectTradeAmt + NotHelpDirectTradeAmt + ProfitDirectTradeAmt + HelpDirectDebitTradeAmt + NotHelpDirectDebitTradeAmt + ProfitDirectDebitTradeAmt) from TradeDaySummary" + month + " where SeoTitle='self' and UserId=main.UserId) samt" + month + ",(select sum(HelpDirectTradeAmt + NotHelpDirectTradeAmt + ProfitDirectTradeAmt + HelpDirectDebitTradeAmt + NotHelpDirectDebitTradeAmt + ProfitDirectDebitTradeAmt) from TradeDaySummary" + checkMonth + " where SeoTitle='self' and UserId=main.UserId) samt" + checkMonth + " from TradeDaySummary" + month + " main where SeoTitle='team' and UserId>0 group by UserId) tb) tb2", MysqlConn.RdsStatSqlConnStr);
  76. string sql = "";
  77. int num = 0;
  78. foreach(DataRow dr in dt.Rows)
  79. {
  80. sql += dr[0].ToString();
  81. num += 1;
  82. if(num >= 200)
  83. {
  84. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  85. sql = "";
  86. num = 0;
  87. }
  88. }
  89. if(!string.IsNullOrEmpty(sql))
  90. {
  91. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  92. }
  93. CustomerSqlConn.op("update LeaderCompTradeStat leader set ParentUserId=case when (select ParentUserId from Users where Id=leader.UserId) is null then 0 else (select ParentUserId from Users where Id=leader.UserId) end,ParentNav=(select ParentNav from Users where Id=leader.UserId) where StatMonth='" + month + "-" + checkMonth + "' and UserId>1", MysqlConn.SqlConnStr);
  94. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  95. //计算创客名下的达标人数,直推和间接
  96. function.WriteLog("MonthString:" + MonthString, "领导人达标奖励日志");
  97. List<LeaderCompTradeStat> stats = db.LeaderCompTradeStat.Where(m => m.StatMonth == MonthString).ToList();
  98. function.WriteLog(stats.Count.ToString(), "领导人达标奖励日志");
  99. foreach(LeaderCompTradeStat stat in stats)
  100. {
  101. decimal totalAmount = stat.SelfTradeAmount - stat.CheckSelfTradeAmount; //当月交易
  102. List<LeaderCompTradeStat> directs = stats.Where(m => m.ParentUserId == stat.UserId).ToList();
  103. foreach(LeaderCompTradeStat direct in directs)
  104. {
  105. decimal parentTotalAmount = direct.TradeAmount - direct.CheckTradeAmount;
  106. if(parentTotalAmount < 30000000)
  107. {
  108. totalAmount += parentTotalAmount;
  109. }
  110. }
  111. function.WriteLog("UserId:" + stat.UserId, "领导人达标奖励日志");
  112. function.WriteLog("totalAmount:" + totalAmount, "领导人达标奖励日志");
  113. if(totalAmount >= 30000000 && !string.IsNullOrEmpty(stat.ParentNav))
  114. {
  115. LeaderCompTradeStat statEdit = db.LeaderCompTradeStat.FirstOrDefault(m => m.UserId == stat.UserId && m.StatMonth == MonthString);
  116. if(statEdit != null)
  117. {
  118. statEdit.ResultTradeAmount = totalAmount;
  119. db.SaveChanges();
  120. }
  121. LeaderCompTmp tmp = db.LeaderCompTmp.FirstOrDefault(m => m.Id == stat.UserId);
  122. if(tmp == null)
  123. {
  124. tmp = db.LeaderCompTmp.Add(new LeaderCompTmp()
  125. {
  126. Id = stat.UserId,
  127. ParentUserId = stat.ParentUserId,
  128. ParentNav = stat.ParentNav
  129. }).Entity;
  130. db.SaveChanges();
  131. }
  132. Dictionary<string, object> EveryMonthData = new Dictionary<string, object>();
  133. if(!string.IsNullOrEmpty(tmp.EveryMonthData))
  134. {
  135. EveryMonthData = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(tmp.EveryMonthData);
  136. }
  137. if(!EveryMonthData.ContainsKey(MonthString))
  138. {
  139. EveryMonthData.Add(MonthString, 1);
  140. }
  141. else
  142. {
  143. EveryMonthData[MonthString] = 1;
  144. }
  145. tmp.EveryMonthData = Newtonsoft.Json.JsonConvert.SerializeObject(EveryMonthData);
  146. db.SaveChanges();
  147. string[] parents = stat.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  148. Array.Reverse(parents);
  149. int index = 0;
  150. foreach(string parent in parents)
  151. {
  152. index += 1;
  153. int ParentUserId = int.Parse(function.CheckInt(parent));
  154. LeaderCompPrize prize = db.LeaderCompPrize.FirstOrDefault(m => m.UserId == ParentUserId && m.StatMonth == MonthString);
  155. if(prize == null)
  156. {
  157. Users user = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  158. prize = db.LeaderCompPrize.Add(new LeaderCompPrize()
  159. {
  160. CreateDate = DateTime.Now,
  161. UserId = ParentUserId,
  162. ParentUserId = user.ParentUserId,
  163. ParentNav = user.ParentNav,
  164. StatMonth = MonthString,
  165. }).Entity;
  166. db.SaveChanges();
  167. }
  168. if(index == 1)
  169. {
  170. prize.DirectCount += 1;
  171. }
  172. else
  173. {
  174. prize.NotDirectCount += 1;
  175. }
  176. db.SaveChanges();
  177. }
  178. }
  179. LeaderCompAddTrade addTrade = db.LeaderCompAddTrade.FirstOrDefault(m => m.UserId == stat.UserId && m.StatMonth == MonthString);
  180. if(addTrade == null)
  181. {
  182. addTrade = db.LeaderCompAddTrade.Add(new LeaderCompAddTrade()
  183. {
  184. CreateDate = DateTime.Now,
  185. UserId = stat.UserId,
  186. StatMonth = MonthString,
  187. }).Entity;
  188. db.SaveChanges();
  189. }
  190. addTrade.AddTradeAmount = totalAmount;
  191. db.SaveChanges();
  192. function.WriteLog(DateTime.Now.ToString() + "------" + stat.UserId, "领导人达标奖励日志");
  193. }
  194. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  195. db.Dispose();
  196. }
  197. catch(Exception ex)
  198. {
  199. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "领导达标人奖励异常");
  200. }
  201. }
  202. private void SendPrize(string month, string checkDate)
  203. {
  204. string MonthFlag = month + "-" + checkDate;
  205. WebCMSEntities db = new WebCMSEntities();
  206. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  207. //计算运营中心或大盟主达标奖励
  208. List<LeaderCompPrize> leaderPrizes = db.LeaderCompPrize.Where(m => m.StatMonth == MonthFlag).ToList();
  209. function.WriteLog(leaderPrizes.Count.ToString(), "领导人达标奖励日志");
  210. foreach(LeaderCompPrize leaderPrize in leaderPrizes)
  211. {
  212. Users user = db.Users.FirstOrDefault(m => m.Id == leaderPrize.UserId) ?? new Users();
  213. // if(user.UserType == 1)
  214. // {
  215. // OpModels.SysAdmin opSys = opdb.SysAdmin.FirstOrDefault(m => m.UserId == user.Id);
  216. // if(opSys != null)
  217. // {
  218. // // checkDate = opSys.CreateDate.Value.AddMonths(-1).ToString("yyyyMM");
  219. // }
  220. // }
  221. // else if(user.LeaderLevel == 2)
  222. // {
  223. // Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == user.Id);
  224. // if(leader != null)
  225. // {
  226. // // checkDate = leader.LastBuyDate.Value.AddMonths(-1).ToString("yyyyMM");
  227. // }
  228. // }
  229. string CheckJson = "\"" + MonthFlag + "\":1";
  230. string NavUserId = "," + user.Id + ",";
  231. int person = 0;
  232. int directPerson = 0;
  233. int totalPerson = db.LeaderCompTmp.Count(m => (m.ParentNav.Contains(NavUserId)) && m.EveryMonthData.Contains(CheckJson));
  234. var subusers = db.Users.Select(m => new { m.Id, m.ParentUserId }).Where(m => m.ParentUserId == user.Id).ToList();
  235. foreach(var subuser in subusers)
  236. {
  237. string subNavUserId = "," + subuser.Id + ",";
  238. if(db.LeaderCompTmp.Count(m => (m.ParentNav.Contains(subNavUserId) || m.Id == subuser.Id) && m.EveryMonthData.Contains(CheckJson)) > 0)
  239. {
  240. person += 1;
  241. }
  242. if(db.LeaderCompTmp.Any(m => m.Id == subuser.Id && m.EveryMonthData.Contains(CheckJson)))
  243. {
  244. directPerson += 1;
  245. }
  246. }
  247. // List<LeaderCompPrize> list = db.LeaderCompPrize.Where(m => m.StatMonth == MonthFlag && m.ParentUserId == user.Id).ToList();
  248. // foreach(LeaderCompPrize sub in list)
  249. // {
  250. // if(sub.DirectCount + sub.NotDirectCount > 0)
  251. // {
  252. // person += 1;
  253. // }
  254. // totalPerson += sub.DirectCount + sub.NotDirectCount;
  255. // }
  256. int maxPerson = person * person;
  257. decimal CompPrize = 0;
  258. if(totalPerson >= maxPerson)
  259. {
  260. CompPrize = GetPrize(maxPerson - person, person);
  261. }
  262. else
  263. {
  264. CompPrize = GetPrize(totalPerson - person, person);
  265. }
  266. LeaderCompPrize edit = db.LeaderCompPrize.FirstOrDefault(m => m.StatMonth == MonthFlag && m.UserId == leaderPrize.UserId);
  267. if(edit != null)
  268. {
  269. edit.DirectCount = directPerson;
  270. edit.NotDirectCount = totalPerson - person;
  271. edit.SecDirectCount = person;
  272. edit.CompPrize = CompPrize;
  273. }
  274. db.SaveChanges();
  275. }
  276. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  277. db.Dispose();
  278. opdb.Dispose();
  279. }
  280. private decimal GetPrize(int count, int direct)
  281. {
  282. decimal prize = 0;
  283. if (count + direct >= 30)
  284. {
  285. prize = 3000 * (count + direct * 2);
  286. }
  287. else if (count + direct >= 25)
  288. {
  289. prize = 2500 * (count + direct * 2);
  290. }
  291. else if (count + direct >= 20)
  292. {
  293. prize = 2000 * (count + direct * 2);
  294. }
  295. else if (count + direct >= 15)
  296. {
  297. prize = 1500 * (count + direct * 2);
  298. }
  299. else if (count + direct >= 10)
  300. {
  301. prize = 1000 * (count + direct * 2);
  302. }
  303. else if (count + direct >= 5)
  304. {
  305. prize = 500 * (count + direct * 2);
  306. }
  307. return prize;
  308. }
  309. public void doEverday(string month, string checkMonth)
  310. {
  311. try
  312. {
  313. WebCMSEntities db = new WebCMSEntities();
  314. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  315. string MonthString = month + "-" + checkMonth;
  316. //统计交易增量
  317. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  318. DataTable dt = CustomerSqlConn.dtable("select CONCAT('insert into LeaderCompTradeStatBak (CreateDate,UserId,StatMonth,TradeAmount,SelfTradeAmount,CheckTradeAmount,CheckSelfTradeAmount) values (now(),', UserId,',\\\'" + month + "-" + checkMonth + "\\\',', amt" + month + ",',', samt" + month + ",',', amt" + checkMonth + ",',', samt" + checkMonth + ",');') from (select UserId,(case when amt" + month + " is null then 0 else amt" + month + " end) amt" + month + ",(case when samt" + month + " is null then 0 else samt" + month + " end) samt" + month + ",(case when amt" + checkMonth + " is null then 0 else amt" + checkMonth + " end) amt" + checkMonth + ",(case when samt" + checkMonth + " is null then 0 else samt" + checkMonth + " end) samt" + checkMonth + " from (select UserId,sum(HelpNonDirectTradeAmt + NotHelpNonDirectTradeAmt + ProfitNonDirectTradeAmt + HelpNonDirectDebitTradeAmt + NotHelpNonDirectDebitTradeAmt + ProfitNonDirectDebitTradeAmt) amt" + month + ",(select sum(HelpNonDirectTradeAmt + NotHelpNonDirectTradeAmt + ProfitNonDirectTradeAmt + HelpNonDirectDebitTradeAmt + NotHelpNonDirectDebitTradeAmt + ProfitNonDirectDebitTradeAmt) from TradeDaySummary" + checkMonth + " where SeoTitle='team' and UserId=main.UserId) amt" + checkMonth + ",(select sum(HelpDirectTradeAmt + NotHelpDirectTradeAmt + ProfitDirectTradeAmt + HelpDirectDebitTradeAmt + NotHelpDirectDebitTradeAmt + ProfitDirectDebitTradeAmt) from TradeDaySummary" + month + " where SeoTitle='self' and UserId=main.UserId) samt" + month + ",(select sum(HelpDirectTradeAmt + NotHelpDirectTradeAmt + ProfitDirectTradeAmt + HelpDirectDebitTradeAmt + NotHelpDirectDebitTradeAmt + ProfitDirectDebitTradeAmt) from TradeDaySummary" + checkMonth + " where SeoTitle='self' and UserId=main.UserId) samt" + checkMonth + " from TradeDaySummary" + month + " main where SeoTitle='team' and UserId>0 group by UserId) tb) tb2", MysqlConn.RdsStatSqlConnStr);
  319. string sql = "";
  320. int num = 0;
  321. foreach(DataRow dr in dt.Rows)
  322. {
  323. sql += dr[0].ToString();
  324. num += 1;
  325. if(num >= 200)
  326. {
  327. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  328. sql = "";
  329. num = 0;
  330. }
  331. }
  332. if(!string.IsNullOrEmpty(sql))
  333. {
  334. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  335. }
  336. CustomerSqlConn.op("update LeaderCompTradeStatBak leader set ParentUserId=case when (select ParentUserId from Users where Id=leader.UserId) is null then 0 else (select ParentUserId from Users where Id=leader.UserId) end,ParentNav=(select ParentNav from Users where Id=leader.UserId) where StatMonth='" + month + "-" + checkMonth + "' and UserId>1", MysqlConn.SqlConnStr);
  337. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  338. //计算创客名下的达标人数,直推和间接
  339. function.WriteLog("MonthString:" + MonthString, "领导人达标奖励日志");
  340. List<LeaderCompTradeStatBak> stats = db.LeaderCompTradeStatBak.Where(m => m.StatMonth == MonthString).ToList();
  341. function.WriteLog(stats.Count.ToString(), "领导人达标奖励日志");
  342. foreach(LeaderCompTradeStatBak stat in stats)
  343. {
  344. decimal totalAmount = stat.SelfTradeAmount - stat.CheckSelfTradeAmount; //当月交易
  345. List<LeaderCompTradeStatBak> directs = stats.Where(m => m.ParentUserId == stat.UserId).ToList();
  346. foreach(LeaderCompTradeStatBak direct in directs)
  347. {
  348. decimal parentTotalAmount = direct.TradeAmount - direct.CheckTradeAmount;
  349. if(parentTotalAmount < 30000000)
  350. {
  351. totalAmount += parentTotalAmount;
  352. }
  353. }
  354. function.WriteLog("UserId:" + stat.UserId, "领导人达标奖励日志");
  355. function.WriteLog("totalAmount:" + totalAmount, "领导人达标奖励日志");
  356. if(totalAmount >= 30000000 && !string.IsNullOrEmpty(stat.ParentNav))
  357. {
  358. LeaderCompTradeStatBak statEdit = db.LeaderCompTradeStatBak.FirstOrDefault(m => m.UserId == stat.UserId && m.StatMonth == MonthString);
  359. if(statEdit != null)
  360. {
  361. statEdit.ResultTradeAmount = totalAmount;
  362. db.SaveChanges();
  363. }
  364. LeaderCompTmpBak tmp = db.LeaderCompTmpBak.FirstOrDefault(m => m.Id == stat.UserId);
  365. if(tmp == null)
  366. {
  367. tmp = db.LeaderCompTmpBak.Add(new LeaderCompTmpBak()
  368. {
  369. Id = stat.UserId,
  370. ParentUserId = stat.ParentUserId,
  371. ParentNav = stat.ParentNav
  372. }).Entity;
  373. db.SaveChanges();
  374. }
  375. Dictionary<string, object> EveryMonthData = new Dictionary<string, object>();
  376. if(!string.IsNullOrEmpty(tmp.EveryMonthData))
  377. {
  378. EveryMonthData = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(tmp.EveryMonthData);
  379. }
  380. if(!EveryMonthData.ContainsKey(MonthString))
  381. {
  382. EveryMonthData.Add(MonthString, 1);
  383. }
  384. else
  385. {
  386. EveryMonthData[MonthString] = 1;
  387. }
  388. tmp.EveryMonthData = Newtonsoft.Json.JsonConvert.SerializeObject(EveryMonthData);
  389. db.SaveChanges();
  390. string[] parents = stat.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  391. Array.Reverse(parents);
  392. int index = 0;
  393. foreach(string parent in parents)
  394. {
  395. index += 1;
  396. int ParentUserId = int.Parse(function.CheckInt(parent));
  397. LeaderCompPrizeBak prize = db.LeaderCompPrizeBak.FirstOrDefault(m => m.UserId == ParentUserId && m.StatMonth == MonthString);
  398. if(prize == null)
  399. {
  400. Users user = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  401. prize = db.LeaderCompPrizeBak.Add(new LeaderCompPrizeBak()
  402. {
  403. CreateDate = DateTime.Now,
  404. UserId = ParentUserId,
  405. ParentUserId = user.ParentUserId,
  406. ParentNav = user.ParentNav,
  407. StatMonth = MonthString,
  408. }).Entity;
  409. db.SaveChanges();
  410. }
  411. if(index == 1)
  412. {
  413. prize.DirectCount += 1;
  414. }
  415. else
  416. {
  417. prize.NotDirectCount += 1;
  418. }
  419. db.SaveChanges();
  420. }
  421. }
  422. function.WriteLog(DateTime.Now.ToString() + "------" + stat.UserId, "领导人达标奖励日志");
  423. }
  424. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  425. db.Dispose();
  426. }
  427. catch(Exception ex)
  428. {
  429. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "领导达标人奖励异常");
  430. }
  431. }
  432. private void SendPrizeEveryDay(string month, string checkDate)
  433. {
  434. string MonthFlag = month + "-" + checkDate;
  435. WebCMSEntities db = new WebCMSEntities();
  436. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  437. //计算运营中心或大盟主达标奖励
  438. List<LeaderCompPrizeBak> leaderPrizes = db.LeaderCompPrizeBak.Where(m => m.StatMonth == MonthFlag).ToList();
  439. function.WriteLog(leaderPrizes.Count.ToString(), "领导人达标奖励日志");
  440. foreach(LeaderCompPrizeBak leaderPrize in leaderPrizes)
  441. {
  442. Users user = db.Users.FirstOrDefault(m => m.Id == leaderPrize.UserId) ?? new Users();
  443. string CheckJson = "\"" + MonthFlag + "\":1";
  444. string NavUserId = "," + user.Id + ",";
  445. int person = 0;
  446. int directPerson = 0;
  447. int totalPerson = db.LeaderCompTmpBak.Count(m => (m.ParentNav.Contains(NavUserId)) && m.EveryMonthData.Contains(CheckJson));
  448. var subusers = db.Users.Select(m => new { m.Id, m.ParentUserId }).Where(m => m.ParentUserId == user.Id).ToList();
  449. foreach(var subuser in subusers)
  450. {
  451. string subNavUserId = "," + subuser.Id + ",";
  452. if(db.LeaderCompTmpBak.Count(m => (m.ParentNav.Contains(subNavUserId) || m.Id == subuser.Id) && m.EveryMonthData.Contains(CheckJson)) > 0)
  453. {
  454. person += 1;
  455. }
  456. if(db.LeaderCompTmpBak.Any(m => m.Id == subuser.Id && m.EveryMonthData.Contains(CheckJson)))
  457. {
  458. directPerson += 1;
  459. }
  460. }
  461. int maxPerson = person * person;
  462. decimal CompPrize = 0;
  463. if(totalPerson >= maxPerson)
  464. {
  465. CompPrize = GetPrize(maxPerson - person, person);
  466. }
  467. else
  468. {
  469. CompPrize = GetPrize(totalPerson - person, person);
  470. }
  471. LeaderCompPrizeBak edit = db.LeaderCompPrizeBak.FirstOrDefault(m => m.StatMonth == MonthFlag && m.UserId == leaderPrize.UserId);
  472. if(edit != null)
  473. {
  474. edit.DirectCount = directPerson;
  475. edit.NotDirectCount = totalPerson - person;
  476. edit.SecDirectCount = person;
  477. edit.CompPrize = CompPrize;
  478. }
  479. db.SaveChanges();
  480. }
  481. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  482. db.Dispose();
  483. opdb.Dispose();
  484. }
  485. }
  486. }