LeaderPrizeService.cs 4.0 KB

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