OperatePrizeService.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 OperatePrizeService
  12. {
  13. public readonly static OperatePrizeService Instance = new OperatePrizeService();
  14. private OperatePrizeService()
  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>("OperatePrizeQueue");
  27. if (!string.IsNullOrEmpty(data))
  28. {
  29. try
  30. {
  31. int PosId = int.Parse(function.CheckInt(data));
  32. WebCMSEntities db = new WebCMSEntities();
  33. OpModels.WebCMSEntities spdb = new OpModels.WebCMSEntities();
  34. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == PosId);
  35. if (pos != null)
  36. {
  37. int OpId = 0;
  38. int UserId = pos.BuyUserId;
  39. while(UserId > 0)
  40. {
  41. Users user = db.Users.FirstOrDefault(m => m.Id == UserId);
  42. if(user != null)
  43. {
  44. OpModels.SysAdmin sys = spdb.SysAdmin.FirstOrDefault(m => m.UserId == user.Id) ?? new OpModels.SysAdmin();
  45. if(user.UserType >= 1 && sys.ExpireDate > DateTime.Now)
  46. {
  47. OpId = user.Id;
  48. UserId = 0;
  49. }
  50. else
  51. {
  52. UserId = user.ParentUserId;
  53. }
  54. }
  55. else
  56. {
  57. UserId = 0;
  58. }
  59. }
  60. if(OpId > 0)
  61. {
  62. decimal ChangeAmount = 5;
  63. if(pos.BrandId == 14)
  64. {
  65. ChangeAmount = 19 * 0.05M;
  66. }
  67. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == OpId);
  68. if (account == null)
  69. {
  70. account = db.UserAccount.Add(new UserAccount()
  71. {
  72. Id = OpId,
  73. UserId = OpId,
  74. }).Entity;
  75. db.SaveChanges();
  76. }
  77. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  78. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  79. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  80. account.BalanceAmount += ChangeAmount;
  81. account.TotalAmount += ChangeAmount;
  82. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  83. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  84. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  85. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  86. {
  87. CreateDate = DateTime.Now,
  88. UpdateDate = DateTime.Now,
  89. UserId = OpId, //创客
  90. ChangeType = 121, //变动类型
  91. ChangeAmount = ChangeAmount, //变更金额
  92. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  93. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  94. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  95. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  96. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  97. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  98. QueryCount = PosId,
  99. }).Entity;
  100. db.SaveChanges();
  101. }
  102. }
  103. db.Dispose();
  104. spdb.Dispose();
  105. }
  106. catch (Exception ex)
  107. {
  108. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "运营中心奖励异常");
  109. }
  110. Thread.Sleep(100);
  111. }
  112. else
  113. {
  114. Thread.Sleep(60000);
  115. }
  116. }
  117. }
  118. }
  119. }