|
@@ -8,6 +8,8 @@ using System.Net;
|
|
|
using Fuel.Payment.Service.Pay;
|
|
|
using DFS.Core.Abstractions.View;
|
|
|
using Fuel.Core;
|
|
|
+using System;
|
|
|
+using System.Transactions;
|
|
|
|
|
|
|
|
|
namespace Fuel.Application.Service
|
|
@@ -17,11 +19,13 @@ namespace Fuel.Application.Service
|
|
|
private readonly EntityHelper _entityHelper;
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
private readonly IPayService _payService;
|
|
|
- public TransactionsService(EntityHelper entityHelper, IHttpContextAccessor httpContextAccessor, IPayService payService)
|
|
|
+ public readonly IFreeSql _fsql;
|
|
|
+ public TransactionsService(EntityHelper entityHelper, IHttpContextAccessor httpContextAccessor, IPayService payService, IFreeSql fsql)
|
|
|
{
|
|
|
_entityHelper = entityHelper;
|
|
|
_httpContextAccessor = httpContextAccessor;
|
|
|
_payService = payService;
|
|
|
+ _fsql = fsql;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -31,40 +35,60 @@ namespace Fuel.Application.Service
|
|
|
/// <returns></returns>
|
|
|
public async Task<ServiceResponse> CreateTransactions(UploadTransactions uploadTransactions)
|
|
|
{
|
|
|
- string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
|
|
|
- Guid guid = Guid.Parse(Buid);
|
|
|
+ Guid Buid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
string key = string.Empty;
|
|
|
- if (uploadTransactions.Type == 1)//预支付
|
|
|
+ int OrderType = 0;//订单类型
|
|
|
+ if (OrderType == 1)//预支付
|
|
|
{
|
|
|
- key = uploadTransactions.NozzleId + ":" +
|
|
|
+ key = uploadTransactions.ExternalGunNumber + ":" +
|
|
|
uploadTransactions.OriginalAmount + ":" +
|
|
|
uploadTransactions.Qty + ":" +
|
|
|
uploadTransactions.MiniProgramID + ":" +
|
|
|
- Buid;
|
|
|
+ Buid.ToString();
|
|
|
}
|
|
|
else//后支付
|
|
|
{
|
|
|
- key = uploadTransactions.NozzleId + ":" +
|
|
|
+ key = uploadTransactions.ExternalGunNumber + ":" +
|
|
|
uploadTransactions.OriginalAmount + ":" +
|
|
|
uploadTransactions.Qty + ":" +
|
|
|
uploadTransactions.MiniProgramID + ":" +
|
|
|
uploadTransactions.FuelItemTransactionEndTime + ":" +
|
|
|
uploadTransactions.TransactionNumber + ":" +
|
|
|
- Buid;
|
|
|
+ Buid.ToString();
|
|
|
}
|
|
|
transactions output = await GetRedisTransactions(uploadTransactions, key);
|
|
|
if (output != null)
|
|
|
{
|
|
|
return ServiceResponse.Ok(output);
|
|
|
}
|
|
|
- var _product = await _entityHelper.GetEntitiesAsync<product>(_ => _.Buid == guid && _.ProductName == uploadTransactions.Product);
|
|
|
- var _nozzle = await _entityHelper.GetEntitiesAsync<nozzle>(_ => _.Buid == guid && _.ExternalGunNumber == uploadTransactions.NozzleId);
|
|
|
- var transactions = uploadTransactions.ToTransactions(uploadTransactions, guid, _product.FirstOrDefault(), _nozzle.FirstOrDefault());
|
|
|
- var respond = await _entityHelper.InsertEntityAsync(transactions);
|
|
|
- string jsonString = JsonConvert.SerializeObject(respond);
|
|
|
+ var _product = await _entityHelper.GetEntitiesAsync<product>(_ => _.Buid == Buid && _.ProductName == uploadTransactions.Product);
|
|
|
+ if (_product.Count() <= 0)
|
|
|
+ {
|
|
|
+ return ServiceResponse.Error("油品查询失败,油品名称:" + uploadTransactions.Product);
|
|
|
+ }
|
|
|
+ var _nozzle = await _entityHelper.GetEntitiesAsync<nozzle>(_ => _.Buid == Buid && _.ExternalGunNumber == uploadTransactions.ExternalGunNumber);
|
|
|
+ if (_nozzle.Count() <= 0)
|
|
|
+ {
|
|
|
+ return ServiceResponse.Error("油枪查询失败,油枪名称:" + uploadTransactions.ExternalGunNumber);
|
|
|
+ }
|
|
|
+ var trx = uploadTransactions.ToTransactions(uploadTransactions, Buid, _product.FirstOrDefault(), _nozzle.FirstOrDefault(), OrderType);
|
|
|
+ //int affectedRows = _fsql.Insert<transactions>().AppendData(trx).ExecuteAffrows();
|
|
|
+ // var affectedRows = _fsql.Insert<transactions>().AppendData(trx).ExecuteInserted();
|
|
|
+ var id = _fsql.Insert(trx).ExecuteIdentity();
|
|
|
+ if (id > 0)
|
|
|
+ {
|
|
|
+ var insertedTransaction = _fsql.Select<transactions>()
|
|
|
+ .Where(t => t.Id == id)
|
|
|
+ .First();
|
|
|
+ string jsonString = JsonConvert.SerializeObject(insertedTransaction);
|
|
|
|
|
|
- RedisHelper.SetAsync(key, jsonString, 3600);
|
|
|
- return ServiceResponse.Ok(respond);
|
|
|
+ RedisHelper.SetAsync(key, jsonString, 3600);
|
|
|
+ return ServiceResponse.Ok(insertedTransaction);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return ServiceResponse.Error("订单信息插入失败");
|
|
|
+ }
|
|
|
}
|
|
|
public async Task<ServiceResponse> GetTransactionsAsync(TransactionsInput input)
|
|
|
{
|
|
@@ -116,6 +140,24 @@ namespace Fuel.Application.Service
|
|
|
var result = await _entityHelper.GetEntitiesAsync<transactions>(where);
|
|
|
return ServiceResponse.Ok(result);
|
|
|
}
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 小程序用户根据抢号查询未支付订单
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<ServiceResponse> GetMiniProgramTransactionsUnpaidNozzleAsync(TransactionsInput input)
|
|
|
+ {
|
|
|
+ string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
|
|
|
+ Guid guid = Guid.Parse(Buid);
|
|
|
+ Expression<Func<transactions, bool>> where = p => p.Buid == guid;
|
|
|
+ if (input.MiniProgramID == null)
|
|
|
+ {
|
|
|
+ return ServiceResponse.Error("用户id为空");
|
|
|
+ }
|
|
|
+ where = CombineExpressions(where, p => p.NozzleId == input.NozzleId && p.OrderStatus == transactionsORDERSTATUS.Unpaid);
|
|
|
+ var result = await _entityHelper.GetEntitiesAsync<transactions>(where);
|
|
|
+ return ServiceResponse.Ok(result);
|
|
|
+ }
|
|
|
/// <summary>
|
|
|
/// 小程序查询已支付订单
|
|
|
/// </summary>
|
|
@@ -253,7 +295,7 @@ namespace Fuel.Application.Service
|
|
|
return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "该笔单无需退款");
|
|
|
}
|
|
|
//退款
|
|
|
- var serviceResult = await _payService.ReturnProcess(RefundAmount, trx.TransactionNumber);
|
|
|
+ var serviceResult = await _payService.ReturnProcess(RefundAmount, "ALL_IN_SCAN", trx.TransactionNumber);
|
|
|
Payment.Core.Models.ElectronicOrderProcessResultModel payResult = (Payment.Core.Models.ElectronicOrderProcessResultModel)serviceResult.Data;
|
|
|
if (!serviceResult.IsSuccessful() || payResult.ResultCode == "PAY_ERROR")
|
|
|
{
|
|
@@ -264,6 +306,20 @@ namespace Fuel.Application.Service
|
|
|
_entityHelper.UpdateAsync(trx);
|
|
|
return ServiceResponse.Ok(trx);
|
|
|
}
|
|
|
+ public async Task<ServiceResponse> UnifiedOrder(int trxId)
|
|
|
+ {
|
|
|
+ var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Id == trxId).Result.FirstOrDefault();
|
|
|
+ if (trx == null)
|
|
|
+ {
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未查询到订单!");
|
|
|
+ }
|
|
|
+ var serviceResult = await _payService.UnifiedOrder((decimal)trx.OriginalAmount, "ALL_IN_SCAN");
|
|
|
+ if (!serviceResult.IsSuccessful())
|
|
|
+ {
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "统一下单失败");
|
|
|
+ }
|
|
|
+ return ServiceResponse.Ok(serviceResult.Data);
|
|
|
+ }
|
|
|
public async Task<ServiceResponse> Redeem(int trxId, decimal OriginalQty)
|
|
|
{
|
|
|
var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Id == trxId).Result.FirstOrDefault();
|