From 9f7caf736c775f84ddb16ba2b56a45e48e8b6636 Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Sat, 15 Apr 2023 23:10:30 +0800 Subject: [PATCH] Add `RuleData` in OrderDiscountPreviewInfoModel --- common.props | 2 +- .../Products/OrderDiscountPreviewInfoModel.cs | 19 +++++++++++++++---- .../Products/DemoProductDiscountProvider.cs | 4 ++-- ...inQuantityOrderDiscountPromotionHandler.cs | 15 ++++++++++++--- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/common.props b/common.props index 4428ff6a..88ae6157 100644 --- a/common.props +++ b/common.props @@ -1,7 +1,7 @@ latest - 4.0.0-preview.1 + 4.0.0-preview.2 $(NoWarn);CS1591 true EasyAbp Team diff --git a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain.Shared/EasyAbp/EShop/Products/Products/OrderDiscountPreviewInfoModel.cs b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain.Shared/EasyAbp/EShop/Products/Products/OrderDiscountPreviewInfoModel.cs index 9a4c9e27..3b02eb13 100644 --- a/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain.Shared/EasyAbp/EShop/Products/Products/OrderDiscountPreviewInfoModel.cs +++ b/modules/EasyAbp.EShop.Products/src/EasyAbp.EShop.Products.Domain.Shared/EasyAbp/EShop/Products/Products/OrderDiscountPreviewInfoModel.cs @@ -6,19 +6,28 @@ namespace EasyAbp.EShop.Products.Products; [Serializable] public class OrderDiscountPreviewInfoModel : DiscountInfoModel, ICloneable { + /// + /// This property can be used as an additional rule explanation for the UI. + /// For example, 2,rate,0.1 could mean 10% off while the quantity of order line >= 2. + /// UI cannot always calculate the discount amount correctly, but if you need to, this property can help. + /// + [CanBeNull] + public string RuleData { get; } + public OrderDiscountPreviewInfoModel() { } public OrderDiscountPreviewInfoModel([CanBeNull] string effectGroup, [NotNull] string name, [CanBeNull] string key, - [CanBeNull] string displayName, DateTime? fromTime, DateTime? toTime) : base(effectGroup, name, key, - displayName, fromTime, toTime) + [CanBeNull] string displayName, DateTime? fromTime, DateTime? toTime, [CanBeNull] string ruleData) : base( + effectGroup, name, key, displayName, fromTime, toTime) { + RuleData = ruleData; } public virtual object Clone() { - return new OrderDiscountPreviewInfoModel(EffectGroup, Name, Key, DisplayName, FromTime, ToTime); + return new OrderDiscountPreviewInfoModel(EffectGroup, Name, Key, DisplayName, FromTime, ToTime, RuleData); } public override bool Equals(object obj) @@ -33,7 +42,8 @@ public class OrderDiscountPreviewInfoModel : DiscountInfoModel, ICloneable Key == other.Key && DisplayName == other.DisplayName && Nullable.Equals(FromTime, other.FromTime) && - Nullable.Equals(ToTime, other.ToTime); + Nullable.Equals(ToTime, other.ToTime) && + RuleData == other.RuleData; } public override int GetHashCode() @@ -46,6 +56,7 @@ public class OrderDiscountPreviewInfoModel : DiscountInfoModel, ICloneable hashCode = (hashCode * 397) ^ (DisplayName != null ? DisplayName.GetHashCode() : 0); hashCode = (hashCode * 397) ^ FromTime.GetHashCode(); hashCode = (hashCode * 397) ^ ToTime.GetHashCode(); + hashCode = (hashCode * 397) ^ (RuleData != null ? RuleData.GetHashCode() : 0); return hashCode; } } diff --git a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/DemoProductDiscountProvider.cs b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/DemoProductDiscountProvider.cs index d5a6117a..a6b1b770 100644 --- a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/DemoProductDiscountProvider.cs +++ b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/DemoProductDiscountProvider.cs @@ -57,8 +57,8 @@ public class DemoProductDiscountProvider : IProductDiscountProvider var orderDiscountPreviewInfoModels = new List { - new(null, "DemoDiscount", "1", "Demo Discount 1", null, null), - new(null, "DemoDiscount", "2", "Demo Discount 2", _clock.Now.AddDays(-1), _clock.Now.AddDays(1)), + new(null, "DemoDiscount", "1", "Demo Discount 1", null, null, null), + new(null, "DemoDiscount", "2", "Demo Discount 2", _clock.Now.AddDays(-1), _clock.Now.AddDays(1), null), }; foreach (var model in orderDiscountPreviewInfoModels) diff --git a/plugins/Promotions/src/EasyAbp.EShop.Plugins.Promotions.Domain/EasyAbp/EShop/Plugins/Promotions/PromotionTypes/MinQuantityOrderDiscount/MinQuantityOrderDiscountPromotionHandler.cs b/plugins/Promotions/src/EasyAbp.EShop.Plugins.Promotions.Domain/EasyAbp/EShop/Plugins/Promotions/PromotionTypes/MinQuantityOrderDiscount/MinQuantityOrderDiscountPromotionHandler.cs index a853c812..6aa92efa 100644 --- a/plugins/Promotions/src/EasyAbp.EShop.Plugins.Promotions.Domain/EasyAbp/EShop/Plugins/Promotions/PromotionTypes/MinQuantityOrderDiscount/MinQuantityOrderDiscountPromotionHandler.cs +++ b/plugins/Promotions/src/EasyAbp.EShop.Plugins.Promotions.Domain/EasyAbp/EShop/Plugins/Promotions/PromotionTypes/MinQuantityOrderDiscount/MinQuantityOrderDiscountPromotionHandler.cs @@ -17,10 +17,15 @@ public class MinQuantityOrderDiscountPromotionHandler : PromotionHandlerBase, IS { } - public override Task HandleProductAsync(ProductDiscountContext context, Promotion promotion) + public override async Task HandleProductAsync(ProductDiscountContext context, Promotion promotion) { foreach (var discountModel in GetConfigurations(promotion).Discounts) { + if (context.ProductSku.Currency != discountModel.DynamicDiscountAmount.Currency) + { + continue; + } + if (!discountModel.IsInScope(context.Product.ProductGroupName, context.Product.Id, context.ProductSku.Id)) { continue; @@ -28,12 +33,16 @@ public class MinQuantityOrderDiscountPromotionHandler : PromotionHandlerBase, IS var newDiscount = new OrderDiscountPreviewInfoModel(PromotionConsts.PromotionEffectGroup, PromotionConsts.PromotionDiscountName, promotion.UniqueName, promotion.DisplayName, promotion.FromTime, - promotion.ToTime); + promotion.ToTime, await CreateOrderDiscountPreviewRuleDataAsync(discountModel, context, promotion)); context.OrderDiscountPreviews.Add(newDiscount); } + } - return Task.CompletedTask; + protected virtual Task CreateOrderDiscountPreviewRuleDataAsync(MinQuantityOrderDiscountModel discountModel, + ProductDiscountContext context, Promotion promotion) + { + return Task.FromResult(null); } public override Task HandleOrderAsync(OrderDiscountContext context, Promotion promotion)