LeaderPrizeService.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 LeaderPrizeService
  12. {
  13. public readonly static LeaderPrizeService Instance = new LeaderPrizeService();
  14. private LeaderPrizeService()
  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>("LeaderPrizeQueue");
  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 == 116))
  34. {
  35. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == PosId && m.LeaderUserId > 0);
  36. if (pos != null)
  37. {
  38. decimal ChangeAmount = 10;
  39. if(pos.BrandId == 14)
  40. {
  41. ChangeAmount = 19 * 0.15M;
  42. }
  43. if(pos.BrandId == 23 || pos.BrandId == 24 || pos.BrandId == 25 || pos.BrandId == 26)
  44. {
  45. ChangeAmount = 4.5M;
  46. }
  47. int LeaderUserId = pos.LeaderUserId;
  48. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId);
  49. if (account == null)
  50. {
  51. account = db.UserAccount.Add(new UserAccount()
  52. {
  53. Id = LeaderUserId,
  54. UserId = LeaderUserId,
  55. }).Entity;
  56. db.SaveChanges();
  57. }
  58. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  59. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  60. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  61. account.BalanceAmount += ChangeAmount;
  62. account.TotalAmount += ChangeAmount;
  63. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  64. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  65. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  66. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  67. {
  68. CreateDate = DateTime.Now,
  69. UpdateDate = DateTime.Now,
  70. UserId = LeaderUserId, //创客
  71. ProductType = pos.BrandId, //品牌
  72. ChangeType = 116, //变动类型
  73. ChangeAmount = ChangeAmount, //变更金额
  74. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  75. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  76. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  77. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  78. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  79. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  80. QueryCount = PosId,
  81. }).Entity;
  82. db.SaveChanges();
  83. }
  84. }
  85. db.Dispose();
  86. }
  87. catch (Exception ex)
  88. {
  89. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "大盟主奖励异常");
  90. }
  91. Thread.Sleep(100);
  92. }
  93. else
  94. {
  95. Thread.Sleep(60000);
  96. }
  97. }
  98. }
  99. }
  100. }