BalancePayBackService.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using System.Threading;
  6. using MySystem.PxcModels;
  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. function.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. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId);
  58. if (account != null)
  59. {
  60. if(account.BalanceAmount >= TotalPrice)
  61. {
  62. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  63. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  64. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  65. account.BalanceAmount -= TotalPrice;
  66. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  67. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  68. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  69. db.SaveChanges();
  70. UserAccountRecord accountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  71. {
  72. CreateDate = DateTime.Now,
  73. UpdateDate = DateTime.Now,
  74. UserId = order.UserId, //创客
  75. ChangeType = 20, //变动类型
  76. ChangeAmount = TotalPrice, //变更金额
  77. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  78. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  79. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  80. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  81. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  82. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  83. TransRecordNo = order.OrderNo, //交易流水编号
  84. Remark = "",
  85. }).Entity;
  86. db.SaveChanges();
  87. AlipayPayBack2Service.Instance.DoOrder(db, OrderId);
  88. }
  89. }
  90. }
  91. db.Dispose();
  92. }
  93. }
  94. }