ActiveRewardService.cs 30 KB

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