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