From 696f4792344003a360024a38bf458505b4774e7f Mon Sep 17 00:00:00 2001 From: shanj <18996038927@163.com> Date: Tue, 12 Apr 2022 06:02:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E4=BB=B7=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PurchaseOrder/PurchaseOrderBusiness.cs | 103 +++++++++++++----- 1 file changed, 74 insertions(+), 29 deletions(-) diff --git a/BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs b/BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs index 910c840b..f3c719a1 100644 --- a/BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs +++ b/BBWY.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs @@ -343,7 +343,7 @@ namespace BBWY.Server.Business insertOrderCost = fsql.Insert(orderCost); #endregion - #region 采购信息 + #region 代发信息 var orderDropShipping = new OrderDropShipping() { OrderId = createOnlinePurchaseOrderRequest.OrderId, @@ -532,15 +532,6 @@ namespace BBWY.Server.Business throw new Exception("未查询到代发信息"); #endregion - #region 查询采购账号 - currentProgress = "查询采购账号"; - var purchaseAccount = fsql.Select().WhereIf(orderDropshipping.PurchaseAccountId != 0, pa => pa.Id == orderDropshipping.PurchaseAccountId) - .WhereIf(orderDropshipping.PurchaseAccountId == 0, pa => pa.AccountName == orderDropshipping.BuyerAccount) - .Where(pa => pa.PurchasePlatformId == callbackPlatform).ToOne(); - if (purchaseAccount == null) - throw new Exception($"未查询到采购账号{orderDropshipping.BuyerAccount}"); - #endregion - #region 查询订单Sku currentProgress = "查询订单Sku"; var orderSkuList = fsql.Select().Where(osku => osku.OrderId == orderDropshipping.OrderId).ToList(); @@ -548,19 +539,20 @@ namespace BBWY.Server.Business throw new BusinessException("订单Sku不存在"); #endregion - #region 查询订单 - currentProgress = "查询订单"; - var dbOrder = fsql.Select(orderDropshipping.OrderId).ToOne(); - if (dbOrder == null) - throw new BusinessException("订单不存在"); - #endregion - #region 查询采购单 currentProgress = "查询采购单"; - var purchaseOrderList = fsql.Select().Where(po => po.PurchaseOrderId == purchaseOrderId && - po.PurchaseMethod == Enums.PurchaseMethod.线上采购).ToList(); + var purchaseOrderList = fsql.Select().Where(po => po.PurchaseOrderId == purchaseOrderId).ToList(); if (purchaseOrderList == null || purchaseOrderList.Count() == 0) throw new BusinessException("采购单不存在"); + if (orderSkuList.Count() > 1 && purchaseOrderList.Any(p => p.PurchaseMethod == Enums.PurchaseMethod.线下采购)) + throw new Exception("多sku订单关联采购单不支持改价"); + #endregion + + #region 查询成本 + currentProgress = "查询成本"; + var orderCost = fsql.Select(orderDropshipping.OrderId).ToOne(); + if (orderCost == null) + throw new BusinessException("订单成本不存在"); #endregion #region 查询成本明细 @@ -570,11 +562,13 @@ namespace BBWY.Server.Business throw new BusinessException("订单成本明细不存在"); #endregion - #region 查询订单成本 - currentProgress = "查询订单成本"; - var orderCost = fsql.Select(orderDropshipping.OrderId).ToOne(); - if (orderCost == null) - throw new BusinessException("订单成本不存在"); + #region 查询采购账号 + currentProgress = "查询采购账号"; + var purchaseAccount = fsql.Select().WhereIf(orderDropshipping.PurchaseAccountId != 0, pa => pa.Id == orderDropshipping.PurchaseAccountId) + .WhereIf(orderDropshipping.PurchaseAccountId == 0, pa => pa.AccountName == orderDropshipping.BuyerAccount) + .Where(pa => pa.PurchasePlatformId == callbackPlatform).ToOne(); + if (purchaseAccount == null) + throw new Exception($"未查询到采购账号{orderDropshipping.BuyerAccount}"); #endregion #region 查询改价后的订单金额 @@ -589,19 +583,70 @@ namespace BBWY.Server.Business }); #endregion + #region 查询采购单明细 + currentProgress = "查询采购单明细"; + var purchaseOrderDetails = fsql.Select().Where(p => p.OrderId == orderDropshipping.OrderId); + #endregion + + #region 查询订单 + currentProgress = "查询订单"; + var dbOrder = fsql.Select(orderDropshipping.OrderId).ToOne(); + if (dbOrder == null) + throw new BusinessException("订单不存在"); + #endregion + + IList> updatePurchaseOrders = new List>(); + IList> updateOrderCostDetails = new List>(); + foreach (var orderSku in orderSkuList) { + var currentOrderSkuProductAmount = 0M; //采购成本 + if (orderSkuList.Count() != 1) + { + var currentOrderSkuPurchaseOrderDetails = purchaseOrderDetails.Where(p => p.SkuId == orderSku.SkuId); //找当前skuId的采购skuId + currentOrderSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => currentOrderSkuPurchaseOrderDetails.Any(p1 => p1.PurchaseSkuId == p.SkuId)) + ?.Sum(p => p.ProductAmount) ?? 0M; + } + else + { + currentOrderSkuProductAmount = purchaseOrderSimpleInfo.ProductAmount; + } + var currentOrderSkuFreightAmount = purchaseOrderSimpleInfo.FreightAmount / orderSkuList.Count(); //采购运费(按sku数均分) + var purchaseOrder = purchaseOrderList.FirstOrDefault(po => po.SkuId == orderSku.SkuId); + var orderCostDetail = orderCostDetailList.FirstOrDefault(oc => oc.PurchaseOrderPKId == purchaseOrder.Id); + + purchaseOrder.SingleSkuAmount = currentOrderSkuProductAmount / orderSku.ItemTotal.Value; + purchaseOrder.SingleFreight = currentOrderSkuFreightAmount / orderSku.ItemTotal.Value; + + orderCostDetail.SkuAmount = currentOrderSkuProductAmount; + orderCostDetail.PurchaseFreight = currentOrderSkuFreightAmount; + orderCostDetail.UnitCost = purchaseOrder.UnitCost; + orderCostDetail.TotalCost = currentOrderSkuProductAmount + currentOrderSkuFreightAmount; + + updatePurchaseOrders.Add(fsql.Update().SetSource(purchaseOrder)); + updateOrderCostDetails.Add(fsql.Update().SetSource(orderCostDetail)); + } + orderCost.PurchaseAmount = purchaseOrderSimpleInfo.TotalAmount; + orderCost.Profit = dbOrder.OrderSellerPrice + + dbOrder.FreightPrice - + orderCost.PurchaseAmount - + orderCost.DeliveryExpressFreight - + orderCost.PlatformCommissionAmount; + + orderDropshipping.PurchaseAmount = purchaseOrderSimpleInfo.TotalAmount; + fsql.Transaction(() => { - //fsql.Update(purchaseOrderId).Set(po => po.SingleSkuAmount, orderAmountResponse.ProductAmount / orderSku.ItemTotal.Value) - // .Set(po => po.SingleFreight, orderAmountResponse.FreightAmount / orderSku.ItemTotal.Value) - // .ExecuteAffrows(); - //fsql.Update().Where(ocd=>) - + foreach (var update in updatePurchaseOrders) + update.ExecuteAffrows(); + foreach (var update in updateOrderCostDetails) + update.ExecuteAffrows(); + fsql.Update(orderCost.OrderId).SetSource(orderCost).ExecuteAffrows(); + fsql.Update(orderDropshipping.OrderId).SetSource(orderDropshipping).ExecuteAffrows(); }); } catch (Exception ex)