OperateAmountService.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.OpModels;
  9. namespace MySystem
  10. {
  11. public class OperateAmountService
  12. {
  13. public readonly static OperateAmountService Instance = new OperateAmountService();
  14. private OperateAmountService()
  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>("OperateAmountQueue");
  27. if (!string.IsNullOrEmpty(data))
  28. {
  29. try
  30. {
  31. WebCMSEntities db = new WebCMSEntities();
  32. function.WriteLog(DateTime.Now.ToString() + "\r\n" + data + "\r\n\r\n", "运营中心返额度日志");
  33. JsonData jsonObj = JsonMapper.ToObject(data);
  34. int UserId = int.Parse(function.CheckInt(jsonObj["UserId"].ToString())); //运营中心所属人创客Id
  35. int DataId = int.Parse(function.CheckInt(jsonObj["DataId"].ToString())); //机具Id或机具券Id
  36. int Kind = int.Parse(function.CheckInt(jsonObj["Kind"].ToString())); //1-机具,2-机具券
  37. decimal Amount = decimal.Parse(function.CheckNum(jsonObj["Amount"].ToString()));
  38. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  39. if (account == null)
  40. {
  41. account = db.UserAccount.Add(new UserAccount()
  42. {
  43. Id = UserId,
  44. UserId = UserId,
  45. }).Entity;
  46. db.SaveChanges();
  47. }
  48. decimal BeforeAmount = account.ValidAmount; //变更前总金额
  49. account.ValidAmount += Amount;
  50. decimal AfterAmount = account.ValidAmount; //变更后总金额
  51. AmountRecord add = db.AmountRecord.Add(new AmountRecord()
  52. {
  53. CreateDate = DateTime.Now,
  54. UpdateDate = DateTime.Now,
  55. OperateType = 1,
  56. AfterAmount = AfterAmount,
  57. AfterValidForGetAmount = account.ValidForGetAmount,
  58. AfterTotalAmt = account.TotalAmt,
  59. AfterValidAmount = account.ValidAmount,
  60. BeforeAmount = BeforeAmount,
  61. UseAmount = Amount,
  62. UserId = UserId,
  63. ApplyId = DataId,
  64. SeoDescription = Kind == 1 ? "机具激活返还" : "机具券兑换返还",
  65. Version = 3,
  66. }).Entity;
  67. db.SaveChanges();
  68. AmountChangeRecord amountChangeRecord = db.AmountChangeRecord.Add(new AmountChangeRecord()
  69. {
  70. AmountType = 2,//1 未使用额度 2 可提现额度 3 关联分仓额度
  71. CreateDate = DateTime.Now,
  72. Title = Kind == 1 ? "机具激活返还" : "机具券兑换返还",
  73. UserId = UserId, //运营中心Id
  74. BeforeAmount = BeforeAmount,//使用前剩余额度
  75. AfterAmount = AfterAmount,//使用后剩余额度
  76. ChangeAmount = Amount,//操作金额
  77. OperateType = 1,//操作类别
  78. }).Entity;
  79. db.Dispose();
  80. // OpAmountItem item = new OpAmountItem()
  81. // {
  82. // UserId = UserId,
  83. // OperateType = 1,
  84. // ChangeType = Kind == 1 ? 9 : 23,
  85. // Remark = Kind == 1 ? "机具激活返还" : "机具券兑换返还",
  86. // UseAmount = Amount,
  87. // UseValidAmount = Amount,
  88. // DataType = Kind == 1 ? 2 : 6,
  89. // DataId = DataId,
  90. // };
  91. // RedisDbconn.Instance.AddList("OperateAmountRecordServiceQueue", item);
  92. }
  93. catch (Exception ex)
  94. {
  95. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "运营中心返额度异常");
  96. }
  97. }
  98. else
  99. {
  100. Thread.Sleep(5000);
  101. }
  102. }
  103. }
  104. }
  105. }