TransactionsService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Fuel.Core.Transactions.Dto;
  2. using Org.BouncyCastle.Asn1.Ocsp;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using static FreeSql.Internal.GlobalFilter;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Newtonsoft.Json;
  12. using FuelServer.Core.Entity;
  13. using Fuel.Core.Nozzle.Dto;
  14. using Org.BouncyCastle.Asn1.X509;
  15. using System.Linq.Expressions;
  16. namespace Fuel.Application.Service
  17. {
  18. public class TransactionsService : ITransactionsService
  19. {
  20. private readonly EntityHelper _entityHelper;
  21. private readonly IHttpContextAccessor _httpContextAccessor;
  22. public TransactionsService(EntityHelper entityHelper, IHttpContextAccessor httpContextAccessor)
  23. {
  24. _entityHelper = entityHelper;
  25. _httpContextAccessor = httpContextAccessor;
  26. }
  27. public async Task<transactions> CreateTransactions(UploadTransactions uploadTransactions)
  28. {
  29. string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
  30. Guid guid = Guid.Parse(Buid);
  31. string key = string.Empty;
  32. if (uploadTransactions.Type == 1)//预支付
  33. {
  34. key = uploadTransactions.NozzleId + ":" +
  35. uploadTransactions.OriginalAmount + ":" +
  36. uploadTransactions.Qty + ":" +
  37. uploadTransactions.MiniProgramID + ":" +
  38. Buid;
  39. }
  40. else//后支付
  41. {
  42. key = uploadTransactions.NozzleId + ":" +
  43. uploadTransactions.OriginalAmount + ":" +
  44. uploadTransactions.Qty + ":" +
  45. uploadTransactions.MiniProgramID + ":" +
  46. uploadTransactions.FuelItemTransactionEndTime + ":" +
  47. uploadTransactions.TransactionNumber + ":" +
  48. Buid;
  49. }
  50. transactions output = await GetRedisTransactions(uploadTransactions, key);
  51. if (output != null)
  52. {
  53. return output;
  54. }
  55. var _product = await _entityHelper.GetEntitiesAsync<product>(_ => _.Buid == guid && _.ProductName == uploadTransactions.Product);
  56. var _nozzle = await _entityHelper.GetEntitiesAsync<nozzle>(_ => _.Buid == guid && _.ExternalGunNumber == uploadTransactions.NozzleId);
  57. var transactions = uploadTransactions.ToTransactions(uploadTransactions, guid, _product.FirstOrDefault(), _nozzle.FirstOrDefault());
  58. var respond = await _entityHelper.InsertEntityAsync(transactions);
  59. string jsonString = JsonConvert.SerializeObject(respond);
  60. RedisHelper.SetAsync(key, jsonString, 3600);
  61. return respond;
  62. }
  63. public async Task<List<transactions>> GetTransactionsAsync(TransactionsInput input)
  64. {
  65. string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
  66. Guid guid = Guid.Parse(Buid);
  67. Expression<Func<transactions, bool>> where = p => p.Buid == guid;
  68. if (input.TransactionID != null)
  69. {
  70. where = CombineExpressions(where, p => p.Id == input.TransactionID);
  71. }
  72. if (input.type != null)
  73. {
  74. var status = (transactionsORDERSTATUS)input.type.Value;
  75. where = CombineExpressions(where, p => p.OrderStatus == status);
  76. }
  77. if (input.MiniProgramID != null)
  78. {
  79. where = CombineExpressions(where, p => p.MiniProgramID == input.MiniProgramID);
  80. }
  81. if (input.TransactionSTime != null)
  82. {
  83. where = CombineExpressions(where, p => p.TransactionTime >= input.TransactionSTime);
  84. }
  85. if (input.TransactionETime != null)
  86. {
  87. where = CombineExpressions(where, p => p.TransactionTime == input.TransactionETime);
  88. }
  89. if (!string.IsNullOrEmpty(input.Product))
  90. {
  91. where = CombineExpressions(where, p => p.ProductName == input.Product);
  92. }
  93. return await _entityHelper.GetEntitiesAsync<transactions>(where);
  94. }
  95. /// <summary>
  96. /// 查询redis订单缓存
  97. /// </summary>
  98. /// <returns></returns>
  99. public async Task<transactions> GetRedisTransactions(UploadTransactions uploadTransactions,string key)
  100. {
  101. string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
  102. var respond = RedisHelper.GetAsync(key).Result;
  103. if (respond == null)
  104. {
  105. return null;
  106. }
  107. transactions transactions = JsonConvert.DeserializeObject<transactions>(respond);
  108. return transactions;
  109. }
  110. // 辅助方法:组合两个表达式
  111. private static Expression<Func<T, bool>> CombineExpressions<T>(Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
  112. {
  113. ParameterExpression param = expr1.Parameters[0];
  114. Expression body = Expression.AndAlso(expr1.Body, Expression.Invoke(expr2, param));
  115. return Expression.Lambda<Func<T, bool>>(body, param);
  116. }
  117. }
  118. }