LeaderCompPrizeHelper.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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;", 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;", 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. function.WriteLog(DateTime.Now.ToString() + "------" + stat.UserId, "领导人达标奖励日志");
  180. }
  181. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  182. db.Dispose();
  183. }
  184. catch(Exception ex)
  185. {
  186. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "领导达标人奖励异常");
  187. }
  188. }
  189. private void SendPrize(string month, string checkDate)
  190. {
  191. string MonthFlag = month + "-" + checkDate;
  192. WebCMSEntities db = new WebCMSEntities();
  193. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  194. //计算运营中心或大盟主达标奖励
  195. List<LeaderCompPrize> leaderPrizes = db.LeaderCompPrize.Where(m => m.StatMonth == MonthFlag).ToList();
  196. function.WriteLog(leaderPrizes.Count.ToString(), "领导人达标奖励日志");
  197. foreach(LeaderCompPrize leaderPrize in leaderPrizes)
  198. {
  199. Users user = db.Users.FirstOrDefault(m => m.Id == leaderPrize.UserId) ?? new Users();
  200. // if(user.UserType == 1)
  201. // {
  202. // OpModels.SysAdmin opSys = opdb.SysAdmin.FirstOrDefault(m => m.UserId == user.Id);
  203. // if(opSys != null)
  204. // {
  205. // // checkDate = opSys.CreateDate.Value.AddMonths(-1).ToString("yyyyMM");
  206. // }
  207. // }
  208. // else if(user.LeaderLevel == 2)
  209. // {
  210. // Leaders leader = db.Leaders.FirstOrDefault(m => m.Id == user.Id);
  211. // if(leader != null)
  212. // {
  213. // // checkDate = leader.LastBuyDate.Value.AddMonths(-1).ToString("yyyyMM");
  214. // }
  215. // }
  216. string CheckJson = "\"" + MonthFlag + "\":1";
  217. string NavUserId = "," + user.Id + ",";
  218. int person = 0;
  219. int directPerson = 0;
  220. int totalPerson = db.LeaderCompTmp.Count(m => (m.ParentNav.Contains(NavUserId)) && m.EveryMonthData.Contains(CheckJson));
  221. var subusers = db.Users.Select(m => new { m.Id, m.ParentUserId }).Where(m => m.ParentUserId == user.Id).ToList();
  222. foreach(var subuser in subusers)
  223. {
  224. string subNavUserId = "," + subuser.Id + ",";
  225. if(db.LeaderCompTmp.Count(m => (m.ParentNav.Contains(subNavUserId) || m.Id == subuser.Id) && m.EveryMonthData.Contains(CheckJson)) > 0)
  226. {
  227. person += 1;
  228. }
  229. if(db.LeaderCompTmp.Any(m => m.Id == subuser.Id && m.EveryMonthData.Contains(CheckJson)))
  230. {
  231. directPerson += 1;
  232. }
  233. }
  234. // List<LeaderCompPrize> list = db.LeaderCompPrize.Where(m => m.StatMonth == MonthFlag && m.ParentUserId == user.Id).ToList();
  235. // foreach(LeaderCompPrize sub in list)
  236. // {
  237. // if(sub.DirectCount + sub.NotDirectCount > 0)
  238. // {
  239. // person += 1;
  240. // }
  241. // totalPerson += sub.DirectCount + sub.NotDirectCount;
  242. // }
  243. int maxPerson = person * person;
  244. decimal CompPrize = 0;
  245. if(totalPerson >= maxPerson)
  246. {
  247. CompPrize = GetPrize(maxPerson - person, person);
  248. }
  249. else
  250. {
  251. CompPrize = GetPrize(totalPerson - person, person);
  252. }
  253. LeaderCompPrize edit = db.LeaderCompPrize.FirstOrDefault(m => m.StatMonth == MonthFlag && m.UserId == leaderPrize.UserId);
  254. if(edit != null)
  255. {
  256. edit.DirectCount = directPerson;
  257. edit.NotDirectCount = totalPerson - person;
  258. edit.SecDirectCount = person;
  259. edit.CompPrize = CompPrize;
  260. }
  261. db.SaveChanges();
  262. }
  263. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  264. db.Dispose();
  265. opdb.Dispose();
  266. }
  267. private decimal GetPrize(int count, int direct)
  268. {
  269. decimal prize = 0;
  270. if (count + direct >= 30)
  271. {
  272. prize = 3000 * (count + direct * 2);
  273. }
  274. else if (count + direct >= 25)
  275. {
  276. prize = 2500 * (count + direct * 2);
  277. }
  278. else if (count + direct >= 20)
  279. {
  280. prize = 2000 * (count + direct * 2);
  281. }
  282. else if (count + direct >= 15)
  283. {
  284. prize = 1500 * (count + direct * 2);
  285. }
  286. else if (count + direct >= 10)
  287. {
  288. prize = 1000 * (count + direct * 2);
  289. }
  290. else if (count + direct >= 5)
  291. {
  292. prize = 500 * (count + direct * 2);
  293. }
  294. return prize;
  295. }
  296. public void doEverday(string month, string checkMonth)
  297. {
  298. try
  299. {
  300. WebCMSEntities db = new WebCMSEntities();
  301. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  302. string MonthString = month + "-" + checkMonth;
  303. //统计交易增量
  304. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  305. 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);
  306. string sql = "";
  307. int num = 0;
  308. foreach(DataRow dr in dt.Rows)
  309. {
  310. sql += dr[0].ToString();
  311. num += 1;
  312. if(num >= 200)
  313. {
  314. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  315. sql = "";
  316. num = 0;
  317. }
  318. }
  319. if(!string.IsNullOrEmpty(sql))
  320. {
  321. CustomerSqlConn.op(sql, MysqlConn.SqlConnStr);
  322. }
  323. 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);
  324. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  325. //计算创客名下的达标人数,直推和间接
  326. function.WriteLog("MonthString:" + MonthString, "领导人达标奖励日志");
  327. List<LeaderCompTradeStatBak> stats = db.LeaderCompTradeStatBak.Where(m => m.StatMonth == MonthString).ToList();
  328. function.WriteLog(stats.Count.ToString(), "领导人达标奖励日志");
  329. foreach(LeaderCompTradeStatBak stat in stats)
  330. {
  331. decimal totalAmount = stat.SelfTradeAmount - stat.CheckSelfTradeAmount; //当月交易
  332. List<LeaderCompTradeStatBak> directs = stats.Where(m => m.ParentUserId == stat.UserId).ToList();
  333. foreach(LeaderCompTradeStatBak direct in directs)
  334. {
  335. decimal parentTotalAmount = direct.TradeAmount - direct.CheckTradeAmount;
  336. if(parentTotalAmount < 30000000)
  337. {
  338. totalAmount += parentTotalAmount;
  339. }
  340. }
  341. function.WriteLog("UserId:" + stat.UserId, "领导人达标奖励日志");
  342. function.WriteLog("totalAmount:" + totalAmount, "领导人达标奖励日志");
  343. if(totalAmount >= 30000000 && !string.IsNullOrEmpty(stat.ParentNav))
  344. {
  345. LeaderCompTradeStatBak statEdit = db.LeaderCompTradeStatBak.FirstOrDefault(m => m.UserId == stat.UserId && m.StatMonth == MonthString);
  346. if(statEdit != null)
  347. {
  348. statEdit.ResultTradeAmount = totalAmount;
  349. db.SaveChanges();
  350. }
  351. LeaderCompTmpBak tmp = db.LeaderCompTmpBak.FirstOrDefault(m => m.Id == stat.UserId);
  352. if(tmp == null)
  353. {
  354. tmp = db.LeaderCompTmpBak.Add(new LeaderCompTmpBak()
  355. {
  356. Id = stat.UserId,
  357. ParentUserId = stat.ParentUserId,
  358. ParentNav = stat.ParentNav
  359. }).Entity;
  360. db.SaveChanges();
  361. }
  362. Dictionary<string, object> EveryMonthData = new Dictionary<string, object>();
  363. if(!string.IsNullOrEmpty(tmp.EveryMonthData))
  364. {
  365. EveryMonthData = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(tmp.EveryMonthData);
  366. }
  367. if(!EveryMonthData.ContainsKey(MonthString))
  368. {
  369. EveryMonthData.Add(MonthString, 1);
  370. }
  371. else
  372. {
  373. EveryMonthData[MonthString] = 1;
  374. }
  375. tmp.EveryMonthData = Newtonsoft.Json.JsonConvert.SerializeObject(EveryMonthData);
  376. db.SaveChanges();
  377. string[] parents = stat.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  378. Array.Reverse(parents);
  379. int index = 0;
  380. foreach(string parent in parents)
  381. {
  382. index += 1;
  383. int ParentUserId = int.Parse(function.CheckInt(parent));
  384. LeaderCompPrizeBak prize = db.LeaderCompPrizeBak.FirstOrDefault(m => m.UserId == ParentUserId && m.StatMonth == MonthString);
  385. if(prize == null)
  386. {
  387. Users user = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  388. prize = db.LeaderCompPrizeBak.Add(new LeaderCompPrizeBak()
  389. {
  390. CreateDate = DateTime.Now,
  391. UserId = ParentUserId,
  392. ParentUserId = user.ParentUserId,
  393. ParentNav = user.ParentNav,
  394. StatMonth = MonthString,
  395. }).Entity;
  396. db.SaveChanges();
  397. }
  398. if(index == 1)
  399. {
  400. prize.DirectCount += 1;
  401. }
  402. else
  403. {
  404. prize.NotDirectCount += 1;
  405. }
  406. db.SaveChanges();
  407. }
  408. }
  409. function.WriteLog(DateTime.Now.ToString() + "------" + stat.UserId, "领导人达标奖励日志");
  410. }
  411. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  412. db.Dispose();
  413. }
  414. catch(Exception ex)
  415. {
  416. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "领导达标人奖励异常");
  417. }
  418. }
  419. private void SendPrizeEveryDay(string month, string checkDate)
  420. {
  421. string MonthFlag = month + "-" + checkDate;
  422. WebCMSEntities db = new WebCMSEntities();
  423. OpModels.WebCMSEntities opdb = new OpModels.WebCMSEntities();
  424. //计算运营中心或大盟主达标奖励
  425. List<LeaderCompPrizeBak> leaderPrizes = db.LeaderCompPrizeBak.Where(m => m.StatMonth == MonthFlag).ToList();
  426. function.WriteLog(leaderPrizes.Count.ToString(), "领导人达标奖励日志");
  427. foreach(LeaderCompPrizeBak leaderPrize in leaderPrizes)
  428. {
  429. Users user = db.Users.FirstOrDefault(m => m.Id == leaderPrize.UserId) ?? new Users();
  430. string CheckJson = "\"" + MonthFlag + "\":1";
  431. string NavUserId = "," + user.Id + ",";
  432. int person = 0;
  433. int directPerson = 0;
  434. int totalPerson = db.LeaderCompTmpBak.Count(m => (m.ParentNav.Contains(NavUserId)) && m.EveryMonthData.Contains(CheckJson));
  435. var subusers = db.Users.Select(m => new { m.Id, m.ParentUserId }).Where(m => m.ParentUserId == user.Id).ToList();
  436. foreach(var subuser in subusers)
  437. {
  438. string subNavUserId = "," + subuser.Id + ",";
  439. if(db.LeaderCompTmpBak.Count(m => (m.ParentNav.Contains(subNavUserId) || m.Id == subuser.Id) && m.EveryMonthData.Contains(CheckJson)) > 0)
  440. {
  441. person += 1;
  442. }
  443. if(db.LeaderCompTmpBak.Any(m => m.Id == subuser.Id && m.EveryMonthData.Contains(CheckJson)))
  444. {
  445. directPerson += 1;
  446. }
  447. }
  448. int maxPerson = person * person;
  449. decimal CompPrize = 0;
  450. if(totalPerson >= maxPerson)
  451. {
  452. CompPrize = GetPrize(maxPerson - person, person);
  453. }
  454. else
  455. {
  456. CompPrize = GetPrize(totalPerson - person, person);
  457. }
  458. LeaderCompPrizeBak edit = db.LeaderCompPrizeBak.FirstOrDefault(m => m.StatMonth == MonthFlag && m.UserId == leaderPrize.UserId);
  459. if(edit != null)
  460. {
  461. edit.DirectCount = directPerson;
  462. edit.NotDirectCount = totalPerson - person;
  463. edit.SecDirectCount = person;
  464. edit.CompPrize = CompPrize;
  465. }
  466. db.SaveChanges();
  467. }
  468. function.WriteLog(DateTime.Now.ToString(), "领导人达标奖励日志");
  469. db.Dispose();
  470. opdb.Dispose();
  471. }
  472. }
  473. }