Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据库设计头脑风暴:销售价格_C#_Sql_Linq To Sql_Database Design_E Commerce - Fatal编程技术网

C# 数据库设计头脑风暴:销售价格

C# 数据库设计头脑风暴:销售价格,c#,sql,linq-to-sql,database-design,e-commerce,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和/或

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

当前表格:

Products
Columns: ProductId, ProductTypeId, ReleaseDate

ProductPrices
Columns: ProductPriceId, ProductPriceTypeId (one product may have n prices), ProductId, Price
我们希望能够按ProductId和/或ProductTypeId和/或ProductPriceTypeId和/或发布日期进行折扣

示例销售:

  • 折扣一个ProductId
  • 折扣指定ProductTypeId的所有产品,并 ProductPriceTypeId
  • 使用 上个月内的发布日期
  • #2具有挑战性的方面不是字面上的例子,而是考虑到将来添加新字段时的长期可伸缩性

    由于发布日期的关系,我很难处理第三个问题

    下面是在我意识到我需要来Stackoverflow之前我在心里想的。您可以看到,刚性结构将不允许良好的可伸缩性,因为显式包含了列—如果我们将来添加了新标准,那么这些列将需要添加到表中—更不用说它甚至不能处理ReleaseDate要求

    新表格:

    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) :
    }
    

    我只举了一个糟糕的例子来说明我需要如何使用这个新产品折扣功能的最终结果。有谁能为处理这种情况的好方法提供建议吗?谢谢。

    我想你需要一张单独的
    折扣明细表。差不多

       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
    s和时髦的计算,您应该能够在任何特定时间获得所有产品/类型和折扣价格的列表…

    我最终使用了我的原始设计,当我使用linq to sql查询数据库时,我使用一种方法获得了“折扣价格”。该方法运行自己的查询,并在返回结果之前对其执行条件逻辑。因此,这让我可以选择让我的代码做查询无法完成的额外工作。我还为特殊的ProductPriceDiscountId创建了一个Enum类,这样就可以很容易地识别它们。我希望这能帮助其他处于类似情况的人——到目前为止,这对我来说非常有效