OrderRefundService.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.PxcModels;
  9. namespace MySystem
  10. {
  11. public class OrderRefundService
  12. {
  13. public readonly static OrderRefundService Instance = new OrderRefundService();
  14. private OrderRefundService()
  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>("RefundQueue");
  27. if (!string.IsNullOrEmpty(data))
  28. {
  29. try
  30. {
  31. JsonData jsonOj = JsonMapper.ToObject(data);
  32. int OrderId = int.Parse(function.CheckInt(jsonOj["OrderId"].ToString()));
  33. string Operator = jsonOj["Operator"].ToString();
  34. decimal Amount = decimal.Parse(function.CheckNum(jsonOj["Amount"].ToString()));
  35. string Remark = jsonOj["Remark"].ToString();
  36. WebCMSEntities db = new WebCMSEntities();
  37. Orders order = db.Orders.FirstOrDefault(m => m.Id == OrderId);
  38. if(order != null)
  39. {
  40. bool op = true;
  41. decimal TotalPrice = order.TotalPrice;
  42. if(order.PayMode == 1)
  43. {
  44. PublicAccountSet set = db.PublicAccountSet.FirstOrDefault() ?? new PublicAccountSet();
  45. string result = new Alipay.AlipayPublicMethod().Refund(set.AlipayAppId, set.AlipayPrivateKey, set.AlipayPublicKey, order.OrderNo, Amount);
  46. }
  47. else if(order.PayMode == 3)
  48. {
  49. UserAccount user = db.UserAccount.FirstOrDefault(m => m.Id == order.UserId);
  50. if (user == null)
  51. {
  52. user = db.UserAccount.Add(new UserAccount()
  53. {
  54. Id = order.UserId,
  55. UserId = order.UserId,
  56. }).Entity;
  57. db.SaveChanges();
  58. }
  59. decimal BeforeTotalAmount = user.TotalAmount; //变更前总金额
  60. decimal BeforeFreezeAmount = user.FreezeAmount; //变更前冻结金额
  61. decimal BeforeBalanceAmount = user.BalanceAmount; //变更前余额
  62. user.BalanceAmount += Amount;
  63. decimal AfterTotalAmount = user.TotalAmount; //变更后总金额
  64. decimal AfterFreezeAmount = user.FreezeAmount; //变更后冻结金额
  65. decimal AfterBalanceAmount = user.BalanceAmount; //变更后余额
  66. db.UserAccountRecord.Add(new UserAccountRecord()
  67. {
  68. CreateDate = DateTime.Now,
  69. UpdateDate = DateTime.Now,
  70. UserId = order.UserId, //创客
  71. ChangeType = 21, //变动类型
  72. ChangeAmount = Amount, //变更金额
  73. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  74. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  75. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  76. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  77. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  78. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  79. QueryCount = order.Id,
  80. Remark = Remark,
  81. });
  82. }
  83. if(op)
  84. {
  85. order.RefundStatus = 1;
  86. order.RefundActAmount = Amount;
  87. db.OrderRefund.Add(new OrderRefund()
  88. {
  89. CreateDate = DateTime.Now,
  90. Mobile = order.Mobile,
  91. Amount = Amount,
  92. SeoDescription = Remark,
  93. ProductId = order.ProductId.ToString(),
  94. OrderId = order.Id,
  95. UserId = order.UserId,
  96. });
  97. }
  98. else
  99. {
  100. order.RefundStatus = 0;
  101. order.RefundReason = null;
  102. order.RefundActAmount = 0;
  103. }
  104. db.SaveChanges();
  105. }
  106. db.Dispose();
  107. }
  108. catch (Exception ex)
  109. {
  110. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "订单退款异常");
  111. }
  112. }
  113. else
  114. {
  115. Thread.Sleep(60000);
  116. }
  117. }
  118. }
  119. }
  120. }