BalancePayBackService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using System.Threading;
  6. using MySystem.Models;
  7. using Library;
  8. using LitJson;
  9. namespace MySystem
  10. {
  11. public class BalancePayBackService
  12. {
  13. public readonly static BalancePayBackService Instance = new BalancePayBackService();
  14. private BalancePayBackService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(dosomething);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. private void dosomething()
  23. {
  24. while (true)
  25. {
  26. try
  27. {
  28. string content = RedisDbconn.Instance.RPop<string>("BalancePayQueue");
  29. if (!string.IsNullOrEmpty(content))
  30. {
  31. sloveAlipayCallBack(content);
  32. }
  33. else
  34. {
  35. Thread.Sleep(2000);
  36. }
  37. }
  38. catch (Exception ex)
  39. {
  40. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "商城订单余额支付异常");
  41. Thread.Sleep(2000);
  42. }
  43. }
  44. }
  45. public void sloveAlipayCallBack(string content)
  46. {
  47. int OrderId = int.Parse(function.CheckInt(content));
  48. WebCMSEntities db = new WebCMSEntities();
  49. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId && m.PayMode == 3 && m.PayStatus == 0);
  50. if (order != null)
  51. {
  52. decimal TotalPrice = order.TotalPrice;
  53. if (order.UserId == 1)
  54. {
  55. TotalPrice = 0.01M;
  56. }
  57. string ProductName = "";
  58. List<OrderProduct> orderPros = db.OrderProduct.Where(m => m.OrderId == OrderId).ToList();
  59. foreach(OrderProduct orderPro in orderPros)
  60. {
  61. ProductName += orderPro.ProductName + ",";
  62. }
  63. if(ProductName.Length > 64)
  64. {
  65. ProductName = ProductName.Substring(0, 64);
  66. }
  67. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId);
  68. if (account != null)
  69. {
  70. if(account.BalanceAmount >= TotalPrice)
  71. {
  72. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  73. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  74. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  75. account.BalanceAmount -= TotalPrice;
  76. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  77. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  78. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  79. db.SaveChanges();
  80. UserAccountRecord accountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  81. {
  82. CreateDate = DateTime.Now,
  83. UpdateDate = DateTime.Now,
  84. UserId = order.UserId, //创客
  85. ChangeType = 20, //变动类型
  86. ChangeAmount = TotalPrice, //变更金额
  87. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  88. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  89. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  90. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  91. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  92. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  93. TransRecordNo = order.OrderNo, //交易流水编号
  94. Remark = "购买" + ProductName,
  95. }).Entity;
  96. db.SaveChanges();
  97. // AlipayPayBack2Service.Instance.DoOrderV2(db, OrderId);
  98. }
  99. }
  100. }
  101. db.Dispose();
  102. }
  103. }
  104. }