LeaderPrizeService.cs 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using LitJson;
  5. using System.Linq;
  6. using System.Data;
  7. using System.Threading;
  8. using MySystem.PxcModels;
  9. namespace MySystem
  10. {
  11. public class LeaderPrizeService
  12. {
  13. public readonly static LeaderPrizeService Instance = new LeaderPrizeService();
  14. private LeaderPrizeService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(dosomething);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. public void dosomething()
  23. {
  24. while (true)
  25. {
  26. string data = RedisDbconn.Instance.RPop<string>("LeaderPrizeQueue");
  27. if (!string.IsNullOrEmpty(data))
  28. {
  29. try
  30. {
  31. int PosId = int.Parse(function.CheckInt(data));
  32. WebCMSEntities db = new WebCMSEntities();
  33. if(!db.UserAccountRecord.Any(m => m.QueryCount == PosId && m.ChangeType == 116))
  34. {
  35. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == PosId);
  36. if (pos != null)
  37. {
  38. decimal ChangeAmount = 10;
  39. if(pos.BrandId == 14)
  40. {
  41. ChangeAmount = 19 * 0.15M;
  42. }
  43. int LeaderUserId = pos.LeaderUserId;
  44. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId);
  45. if (account == null)
  46. {
  47. account = db.UserAccount.Add(new UserAccount()
  48. {
  49. Id = LeaderUserId,
  50. UserId = LeaderUserId,
  51. }).Entity;
  52. db.SaveChanges();
  53. }
  54. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  55. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  56. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  57. account.BalanceAmount += ChangeAmount;
  58. account.TotalAmount += ChangeAmount;
  59. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  60. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  61. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  62. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  63. {
  64. CreateDate = DateTime.Now,
  65. UpdateDate = DateTime.Now,
  66. UserId = LeaderUserId, //创客
  67. ChangeType = 116, //变动类型
  68. ChangeAmount = ChangeAmount, //变更金额
  69. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  70. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  71. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  72. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  73. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  74. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  75. QueryCount = PosId,
  76. }).Entity;
  77. db.SaveChanges();
  78. }
  79. }
  80. db.Dispose();
  81. }
  82. catch (Exception ex)
  83. {
  84. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "大盟主奖励异常");
  85. }
  86. Thread.Sleep(100);
  87. }
  88. else
  89. {
  90. Thread.Sleep(60000);
  91. }
  92. }
  93. }
  94. }
  95. }