LeaderPrizeService.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. int LeaderUserId = pos.LeaderUserId;
  44. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == LeaderUserId);
  45. if (account == null)
  46. {
  47. account = db.UserAccount.Add(new UserAccount()
  48. {
  49. Id = LeaderUserId,
  50. UserId = LeaderUserId,
  51. }).Entity;
  52. db.SaveChanges();
  53. }
  54. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  55. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  56. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  57. account.BalanceAmount += ChangeAmount;
  58. account.TotalAmount += ChangeAmount;
  59. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  60. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  61. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  62. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  63. {
  64. CreateDate = DateTime.Now,
  65. UpdateDate = DateTime.Now,
  66. UserId = LeaderUserId, //创客
  67. ProductType = pos.BrandId, //品牌
  68. ChangeType = 116, //变动类型
  69. ChangeAmount = ChangeAmount, //变更金额
  70. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  71. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  72. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  73. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  74. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  75. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  76. QueryCount = PosId,
  77. }).Entity;
  78. db.SaveChanges();
  79. }
  80. }
  81. db.Dispose();
  82. }
  83. catch (Exception ex)
  84. {
  85. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "大盟主奖励异常");
  86. }
  87. Thread.Sleep(100);
  88. }
  89. else
  90. {
  91. Thread.Sleep(60000);
  92. }
  93. }
  94. }
  95. }
  96. }