ActiveRewardService.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using System.Threading;
  6. using MySystem.Models.Main;
  7. using Library;
  8. using LitJson;
  9. namespace MySystem
  10. {
  11. public class ActiveRewardService
  12. {
  13. public readonly static ActiveRewardService Instance = new ActiveRewardService();
  14. private ActiveRewardService()
  15. { }
  16. //激活奖
  17. public void StartAct()
  18. {
  19. Thread th = new Thread(StartActListen);
  20. th.IsBackground = true;
  21. th.Start();
  22. }
  23. private void StartActListen()
  24. {
  25. while(true)
  26. {
  27. if(RedisDbconn.Instance.Get<string>("ActiveRewardThreadStatus") == "1")
  28. {
  29. StartActDo();
  30. Thread.Sleep(5000);
  31. }
  32. }
  33. }
  34. public void StartActDo()
  35. {
  36. WebCMSEntities db = new WebCMSEntities();
  37. Models.KxsMain.WebCMSEntities kxsdb = new Models.KxsMain.WebCMSEntities();
  38. DateTime yesterday = DateTime.Now.AddDays(-1);
  39. IQueryable<MerchantDepositOrder> orderList = db.MerchantDepositOrder.Where(m => m.Status == 1 && m.UpdateDate >= yesterday);
  40. foreach (MerchantDepositOrder order in orderList.ToList())
  41. {
  42. doActiveReward(db, kxsdb, order, 300);
  43. RedisDbconn.Instance.AddList("OpenRewardQueue2", order.MerchantId);
  44. RedisDbconn.Instance.AddList("LeaderPrizeQueue2", order.MerchantId);
  45. RedisDbconn.Instance.AddList("OperatePrizeQueue2", order.MerchantId);
  46. MerchantDepositOrder edit = db.MerchantDepositOrder.FirstOrDefault(m => m.Id == order.Id);
  47. if(edit != null)
  48. {
  49. edit.Status = 2;
  50. db.SaveChanges();
  51. Utils.Instance.SendActData(db.MerchantInfo.FirstOrDefault(m => m.Id == edit.MerchantId) ?? new MerchantInfo(), 4);
  52. }
  53. }
  54. db.Dispose();
  55. kxsdb.Dispose();
  56. }
  57. public void doActiveReward(WebCMSEntities db, Models.KxsMain.WebCMSEntities kxsdb, MerchantDepositOrder order, decimal ActPrize)
  58. {
  59. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == order.MerchantId) ?? new MerchantInfo();
  60. Models.KxsMain.Users user = kxsdb.Users.FirstOrDefault(m => m.Id == merchant.UserId) ?? new Models.KxsMain.Users();
  61. int GetUserId = user.Id;
  62. string ParentNav = user.ParentNav;
  63. int TopUserId = 0;
  64. if (!string.IsNullOrEmpty(ParentNav))
  65. {
  66. TopUserId = int.Parse(ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  67. }
  68. string IdBrand = GetUserId + "_0";
  69. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  70. if (userData == null)
  71. {
  72. userData = db.UserMachineData.Add(new UserMachineData()
  73. {
  74. IdBrand = IdBrand,
  75. }).Entity;
  76. db.SaveChanges();
  77. }
  78. userData.ActProfit += ActPrize;
  79. db.SaveChanges();
  80. Models.KxsMain.UserAccount account = kxsdb.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  81. if (account == null)
  82. {
  83. account = kxsdb.UserAccount.Add(new Models.KxsMain.UserAccount()
  84. {
  85. Id = GetUserId,
  86. UserId = GetUserId,
  87. }).Entity;
  88. kxsdb.SaveChanges();
  89. }
  90. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  91. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  92. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  93. account.BalanceAmount += ActPrize;
  94. account.TotalAmount += ActPrize;
  95. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  96. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  97. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  98. Models.KxsMain.UserAccountRecord userAccountRecord = kxsdb.UserAccountRecord.Add(new Models.KxsMain.UserAccountRecord()
  99. {
  100. CreateDate = DateTime.Now,
  101. UpdateDate = DateTime.Now,
  102. UserId = GetUserId, //创客
  103. ChangeType = 311, //变动类型
  104. ChangeAmount = ActPrize, //变更金额
  105. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  106. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  107. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  108. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  109. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  110. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  111. QueryCount = merchant.Id,
  112. }).Entity;
  113. kxsdb.SaveChanges();
  114. }
  115. //开户奖
  116. public void StartOpenReward()
  117. {
  118. Thread th = new Thread(StartOpenRewardDo);
  119. th.IsBackground = true;
  120. th.Start();
  121. }
  122. public void StartOpenRewardDo()
  123. {
  124. while(true)
  125. {
  126. string content = RedisDbconn.Instance.RPop<string>("OpenRewardQueue2");
  127. if(!string.IsNullOrEmpty(content))
  128. {
  129. try
  130. {
  131. WebCMSEntities db = new WebCMSEntities();
  132. Models.KxsMain.WebCMSEntities kxsdb = new Models.KxsMain.WebCMSEntities();
  133. function.WriteLog("\n\n" + DateTime.Now.ToString() + "\nposid:" + content, "开机奖励发放日志");
  134. int MerchantId = int.Parse(content);
  135. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
  136. if (merchant != null)
  137. {
  138. function.WriteLog("商户:" + merchant.Name, "开机奖励发放日志");
  139. Models.KxsMain.Users user = kxsdb.Users.FirstOrDefault(m => m.Id == merchant.UserId);
  140. if (user != null)
  141. {
  142. function.WriteLog("所属人:" + user.MakerCode, "开机奖励发放日志");
  143. string ParentNav = function.CheckNull(user.ParentNav) + "," + user.Id + ",";
  144. if (!string.IsNullOrEmpty(ParentNav))
  145. {
  146. decimal Prize = 20;
  147. string[] ParentNavs = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  148. for (int i = ParentNavs.Length - 1; i >= 0; i--)
  149. {
  150. int UserId = int.Parse(ParentNavs[i]);
  151. Models.KxsMain.Users puser = kxsdb.Users.FirstOrDefault(m => m.Id == UserId && m.AuthFlag == 1 && m.Status > -1);
  152. if (puser != null && Prize > 0)
  153. {
  154. function.WriteLog("上级:" + puser.MakerCode, "开机奖励发放日志");
  155. function.WriteLog("条件:LeaderLevel:" + puser.LeaderLevel + ",UserType:" + puser.UserType, "开机奖励发放日志");
  156. if(puser.LeaderLevel > 0 || puser.UserType == 1)
  157. {
  158. bool leaderOp = true;
  159. if(puser.LeaderLevel > 0 && puser.UserType == 0)
  160. {
  161. DateTime now = DateTime.Now;
  162. leaderOp = kxsdb.Leaders.Any(m => m.Id == puser.Id && m.ExpiredDate >= now);
  163. function.WriteLog("条件:leaderOp:" + leaderOp, "开机奖励发放日志");
  164. }
  165. if(leaderOp)
  166. {
  167. function.WriteLog("满足条件", "开机奖励发放日志");
  168. int pTopUserId = 0;
  169. if (!string.IsNullOrEmpty(puser.ParentNav))
  170. {
  171. pTopUserId = int.Parse(puser.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  172. }
  173. string IdBrand = puser.Id + "_0";
  174. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  175. if (userData == null)
  176. {
  177. userData = db.UserMachineData.Add(new UserMachineData()
  178. {
  179. IdBrand = IdBrand,
  180. }).Entity;
  181. db.SaveChanges();
  182. }
  183. userData.OpenProfit += Prize;
  184. db.SaveChanges();
  185. //账户入库
  186. Models.KxsMain.UserAccount account = kxsdb.UserAccount.FirstOrDefault(m => m.Id == puser.Id);
  187. if (account == null)
  188. {
  189. account = kxsdb.UserAccount.Add(new Models.KxsMain.UserAccount()
  190. {
  191. Id = puser.Id,
  192. UserId = puser.Id,
  193. }).Entity;
  194. kxsdb.SaveChanges();
  195. }
  196. //收支明细入库
  197. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  198. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  199. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  200. account.BalanceAmount += Prize;
  201. account.TotalAmount += Prize;
  202. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  203. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  204. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  205. Models.KxsMain.UserAccountRecord userAccountRecord = kxsdb.UserAccountRecord.Add(new Models.KxsMain.UserAccountRecord()
  206. {
  207. CreateDate = DateTime.Now,
  208. UpdateDate = DateTime.Now,
  209. UserId = puser.Id, //创客
  210. ChangeType = 312, //变动类型
  211. ChangeAmount = Prize, //变更金额
  212. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  213. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  214. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  215. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  216. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  217. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  218. QueryCount = MerchantId,
  219. }).Entity;
  220. kxsdb.SaveChanges();
  221. Prize = 0;
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }
  228. }
  229. db.Dispose();
  230. kxsdb.Dispose();
  231. }
  232. catch(Exception ex)
  233. {
  234. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "实时获取开机奖励异常");
  235. }
  236. }
  237. else
  238. {
  239. Thread.Sleep(5000);
  240. }
  241. }
  242. }
  243. //盟主奖
  244. public void StartLeaderReward()
  245. {
  246. Thread th = new Thread(StartLeaderRewardDo);
  247. th.IsBackground = true;
  248. th.Start();
  249. }
  250. public void StartLeaderRewardDo()
  251. {
  252. while (true)
  253. {
  254. string content = RedisDbconn.Instance.RPop<string>("LeaderPrizeQueue2");
  255. if (!string.IsNullOrEmpty(content))
  256. {
  257. try
  258. {
  259. WebCMSEntities db = new WebCMSEntities();
  260. Models.KxsMain.WebCMSEntities kxsdb = new Models.KxsMain.WebCMSEntities();
  261. function.WriteLog("\n\n" + DateTime.Now.ToString() + "\nposid:" + content, "盟主奖励发放日志");
  262. int MerchantId = int.Parse(content);
  263. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
  264. if (merchant != null)
  265. {
  266. function.WriteLog("商户:" + merchant.Name, "盟主奖励发放日志");
  267. Models.KxsMain.Users user = kxsdb.Users.FirstOrDefault(m => m.Id == merchant.UserId);
  268. if (user != null)
  269. {
  270. function.WriteLog("所属人:" + user.MakerCode, "盟主奖励发放日志");
  271. string ParentNav = function.CheckNull(user.ParentNav) + "," + user.Id + ",";
  272. if (!string.IsNullOrEmpty(ParentNav))
  273. {
  274. decimal Prize = 10;
  275. string[] ParentNavs = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  276. for (int i = ParentNavs.Length - 1; i >= 0; i--)
  277. {
  278. int UserId = int.Parse(ParentNavs[i]);
  279. Models.KxsMain.Users puser = kxsdb.Users.FirstOrDefault(m => m.Id == UserId && m.AuthFlag == 1 && m.Status > -1);
  280. if (puser != null && Prize > 0)
  281. {
  282. function.WriteLog("上级:" + puser.MakerCode, "盟主奖励发放日志");
  283. function.WriteLog("条件:LeaderLevel:" + puser.LeaderLevel + ",UserType:" + puser.UserType, "盟主奖励发放日志");
  284. if(puser.LeaderLevel == 2)
  285. {
  286. DateTime now = DateTime.Now;
  287. bool leaderOp = kxsdb.Leaders.Any(m => m.Id == puser.Id && m.ExpiredDate >= now);
  288. function.WriteLog("条件:leaderOp:" + leaderOp, "盟主奖励发放日志");
  289. if(leaderOp)
  290. {
  291. function.WriteLog("满足条件", "盟主奖励发放日志");
  292. int pTopUserId = 0;
  293. if (!string.IsNullOrEmpty(puser.ParentNav))
  294. {
  295. pTopUserId = int.Parse(puser.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  296. }
  297. string IdBrand = puser.Id + "_0";
  298. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  299. if (userData == null)
  300. {
  301. userData = db.UserMachineData.Add(new UserMachineData()
  302. {
  303. IdBrand = IdBrand,
  304. }).Entity;
  305. db.SaveChanges();
  306. }
  307. userData.OpenProfit += Prize;
  308. db.SaveChanges();
  309. //账户入库
  310. Models.KxsMain.UserAccount account = kxsdb.UserAccount.FirstOrDefault(m => m.Id == puser.Id);
  311. if (account == null)
  312. {
  313. account = kxsdb.UserAccount.Add(new Models.KxsMain.UserAccount()
  314. {
  315. Id = puser.Id,
  316. UserId = puser.Id,
  317. }).Entity;
  318. kxsdb.SaveChanges();
  319. }
  320. //收支明细入库
  321. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  322. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  323. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  324. account.BalanceAmount += Prize;
  325. account.TotalAmount += Prize;
  326. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  327. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  328. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  329. Models.KxsMain.UserAccountRecord userAccountRecord = kxsdb.UserAccountRecord.Add(new Models.KxsMain.UserAccountRecord()
  330. {
  331. CreateDate = DateTime.Now,
  332. UpdateDate = DateTime.Now,
  333. UserId = puser.Id, //创客
  334. ChangeType = 313, //变动类型
  335. ChangeAmount = Prize, //变更金额
  336. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  337. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  338. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  339. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  340. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  341. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  342. QueryCount = MerchantId,
  343. }).Entity;
  344. kxsdb.SaveChanges();
  345. Prize = 0;
  346. }
  347. }
  348. }
  349. }
  350. }
  351. }
  352. }
  353. db.Dispose();
  354. kxsdb.Dispose();
  355. }
  356. catch (Exception ex)
  357. {
  358. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "盟主奖发放日志异常");
  359. }
  360. Thread.Sleep(100);
  361. }
  362. else
  363. {
  364. Thread.Sleep(60000);
  365. }
  366. }
  367. }
  368. //运营中心奖
  369. public void StartOperateReward()
  370. {
  371. Thread th = new Thread(StartOperateRewardDo);
  372. th.IsBackground = true;
  373. th.Start();
  374. }
  375. public void StartOperateRewardDo()
  376. {
  377. while (true)
  378. {
  379. string content = RedisDbconn.Instance.RPop<string>("OperatePrizeQueue2");
  380. if (!string.IsNullOrEmpty(content))
  381. {
  382. try
  383. {
  384. WebCMSEntities db = new WebCMSEntities();
  385. Models.KxsMain.WebCMSEntities kxsdb = new Models.KxsMain.WebCMSEntities();
  386. function.WriteLog("\n\n" + DateTime.Now.ToString() + "\nposid:" + content, "运营中心奖励发放日志");
  387. int MerchantId = int.Parse(content);
  388. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
  389. if (merchant != null)
  390. {
  391. function.WriteLog("商户:" + merchant.Name, "运营中心奖励发放日志");
  392. Models.KxsMain.Users user = kxsdb.Users.FirstOrDefault(m => m.Id == merchant.UserId);
  393. if (user != null)
  394. {
  395. function.WriteLog("所属人:" + user.MakerCode, "运营中心奖励发放日志");
  396. string ParentNav = function.CheckNull(user.ParentNav) + "," + user.Id + ",";
  397. if (!string.IsNullOrEmpty(ParentNav))
  398. {
  399. decimal Prize = 5;
  400. string[] ParentNavs = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  401. for (int i = ParentNavs.Length - 1; i >= 0; i--)
  402. {
  403. int UserId = int.Parse(ParentNavs[i]);
  404. Models.KxsMain.Users puser = kxsdb.Users.FirstOrDefault(m => m.Id == UserId && m.AuthFlag == 1 && m.Status > -1);
  405. if (puser != null && Prize > 0)
  406. {
  407. function.WriteLog("上级:" + puser.MakerCode, "运营中心奖励发放日志");
  408. function.WriteLog("条件:LeaderLevel:" + puser.LeaderLevel + ",UserType:" + puser.UserType, "运营中心奖励发放日志");
  409. if(puser.UserType == 1)
  410. {
  411. function.WriteLog("满足条件", "运营中心奖励发放日志");
  412. int pTopUserId = 0;
  413. if (!string.IsNullOrEmpty(puser.ParentNav))
  414. {
  415. pTopUserId = int.Parse(puser.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  416. }
  417. string IdBrand = puser.Id + "_0";
  418. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  419. if (userData == null)
  420. {
  421. userData = db.UserMachineData.Add(new UserMachineData()
  422. {
  423. IdBrand = IdBrand,
  424. }).Entity;
  425. db.SaveChanges();
  426. }
  427. userData.OpenProfit += Prize;
  428. db.SaveChanges();
  429. //账户入库
  430. Models.KxsMain.UserAccount account = kxsdb.UserAccount.FirstOrDefault(m => m.Id == puser.Id);
  431. if (account == null)
  432. {
  433. account = kxsdb.UserAccount.Add(new Models.KxsMain.UserAccount()
  434. {
  435. Id = puser.Id,
  436. UserId = puser.Id,
  437. }).Entity;
  438. kxsdb.SaveChanges();
  439. }
  440. //收支明细入库
  441. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  442. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  443. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  444. account.BalanceAmount += Prize;
  445. account.TotalAmount += Prize;
  446. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  447. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  448. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  449. Models.KxsMain.UserAccountRecord userAccountRecord = kxsdb.UserAccountRecord.Add(new Models.KxsMain.UserAccountRecord()
  450. {
  451. CreateDate = DateTime.Now,
  452. UpdateDate = DateTime.Now,
  453. UserId = puser.Id, //创客
  454. ChangeType = 314, //变动类型
  455. ChangeAmount = Prize, //变更金额
  456. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  457. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  458. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  459. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  460. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  461. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  462. QueryCount = MerchantId,
  463. }).Entity;
  464. kxsdb.SaveChanges();
  465. Prize = 0;
  466. }
  467. }
  468. }
  469. }
  470. }
  471. }
  472. db.Dispose();
  473. kxsdb.Dispose();
  474. }
  475. catch (Exception ex)
  476. {
  477. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "运营中心奖发放日志异常");
  478. }
  479. Thread.Sleep(100);
  480. }
  481. else
  482. {
  483. Thread.Sleep(60000);
  484. }
  485. }
  486. }
  487. }
  488. }