From 6b0ca05981aa8806237a09367e9ce286c17f62d2 Mon Sep 17 00:00:00 2001 From: shanj <18996038927@163.com> Date: Fri, 23 Sep 2022 05:42:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E5=90=8C=E6=AD=A5=E6=A3=80?= =?UTF-8?q?=E6=9F=A5SD=E5=9F=8B=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewModels/Order/OrderListViewModel.cs | 18 ++++- BBWY.Server.API/Startup.cs | 2 + .../Extensions/OrderCostExtension.cs | 12 ++-- BBWY.Server.Business/Order/OrderBusiness.cs | 15 +++- .../Sync/OrderSyncBusiness.cs | 68 ++++++++++++++++--- 5 files changed, 94 insertions(+), 21 deletions(-) diff --git a/BBWY.Client/ViewModels/Order/OrderListViewModel.cs b/BBWY.Client/ViewModels/Order/OrderListViewModel.cs index 5d6a2a0b..cc48f36d 100644 --- a/BBWY.Client/ViewModels/Order/OrderListViewModel.cs +++ b/BBWY.Client/ViewModels/Order/OrderListViewModel.cs @@ -58,7 +58,7 @@ namespace BBWY.Client.ViewModels public IList OrderList { get; set; } - public IList AfterSaleOrderList { get; set; } + public IList AfterSaleOrderList { get; set; } public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } } @@ -613,11 +613,23 @@ namespace BBWY.Client.ViewModels if (!response.Success) { IsLoading = false; - App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "设置刷单成本")); + App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "SD成本")); return; } //LoadOrder(PageIndex); //设置刷单刷新订单列表 - RefreshOrder(orderId); + var order = OrderList.FirstOrDefault(o => o.Id == orderId); + if (order != null && order.OrderState == Models.OrderState.待付款) + { + App.Current.Dispatcher.Invoke(() => + { + OrderList.Remove(order); + MessageBox.Show("已设置SD成本", "SD成本"); + }); + } + else + { + RefreshOrder(orderId); + } }); } diff --git a/BBWY.Server.API/Startup.cs b/BBWY.Server.API/Startup.cs index d667b210..d18b9e66 100644 --- a/BBWY.Server.API/Startup.cs +++ b/BBWY.Server.API/Startup.cs @@ -36,6 +36,8 @@ namespace BBWY.Server.API // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + services.AddMemoryCache(); + var idOption = new IdGeneratorOptions(1); var idGenerator = new DefaultIdGenerator(idOption); services.AddSingleton(typeof(IIdGenerator), idGenerator); diff --git a/BBWY.Server.Business/Extensions/OrderCostExtension.cs b/BBWY.Server.Business/Extensions/OrderCostExtension.cs index 99602be0..bd9154f6 100644 --- a/BBWY.Server.Business/Extensions/OrderCostExtension.cs +++ b/BBWY.Server.Business/Extensions/OrderCostExtension.cs @@ -10,9 +10,9 @@ namespace BBWY.Server.Business.Extensions { public static void CalculationOrderProfitAndCost(this OrderCost orderCost, Order order, IList afterSaleOrders) { - orderCost.AfterTotalCost = afterSaleOrders.Count == 0 ? 0M : afterSaleOrders.Sum(aso => aso.AfterTotalCost); - orderCost.RefundAmount = afterSaleOrders.Count == 0 ? 0M : afterSaleOrders.Sum(aso => aso.RefundAmount ?? 0M); - orderCost.RefundPurchaseAmount = afterSaleOrders.Count == 0M ? 0M : afterSaleOrders.Sum(aso => aso.RefundPurchaseAmount ?? 0); + orderCost.AfterTotalCost = afterSaleOrders == null || afterSaleOrders.Count == 0 ? 0M : afterSaleOrders.Sum(aso => aso.AfterTotalCost); + orderCost.RefundAmount = afterSaleOrders == null || afterSaleOrders.Count == 0 ? 0M : afterSaleOrders.Sum(aso => aso.RefundAmount ?? 0M); + orderCost.RefundPurchaseAmount = afterSaleOrders == null || afterSaleOrders.Count == 0M ? 0M : afterSaleOrders.Sum(aso => aso.RefundPurchaseAmount ?? 0); //退款之后平台扣点 orderCost.PlatformCommissionAmount = (order.OrderSellerPrice - orderCost.RefundAmount) * (orderCost.PlatformCommissionRatio); @@ -27,9 +27,9 @@ namespace BBWY.Server.Business.Extensions public static void CalculationSDOrderProfitAndCost(this OrderCost orderCost, Order order, IList afterSaleOrders) { - orderCost.AfterTotalCost = afterSaleOrders.Count == 0 ? 0M : afterSaleOrders.Sum(aso => aso.AfterTotalCost); - orderCost.RefundAmount = afterSaleOrders.Count == 0 ? 0M : afterSaleOrders.Sum(aso => aso.RefundAmount ?? 0M); - orderCost.RefundPurchaseAmount = afterSaleOrders.Count == 0M ? 0M : afterSaleOrders.Sum(aso => aso.RefundPurchaseAmount ?? 0); + orderCost.AfterTotalCost = afterSaleOrders == null || afterSaleOrders.Count == 0 ? 0M : afterSaleOrders.Sum(aso => aso.AfterTotalCost); + orderCost.RefundAmount = afterSaleOrders == null || afterSaleOrders.Count == 0 ? 0M : afterSaleOrders.Sum(aso => aso.RefundAmount ?? 0M); + orderCost.RefundPurchaseAmount = afterSaleOrders == null || afterSaleOrders.Count == 0M ? 0M : afterSaleOrders.Sum(aso => aso.RefundPurchaseAmount ?? 0); //退款之后平台扣点 orderCost.PlatformCommissionAmount = (order.OrderSellerPrice - orderCost.RefundAmount) * (orderCost.PlatformCommissionRatio); diff --git a/BBWY.Server.Business/Order/OrderBusiness.cs b/BBWY.Server.Business/Order/OrderBusiness.cs index 4ba81a4e..60569900 100644 --- a/BBWY.Server.Business/Order/OrderBusiness.cs +++ b/BBWY.Server.Business/Order/OrderBusiness.cs @@ -7,6 +7,7 @@ using BBWY.Server.Model.Db; using BBWY.Server.Model.Db.Mds; using BBWY.Server.Model.Dto; using FreeSql; +using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Newtonsoft.Json; @@ -32,14 +33,19 @@ namespace BBWY.Server.Business private Lazy freeSqlMultiDBManagerLazy; private FreeSqlMultiDBManager freeSqlMultiDBManager => freeSqlMultiDBManagerLazy.Value; + private IMemoryCache memoryCache; + private static TimeSpan sdGroupExpirationTimeSpan = TimeSpan.FromMinutes(20); + public OrderBusiness(RestApiService restApiService, IFreeSql fsql, IIdGenerator idGenerator, IOptions options, - IServiceProvider serviceProvider) : base(restApiService, options) + IServiceProvider serviceProvider, + IMemoryCache memoryCache) : base(restApiService, options) { this.fsql = fsql; this.idGenerator = idGenerator; + this.memoryCache = memoryCache; freeSqlMultiDBManagerLazy = new Lazy(() => serviceProvider.GetService()); productBusinessLazy = new Lazy(() => serviceProvider.GetService()); } @@ -645,7 +651,12 @@ namespace BBWY.Server.Business { var dbOrder = fsql.Select(sdCalculationCostRequest.OrderId).ToOne(); if (dbOrder == null) - throw new BusinessException($"订单号{sdCalculationCostRequest.OrderId}不存在"); + { + //待付款订单SD埋点 + //memoryCache.Remove(sdCalculationCostRequest.OrderId); + memoryCache.Set(sdCalculationCostRequest.OrderId, sdCalculationCostRequest, sdGroupExpirationTimeSpan); + return; + } //修改平台订单备注 var relayAPIHost = GetPlatformRelayAPIHost(sdCalculationCostRequest.Platform); diff --git a/BBWY.Server.Business/Sync/OrderSyncBusiness.cs b/BBWY.Server.Business/Sync/OrderSyncBusiness.cs index f8149895..bd6c61a6 100644 --- a/BBWY.Server.Business/Sync/OrderSyncBusiness.cs +++ b/BBWY.Server.Business/Sync/OrderSyncBusiness.cs @@ -1,10 +1,12 @@ using BBWY.Common.Http; using BBWY.Common.Models; +using BBWY.Server.Business.Extensions; using BBWY.Server.Business.PlatformSDK.DataExtension; using BBWY.Server.Model; using BBWY.Server.Model.Db; using BBWY.Server.Model.Dto; using FreeSql; +using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -22,13 +24,16 @@ namespace BBWY.Server.Business { private IDictionary> syncOrderMethodDic; + private IMemoryCache memoryCache; + public OrderSyncBusiness(RestApiService restApiService, IOptions options, ILogger logger, IFreeSql fsql, IIdGenerator idGenerator, TaskSchedulerManager taskSchedulerManager, - VenderBusiness venderBusiness) : base(restApiService, + VenderBusiness venderBusiness, + IMemoryCache memoryCache) : base(restApiService, options, logger, fsql, @@ -36,6 +41,7 @@ namespace BBWY.Server.Business taskSchedulerManager, venderBusiness) { + this.memoryCache = memoryCache; syncOrderMethodDic = new Dictionary>() { { Enums.Platform.京东, SyncJDOrder } @@ -210,6 +216,7 @@ namespace BBWY.Server.Business var orderId = orderJToken.Value("orderId"); var dbOrder = dbOrderList.FirstOrDefault(o => o.Id == orderId); var isNewOrder = dbOrder == null; + SDCalculationCostRequest sDCalculationCostRequest = null; //SD信息埋点 #region 订单基本信息 var buyerRemark = orderJToken.Value("orderRemark"); @@ -241,6 +248,19 @@ namespace BBWY.Server.Business StoreId = orderJToken.Value("storeId") }; + if (memoryCache.TryGetValue(orderId, out sDCalculationCostRequest)) + { + dbOrder.StorageType = Enums.StorageType.SD; + dbOrder.SDType = sDCalculationCostRequest.SDType; + dbOrder.Flag = sDCalculationCostRequest.Flag; + if (!string.IsNullOrEmpty(sDCalculationCostRequest.VenderRemark)) + dbOrder.VenderRemark = sDCalculationCostRequest.VenderRemark; + dbOrder.SDKey = sDCalculationCostRequest.SDKey; + dbOrder.SDPayBillNo = sDCalculationCostRequest.SDPayBillNo; + dbOrder.SDOperator = sDCalculationCostRequest.SDOperator; + dbOrder.SDPayChannel = sDCalculationCostRequest.SDPayChannel; + } + if (dbOrder.StoreOrder.Contains("京仓")) dbOrder.StorageType = Enums.StorageType.京仓; else if (dbOrder.StoreOrder.Contains("云仓")) @@ -401,7 +421,6 @@ namespace BBWY.Server.Business #region 扣减库存, 计算成本 if (dbOrder.StorageType != null && - dbOrder.StorageType != Enums.StorageType.SD && dbOrder.StorageType != Enums.StorageType.代发 && orderState != null && orderState != Enums.OrderState.待付款 && @@ -410,8 +429,26 @@ namespace BBWY.Server.Business var orderCost = dbOrderCostList.FirstOrDefault(oc => oc.OrderId == dbOrder.Id); if (orderCost == null) { - //再查询一次数据库,以防同步开始执行后被人为操作扣减库存,造成重复扣减库存 - if (!fsql.Select(dbOrder.Id).Any()) + if (isNewOrder && dbOrder.StorageType == Enums.StorageType.SD && sDCalculationCostRequest != null) + { + //检查SD埋点 + if (sDCalculationCostRequest.PlatformCommissionRatio == 0M) + sDCalculationCostRequest.PlatformCommissionRatio = 0.05M; + orderCost = new OrderCost() + { + OrderId = sDCalculationCostRequest.OrderId, + PlatformCommissionRatio = sDCalculationCostRequest.PlatformCommissionRatio, + PreferentialAmount = dbOrder.PreferentialAmount, + Profit = 0, + DeliveryExpressFreight = sDCalculationCostRequest.DeliveryExpressFreight, + CreateTime = DateTime.Now, + IsManualEdited = true, + SDCommissionAmount = sDCalculationCostRequest.SDCommissionAmount + }; + orderCost.CalculationSDOrderProfitAndCost(dbOrder, null); + insertOrderCostList.Add(orderCost); + } + else if (!fsql.Select(dbOrder.Id).Any()) //再查询一次数据库,以防同步开始执行后被人为操作扣减库存,造成重复扣减库存 { var orderSkuJArray = orderJToken["itemInfoList"].Where(skuJToken => skuJToken.Value("jdPrice") != 0M); if (orderSkuJArray != null && orderSkuJArray.Count() > 0) @@ -491,12 +528,13 @@ namespace BBWY.Server.Business DeliveryExpressFreight = orderDeliveryExpressFreight, CreateTime = DateTime.Now }; - orderCost.PlatformCommissionAmount = dbOrder.OrderSellerPrice * orderCost.PlatformCommissionRatio; - orderCost.Profit = dbOrder.OrderSellerPrice + - dbOrder.FreightPrice - - orderCost.PurchaseAmount - - orderCost.DeliveryExpressFreight - - orderCost.PlatformCommissionAmount; + orderCost.CalculationOrderProfitAndCost(dbOrder, null); + //orderCost.PlatformCommissionAmount = dbOrder.OrderSellerPrice * orderCost.PlatformCommissionRatio; + //orderCost.Profit = dbOrder.OrderSellerPrice + + // dbOrder.FreightPrice - + // orderCost.PurchaseAmount - + // orderCost.DeliveryExpressFreight - + // orderCost.PlatformCommissionAmount; insertOrderCostList.Add(orderCost); #endregion } @@ -526,6 +564,16 @@ namespace BBWY.Server.Business updateOrderList.Add(updateSql); } #endregion + + #region 删除SD埋点 + try + { + if (sDCalculationCostRequest != null) + memoryCache.Remove(orderId); + } + catch { } + #endregion + } #region 补齐sku logo