OperatePrizeService.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. if(pos.BrandId == 23 || pos.BrandId == 24 || pos.BrandId == 25 || pos.BrandId == 26)
  63. {
  64. PxcModels.WifiTradeRecord trade = db.WifiTradeRecord.FirstOrDefault(m => m.SnNo == pos.PosSn);
  65. if (trade != null)
  66. {
  67. ChangeAmount = trade.TradeAmount * 0.005M;
  68. ChangeAmount = PublicFunction.NumberFormat(ChangeAmount);
  69. }
  70. }
  71. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == OpId);
  72. if (account == null)
  73. {
  74. account = db.UserAccount.Add(new UserAccount()
  75. {
  76. Id = OpId,
  77. UserId = OpId,
  78. }).Entity;
  79. db.SaveChanges();
  80. }
  81. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  82. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  83. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  84. account.BalanceAmount += ChangeAmount;
  85. account.TotalAmount += ChangeAmount;
  86. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  87. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  88. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  89. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  90. {
  91. CreateDate = DateTime.Now,
  92. UpdateDate = DateTime.Now,
  93. UserId = OpId, //创客
  94. ProductType = pos.BrandId, //品牌
  95. ChangeType = 121, //变动类型
  96. ChangeAmount = ChangeAmount, //变更金额
  97. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  98. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  99. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  100. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  101. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  102. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  103. QueryCount = PosId,
  104. }).Entity;
  105. db.SaveChanges();
  106. //发送APP推送消息
  107. Utils.Instance.PrizePush(OpId, ChangeAmount);
  108. }
  109. }
  110. db.Dispose();
  111. spdb.Dispose();
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "运营中心奖励异常");
  117. }
  118. Thread.Sleep(100);
  119. }
  120. else
  121. {
  122. Thread.Sleep(60000);
  123. }
  124. }
  125. }
  126. }
  127. }