OperatePrizeService.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.Models;
  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. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == OpId);
  61. if (account == null)
  62. {
  63. account = db.UserAccount.Add(new UserAccount()
  64. {
  65. Id = OpId,
  66. UserId = OpId,
  67. }).Entity;
  68. db.SaveChanges();
  69. }
  70. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  71. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  72. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  73. account.BalanceAmount += 5;
  74. account.TotalAmount += 5;
  75. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  76. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  77. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  78. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  79. {
  80. CreateDate = DateTime.Now,
  81. UpdateDate = DateTime.Now,
  82. UserId = OpId, //创客
  83. ChangeType = 121, //变动类型
  84. ChangeAmount = 5, //变更金额
  85. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  86. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  87. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  88. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  89. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  90. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  91. QueryCount = PosId,
  92. }).Entity;
  93. db.SaveChanges();
  94. }
  95. }
  96. db.Dispose();
  97. }
  98. catch (Exception ex)
  99. {
  100. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "运营中心奖励异常");
  101. }
  102. Thread.Sleep(100);
  103. }
  104. else
  105. {
  106. Thread.Sleep(60000);
  107. }
  108. }
  109. }
  110. }
  111. }