TransactionsService.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using Fuel.Core.Transactions.Dto;
  2. using Microsoft.AspNetCore.Http;
  3. using Newtonsoft.Json;
  4. using FuelServer.Core.Entity;
  5. using System.Linq.Expressions;
  6. using Fuel.Core.Models;
  7. using System.Net;
  8. using Fuel.Payment.Service.Pay;
  9. using DFS.Core.Abstractions.View;
  10. namespace Fuel.Application.Service
  11. {
  12. public class TransactionsService : ITransactionsService
  13. {
  14. private readonly EntityHelper _entityHelper;
  15. private readonly IHttpContextAccessor _httpContextAccessor;
  16. private readonly IPayService _payService;
  17. public TransactionsService(EntityHelper entityHelper, IHttpContextAccessor httpContextAccessor, IPayService payService)
  18. {
  19. _entityHelper = entityHelper;
  20. _httpContextAccessor = httpContextAccessor;
  21. _payService = payService;
  22. }
  23. /// <summary>
  24. /// 创建订单
  25. /// </summary>
  26. /// <param name="uploadTransactions"></param>
  27. /// <returns></returns>
  28. public async Task<ServiceResponse> CreateTransactions(UploadTransactions uploadTransactions)
  29. {
  30. string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
  31. Guid guid = Guid.Parse(Buid);
  32. string key = string.Empty;
  33. if (uploadTransactions.Type == 1)//预支付
  34. {
  35. key = uploadTransactions.NozzleId + ":" +
  36. uploadTransactions.OriginalAmount + ":" +
  37. uploadTransactions.Qty + ":" +
  38. uploadTransactions.MiniProgramID + ":" +
  39. Buid;
  40. }
  41. else//后支付
  42. {
  43. key = uploadTransactions.NozzleId + ":" +
  44. uploadTransactions.OriginalAmount + ":" +
  45. uploadTransactions.Qty + ":" +
  46. uploadTransactions.MiniProgramID + ":" +
  47. uploadTransactions.FuelItemTransactionEndTime + ":" +
  48. uploadTransactions.TransactionNumber + ":" +
  49. Buid;
  50. }
  51. transactions output = await GetRedisTransactions(uploadTransactions, key);
  52. if (output != null)
  53. {
  54. return ServiceResponse.Ok(output);
  55. }
  56. var _product = await _entityHelper.GetEntitiesAsync<product>(_ => _.Buid == guid && _.ProductName == uploadTransactions.Product);
  57. var _nozzle = await _entityHelper.GetEntitiesAsync<nozzle>(_ => _.Buid == guid && _.ExternalGunNumber == uploadTransactions.NozzleId);
  58. var transactions = uploadTransactions.ToTransactions(uploadTransactions, guid, _product.FirstOrDefault(), _nozzle.FirstOrDefault());
  59. var respond = await _entityHelper.InsertEntityAsync(transactions);
  60. string jsonString = JsonConvert.SerializeObject(respond);
  61. RedisHelper.SetAsync(key, jsonString, 3600);
  62. return ServiceResponse.Ok(respond);
  63. }
  64. public async Task<ServiceResponse> GetTransactionsAsync(TransactionsInput input)
  65. {
  66. string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
  67. Guid guid = Guid.Parse(Buid);
  68. Expression<Func<transactions, bool>> where = p => p.Buid == guid;
  69. if (input.TransactionID != null)
  70. {
  71. where = CombineExpressions(where, p => p.Id == input.TransactionID);
  72. }
  73. if (input.type != null)
  74. {
  75. var status = (transactionsORDERSTATUS)input.type.Value;
  76. where = CombineExpressions(where, p => p.OrderStatus == status);
  77. }
  78. if (input.MiniProgramID != null)
  79. {
  80. where = CombineExpressions(where, p => p.MiniProgramID == input.MiniProgramID);
  81. }
  82. if (input.TransactionSTime != null)
  83. {
  84. where = CombineExpressions(where, p => p.TransactionTime >= input.TransactionSTime);
  85. }
  86. if (input.TransactionETime != null)
  87. {
  88. where = CombineExpressions(where, p => p.TransactionTime == input.TransactionETime);
  89. }
  90. if (!string.IsNullOrEmpty(input.Product))
  91. {
  92. where = CombineExpressions(where, p => p.ProductName == input.Product);
  93. }
  94. var result = await _entityHelper.GetEntitiesAsync<transactions>(where);
  95. return ServiceResponse.Ok(result);
  96. }
  97. /// <summary>
  98. /// 小程序查询未支付订单
  99. /// </summary>
  100. /// <returns></returns>
  101. public async Task<ServiceResponse> GetMiniProgramTransactionsUnpaidAsync(TransactionsInput input)
  102. {
  103. string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
  104. Guid guid = Guid.Parse(Buid);
  105. Expression<Func<transactions, bool>> where = p => p.Buid == guid;
  106. if (input.MiniProgramID == null)
  107. {
  108. return ServiceResponse.Error("用户id为空");
  109. }
  110. where = CombineExpressions(where, p => p.MiniProgramID == input.MiniProgramID && p.OrderStatus == transactionsORDERSTATUS.Unpaid);
  111. var result = await _entityHelper.GetEntitiesAsync<transactions>(where);
  112. return ServiceResponse.Ok(result);
  113. }
  114. /// <summary>
  115. /// 小程序查询已支付订单
  116. /// </summary>
  117. /// <returns></returns>
  118. public async Task<ServiceResponse> GetMiniProgramTransactionsPaidAsync(TransactionsInput input)
  119. {
  120. string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
  121. Guid guid = Guid.Parse(Buid);
  122. Expression<Func<transactions, bool>> where = p => p.Buid == guid;
  123. if (input.MiniProgramID == null)
  124. {
  125. return ServiceResponse.Error("用户id为空");
  126. }
  127. where = CombineExpressions(where, p => p.MiniProgramID == input.MiniProgramID && p.OrderStatus == transactionsORDERSTATUS.Paid);
  128. var result = await _entityHelper.GetEntitiesAsync<transactions>(where);
  129. return ServiceResponse.Ok(result);
  130. }
  131. /// <summary>
  132. /// 提交支付
  133. /// </summary>
  134. /// <param name="input"></param>
  135. /// <returns></returns>
  136. public async Task<ServiceResponse> CommitPayment(int trxId, string AuthCode)
  137. {
  138. bool paymentOK = false;
  139. var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Id == trxId).Result.FirstOrDefault();
  140. if (trx == null)
  141. {
  142. return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未查询到订单!");
  143. }
  144. var paytype = _entityHelper.GetEntitiesAsync<paytype>(_ => _.Id == trx.PaymentMethod).Result.FirstOrDefault();
  145. string billNumber = SequenceNumber.Next();//订单编号
  146. trx.TransactionNumber = billNumber;
  147. decimal payDueAmount = (decimal)trx.OriginalAmount;
  148. string Channel = paytype.Channel;
  149. var serviceResult = await _payService.PerformElectronicProcess(AuthCode, payDueAmount, billNumber);
  150. Payment.Core.Models.ElectronicOrderProcessResultModel payResult = (Payment.Core.Models.ElectronicOrderProcessResultModel)serviceResult.Data;
  151. if (!serviceResult.IsSuccessful() || payResult.ResultCode == "PAY_ERROR")
  152. {
  153. return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "支付失败");
  154. }
  155. trx.ActualPaymentAmount = payDueAmount;//实际支付金额
  156. trx.ResultCode = payResult?.ResultCode;//支付成功应该为0
  157. trx.ErrorDetail = payResult?.ErrorDetail;
  158. _entityHelper.UpdateAsync(trx);
  159. return ServiceResponse.Ok(trx);
  160. }
  161. /// <summary>
  162. /// 查询redis订单缓存
  163. /// </summary>
  164. /// <returns></returns>
  165. public async Task<transactions> GetRedisTransactions(UploadTransactions uploadTransactions, string key)
  166. {
  167. string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
  168. var respond = RedisHelper.GetAsync(key).Result;
  169. if (respond == null)
  170. {
  171. return null;
  172. }
  173. transactions transactions = JsonConvert.DeserializeObject<transactions>(respond);
  174. return transactions;
  175. }
  176. // 辅助方法:组合两个表达式
  177. private static Expression<Func<T, bool>> CombineExpressions<T>(Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
  178. {
  179. ParameterExpression param = expr1.Parameters[0];
  180. Expression body = Expression.AndAlso(expr1.Body, Expression.Invoke(expr2, param));
  181. return Expression.Lambda<Func<T, bool>>(body, param);
  182. }
  183. }
  184. }