feat 添加订单添加逻辑

This commit is contained in:
AERWEN\26795 2023-10-14 15:18:59 +08:00
parent 1d01570f8f
commit 1527a229f6
7 changed files with 279 additions and 73 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using ARW.Model.Dto.Api.Pay;
using ARW.Model.Models.Business.GoodsManager.Goodss;
namespace ARW.Model.Dto.Api.GoodsManager.Goodss
@ -26,25 +27,4 @@ namespace ARW.Model.Dto.Api.GoodsManager.Goodss
}
public class GoodsRequest
{
/// <summary>
/// 商品Id
/// </summary>
public long SpuId { get; set; }
/// <summary>
/// 商品SkuId
/// </summary>
public int SkuId { get; set; }
/// <summary>
/// 购买数量
/// </summary>
public int Quantity { get; set; }
}
}

View File

@ -39,10 +39,20 @@ namespace ARW.Model.Dto.Api.Pay
/// </summary>
public int CouponId { get; set;}
/// <summary>
/// 前端传递金额
/// </summary>
public decimal TotalAmount { get; set; }
/// <summary>
/// 待结算购物车商品
/// </summary>
public List<GoodsRequest> GoodsRequestList { get; set; }
/// <summary>
/// 客户地址guid
/// </summary>
public long CustomerAddressGuid { get; set; }
}
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using ARW.Model.Models.Business.Carts;
namespace ARW.Model.Dto.Api.Pay
{
/// <summary>
/// 优惠后的结果
///
/// @author lwh
/// @date 2023-10-14
/// </summary>
public class DiscountRes
{
/// <summary>
/// 优惠券guid
/// </summary>
public long CouponGuid { get; set; }
/// <summary>
/// 优惠券金额
/// </summary>
public decimal CouponMoney { get; set; }
/// <summary>
/// 优惠后的价格
/// </summary>
public decimal DiscountPrice { get; set; }
}
}

View File

@ -19,5 +19,22 @@ namespace ARW.Model.Dto.Api.Pay
/// </summary>
public int CartId { get; set; }
/// <summary>
/// 商品Id
/// </summary>
public long SpuId { get; set; }
/// <summary>
/// 商品SkuId
/// </summary>
public int SkuId { get; set; }
/// <summary>
/// 购买数量
/// </summary>
public int Quantity { get; set; }
}
}

View File

@ -11,14 +11,19 @@ using Senparc.CO2NET.HttpUtility;
using ARW.Service.Api.IBusinessService.PayManage;
using ARW.Repository.Business.Payments;
using ARW.Repository.Business.OrderManage.Orders;
using ARW.Model.Models.Business.OrderManage.Orders;
using ARW.Model.Dto.Api.Carts;
using ARW.Repository.Business.Marketing.CouponManage.Coupons;
using Org.BouncyCastle.Crypto.Prng;
using Aliyun.Acs.Core.Logging;
using ARW.Model.Dto.Api.Pay;
using ARW.Repository.Business.Carts;
using ARW.Repository.Business.GoodsManager.Goodss;
using ARW.Repository.Business.GoodsManager.GoodsSpecs.GoodsSkus;
using ARW.Service.Api.IBusinessService.GoodsManager.Goodss;
using ARW.Model.Dto.Api.GoodsManager.Goodss;
using ARW.Model.Models.Business.OrderManage.OrderGoodss;
using ARW.Repository.Business.OrderManage.OrderGoodss;
using ARW.Repository.Business.OrderManage.OrderCustomerAddreses;
using ARW.Repository.Business.Custom.CustomerAddresses;
using ARW.Model.Models.Business.OrderManage.OrderCustomerAddreses;
using ARW.Model.Models.Business.GoodsManager.Goodss;
namespace ARW.Service.Api.BusinessService.PaymentManage
{
@ -34,6 +39,8 @@ namespace ARW.Service.Api.BusinessService.PaymentManage
private readonly SenparcHttpClient _httpClient;
private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly IGoodsServiceApi _GoodsServiceApi;
private readonly ShopRepository _ShopRepository;
private readonly GoodsRepository _GoodsRepository;
private readonly GoodsCategoryRepository _GoodsCategoryRepository;
@ -42,10 +49,14 @@ namespace ARW.Service.Api.BusinessService.PaymentManage
private readonly PaymentRepository _PaymentRepository;
private readonly CouponRepository _CouponRepository;
private readonly CartRepository _CartRepository;
private readonly GoodsSkuRepository _GoodsSkuRepository;
private readonly OrderGoodsRepository _OrderGoodsRepository;
private readonly OrderCustomerAddressRepository _OrderCustomerAddressRepository;
private readonly CustomerAddressRepository _CustomerAddressRepository;
public PayServiceApi(ShopRepository ShopRepository, GoodsCategoryRepository goodsCategoryRepository, CustomerRepository customerRepository, OrderRepository orderRepository, PaymentRepository paymentRepository, CouponRepository couponRepository, SenparcHttpClient httpClient, GoodsRepository goodsRepository)
public PayServiceApi(ShopRepository ShopRepository, GoodsCategoryRepository goodsCategoryRepository, CustomerRepository customerRepository, OrderRepository orderRepository, PaymentRepository paymentRepository, CouponRepository couponRepository, SenparcHttpClient httpClient, GoodsRepository goodsRepository, GoodsSkuRepository goodsSkuRepository, IGoodsServiceApi goodsServiceApi, CartRepository cartRepository, OrderGoodsRepository orderGoodsRepository, OrderCustomerAddressRepository orderCustomerAddressRepository, CustomerAddressRepository customerAddressRepository)
{
this._ShopRepository = ShopRepository;
_GoodsCategoryRepository = goodsCategoryRepository;
@ -55,6 +66,12 @@ namespace ARW.Service.Api.BusinessService.PaymentManage
_CouponRepository = couponRepository;
_httpClient = httpClient;
_GoodsRepository = goodsRepository;
_GoodsSkuRepository = goodsSkuRepository;
_GoodsServiceApi = goodsServiceApi;
_CartRepository = cartRepository;
_OrderGoodsRepository = orderGoodsRepository;
_OrderCustomerAddressRepository = orderCustomerAddressRepository;
_CustomerAddressRepository = customerAddressRepository;
}
#region Api接口代码
@ -79,14 +96,33 @@ namespace ARW.Service.Api.BusinessService.PaymentManage
{
#region
/* 计算商品总价 */
/* 计算核销前价格(商品总额) */
var goodsPrice = await CalculateBeforePrice(parm);
/* 计算运费 */
GoodsFreightDto goodsFreightDto = new()
{
GoodsRequestList = parm.GoodsRequestList,
CustomerAddressGuid = parm.CustomerAddressGuid,
};
var freight = await _GoodsServiceApi.GetGoodsFreight(goodsFreightDto);
var totalPrice = goodsPrice + freight;
if (parm.TotalAmount != totalPrice) throw new CustomException("前端传递金额不合法!下单失败!");
parm.TotalAmount = totalPrice;
/* 计算核销前后价格(优惠后价格) */
var afterPriceRes = await CalculateAfterPrice(parm);
/* 添加订单流水 */
var paymentGuid = await AddPayment(parm, orderNo);
var paymentGuid = await AddPayment(parm, orderNo, totalPrice, afterPriceRes.DiscountPrice);
/* 添加业务订单 */
var orderGuid = await AddOrder(parm, orderNo, paymentGuid);
var orderGuid = await AddOrder(parm, orderNo, paymentGuid, goodsPrice, afterPriceRes, freight);
/* 添加订单商品 */
await AddOrderGoods(parm, orderGuid);
/* 添加订单用户地址 */
await AddOrderCustomerAddress(parm, orderGuid);
#endregion
@ -109,7 +145,12 @@ namespace ARW.Service.Api.BusinessService.PaymentManage
#region
public async Task<decimal> CalculatePrice(CommitPayDtoApi parm)
/// <summary>
/// 计算核销前的价格
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public async Task<decimal> CalculateBeforePrice(CommitPayDtoApi parm)
{
decimal price = 0;
@ -117,27 +158,71 @@ namespace ARW.Service.Api.BusinessService.PaymentManage
{
var cart = await _CartRepository.GetFirstAsync(s => s.CartId == item.CartId);
var goods = await _GoodsRepository.GetFirstAsync(s => s.GoodsGuid == cart.GoodsGuid);
var quantity = cart.CartGoodsNum;
/* 计算商品总价 */
// 是否有规格
if(cart.GoodsSkuId != 0)
if (cart.GoodsSkuId != 0)
{
var sku = await _GoodsSkuRepository.GetFirstAsync(s => s.GoodsSkuId == cart.GoodsSkuId);
if (sku != null)
{
price += sku.GoodsSkuPrice * quantity;
}
}
else
{
price += goods.GoodsPriceHighest * quantity;
}
}
return price;
}
/// <summary>
/// 计算核后的价格
/// </summary>
/// <param name="parm"></param>
/// <returns></returns>
public async Task<DiscountRes> CalculateAfterPrice(CommitPayDtoApi parm)
{
var res = new DiscountRes();
res.DiscountPrice = parm.TotalAmount;
// 优惠券减免价格
if (parm.CouponId != 0)
{
var coupon = await _CouponRepository.GetFirstAsync(s => s.CouponId == parm.CouponId);
if (coupon != null)
{
res.CouponGuid = coupon.CouponGuid;
res.CouponMoney = coupon.CouponDeductionMoney;
res.DiscountPrice -= coupon.CouponDeductionMoney;
}
else
{
throw new CustomException($"优惠券Id为 {parm.CouponId} 找不到");
}
}
return res;
}
/// <summary>
/// 添加订单流水
/// </summary>
/// <param name="parm">下单信息Dto对象</param>
/// <param name="orderNo">订单号</param>
/// <param name="beforeMoney">核销前价格</param>
/// <param name="afterMoney">核销后价格</param>
/// <returns></returns>
/// <exception cref="CustomException"></exception>
public async Task<long> AddPayment(CommitPayDtoApi parm, string orderNo)
public async Task<long> AddPayment(CommitPayDtoApi parm, string orderNo, decimal beforeMoney, decimal afterMoney)
{
// 添加订单流水
Payment payment = new()
@ -145,8 +230,8 @@ namespace ARW.Service.Api.BusinessService.PaymentManage
CustomerGuid = parm.UserId,
PaymentNumber = orderNo,
PaymentBuytype = parm.PayType,
PaymentBeforeMoney = parm.BeforeMoney,
PaymentMoney = parm.Money,
PaymentBeforeMoney = beforeMoney,
PaymentMoney = afterMoney,
PaymentStatus = 1
};
var response = await _PaymentRepository.InsertReturnSnowflakeIdAsync(payment);
@ -163,51 +248,38 @@ namespace ARW.Service.Api.BusinessService.PaymentManage
/// <param name="parm">下单信息Dto对象</param>
/// <param name="orderNo">订单号</param>
/// <param name="paymentGuid">支付订单流水guid</param>
/// <param name="beforeMoney">商品总额</param>
/// <param name="discountRes">优惠后的结果</param>
/// <param name="freight">运费</param>
/// <returns></returns>
/// <exception cref="CustomException"></exception>
public async Task<long> AddOrder(CommitPayDtoApi parm, string orderNo, long paymentGuid)
public async Task<long> AddOrder(CommitPayDtoApi parm, string orderNo, long paymentGuid, decimal beforeMoney, DiscountRes discountRes, decimal freight)
{
Order order = new()
Model.Models.Business.OrderManage.Orders.Order order = new()
{
PaymentGuid = paymentGuid,
CustomerGuid = parm.UserId,
OrderNumber = orderNo,
GoodsTotalAmoun = parm.BeforeMoney,
OrderAmount = parm.Money,
PayPrice = parm.Money,
CouponGuid = discountRes?.CouponGuid,
CouponMoney = discountRes.CouponMoney,
GoodsTotalAmoun = beforeMoney,
OrderAmount = discountRes.DiscountPrice,
PayPrice = discountRes.DiscountPrice,
OrderRemark = parm?.Remark,
PayType = parm.PayType,
PayStatus = 1,
DeliveryStatus = 1,
ReceiptStatus = 1,
IsComment = 1,
IsSettled = 1,
OrderSource = 1,
OrderStatus = 1,
DeliveryType = 1,
ExpressPrice = freight,
};
// 计算总额
// 优惠券减免价格
if (parm.CouponId != 0)
{
var coupon = await _CouponRepository.GetFirstAsync(s => s.CouponId == parm.CouponId);
if (coupon != null)
{
order.CouponGuid = coupon.CouponGuid;
order.CouponMoney = coupon.CouponDeductionMoney;
order.PayPrice = order.OrderAmount - coupon.CouponDeductionMoney;
}
else
{
throw new CustomException($"优惠券Id为 {parm.CouponId} 找不到");
}
}
// 运费计算
// TODO:余额支付
var response = await _OrderRepository.InsertReturnSnowflakeIdAsync(order);
if (response == 0)
throw new CustomException("业务订单数据加入失败");
@ -216,6 +288,84 @@ namespace ARW.Service.Api.BusinessService.PaymentManage
}
/// <summary>
/// 添加OrderGoods(订单商品副表)
/// </summary>
/// <param name="parm"></param>
/// <param name="orderGuid"></param>
/// <returns></returns>
public async Task AddOrderGoods(CommitPayDtoApi parm, long orderGuid)
{
foreach (var item in parm.GoodsRequestList)
{
decimal singlePrice = 0;
decimal toatlPrice = 0;
var cart = await _CartRepository.GetFirstAsync(s => s.CartId == item.CartId);
var goods = await _GoodsRepository.GetFirstAsync(s => s.GoodsGuid == cart.GoodsGuid);
var quantity = cart.CartGoodsNum;
/* 计算商品总价 */
// 是否有规格
if (cart.GoodsSkuId != 0)
{
var sku = await _GoodsSkuRepository.GetFirstAsync(s => s.GoodsSkuId == cart.GoodsSkuId);
if (sku != null)
{
singlePrice = sku.GoodsSkuPrice;
toatlPrice = sku.GoodsSkuPrice * quantity;
}
}
else
{
singlePrice = goods.GoodsPriceHighest;
toatlPrice = goods.GoodsPriceHighest * quantity;
}
/* 添加OrderGoods(订单商品副表) */
OrderGoods orderGoods = new()
{
OrderGuid = orderGuid,
GoodsGuid = cart.GoodsGuid,
GoodsSkuId = cart.GoodsSkuId,
GoodsPrice = singlePrice,
GoodsTotalNum = quantity,
GoodsTotalAmoun = toatlPrice,
};
var response = await _OrderGoodsRepository.InsertReturnSnowflakeIdAsync(orderGoods);
if (response == 0)
throw new CustomException("订单商品数据加入失败");
}
}
/// <summary>
/// 添加订单用户地址
/// </summary>
/// <param name="parm"></param>
/// <param name="orderGuid"></param>
/// <returns></returns>
public async Task AddOrderCustomerAddress(CommitPayDtoApi parm, long orderGuid)
{
var customerAddress = await _CustomerAddressRepository.GetFirstAsync(s => s.CustomerAddressGuid == parm.CustomerAddressGuid);
OrderCustomerAddress orderCustomerAddress = new()
{
OrderGuid = orderGuid,
CustomerGuid = parm.UserId,
CustomerAddressGuid = customerAddress.CustomerAddressGuid,
ConsigneeName = customerAddress.CustomerAddressName,
Phont = customerAddress.CustomerAddressPhone,
ProvinceId = customerAddress.CustomerAddressProvinceId,
CityId = customerAddress.CustomerAddressCityId,
RegionId = customerAddress.CustomerAddressAreaId,
Detail = customerAddress.CustomerAddressDetailed
};
var response = await _OrderCustomerAddressRepository.InsertReturnSnowflakeIdAsync(orderCustomerAddress);
if (response == 0)
throw new CustomException("订单用户地址数据加入失败");
}
#endregion

View File

@ -317,7 +317,7 @@ namespace ARW.Service.Business.BusinessService.OrderManage.Orders
orderGoodsInfo.GoodsPicture = goods.GoodsPicture;
orderGoodsInfo.GoodsTotalNum = orderGoods.GoodsTotalNum;
orderGoodsInfo.GoodsTotalNum = orderGoods.GoodsTotalNum;
if (orderGoods.GoodsSkuId != null)
if (orderGoods.GoodsSkuId != 0)
{
var specName = await _GoodsSkuService.GetSpecValueFullName(orderGoods.GoodsSkuId);
var sku = await _GoodsSkuService.GetByIdAsync(orderGoods.GoodsSkuId);

View File

@ -17,6 +17,8 @@ using Microsoft.AspNetCore.Authorization;
using ARW.Admin.WebApi.Framework;
using ARW.Service.Api.IBusinessService.OrderManage.Orders;
using Geocoding;
using ARW.Service.Business.IBusinessService.OrderManage.OrderGoodss;
using ARW.Service.Business.IBusinessService.OrderManage.OrderCustomerAddreses;
namespace ARW.WebApi.Controllers.Business.OrderManage.Orders
{
@ -31,14 +33,18 @@ namespace ARW.WebApi.Controllers.Business.OrderManage.Orders
public class OrderController : BaseController
{
private readonly IOrderService _OrderService;
private readonly IOrderGoodsService _OrderGoodsService;
private readonly IOrderCustomerAddressService _OrderCustomerAddressService;
/// <summary>
/// 依赖注入
/// </summary>
/// <param name="OrderService">订单服务</param>
public OrderController(IOrderService OrderService)
public OrderController(IOrderService OrderService, IOrderGoodsService orderGoodsService, IOrderCustomerAddressService orderCustomerAddressService)
{
_OrderService = OrderService;
_OrderGoodsService = orderGoodsService;
_OrderCustomerAddressService = orderCustomerAddressService;
}
@ -155,11 +161,20 @@ namespace ARW.WebApi.Controllers.Business.OrderManage.Orders
[HttpDelete("{ids}")]
[ActionPermissionFilter(Permission = "business:order:delete")]
[Log(Title = "订单删除", BusinessType = BusinessType.DELETE)]
public IActionResult Delete(string ids)
public async Task<IActionResult> Delete(string ids)
{
long[] idsArr = Tools.SpitLongArrary(ids);
int[] idsArr = Tools.SpitIntArrary(ids);
if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }
//foreach (var item in idsArr)
//{
// var order = await _OrderService.GetFirstAsync(s => s.OrderId == item);
// await _OrderCustomerAddressService.DeleteAsync(s => s.OrderGuid == order.OrderGuid);
// await _OrderGoodsService.DeleteAsync(s => s.OrderGuid == order.OrderGuid);
//}
var response = _OrderService.Delete(idsArr);
return SUCCESS("删除成功!");
}