OperatePrizeService.cs 5.3 KB

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