我需要創建一個數據庫解決方案來提供產品折扣。數據庫設計頭腦風暴:銷售價格
當前表:
Products
Columns: ProductId, ProductTypeId, ReleaseDate
ProductPrices
Columns: ProductPriceId, ProductPriceTypeId (one product may have n prices), ProductId, Price
我們希望能夠通過產品編號和/或ProductTypeId和/或ProductPriceTypeId和/或RELEASEDATE打折。
實施例銷售:
- 折扣單個產品編號。
- 折扣指定的ProductTypeId和 ProductPriceTypeId的所有產品。
- 在上個月內使用 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) :
}
我只包含這個壞榜樣,我想出了展示的是如何最終結果我需要能夠使用這種新產品折扣功能。任何人都可以提供一個好的方法來處理這種情況的建議?謝謝。