LeaderCompPrizeHelper.cs 28 KB

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