LeaderCompPrizeHelper.cs 34 KB

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