diff --git a/BBWYB.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs b/BBWYB.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs index 7289b69..a753d63 100644 --- a/BBWYB.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs +++ b/BBWYB.Server.Business/PurchaseOrder/PurchaseOrderBusiness.cs @@ -739,7 +739,7 @@ namespace BBWYB.Server.Business purchaserId = purchaserId.Replace("b2b-", string.Empty); var purchaserId2 = purchaserId.Substring(0, purchaserId.Length - 5); var purchaserIds = new List() { purchaserId, purchaserId2 }; - var dbPurchaser = fsql.Select().Where(p => p.Platform == Enums.Platform.阿里巴巴 && (purchaserIds.Contains(p.Id) || + var dbPurchaser = fsql.Select().Where(p => p.Platform == Enums.Platform.阿里巴巴 && (purchaserIds.Contains(p.Id) || purchaserIds.Contains(p.Id2))).ToOne(); if (dbPurchaser == null) throw new BusinessException($"采购单{purchaseOrder.PurchaseOrderId}缺少有效采购商"); @@ -2112,6 +2112,9 @@ namespace BBWYB.Server.Business case "LOGISTICS_BUYER_VIEW_TRACE": // LogisticsUpdateCallbackFrom1688(jObject);//1688物流信息变更 break; + case "ORDER_BUYER_VIEW_ORDER_PAY": + OrderPayFrom1688(jObject); + break; default: break; } @@ -2370,6 +2373,12 @@ namespace BBWYB.Server.Business var purchaseOrderId = jObject["data"].Value("orderId"); Task.Factory.StartNew(() => OrderPriceModificationCallback(purchaseOrderId, Enums.Platform.阿里巴巴), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler); } + + private void OrderPayFrom1688(JObject jObject) + { + var purchaseOrderId = jObject["data"].Value("orderId"); + Task.Factory.StartNew(() => OrderPayCallback(purchaseOrderId, Enums.Platform.阿里巴巴), CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.PurchaseOrderCallbackTaskScheduler); + } #endregion @@ -2380,179 +2389,12 @@ namespace BBWYB.Server.Business /// private void OrderPriceModificationCallback(string purchaseOrderId, Enums.Platform callbackPlatform) { - bool isEditOrderPrice = true; - try - { - var orderPurchaseInfo = fsql.Select().Where(opi => opi.PurchaseOrderId == purchaseOrderId).ToOne(); - if (orderPurchaseInfo == null) - throw new Exception($"采购单{purchaseOrderId}-未查询到采购单"); - - var purchaseAccount = fsqlManager.MDSfsql.Select().Where(pa => pa.Id == orderPurchaseInfo.PurchaseAccountId).ToOne(); - if (purchaseAccount == null) - throw new Exception($"采购单{purchaseOrderId}-未查询到采购账号{orderPurchaseInfo.PurchaseAccountId}"); - - var dbOrder = fsql.Select(orderPurchaseInfo.OrderId).ToOne(); - var dbOrderCost = fsql.Select(orderPurchaseInfo.OrderId).ToOne(); - if (dbOrderCost == null) - throw new Exception($"采购单{purchaseOrderId}-未查询到订单{orderPurchaseInfo.OrderId}的成本"); - - var shop = fsqlManager.MDSfsql.Select().Where(s => s.ShopId == dbOrder.ShopId.ToString()).ToOne(); - - var dbOrderCostDetails = fsql.Select().Where(ocd => ocd.OrderId == dbOrder.Id && ocd.IsEnabled == true).ToList(); - if (dbOrderCostDetails.Count() == 0) - throw new Exception($"采购单{purchaseOrderId}-未查询到订单{orderPurchaseInfo.OrderId}的明细成本"); - - - var dbOrderPurchaseInfoList = fsql.Select().Where(opi => opi.OrderId == orderPurchaseInfo.OrderId && opi.IsEnabled == true).ToList(); - if (dbOrderPurchaseInfoList.Count() == 0) - throw new Exception($"采购单{purchaseOrderId}-未查询到订单{orderPurchaseInfo.OrderId}的采购单集合"); - - isEditOrderPrice = dbOrderPurchaseInfoList.Count() == dbOrderPurchaseInfoList.Count(opi => opi.PurchasePlatform == Enums.Platform.阿里巴巴 && - opi.PurchaseMethod == Enums.PurchaseMethod.线上采购); - - var dbPurchaseOrderIdList = dbOrderPurchaseInfoList.Select(x => x.PurchaseOrderId).ToList(); - var dbOrderPurchaseRelationInfos = fsql.Select().Where(x => dbPurchaseOrderIdList.Contains(x.PurchaseOrderId)).ToList(); - if (dbOrderPurchaseRelationInfos.Count() == 0) - throw new Exception($"采购单{purchaseOrderId}-未查询到采购单关联明细"); - - var dbOrderSkus = fsql.Select().Where(osku => osku.OrderId == orderPurchaseInfo.OrderId).ToList(); - - List> updateOrderCostDetailList = new List>(); - IUpdate updateOrderCost = null; - - IList> updateOrderSkuList = new List>(); - IUpdate updateOrder = null; - - IList op_EditPriceSkuRequests = new List(); - - var ppclient = ppPlatformClientFactory.GetClient(AdapterEnums.PlatformType.阿里巴巴); - - var totalPurchaseProductAmount = 0M; - var totalPurchaseFreight = 0M; - foreach (var opi in dbOrderPurchaseInfoList) - { - if (opi.PurchasePlatform != Enums.Platform.阿里巴巴) - continue; - - var purchaseOrderSimpleInfo = ppclient.QueryOrderDetail(new PP_QueryOrderDetailRequest() - { - AppKey = purchaseAccount.AppKey, - AppSecret = purchaseAccount.AppSecret, - AppToken = purchaseAccount.AppToken, - OrderId = opi.PurchaseOrderId - }); - - totalPurchaseProductAmount += purchaseOrderSimpleInfo.ProductAmount; - totalPurchaseFreight += purchaseOrderSimpleInfo.FreightAmount; - - var purchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Sum(x => x.Quantity); - var currentPurchaseOrderRelationInfos = dbOrderPurchaseRelationInfos.Where(x => x.PurchaseOrderId == opi.PurchaseOrderId).ToList(); - - var belongSkuGroups = currentPurchaseOrderRelationInfos.GroupBy(p => p.BelongSkuId); - foreach (var belongSkuGroup in belongSkuGroups) - { - var belongSkuId = belongSkuGroup.Key; - - var currentOrderSkuCargoParamList = belongSkuGroup.ToList(); //找当前skuId的采购skuId - var currentOrderSkuProductAmount = 0M; //采购成本 - var currentSkuTotalPurchaseQuantity = currentOrderSkuCargoParamList.Sum(x => x.Quantity); //当前skuId的采购数量总和 - var currentPurchaseFreight = purchaseOrderSimpleInfo.FreightAmount * - (1.0M * currentSkuTotalPurchaseQuantity / purchaseSkuTotalQuantity); //采购运费 - - foreach (var currentOrderSkuCargo in currentOrderSkuCargoParamList) - { - var currentPurchaseSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.PurchaseSkuId) - .Sum(p => p.ProductAmount); - var currentPurchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.PurchaseSkuId) - .Sum(p => p.Quantity); - currentOrderSkuProductAmount += currentPurchaseSkuProductAmount * (1.0M * currentOrderSkuCargo.Quantity.Value / currentPurchaseSkuTotalQuantity); - } - - #region 订单sku平价 - var orderSku = dbOrderSkus.FirstOrDefault(osku => osku.SkuId == belongSkuId); - //if (isEditOrderPrice) - //{ - // orderSku.Price = currentOrderSkuProductAmount / orderSku.ItemTotal; - // orderSku.BuyerPayFreight = currentPurchaseFreight; - // op_EditPriceSkuRequests.Add(new OP_EditPriceSkuRequest() - // { - // Freight = currentPurchaseFreight ?? 0M, - // InPackAmountPrice = orderSku.InPackAmount ?? 0M, - // OrderSkuId = orderSku.Id.ToString(), - // Price = orderSku.Price ?? 0M, - // SkuId = orderSku.SkuId - // }); - // updateOrderSkuList.Add(fsql.Update(orderSku.Id).Set(osku => osku.Price, orderSku.Price) - // .Set(osku => osku.BuyerPayFreight, orderSku.BuyerPayFreight)); - //} - #endregion - - var dbOrderSkuDetail = dbOrderCostDetails.FirstOrDefault(ocd => ocd.SkuId == belongSkuId && ocd.PurchaseOrderId == opi.PurchaseOrderId); - dbOrderSkuDetail.CalculationOrderCostDetailCostAndProfit(orderSku.Price.Value * orderSku.ItemTotal.Value, - orderSku.BuyerPayFreight ?? 0M, - orderSku.InPackAmount ?? 0M, - currentOrderSkuProductAmount, - currentPurchaseFreight ?? 0M, - dbOrderSkuDetail.OutPackAmount ?? 0M, - dbOrderSkuDetail.DeliveryExpressFreight ?? 0M); - updateOrderCostDetailList.Add(fsql.Update().SetSource(dbOrderSkuDetail)); - } - } - //if (isEditOrderPrice) - //{ - // dbOrder.OrderTotalPrice = totalPurchaseProductAmount + totalPurchaseFreight; - // dbOrder.OrderSellerPrice = totalPurchaseProductAmount; - // dbOrder.FreightPrice = totalPurchaseFreight; - // updateOrder = fsql.Update(dbOrder.Id).Set(o => o.OrderTotalPrice, dbOrder.OrderTotalPrice) - // .Set(o => o.OrderSellerPrice, dbOrder.OrderSellerPrice) - // .Set(o => o.FreightPrice, dbOrder.FreightPrice); - //} - - dbOrderCost.CalculationOrderCostCostAndProfit(dbOrder.OrderTotalPrice ?? 0M, - totalPurchaseProductAmount, - totalPurchaseFreight, - dbOrderCostDetails.Sum(ocd => ocd.OutPackAmount ?? 0M), - dbOrderCostDetails.Sum(ocd => ocd.DeliveryExpressFreight ?? 0M)); - updateOrderCost = fsql.Update().SetSource(dbOrderCost); - - fsql.Transaction(() => - { - foreach (var update in updateOrderCostDetailList) - update.ExecuteAffrows(); - updateOrderCost?.ExecuteAffrows(); - //foreach (var update in updateOrderSkuList) - // update.ExecuteAffrows(); - //updateOrder?.ExecuteAffrows(); - }); - + OnSomeOnePurchaseOrderChanged(purchaseOrderId, true); + } - //if (isEditOrderPrice) - //{ - // #region 通知拳探改价 - // var opclient = opPlatformClientFactory.GetClient(AdapterEnums.PlatformType.拳探); - // opclient.EditPrice(new OP_EditPriceRequest() - // { - // AppKey = shop.AppKey, - // AppSecret = shop.AppSecret, - // AppToken = shop.AppToken, - // OrderId = dbOrder.Id, - // EditItems = op_EditPriceSkuRequests - // }); - // #endregion - - // #region 通知C端改价 - // restApiService.SendRequest("https://bbwy.qiyue666.com", - // "/Api/PurchaseOrder/QuanTanEditPriceCallback", - // new { orderId = dbOrder.Id }, - // null, - // HttpMethod.Post); - // #endregion - //} - } - catch (Exception ex) - { - nLogManager.Default().Error(ex, $"OrderPriceModificationCallback 回调平台{callbackPlatform},采购单号{purchaseOrderId}"); - } + private void OrderPayCallback(string purchaseOrderId, Enums.Platform callbackPlatform) + { + OnSomeOnePurchaseOrderChanged(purchaseOrderId, false); } public void KuaiDi100Publish(string param) @@ -2874,6 +2716,193 @@ namespace BBWYB.Server.Business } } + /// + /// 采购单信息变更(改价,付款) + /// + /// 采购单Id + /// 当不需要触发订单改价时是否继续走流程 + private void OnSomeOnePurchaseOrderChanged(string purchaseOrderId, bool keepRunWhenNoEditOrderPirce) + { + bool isEditOrderPrice = true; + try + { + var orderPurchaseInfo = fsql.Select().Where(opi => opi.PurchaseOrderId == purchaseOrderId).ToOne(); + if (orderPurchaseInfo == null) + throw new Exception($"采购单{purchaseOrderId}-未查询到采购单"); + + var dbOrder = fsql.Select(orderPurchaseInfo.OrderId).ToOne(); + + var dbOrderPurchaseInfoList = fsql.Select().Where(opi => opi.OrderId == orderPurchaseInfo.OrderId && opi.IsEnabled == true).ToList(); + if (dbOrderPurchaseInfoList.Count() == 0) + throw new Exception($"采购单{purchaseOrderId}-未查询到订单{orderPurchaseInfo.OrderId}的采购单集合"); + + isEditOrderPrice = dbOrderPurchaseInfoList.Count() == dbOrderPurchaseInfoList.Count(opi => opi.PurchasePlatform == Enums.Platform.阿里巴巴 && + opi.PurchaseMethod == Enums.PurchaseMethod.线上采购) && + dbOrder.OrderState != Enums.OrderState.已取消 && + dbOrder.OrderState != Enums.OrderState.部分采购; + + if (!keepRunWhenNoEditOrderPirce && !isEditOrderPrice) + return; + + var dbOrderCost = fsql.Select(dbOrder.Id).ToOne(); + if (dbOrderCost == null) + throw new Exception($"采购单{purchaseOrderId}-未查询到订单{dbOrder.Id}的成本"); + + var purchaseAccount = fsqlManager.MDSfsql.Select().Where(pa => pa.Id == orderPurchaseInfo.PurchaseAccountId).ToOne(); + if (purchaseAccount == null) + throw new Exception($"采购单{purchaseOrderId}-未查询到采购账号{orderPurchaseInfo.PurchaseAccountId}"); + + var shop = fsqlManager.MDSfsql.Select().Where(s => s.ShopId == dbOrder.ShopId.ToString()).ToOne(); + + var dbOrderCostDetails = fsql.Select().Where(ocd => ocd.OrderId == dbOrder.Id && ocd.IsEnabled == true).ToList(); + if (dbOrderCostDetails.Count() == 0) + throw new Exception($"采购单{purchaseOrderId}-未查询到订单{orderPurchaseInfo.OrderId}的明细成本"); + + + var dbPurchaseOrderIdList = dbOrderPurchaseInfoList.Select(x => x.PurchaseOrderId).ToList(); + var dbOrderPurchaseRelationInfos = fsql.Select().Where(x => dbPurchaseOrderIdList.Contains(x.PurchaseOrderId)).ToList(); + if (dbOrderPurchaseRelationInfos.Count() == 0) + throw new Exception($"采购单{purchaseOrderId}-未查询到采购单关联明细"); + + var dbOrderSkus = fsql.Select().Where(osku => osku.OrderId == orderPurchaseInfo.OrderId).ToList(); + + List> updateOrderCostDetailList = new List>(); + IUpdate updateOrderCost = null; + + IList> updateOrderSkuList = new List>(); + IUpdate updateOrder = null; + + IList op_EditPriceSkuRequests = new List(); + + var ppclient = ppPlatformClientFactory.GetClient(AdapterEnums.PlatformType.阿里巴巴); + + var totalPurchaseProductAmount = 0M; + var totalPurchaseFreight = 0M; + foreach (var opi in dbOrderPurchaseInfoList) + { + if (opi.PurchasePlatform != Enums.Platform.阿里巴巴) + continue; + + var purchaseOrderSimpleInfo = ppclient.QueryOrderDetail(new PP_QueryOrderDetailRequest() + { + AppKey = purchaseAccount.AppKey, + AppSecret = purchaseAccount.AppSecret, + AppToken = purchaseAccount.AppToken, + OrderId = opi.PurchaseOrderId + }); + + totalPurchaseProductAmount += purchaseOrderSimpleInfo.ProductAmount; + totalPurchaseFreight += purchaseOrderSimpleInfo.FreightAmount; + + var purchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Sum(x => x.Quantity); + var currentPurchaseOrderRelationInfos = dbOrderPurchaseRelationInfos.Where(x => x.PurchaseOrderId == opi.PurchaseOrderId).ToList(); + + var belongSkuGroups = currentPurchaseOrderRelationInfos.GroupBy(p => p.BelongSkuId); + foreach (var belongSkuGroup in belongSkuGroups) + { + var belongSkuId = belongSkuGroup.Key; + + var currentOrderSkuCargoParamList = belongSkuGroup.ToList(); //找当前skuId的采购skuId + var currentOrderSkuProductAmount = 0M; //采购成本 + var currentSkuTotalPurchaseQuantity = currentOrderSkuCargoParamList.Sum(x => x.Quantity); //当前skuId的采购数量总和 + var currentPurchaseFreight = purchaseOrderSimpleInfo.FreightAmount * + (1.0M * currentSkuTotalPurchaseQuantity / purchaseSkuTotalQuantity); //采购运费 + + foreach (var currentOrderSkuCargo in currentOrderSkuCargoParamList) + { + var currentPurchaseSkuProductAmount = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.PurchaseSkuId) + .Sum(p => p.ProductAmount); + var currentPurchaseSkuTotalQuantity = purchaseOrderSimpleInfo.ItemList.Where(p => p.SkuId == currentOrderSkuCargo.PurchaseSkuId) + .Sum(p => p.Quantity); + currentOrderSkuProductAmount += currentPurchaseSkuProductAmount * (1.0M * currentOrderSkuCargo.Quantity.Value / currentPurchaseSkuTotalQuantity); + } + + #region 订单sku平价 + var orderSku = dbOrderSkus.FirstOrDefault(osku => osku.SkuId == belongSkuId); + if (isEditOrderPrice) + { + orderSku.Price = currentOrderSkuProductAmount / orderSku.ItemTotal; + orderSku.BuyerPayFreight = currentPurchaseFreight; + op_EditPriceSkuRequests.Add(new OP_EditPriceSkuRequest() + { + Freight = currentPurchaseFreight ?? 0M, + InPackAmountPrice = orderSku.InPackAmount ?? 0M, + OrderSkuId = orderSku.Id.ToString(), + Price = orderSku.Price ?? 0M, + SkuId = orderSku.SkuId + }); + updateOrderSkuList.Add(fsql.Update(orderSku.Id).Set(osku => osku.Price, orderSku.Price) + .Set(osku => osku.BuyerPayFreight, orderSku.BuyerPayFreight)); + } + #endregion + + var dbOrderSkuDetail = dbOrderCostDetails.FirstOrDefault(ocd => ocd.SkuId == belongSkuId && ocd.PurchaseOrderId == opi.PurchaseOrderId); + dbOrderSkuDetail.CalculationOrderCostDetailCostAndProfit(orderSku.Price.Value * orderSku.ItemTotal.Value, + orderSku.BuyerPayFreight ?? 0M, + orderSku.InPackAmount ?? 0M, + currentOrderSkuProductAmount, + currentPurchaseFreight ?? 0M, + dbOrderSkuDetail.OutPackAmount ?? 0M, + dbOrderSkuDetail.DeliveryExpressFreight ?? 0M); + updateOrderCostDetailList.Add(fsql.Update().SetSource(dbOrderSkuDetail)); + } + } + if (isEditOrderPrice) + { + dbOrder.OrderTotalPrice = totalPurchaseProductAmount + totalPurchaseFreight; + dbOrder.OrderSellerPrice = totalPurchaseProductAmount; + dbOrder.FreightPrice = totalPurchaseFreight; + updateOrder = fsql.Update(dbOrder.Id).Set(o => o.OrderTotalPrice, dbOrder.OrderTotalPrice) + .Set(o => o.OrderSellerPrice, dbOrder.OrderSellerPrice) + .Set(o => o.FreightPrice, dbOrder.FreightPrice); + } + + dbOrderCost.CalculationOrderCostCostAndProfit(dbOrder.OrderTotalPrice ?? 0M, + totalPurchaseProductAmount, + totalPurchaseFreight, + dbOrderCostDetails.Sum(ocd => ocd.OutPackAmount ?? 0M), + dbOrderCostDetails.Sum(ocd => ocd.DeliveryExpressFreight ?? 0M)); + updateOrderCost = fsql.Update().SetSource(dbOrderCost); + + fsql.Transaction(() => + { + foreach (var update in updateOrderCostDetailList) + update.ExecuteAffrows(); + updateOrderCost?.ExecuteAffrows(); + foreach (var update in updateOrderSkuList) + update.ExecuteAffrows(); + updateOrder?.ExecuteAffrows(); + }); + + if (isEditOrderPrice) + { + #region 通知拳探改价 + var opclient = opPlatformClientFactory.GetClient(AdapterEnums.PlatformType.拳探); + opclient.EditPrice(new OP_EditPriceRequest() + { + AppKey = shop.AppKey, + AppSecret = shop.AppSecret, + AppToken = shop.AppToken, + OrderId = dbOrder.Id, + EditItems = op_EditPriceSkuRequests + }); + #endregion + + #region 通知C端改价 + restApiService.SendRequest("https://bbwy.qiyue666.com", + "/Api/PurchaseOrder/QuanTanEditPriceCallback", + new { orderId = dbOrder.Id }, + null, + HttpMethod.Post); + #endregion + } + } + catch (Exception ex) + { + nLogManager.Default().Error(ex, $"OrderPriceModificationCallback 采购单号{purchaseOrderId}"); + } + } + private void SendDingDing(string content) { try