74 lines
2.5 KiB
C#
74 lines
2.5 KiB
C#
using HuanMeng.MiaoYu.Code.Cache.Special;
|
|
using HuanMeng.MiaoYu.Code.Payment;
|
|
using HuanMeng.MiaoYu.Model.Dto.Order;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace HuanMeng.MiaoYu.Code.Order
|
|
{
|
|
/// <summary>
|
|
/// 订单数据
|
|
/// </summary>
|
|
public class OrderBLL : MiaoYuBase
|
|
{
|
|
public OrderBLL(IServiceProvider serviceProvider) : base(serviceProvider)
|
|
{
|
|
}
|
|
|
|
public async Task<BaseResponse<IntentOrderDto>> CreateOrder(string paymentMethod, string productId)
|
|
{
|
|
if (_UserId == 0)
|
|
{
|
|
throw new ArgumentNullException("未登录");
|
|
}
|
|
if (string.IsNullOrEmpty(productId))
|
|
{
|
|
throw new ArgumentNullException("产品不能为空");
|
|
}
|
|
ProductEntityCache productEntityCache = new ProductEntityCache(this);
|
|
var products = productEntityCache.GetDataList();
|
|
var product = products.FirstOrDefault(it => it.ProductId == productId);
|
|
if (product == null)
|
|
{
|
|
throw new NullReferenceException("未找到所属产品");
|
|
}
|
|
var redisLock = $"lock:payment:{_UserId}:{productId}";
|
|
if (!RedisCache.StringSetLock(redisLock, "", 5))
|
|
{
|
|
throw new ArgumentNullException("重复创建订单");
|
|
}
|
|
|
|
//productEntityCache.get
|
|
IntentOrderDto intentOrderDto = null;
|
|
//创建订单
|
|
try
|
|
{
|
|
var ip = HttpContextAccessor.HttpContext.GetClientIpAddress();
|
|
var payment = PaymentExtend.GetPayment(paymentMethod);
|
|
(var orderId, var order) = await payment.CreateOrder(product.ProductName, product.Price, product, ip);
|
|
var t = product.ToIntentOrder(paymentMethod, orderId);
|
|
t.UserId = _UserId;
|
|
Dao.daoDbMiaoYu.context.Add(t);
|
|
await Dao.daoDbMiaoYu.context.SaveChangesAsync();
|
|
intentOrderDto = new IntentOrderDto()
|
|
{
|
|
OrderId = orderId,
|
|
Payment = order
|
|
};
|
|
RedisCache.KeyDelete(redisLock);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
RedisCache.KeyDelete(redisLock);
|
|
throw new Exception("创建订单失败");
|
|
}
|
|
|
|
return new BaseResponse<IntentOrderDto>(ResonseCode.Success, "", intentOrderDto);
|
|
}
|
|
}
|
|
}
|