OperateAmountService.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. BeforeAmount = BeforeAmount,
  58. UseAmount = Amount,
  59. UserId = UserId,
  60. ApplyId = DataId,
  61. SeoDescription = Kind == 1 ? "机具激活返还" : "机具券兑换返还",
  62. }).Entity;
  63. db.SaveChanges();
  64. db.Dispose();
  65. }
  66. catch (Exception ex)
  67. {
  68. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "运营中心返额度异常");
  69. }
  70. }
  71. else
  72. {
  73. Thread.Sleep(5000);
  74. }
  75. }
  76. }
  77. }
  78. }