|
@@ -15,6 +15,12 @@ using Fuel.Core.Nozzle.Dto;
|
|
|
using Fuel.Application.MqttService;
|
|
|
using Org.BouncyCastle.Asn1.X509;
|
|
|
using Jayrock.Json;
|
|
|
+using Fuel.Core.Transactions;
|
|
|
+using Microsoft.Extensions.DependencyInjection;
|
|
|
+using static Fuel.Core.WechatServer.WeChatService;
|
|
|
+using System.Linq.Dynamic.Core;
|
|
|
+using System.Text.Json;
|
|
|
+using Org.BouncyCastle.Asn1.Ocsp;
|
|
|
|
|
|
|
|
|
namespace Fuel.Application.Service
|
|
@@ -26,13 +32,17 @@ namespace Fuel.Application.Service
|
|
|
private readonly IPayService _payService;
|
|
|
public readonly IFreeSql _fsql;
|
|
|
private readonly IMqttClientService _mqttService;
|
|
|
- public TransactionsService(EntityHelper entityHelper, IHttpContextAccessor httpContextAccessor, IPayService payService, IFreeSql fsql, IMqttClientService mqttService)
|
|
|
+ private readonly IServiceScopeFactory _serviceScopeFactory;
|
|
|
+ static NLog.Logger logger = NLog.LogManager.LoadConfiguration("nlog.xml").GetLogger("Main");
|
|
|
+
|
|
|
+ public TransactionsService(EntityHelper entityHelper, IHttpContextAccessor httpContextAccessor, IPayService payService, IFreeSql fsql, IMqttClientService mqttService, IServiceScopeFactory serviceScopeFactory)
|
|
|
{
|
|
|
_entityHelper = entityHelper;
|
|
|
_httpContextAccessor = httpContextAccessor;
|
|
|
_payService = payService;
|
|
|
_fsql = fsql;
|
|
|
_mqttService = mqttService;
|
|
|
+ _serviceScopeFactory = serviceScopeFactory;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -43,15 +53,57 @@ namespace Fuel.Application.Service
|
|
|
public async Task<ServiceResponse> CreateTransactions(UploadTransactions uploadTransactions)
|
|
|
{
|
|
|
Guid Buid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
+ var site = _fsql.Select<businessunitinfo>().Where(_ => _.Buid == Buid).First();
|
|
|
+ if (site == null)
|
|
|
+ {
|
|
|
+ return ServiceResponse.Error("站点信息查询为空 buid: " + Buid);
|
|
|
+ }
|
|
|
string key = string.Empty;
|
|
|
- int OrderType = 0;//订单类型
|
|
|
- if (OrderType == 1)//预支付
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ if (site.PaymentMode == 1)//预支付
|
|
|
{
|
|
|
key = uploadTransactions.ExternalGunNumber + ":" +
|
|
|
uploadTransactions.OriginalAmount + ":" +
|
|
|
uploadTransactions.Qty + ":" +
|
|
|
uploadTransactions.MiniProgramID + ":" +
|
|
|
Buid.ToString();
|
|
|
+ string WachatID = HttpRequestReader.GetWachatID(); //用户
|
|
|
+ var userSession = WechatUserSessionRepo.GetUserSession(WachatID.ToString());
|
|
|
+ if (userSession == null)
|
|
|
+ {
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NonAuthoritativeInformation, "未找到用户!");
|
|
|
+ }
|
|
|
+ var user = _fsql.Select<miniprogramusers>().Where(_ => _.OpenId == userSession.openid).First();
|
|
|
+
|
|
|
+
|
|
|
+ if (uploadTransactions.OriginalAmount != null)
|
|
|
+ {
|
|
|
+ decimal decimalValue = uploadTransactions.OriginalAmount.HasValue ? uploadTransactions.OriginalAmount.Value : 0m;
|
|
|
+ decimal ProductPrice = _product.First().ProductPrice.HasValue ? _product.First().ProductPrice.Value : 0m;
|
|
|
+ decimal qty = decimalValue / ProductPrice;
|
|
|
+ uploadTransactions.Qty = qty;
|
|
|
+ }
|
|
|
+ else if (uploadTransactions.Qty != null)
|
|
|
+ {
|
|
|
+ decimal decimalValue = uploadTransactions.Qty.HasValue ? uploadTransactions.Qty.Value : 0m;
|
|
|
+ decimal ProductPrice = _product.First().ProductPrice.HasValue ? _product.First().ProductPrice.Value : 0m;
|
|
|
+ decimal OriginalAmount = decimalValue * ProductPrice;
|
|
|
+ uploadTransactions.OriginalAmount = OriginalAmount;
|
|
|
+ }
|
|
|
+ else if (uploadTransactions.OriginalAmount == null && uploadTransactions.Qty == null)
|
|
|
+ {
|
|
|
+ return ServiceResponse.Error("金额与升数为空");
|
|
|
+ }
|
|
|
+ uploadTransactions.MiniProgramID = user.Id;
|
|
|
}
|
|
|
else//后支付
|
|
|
{
|
|
@@ -68,17 +120,8 @@ namespace Fuel.Application.Service
|
|
|
{
|
|
|
return ServiceResponse.Ok(output);
|
|
|
}
|
|
|
- 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);
|
|
|
+
|
|
|
+ var trx = uploadTransactions.ToTransactions(uploadTransactions, Buid, _product.FirstOrDefault(), _nozzle.FirstOrDefault(), site.PaymentMode);
|
|
|
//int affectedRows = _fsql.Insert<transactions>().AppendData(trx).ExecuteAffrows();
|
|
|
// var affectedRows = _fsql.Insert<transactions>().AppendData(trx).ExecuteInserted();
|
|
|
var id = _fsql.Insert(trx).ExecuteIdentity();
|
|
@@ -97,38 +140,60 @@ namespace Fuel.Application.Service
|
|
|
return ServiceResponse.Error("订单信息插入失败");
|
|
|
}
|
|
|
}
|
|
|
- public async Task<ServiceResponse> GetTransactionsAsync(TransactionsInput input)
|
|
|
+ public async Task<ServiceResponse> GetTransactionsAsync(RequestModel 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.TransactionID != null)
|
|
|
- {
|
|
|
- where = CombineExpressions(where, p => p.Id == input.TransactionID);
|
|
|
- }
|
|
|
- if (input.type != null)
|
|
|
- {
|
|
|
- var status = (transactionsORDERSTATUS)input.type.Value;
|
|
|
- where = CombineExpressions(where, p => p.OrderStatus == status);
|
|
|
- }
|
|
|
- if (input.MiniProgramID != null)
|
|
|
+ Guid Buid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
+ var site = _fsql.Select<businessunitinfo>().Where(_ => _.Buid == Buid).First();
|
|
|
+ var _nozzle = _fsql.Select<nozzle>().ToList();//根据油枪id查询外部枪号
|
|
|
+ var user = _fsql.Select<miniprogramusers>().ToList();
|
|
|
+ var paytype = _fsql.Select<paytype>().ToList();
|
|
|
+ Expression<Func<transactions, bool>> where = p => p.Buid == Buid;
|
|
|
+ if (input.filter.NozzleID != "")
|
|
|
{
|
|
|
- where = CombineExpressions(where, p => p.MiniProgramID == input.MiniProgramID);
|
|
|
+ long.TryParse(input.filter.NozzleID, out long NozzleID);
|
|
|
+ var nozzle = _nozzle.Where(_ => _.ExternalGunNumber == NozzleID).First();//根据油枪id查询外部枪号
|
|
|
+ where = CombineExpressions(where, p => p.NozzleId == nozzle.Id);
|
|
|
}
|
|
|
- if (input.TransactionSTime != null)
|
|
|
+ if (input.filter.ProductName != "")
|
|
|
{
|
|
|
- where = CombineExpressions(where, p => p.TransactionTime >= input.TransactionSTime);
|
|
|
+ where = CombineExpressions(where, p => p.ProductName == input.filter.ProductName);
|
|
|
}
|
|
|
- if (input.TransactionETime != null)
|
|
|
+ if (input.filter.siteName != "")
|
|
|
{
|
|
|
- where = CombineExpressions(where, p => p.TransactionTime == input.TransactionETime);
|
|
|
+
|
|
|
+ where = CombineExpressions(where, p => p.Buid == site.Buid);
|
|
|
}
|
|
|
- if (!string.IsNullOrEmpty(input.Product))
|
|
|
+ if (input.filter.username != "")
|
|
|
{
|
|
|
- where = CombineExpressions(where, p => p.ProductName == input.Product);
|
|
|
+ var userid = user.Where(_ => _.UserName == input.filter.username).First();
|
|
|
+ where = CombineExpressions(where, p => p.MiniProgramID == userid.Id);
|
|
|
}
|
|
|
+ //if (input.TransactionETime != null)
|
|
|
+ //{
|
|
|
+ // where = CombineExpressions(where, p => p.TransactionTime == input.TransactionETime);
|
|
|
+ //}
|
|
|
var result = await _entityHelper.GetEntitiesAsync<transactions>(where);
|
|
|
- return ServiceResponse.Ok(result);
|
|
|
+
|
|
|
+ List<TransactionsList> list = new List<TransactionsList>();
|
|
|
+ foreach (var item in result)
|
|
|
+ {
|
|
|
+ // var nozzle = _nozzle.Where(_ => _.Id == item.NozzleId)?.First();//根据油枪id查询外部枪号
|
|
|
+ var nozzle = _nozzle.FirstOrDefault(_ => _.Id == item.NozzleId);//根据油枪id查询外部枪号
|
|
|
+ var userid = user.Where(_ => _.Id == item.MiniProgramID).First();
|
|
|
+ var pay = paytype.FirstOrDefault(_ => _.Id == item.PaymentMethod)?.Name;
|
|
|
+ TransactionsList transactions = new TransactionsList();
|
|
|
+ transactions.TransactionNumber = item.TransactionNumber;
|
|
|
+ transactions.SiteName = site.Name;
|
|
|
+ transactions.NozzleProductName = nozzle?.ExternalGunNumber + " | " + item.ProductName;
|
|
|
+ transactions.ActualPaymentAmount = item.ActualPaymentAmount.ToString();
|
|
|
+ transactions.TransactionTime = item.TransactionTime?.ToString("yyyy-MM-dd hh:mm:ss");
|
|
|
+ transactions.OrderStatus = GetChineseStatus(item.OrderStatus);
|
|
|
+ transactions.UserName = userid.UserName;
|
|
|
+ transactions.PriceQty = item.Price + " | " + item.Qty;
|
|
|
+ transactions.PaymentMethod = pay;
|
|
|
+ list.Add(transactions);
|
|
|
+ }
|
|
|
+ return ServiceResponse.Ok(list);
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 小程序查询未支付订单
|
|
@@ -136,14 +201,17 @@ namespace Fuel.Application.Service
|
|
|
/// <returns></returns>
|
|
|
public async Task<ServiceResponse> GetMiniProgramTransactionsUnpaidAsync(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)
|
|
|
+ Guid Buid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
+ Expression<Func<transactions, bool>> where = p => p.Buid == Buid;
|
|
|
+ string WachatID = HttpRequestReader.GetWachatID(); //用户
|
|
|
+ var userSession = WechatUserSessionRepo.GetUserSession(WachatID.ToString());
|
|
|
+ if (userSession == null)
|
|
|
{
|
|
|
- return ServiceResponse.Error("用户id为空");
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NonAuthoritativeInformation, "未找到用户!");
|
|
|
}
|
|
|
- where = CombineExpressions(where, p => p.MiniProgramID == input.MiniProgramID && p.OrderStatus == transactionsORDERSTATUS.Unpaid);
|
|
|
+ DateTime dayBeforeTime = DateTime.Now.AddDays(-1);
|
|
|
+ var user = _fsql.Select<miniprogramusers>().Where(_ => _.OpenId == userSession.openid).First();
|
|
|
+ where = CombineExpressions(where, p => p.MiniProgramID == user.Id && p.OrderStatus == transactionsORDERSTATUS.Unpaid && p.TransactionTime >= dayBeforeTime);
|
|
|
var result = await _entityHelper.GetEntitiesAsync<transactions>(where);
|
|
|
return ServiceResponse.Ok(result);
|
|
|
}
|
|
@@ -156,13 +224,15 @@ namespace Fuel.Application.Service
|
|
|
{
|
|
|
Guid Buid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
Expression<Func<transactions, bool>> where = p => p.Buid == Buid;
|
|
|
- Guid WachatID = HttpRequestReader.GetWachatID(); //用户
|
|
|
+ string WachatID = HttpRequestReader.GetWachatID(); //用户
|
|
|
var userSession = WechatUserSessionRepo.GetUserSession(WachatID.ToString());
|
|
|
if (userSession == null)
|
|
|
{
|
|
|
- return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未找到用户!");
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NonAuthoritativeInformation, "未找到用户!");
|
|
|
}
|
|
|
- where = CombineExpressions(where, p => p.NozzleId == NozzleId && p.OrderStatus == transactionsORDERSTATUS.Unpaid);
|
|
|
+ DateTime dayBeforeTime = DateTime.Now.AddDays(-1);
|
|
|
+ var _nozzle = _fsql.Select<nozzle>().Where(_ => _.Id == NozzleId).First();//根据油枪id查询外部枪号
|
|
|
+ where = CombineExpressions(where, p => p.NozzleId == _nozzle.ExternalGunNumber && p.OrderStatus == transactionsORDERSTATUS.Unpaid && p.TransactionTime >= dayBeforeTime);
|
|
|
var result = await _entityHelper.GetEntitiesAsync<transactions>(where);
|
|
|
return ServiceResponse.Ok(result);
|
|
|
}
|
|
@@ -172,18 +242,42 @@ namespace Fuel.Application.Service
|
|
|
/// <returns></returns>
|
|
|
public async Task<ServiceResponse> GetMiniProgramTransactionsPaidAsync(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)
|
|
|
+ Guid Buid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
+ Expression<Func<transactions, bool>> where = p => p.Buid == Buid;
|
|
|
+ string WachatID = HttpRequestReader.GetWachatID(); //用户
|
|
|
+ var userSession = WechatUserSessionRepo.GetUserSession(WachatID.ToString());
|
|
|
+ if (userSession == null)
|
|
|
{
|
|
|
- return ServiceResponse.Error("用户id为空");
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NonAuthoritativeInformation, "未找到用户!");
|
|
|
}
|
|
|
- where = CombineExpressions(where, p => p.MiniProgramID == input.MiniProgramID && p.OrderStatus == transactionsORDERSTATUS.Paid);
|
|
|
+ var user = _fsql.Select<miniprogramusers>().Where(_ => _.OpenId == userSession.openid).First();
|
|
|
+ where = CombineExpressions(where, p => p.MiniProgramID == user.Id && p.OrderStatus == transactionsORDERSTATUS.Paid);
|
|
|
var result = await _entityHelper.GetEntitiesAsync<transactions>(where);
|
|
|
return ServiceResponse.Ok(result);
|
|
|
}
|
|
|
/// <summary>
|
|
|
+ /// 小程序查询已支付订单
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<ServiceResponse> WXFindOrders(DateTime? dateTime, int pageNum, int lineCount)
|
|
|
+ {
|
|
|
+ Guid Buid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
+ Expression<Func<transactions, bool>> where = p => p.Buid == Buid;
|
|
|
+ string WachatID = HttpRequestReader.GetWachatID(); //用户
|
|
|
+ var userSession = WechatUserSessionRepo.GetUserSession(WachatID.ToString());
|
|
|
+ if (userSession == null)
|
|
|
+ {
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NonAuthoritativeInformation, "未找到用户!");
|
|
|
+ }
|
|
|
+ var user = _fsql.Select<miniprogramusers>().Where(_ => _.OpenId == userSession.openid).First();
|
|
|
+ DateTime time = dateTime != null ? (DateTime)dateTime : DateTime.Now;
|
|
|
+ var select = _fsql.Select<transactions>().Where(_ => _.MiniProgramID == user.Id
|
|
|
+ && (_.TransactionTime >= DateTime.Now.AddDays(-30) && _.TransactionTime <= time));
|
|
|
+ var list = await select.Page(pageNum, lineCount).OrderByDescending(_ => _.TransactionTime).ToListAsync();
|
|
|
+
|
|
|
+ return ServiceResponse.Ok(list);
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
/// 提交支付
|
|
|
/// </summary>
|
|
|
/// <param name="input"></param>
|
|
@@ -219,13 +313,18 @@ namespace Fuel.Application.Service
|
|
|
/// <returns></returns>
|
|
|
public async Task<transactions> GetRedisTransactions(UploadTransactions uploadTransactions, string key)
|
|
|
{
|
|
|
- string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
|
|
|
+ Guid Buid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
var respond = RedisHelper.GetAsync(key).Result;
|
|
|
if (respond == null)
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
transactions transactions = JsonConvert.DeserializeObject<transactions>(respond);
|
|
|
+ var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Id == transactions.Id).Result.FirstOrDefault();
|
|
|
+ if (trx == null || trx.OrderStatus != transactionsORDERSTATUS.Unpaid)
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
return transactions;
|
|
|
}
|
|
|
// 辅助方法:组合两个表达式
|
|
@@ -241,44 +340,43 @@ namespace Fuel.Application.Service
|
|
|
/// <param name="input"></param>
|
|
|
/// <returns></returns>
|
|
|
public async Task<ServiceResponse> RefundTrx(int trxId,
|
|
|
- double longitude,
|
|
|
- double latitude)
|
|
|
+ double longitude = 0.0,
|
|
|
+ double latitude = 0.0)
|
|
|
{
|
|
|
- string Buid = _httpContextAccessor.HttpContext.Request.Headers["Buid"].FirstOrDefault();
|
|
|
- Guid guid = Guid.Parse(Buid);
|
|
|
- var businessunitinfo = _entityHelper.GetEntitiesAsync<businessunitinfo>(_ => _.Buid == guid).Result.FirstOrDefault();
|
|
|
+ Guid Buid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
+ var businessunitinfo = _entityHelper.GetEntitiesAsync<businessunitinfo>(_ => _.Buid == Buid).Result.FirstOrDefault();
|
|
|
if (businessunitinfo == null)
|
|
|
{
|
|
|
return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "站点为空");
|
|
|
}
|
|
|
string[] parts = businessunitinfo.GpsCoordinates.Split(',');
|
|
|
- if (parts.Length == 2 &&
|
|
|
- double.TryParse(parts[0], out double latitude2) &&
|
|
|
- double.TryParse(parts[1], out double longitude2))
|
|
|
- {
|
|
|
+ // if (parts.Length == 2 &&
|
|
|
+ //double.TryParse(parts[0], out double latitude2) &&
|
|
|
+ //double.TryParse(parts[1], out double longitude2))
|
|
|
+ // {
|
|
|
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "站点经纬度获取失败");
|
|
|
- }
|
|
|
- //计算调用方和油站的距离,超过距离判定为恶意请求
|
|
|
- double distance = DistanceCalculator.CalculateDistance(longitude, latitude, longitude2, latitude2);
|
|
|
- if (distance > 5)
|
|
|
- {
|
|
|
- return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "该请求大于油站地址5公里");
|
|
|
- }
|
|
|
+ // }
|
|
|
+ // else
|
|
|
+ // {
|
|
|
+ // return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "站点经纬度获取失败");
|
|
|
+ // }
|
|
|
+ // //计算调用方和油站的距离,超过距离判定为恶意请求
|
|
|
+ // double distance = DistanceCalculator.CalculateDistance(longitude, latitude, longitude2, latitude2);
|
|
|
+ // if (distance > 5)
|
|
|
+ // {
|
|
|
+ // return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "该请求大于油站地址5公里");
|
|
|
+ // }
|
|
|
|
|
|
var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Id == trxId).Result.FirstOrDefault();
|
|
|
if (trx == null)
|
|
|
{
|
|
|
return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未查询到订单!");
|
|
|
}
|
|
|
- else if (trx.OrderStatus == transactionsORDERSTATUS.FullyRefunded)
|
|
|
+ else if (trx.RefundStatus == RefundStatus.FullyRefunded)
|
|
|
{
|
|
|
return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "该订单已退款");
|
|
|
}
|
|
|
- else if (trx.OrderStatus == transactionsORDERSTATUS.PartiallyRefunded)
|
|
|
+ else if (trx.RefundStatus == RefundStatus.PartiallyRefunded)
|
|
|
{
|
|
|
return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "该订单已部分退款");
|
|
|
}
|
|
@@ -297,75 +395,233 @@ namespace Fuel.Application.Service
|
|
|
return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "单价获取失败");
|
|
|
}
|
|
|
//计算退款金额
|
|
|
+ //decimal RefundAmount = (decimal)trx.ActualPaymentAmount;
|
|
|
decimal RefundAmount = (decimal)((trx.Qty - trx.OriginalQty) * ProductPrice.Value);
|
|
|
- if (RefundAmount <= 0.0M)
|
|
|
- {
|
|
|
- return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "该笔单无需退款");
|
|
|
- }
|
|
|
+ //if (RefundAmount <= 0.0M)
|
|
|
+ //{
|
|
|
+ // return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "该笔单无需退款");
|
|
|
+ //}
|
|
|
//退款
|
|
|
- var serviceResult = await _payService.ReturnProcess(RefundAmount, "ALL_IN_SCAN", trx.TransactionNumber);
|
|
|
+ var serviceResult = await _payService.ReturnProcess(RefundAmount, (decimal)trx.ActualPaymentAmount, "WX_SCAN", trx.BillNumber);
|
|
|
Payment.Core.Models.ElectronicOrderProcessResultModel payResult = (Payment.Core.Models.ElectronicOrderProcessResultModel)serviceResult.Data;
|
|
|
if (!serviceResult.IsSuccessful() || payResult.ResultCode == "PAY_ERROR")
|
|
|
{
|
|
|
return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "退款失败");
|
|
|
}
|
|
|
-
|
|
|
+ if (RefundAmount == (decimal)trx.ActualPaymentAmount)
|
|
|
+ {
|
|
|
+ trx.RefundStatus = RefundStatus.FullyRefunded;//全额退款
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ trx.RefundStatus = RefundStatus.PartiallyRefunded;//部分退款
|
|
|
+ }
|
|
|
+ trx.OrderStatus = transactionsORDERSTATUS.Cancelled;
|
|
|
trx.RefundAmount = RefundAmount;
|
|
|
- _entityHelper.UpdateAsync(trx);
|
|
|
+ int affectedRows = _fsql.Update<transactions>().SetSource(trx).ExecuteAffrows();
|
|
|
return ServiceResponse.Ok(trx);
|
|
|
}
|
|
|
public async Task<ServiceResponse> UnifiedOrder(int trxId)
|
|
|
{
|
|
|
- Guid Buid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
- Guid WachatID = HttpRequestReader.GetWachatID(); //用户
|
|
|
- var userSession = WechatUserSessionRepo.GetUserSession(WachatID.ToString());
|
|
|
- if (userSession == null)
|
|
|
+ try
|
|
|
{
|
|
|
- return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未找到用户!");
|
|
|
+ logger.Debug("UnifiedOrder start++");
|
|
|
+
|
|
|
+ Guid Buid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
+ string WachatID = HttpRequestReader.GetWachatID(); //用户
|
|
|
+ //var userSession = WechatUserSessionRepo.GetUserSession(WachatID.ToString());
|
|
|
+ //if (userSession == null)
|
|
|
+ //{
|
|
|
+ // return ServiceResponse.Error(HttpStatusCode.NonAuthoritativeInformation, "未找到用户!");
|
|
|
+ //}
|
|
|
+ var userSession = new WechatUserSessionResponse() { buId = "12345678-9abc-def0-1234-56789abcdef0", openid = "o8pFb5cWH1KkBDvGls2X7yMiFkGA" };
|
|
|
+ var site = _fsql.Select<businessunitinfo>().Where(_ => _.Buid == Buid).First();
|
|
|
+ var weChatService = new WeChatService(site.Appid, site.Secret);
|
|
|
+ var user = _fsql.Select<miniprogramusers>().Where(_ => _.OpenId == userSession.openid).First();
|
|
|
+ var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Id == trxId).Result.FirstOrDefault();
|
|
|
+ if (trx == null)
|
|
|
+ {
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未查询到订单!");
|
|
|
+ }
|
|
|
+ logger.Debug("统一下单开始");
|
|
|
+ var serviceResult = await _payService.UnifiedOrder(trx.OriginalAmount.Value, "WX_SCAN", userSession.openid);
|
|
|
+ var dataProperties = serviceResult.Data.GetType().GetProperty("UnifiedOrderResult");
|
|
|
+ if (!serviceResult.IsSuccessful() || dataProperties == null)
|
|
|
+ {
|
|
|
+ logger.Debug("统一下单失败" );
|
|
|
+ return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "统一下单失败");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //小程序支付完后,云端需要通过订单编号去支付平台查询支付状态,以便更新订单信息
|
|
|
+ _ = Task.Delay(1500).ContinueWith(async _ =>
|
|
|
+ {
|
|
|
+ using (var scope = _serviceScopeFactory.CreateScope())
|
|
|
+ {
|
|
|
+ var scopedMqttService = scope.ServiceProvider.GetRequiredService<IMqttClientService>();
|
|
|
+ var dataProperties = serviceResult.Data.GetType().GetProperty("eOrder");
|
|
|
+ var orderModel = (Fuel.Payment.Core.Models.ElectronicOrderModel)dataProperties.GetValue(serviceResult.Data);
|
|
|
+ var genericResponse = await _payService.QueryOrder(orderModel);
|
|
|
+ if (genericResponse.IsSuccessful() && orderModel.TradeStatus == Fuel.Payment.Core.Models.TradeStatus.SUCCESS)
|
|
|
+ {
|
|
|
+ trx.OrderStatus = transactionsORDERSTATUS.Paid;//将订单状态更改成已支付
|
|
|
+ trx.MiniProgramID = user.Id;
|
|
|
+ trx.BillNumber = orderModel.BillNumber;
|
|
|
+ trx.ActualPaymentAmount = trx.OriginalAmount;
|
|
|
+ trx.PaymentMethod = 2;//2 :微信支付
|
|
|
+ try
|
|
|
+ {
|
|
|
+ await scopedMqttService.SubscribeAsync("paid/" + Buid);
|
|
|
+ string jsonString = JsonConvert.SerializeObject(trx);
|
|
|
+ await Task.Delay(2000);
|
|
|
+ var sendjson = new { data = jsonString, UserName = user.UserName, UserPhoneNumber = user.UserPhoneNumber };
|
|
|
+ //支付完成将订单信息推送到fcc
|
|
|
+ await scopedMqttService.PublishAsync("paid/" + Buid, JsonConvert.SerializeObject(sendjson));
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ int affectedRows = _fsql.Update<transactions>().SetSource(trx).ExecuteAffrows();
|
|
|
+ //SendMessage(trx,Buid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ var unifiedOrderResult = dataProperties.GetValue(serviceResult.Data);
|
|
|
+ var options = new JsonSerializerOptions
|
|
|
+ {
|
|
|
+ IgnoreReadOnlyProperties = true // 忽略只读属性
|
|
|
+ };
|
|
|
+ string serializedUnifiedOrderResult = System.Text.Json.JsonSerializer.Serialize(unifiedOrderResult, options);
|
|
|
+ logger.Debug("统一下单 支付信息 " + serializedUnifiedOrderResult);
|
|
|
+ return ServiceResponse.Ok(unifiedOrderResult);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ logger.Debug("统一下单: "+ex.Message);
|
|
|
+ return ServiceResponse.Error(ex.Message);
|
|
|
}
|
|
|
+ }
|
|
|
+ public async Task<ServiceResponse> Redeem(int trxId, decimal OriginalQty)
|
|
|
+ {
|
|
|
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, "WX_SCAN", userSession.unionid);
|
|
|
- var dataProperties = serviceResult.Data.GetType().GetProperty("UnifiedOrderResult");
|
|
|
- if (!serviceResult.IsSuccessful() || dataProperties == null)
|
|
|
+ trx.OriginalQty = OriginalQty;
|
|
|
+ var refund = await RefundTrx(trxId);
|
|
|
+ if (refund.IsSuccessful() || refund.StatusCode == HttpStatusCode.NotAcceptable)
|
|
|
{
|
|
|
- return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "统一下单失败");
|
|
|
+ trx.OrderStatus = transactionsORDERSTATUS.Completed;
|
|
|
}
|
|
|
+ int affectedRows = _fsql.Update<transactions>().SetSource(trx).ExecuteAffrows();
|
|
|
+ return ServiceResponse.Ok(trx);
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 微信发送模板消息
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task SendMessage(int trxId, string orderType)
|
|
|
+ {
|
|
|
+ var data = new Dictionary<string, object>();//动态字段
|
|
|
+
|
|
|
+ Guid Buid = HttpRequestReader.GetCurrentBuId(); //站点id
|
|
|
+ var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Id == trxId).Result.FirstOrDefault();
|
|
|
+ var site = _fsql.Select<businessunitinfo>().Where(_ => _.Buid == Buid).First();
|
|
|
+ var weChatService = new WeChatService(site.Appid, site.Secret);
|
|
|
+ string WachatID = HttpRequestReader.GetWachatID(); //用户
|
|
|
+ //var userSession = WechatUserSessionRepo.GetUserSession(WachatID.ToString());
|
|
|
+ //if (userSession == null)
|
|
|
+ //{
|
|
|
+ //}
|
|
|
+ //var user = _fsql.Select<miniprogramusers>().Where(_ => _.OpenId == userSession.openid).First();
|
|
|
+
|
|
|
+ var user = _fsql.Select<miniprogramusers>().Where(_ => _.OpenId == WachatID).First();
|
|
|
+ var template = _fsql.Select<configuration>().Where(_ => _.Buid == Buid && _.Type == 2 && _.Name == "小程序通知模板").First();
|
|
|
+ if (template == null)
|
|
|
+ {
|
|
|
+ //模板获取失败
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ var config = JsonConvert.DeserializeObject<TemplateConfig>(template.Value);
|
|
|
+ var dataDict = new Dictionary<string, object>();
|
|
|
+ var dynamicData = new Dictionary<string, object>
|
|
|
+ {
|
|
|
+ { "orderType", orderType },
|
|
|
+ { "BillNumber", trx.BillNumber},
|
|
|
+ { "ProductName_Qty", trx.ProductName + " | " + trx.Qty + "L"},
|
|
|
+ { "ActualPaymentAmount", trx.ActualPaymentAmount },
|
|
|
+ { "TransactionTime", ToFormattedString(trx.TransactionTime) }
|
|
|
+ };
|
|
|
+
|
|
|
|
|
|
- //小程序支付完后,云端需要通过订单编号去支付平台查询支付状态,以便更新订单信息
|
|
|
- _ = Task.Delay(5000).ContinueWith(async _ =>
|
|
|
+ foreach (var mapping in config.DataMappings)
|
|
|
{
|
|
|
- var dataProperties = serviceResult.Data.GetType().GetProperty("eOrder");
|
|
|
- var orderModel = (Fuel.Payment.Core.Models.ElectronicOrderModel)dataProperties.GetValue(serviceResult.Data);
|
|
|
- var genericResponse = await _payService.QueryOrder(orderModel);
|
|
|
- if (genericResponse.IsSuccessful())
|
|
|
+ string dataFieldName = mapping.Key;
|
|
|
+ string targetFieldName = mapping.Value;
|
|
|
+
|
|
|
+ if (dynamicData.TryGetValue(dataFieldName, out object value))
|
|
|
{
|
|
|
- trx.OrderStatus = transactionsORDERSTATUS.Paid;//将订单状态更改成已支付
|
|
|
- await _mqttService.SubscribeAsync("fromClound/" + Buid);
|
|
|
- string jsonString = JsonConvert.SerializeObject(trx);
|
|
|
- await Task.Delay(2000);
|
|
|
- var sendjson = new { type = 3, data = jsonString };
|
|
|
- ///支付完成将订单信息推送到fcc
|
|
|
- await _mqttService.PublishAsync("fromClound/" + Buid, JsonConvert.SerializeObject(sendjson));
|
|
|
- _entityHelper.UpdateAsync(trx);
|
|
|
+ dataDict.Add(targetFieldName, new { value = value });
|
|
|
}
|
|
|
- });
|
|
|
- var unifiedOrderResult = dataProperties.GetValue(serviceResult.Data);
|
|
|
- return ServiceResponse.Ok(unifiedOrderResult);
|
|
|
+ }
|
|
|
+ // 准备要发送的模板消息内容
|
|
|
+ var templateMessage = new WeChatService.TemplateMessage
|
|
|
+ {
|
|
|
+ ToUser = user.OpenId,
|
|
|
+ TemplateId = config.TemplateId,
|
|
|
+ Page = "pages/historyOrder/historyOrder",
|
|
|
+ Data = dataDict
|
|
|
+ };
|
|
|
+ //var templateMessage = new WeChatService.TemplateMessage
|
|
|
+ //{
|
|
|
+ // ToUser = user.OpenId,
|
|
|
+ // TemplateId = "V0tl-4n-5hwNZc4SrEttvrmawAyM-SB0pQWZNwp54Ks",
|
|
|
+ // Page = "pages/historyOrder/historyOrder",
|
|
|
+ // Data = new
|
|
|
+ // {
|
|
|
+ // short_thing10 = new { value = orderType },
|
|
|
+ // character_string11 = new { value = trx.BillNumber },
|
|
|
+ // thing12 = new { value = trx.ProductName + " | " + trx.Qty + "L"},
|
|
|
+ // amount13 = new { value = trx.ActualPaymentAmount },
|
|
|
+ // time14 = new { value = ToFormattedString(trx.TransactionTime) },
|
|
|
+ // }
|
|
|
+ //};
|
|
|
+ weChatService.SendTemplateMessage(templateMessage);
|
|
|
}
|
|
|
- public async Task<ServiceResponse> Redeem(int trxId, decimal OriginalQty)
|
|
|
+ public string ToFormattedString(DateTime? dateTime)
|
|
|
{
|
|
|
- var trx = _entityHelper.GetEntitiesAsync<transactions>(_ => _.Id == trxId).Result.FirstOrDefault();
|
|
|
- if (trx == null)
|
|
|
+ if (dateTime.HasValue)
|
|
|
{
|
|
|
- return ServiceResponse.Error(HttpStatusCode.NotAcceptable, "未查询到订单!");
|
|
|
+ return dateTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public static string GetChineseStatus(transactionsORDERSTATUS status)
|
|
|
+ {
|
|
|
+ switch (status)
|
|
|
+ {
|
|
|
+ case transactionsORDERSTATUS.Unpaid:
|
|
|
+ return "订单未支付";
|
|
|
+ case transactionsORDERSTATUS.Paid:
|
|
|
+ return "订单已支付";
|
|
|
+ case transactionsORDERSTATUS.Paying:
|
|
|
+ return "订单正在支付中";
|
|
|
+ case transactionsORDERSTATUS.CardPayment:
|
|
|
+ return "订单通过卡支付";
|
|
|
+ case transactionsORDERSTATUS.Completed:
|
|
|
+ return "订单已完成";
|
|
|
+ case transactionsORDERSTATUS.Cancelled:
|
|
|
+ return "已取消";
|
|
|
+ default:
|
|
|
+ throw new ArgumentOutOfRangeException(nameof(status), status, null);
|
|
|
}
|
|
|
- trx.OriginalQty = OriginalQty;
|
|
|
- _entityHelper.UpdateAsync(trx);
|
|
|
- return ServiceResponse.Ok(trx);
|
|
|
}
|
|
|
}
|
|
|
}
|