From 1527a229f682f778f0c970c3a7c3136dd871736f Mon Sep 17 00:00:00 2001 From: "AERWEN\\26795" <123456789a> Date: Sat, 14 Oct 2023 15:18:59 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=B7=BB=E5=8A=A0=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GoodsManager/Goodss/GoodsFreightDto.cs | 22 +- ARW.Model/Dto/Api/Pay/CommitPayDtoApi.cs | 10 + ARW.Model/Dto/Api/Pay/DiscountRes.cs | 34 +++ ARW.Model/Dto/Api/Pay/GoodsRequest.cs | 17 ++ .../PayManage/PayServiceApi.cs | 246 ++++++++++++++---- .../OrderManage/Orders/OrderService.cs | 2 +- .../OrderManage/Orders/OrderController.cs | 21 +- 7 files changed, 279 insertions(+), 73 deletions(-) create mode 100644 ARW.Model/Dto/Api/Pay/DiscountRes.cs diff --git a/ARW.Model/Dto/Api/GoodsManager/Goodss/GoodsFreightDto.cs b/ARW.Model/Dto/Api/GoodsManager/Goodss/GoodsFreightDto.cs index ff57f30..002ede2 100644 --- a/ARW.Model/Dto/Api/GoodsManager/Goodss/GoodsFreightDto.cs +++ b/ARW.Model/Dto/Api/GoodsManager/Goodss/GoodsFreightDto.cs @@ -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 - { - /// - /// 商品Id - /// - public long SpuId { get; set; } - - - /// - /// 商品SkuId - /// - public int SkuId { get; set; } - - - /// - /// 购买数量 - /// - public int Quantity { get; set; } - - } - } diff --git a/ARW.Model/Dto/Api/Pay/CommitPayDtoApi.cs b/ARW.Model/Dto/Api/Pay/CommitPayDtoApi.cs index f94b8e7..939d96f 100644 --- a/ARW.Model/Dto/Api/Pay/CommitPayDtoApi.cs +++ b/ARW.Model/Dto/Api/Pay/CommitPayDtoApi.cs @@ -39,10 +39,20 @@ namespace ARW.Model.Dto.Api.Pay /// public int CouponId { get; set;} + /// + /// 前端传递金额 + /// + public decimal TotalAmount { get; set; } + /// /// 待结算购物车商品 /// public List GoodsRequestList { get; set; } + /// + /// 客户地址guid + /// + public long CustomerAddressGuid { get; set; } + } } diff --git a/ARW.Model/Dto/Api/Pay/DiscountRes.cs b/ARW.Model/Dto/Api/Pay/DiscountRes.cs new file mode 100644 index 0000000..c416b57 --- /dev/null +++ b/ARW.Model/Dto/Api/Pay/DiscountRes.cs @@ -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 +{ + + /// + /// 优惠后的结果 + /// + /// @author lwh + /// @date 2023-10-14 + /// + public class DiscountRes + { + /// + /// 优惠券guid + /// + public long CouponGuid { get; set; } + + /// + /// 优惠券金额 + /// + public decimal CouponMoney { get; set; } + + + /// + /// 优惠后的价格 + /// + public decimal DiscountPrice { get; set; } + + } +} diff --git a/ARW.Model/Dto/Api/Pay/GoodsRequest.cs b/ARW.Model/Dto/Api/Pay/GoodsRequest.cs index cfe0231..98a2c34 100644 --- a/ARW.Model/Dto/Api/Pay/GoodsRequest.cs +++ b/ARW.Model/Dto/Api/Pay/GoodsRequest.cs @@ -19,5 +19,22 @@ namespace ARW.Model.Dto.Api.Pay /// public int CartId { get; set; } + /// + /// 商品Id + /// + public long SpuId { get; set; } + + + /// + /// 商品SkuId + /// + public int SkuId { get; set; } + + + /// + /// 购买数量 + /// + public int Quantity { get; set; } + } } diff --git a/ARW.Service/Api/BusinessService/PayManage/PayServiceApi.cs b/ARW.Service/Api/BusinessService/PayManage/PayServiceApi.cs index f490d57..a0e4cd6 100644 --- a/ARW.Service/Api/BusinessService/PayManage/PayServiceApi.cs +++ b/ARW.Service/Api/BusinessService/PayManage/PayServiceApi.cs @@ -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 CalculatePrice(CommitPayDtoApi parm) + /// + /// 计算核销前的价格 + /// + /// + /// + public async Task 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; } + + /// + /// 计算核后的价格 + /// + /// + /// + public async Task 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; + } + + + + /// /// 添加订单流水 /// /// 下单信息Dto对象 /// 订单号 + /// 核销前价格 + /// 核销后价格 /// /// - public async Task AddPayment(CommitPayDtoApi parm, string orderNo) + public async Task 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 /// 下单信息Dto对象 /// 订单号 /// 支付订单流水guid + /// 商品总额 + /// 优惠后的结果 + /// 运费 /// /// - public async Task AddOrder(CommitPayDtoApi parm, string orderNo, long paymentGuid) + public async Task 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 } + /// + /// 添加OrderGoods(订单商品副表) + /// + /// + /// + /// + + 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("订单商品数据加入失败"); + } + } + + + /// + /// 添加订单用户地址 + /// + /// + /// + /// + 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 diff --git a/ARW.Service/Business/BusinessService/OrderManage/Orders/OrderService.cs b/ARW.Service/Business/BusinessService/OrderManage/Orders/OrderService.cs index 785f282..4cc2688 100644 --- a/ARW.Service/Business/BusinessService/OrderManage/Orders/OrderService.cs +++ b/ARW.Service/Business/BusinessService/OrderManage/Orders/OrderService.cs @@ -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); diff --git a/ARW.WebApi/Controllers/Business/OrderManage/Orders/OrderController.cs b/ARW.WebApi/Controllers/Business/OrderManage/Orders/OrderController.cs index 792a462..a11b209 100644 --- a/ARW.WebApi/Controllers/Business/OrderManage/Orders/OrderController.cs +++ b/ARW.WebApi/Controllers/Business/OrderManage/Orders/OrderController.cs @@ -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; /// /// 依赖注入 /// /// 订单服务 - 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 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("删除成功!"); }