2012-01-27 50 views
3

我需要創建一個數據庫解決方案來提供產品折扣。數據庫設計頭腦風暴:銷售價格

當前表:

Products 
Columns: ProductId, ProductTypeId, ReleaseDate 

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

我們希望能夠通過產品編號和/或ProductTypeId和/或ProductPriceTypeId和/或RELEASEDATE打折。

實施例銷售:

  1. 折扣單個產品編號。
  2. 折扣指定的ProductTypeId和 ProductPriceTypeId的所有產品。
  3. 在上個月內使用 ReleaseDate折扣指定ProductTypeId的所有產品。

#2的挑戰性方面並不是文字上的例子,但考慮到未來新增字段的長期可擴展性。

由於ReleaseDate,我難以忍受如何處理#3。

下面是我意識到我需要來到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) : 
} 

我只包含這個壞榜樣,我想出了展示的是如何最終結果我需要能夠使用這種新產品折扣功能。任何人都可以提供一個好的方法來處理這種情況的建議?謝謝。

回答

1

我結束了去用我的原始設計,當我使用linq to sql查詢數據庫時,使用方法得到「DiscountedPrice」。該方法運行自己的查詢並在返回結果之前對其執行條件邏輯。所以這給了我讓我的代碼做額外的工作,我的查詢將無法做到的選項。我還爲特殊的ProductPriceDiscountIds製作了Enum類,以便可以輕鬆識別它們。我希望這能幫助任何發現自己處於類似情況的人 - 迄今爲止,這對我來說工作得非常好。

2

我想說你需要一個單獨的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 S和時髦的計算,你應該能夠得到所有產品/類型和在任何特定時間的優惠價格列表...