gpt4 book ai didi

c# - 数据库设计头脑 Storm : Sale Prices

转载 作者:行者123 更新时间:2023-11-30 16:26:14 26 4
gpt4 key购买 nike

我需要创建一个数据库解决方案来提供产品折扣。

当前表:

Products
Columns: ProductId, ProductTypeId, ReleaseDate

ProductPrices
Columns: ProductPriceId, ProductPriceTypeId (one product may have n prices), ProductId, Price

我们希望能够按 ProductId 和/或 ProductTypeId 和/或 ProductPriceTypeId 和/或 ReleaseDate 打折。

销售示例:

  1. 对单个 ProductId 打折。
  2. 为指定 ProductTypeId 的所有产品打折,并且ProductPriceTypeId。
  3. 为指定 ProductTypeId 的所有产品打折发布日期在上个月内。

#2 的挑战性方面不是字面上的例子,而是考虑在未来添加新字段的事件中的长期可扩展性。

由于 ReleaseDate,我不知道如何处理 #3。

下面是我意识到需要使用 Stackoverflow 之前的想法。您可以看到,由于明确包含列,刚性结构将无法实现良好的可伸缩性——如果我们在未来添加新的条件,那么这些列将需要添加到表中——更不用说它甚至不处理发布日期要求。

新表:

ProductPriceDiscounts
Columns: ProductPriceDiscountId, ProductPriceId, ProductTypeId, ProductPriceTypeId, Discount, DiscountTypeId (1 for percentage, 2 for fixed)

然后我可以使用类似这样的东西来获取定价:

from p in Products
join pp in ProductPrices on p.ProductId equals pp.ProductId
let ppd = (
from ppd in ProductPriceDiscounts
.WhereIf(ppd.ProductPriceId != null, ppd.ProductPriceId == pp.ProductPriceId)
.WhereIf(ppd.ProductTypeId != null, ppd.ProductTypeId == pp.ProductTypeId )
.WhereIf(ppd.ProductPriceTypeId != null, ppd.ProductPriceTypeId == pp.ProductPriceId)
select ppd).FirstOrDefault()
where p.ProductId = productId
select new
{
...,
Price = pp.Price,
Discount = pp.Discount,
DiscountedPrice =
(ppd.DiscountTypeId == 1) ?
(pp.Price - (pp.Price * pp.Discount)) :
(pp.Price - pp.Discount) :
}

我只包括了这个我想出的坏例子,以展示我需要如何使用这个新产品折扣功能的最终结果。谁能提供处理这种情况的好方法的建议?谢谢。

最佳答案

我会说您需要一个单独的 DiscountDetail 表。有点像

   DiscountDetailID INT NOT NULL
DiscountTypeID INT NOT NULL --(FK On Discount Types - maybe not necessary)
DiscountProductTypeID INT NULL --(FK ON ProductType)
DiscountProductID INT NULL --(FK ON Product)
DiscountAmount INT NULL --(Some value related to %age reduction perhaps?)
DiscountDateStart DATETIME NOT NULL
DiscountDateEnd DATETIME NULL

通过一些可爱的 left join 和时髦的计算,您应该能够在任何特定时间获得所有产品/类型和折扣价格的列表...

关于c# - 数据库设计头脑 Storm : Sale Prices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9036052/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com