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

标签 c# sql linq-to-sql database-design e-commerce

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

当前表:

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/

相关文章:

c# - 更改外键约束命名约定

c# - 使用BinaryReader/BinaryWriter建立聊天

c# - 如何从 url 中检索 Json 作为字符串?

MySQL - 如果在表 F 上未找到记录,则从关系表 A、B、C、D、E 中选择数据

asp.net-mvc - LINQ:为什么我在此 LINQ 查询中收到错误?

c# - 闭包在 for 和 foreach 循环中表现不同

php - 如何使用条件语句将两个搜索结果语句合为一个?

sql - 使用 InnoDB 进行全文搜索

c# - 使用 2 个 from 子句重写 linq-to-sql 查询以连接

sql - 是否可以在不拖放的情况下使用 Linq-SQL?