OrderService.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System.Threading.Tasks;
  2. using GxPress.Common.WechatPay;
  3. using GxPress.Repository.Interface.Order;
  4. using GxPress.Service.Interface.Order;
  5. using GxPress.Repository.Interface.Media;
  6. using GxPress.Repository.Interface;
  7. using System;
  8. using System.Collections.Generic;
  9. using GxPress.Repository.Interface.VipEquity;
  10. using GxPress.Common.Tools;
  11. using System.Transactions;
  12. using GxPress.EnumConst;
  13. namespace GxPress.Service.Implement.Order
  14. {
  15. public class OrderService : IOrderService
  16. {
  17. private readonly IOrderRepository orderRepository;
  18. private readonly IMediaRepository mediaRepository;
  19. private readonly IUserRepository userRepository;
  20. private readonly IVipEquityRepository vipEquityRepository;
  21. public OrderService(IOrderRepository orderRepository, IMediaRepository mediaRepository, IUserRepository userRepository, IVipEquityRepository vipEquityRepository)
  22. {
  23. this.orderRepository = orderRepository;
  24. this.mediaRepository = mediaRepository;
  25. this.userRepository = userRepository;
  26. this.vipEquityRepository = vipEquityRepository;
  27. }
  28. /// <summary>
  29. /// 创建微信订单
  30. /// </summary>
  31. /// <param name="data"></param>
  32. /// <returns></returns>
  33. public async Task<bool> InsertWxOrderAsync(WxPayData data)
  34. {
  35. var attach = data.GetValue("attach").ToString();
  36. if (attach.Split('_').Length > 1)
  37. {
  38. return true;
  39. }
  40. try
  41. {
  42. using (var Transaction = new TransactionScope())
  43. {
  44. var order = await orderRepository.GetOrderAsync(attach.ToString());
  45. var user = await userRepository.GetAsync(order.UserId);
  46. //获取用户数据
  47. order.Price = order.PayWay == PayWayTypeConst.WeChatPay.GetHashCode() ? Decimal.Parse(data.GetValue("total_fee").ToString()) / 100 : Decimal.Parse(data.GetValue("total_fee").ToString());
  48. order.TransactionId = data.GetValue("transaction_id").ToString();
  49. order.OutTradeNo = data.GetValue("out_trade_no").ToString();
  50. order.IsSuccess = true;
  51. //如果是用户增加
  52. if (order.OrderType == 2)
  53. {
  54. var vipEquity = await vipEquityRepository.GetAsync(order.MediaId);
  55. user.IsVip = true;
  56. if ((VipCardTypeConst)vipEquity.VipType == VipCardTypeConst.Month)
  57. user.EndTime = DateTime.Now.AddMonths(1);
  58. if ((VipCardTypeConst)vipEquity.VipType == VipCardTypeConst.Season)
  59. user.EndTime = DateTime.Now.AddMonths(3);
  60. if ((VipCardTypeConst)vipEquity.VipType == VipCardTypeConst.Year)
  61. user.EndTime = DateTime.Now.AddYears(1);
  62. user.AccumulativePrice = user.AccumulativePrice + order.Price;
  63. await userRepository.UpdateAsync(user);
  64. }
  65. if (user.IsVip)
  66. order.IsVip = true;
  67. await orderRepository.UpdateAsync(order);
  68. Transaction.Complete();
  69. return true;
  70. }
  71. }
  72. catch
  73. {
  74. return false;
  75. }
  76. }
  77. /// <summary>
  78. /// 支付宝
  79. /// </summary>
  80. /// <param name="sArray"></param>
  81. /// <returns></returns>
  82. public async Task<bool> InsertAliPayOrderAsync(Dictionary<string, string> sArray)
  83. {
  84. var attach = sArray["out_trade_no"].ToString();
  85. if (attach.Split('_').Length > 1)
  86. return true;
  87. //获取medio属性
  88. var order = await orderRepository.GetOrderAsync(attach.ToString());
  89. //获取用户数据
  90. var user = await userRepository.GetAsync(order.UserId);
  91. order.OutTradeNo = sArray["out_trade_no"].ToString();
  92. order.Price = decimal.Parse(sArray["total_amount"].ToString());
  93. order.TransactionId = sArray["trade_no"].ToString();
  94. order.IsVip = user.IsVip;
  95. order.PayWay = 2;
  96. return await orderRepository.UpdateAsync(order);
  97. }
  98. }
  99. }