public PurchasePolicy ComposePurchasePolicy(List <DbPurchasePolicy> dbPurchasePolicies) { if (dbPurchasePolicies.Count == 0) { return(new BasketPurchasePolicy(new PurchasePreCondition(CommonStr.PurchasePreCondition.allwaysTrue))); } else { Dictionary <DbPurchasePolicy, List <DbPurchasePolicy> > purchasePolicyGraph = BuildPurchPolicyGraph(dbPurchasePolicies); DbPurchasePolicy root = GetPurchaseRoot(purchasePolicyGraph); return(ConstrucuctPurchasePolicyRecursive(purchasePolicyGraph, root)); } }
private Dictionary <DbPurchasePolicy, List <DbPurchasePolicy> > BuildPurchPolicyGraph(List <DbPurchasePolicy> dbPurchasePolicies) { Dictionary <DbPurchasePolicy, List <DbPurchasePolicy> > graph = new Dictionary <DbPurchasePolicy, List <DbPurchasePolicy> >(); foreach (DbPurchasePolicy dbPurchasePolicy in dbPurchasePolicies) { if (!graph.ContainsKey(dbPurchasePolicy)) { graph.Add(dbPurchasePolicy, new List <DbPurchasePolicy>()); } if (dbPurchasePolicy.ParentId != null) { DbPurchasePolicy parent = DbManager.Instance.getParentPurchasePolicy(dbPurchasePolicy); if (!graph.ContainsKey(parent)) { graph.Add(parent, new List <DbPurchasePolicy>()); } graph[parent].Add(dbPurchasePolicy); } } return(graph); }
private PurchasePolicy ConstrucuctPurchasePolicyRecursive(Dictionary <DbPurchasePolicy, List <DbPurchasePolicy> > purchasePolicyGraph, DbPurchasePolicy node) { if (purchasePolicyGraph[node].Count == 0) { if (node.PurchasePolicyType == CommonStr.PurchasePolicyTypes.ProductPurchasePolicy) { int?preCondition = node.PreConditionNumber; PurchasePreCondition preConditionObj = new PurchasePreCondition((int)preCondition); if (preCondition != null) { if (preCondition == CommonStr.PurchasePreCondition.MaxUnitsOfProductType) { return(new ProductPurchasePolicy((int)node.MaxProductIdUnits, preConditionObj, (int)node.PolicyProductId)); } else if (preCondition == CommonStr.PurchasePreCondition.MinUnitsOfProductType) { return(new ProductPurchasePolicy(preConditionObj, (int)node.MinProductIdUnits, (int)node.PolicyProductId)); } else { return(null); } } else { return(null); } } else if (node.PurchasePolicyType == CommonStr.PurchasePolicyTypes.BasketPurchasePolicy) { int?preCondition = node.PreConditionNumber; if (preCondition != null) { PurchasePreCondition preConditionObj = new PurchasePreCondition((int)preCondition); if (preCondition == CommonStr.PurchasePreCondition.MaxItemsAtBasket) { return(new BasketPurchasePolicy(preConditionObj, (int)node.MaxItemsAtBasket)); } else if (preCondition == CommonStr.PurchasePreCondition.MinItemsAtBasket) { return(new BasketPurchasePolicy((int)node.MinItemsAtBasket, preConditionObj)); } else if (preCondition == CommonStr.PurchasePreCondition.MaxBasketPrice) { return(new BasketPurchasePolicy(preConditionObj, (double)node.MaxBasketPrice)); } else if (preCondition == CommonStr.PurchasePreCondition.MinBasketPrice) { return(new BasketPurchasePolicy((double)node.MinBasketPrice, preConditionObj)); } else if (preCondition == CommonStr.PurchasePreCondition.allwaysTrue) { return(new BasketPurchasePolicy(preConditionObj)); } else { return(null); } } else { return(null); } } else if (node.PurchasePolicyType == CommonStr.PurchasePolicyTypes.SystemPurchasePolicy) { int?preCondition = node.PreConditionNumber; if (preCondition != null) { PurchasePreCondition preConditionObj = new PurchasePreCondition((int)preCondition); if (preCondition == CommonStr.PurchasePreCondition.StoreMustBeActive) { return(new SystemPurchasePolicy(preConditionObj, node.StoreId)); } else { return(null); } } else { return(null); } } else if (node.PurchasePolicyType == CommonStr.PurchasePolicyTypes.UserPurchasePolicy) { int?preCondition = node.PreConditionNumber; if (preCondition != null) { PurchasePreCondition preConditionObj = new PurchasePreCondition((int)preCondition); if (preCondition == CommonStr.PurchasePreCondition.OwnerCantBuy) { return(new UserPurchasePolicy(preConditionObj)); } else { return(null); } } else { return(null); } } else { return(null); } } if (node.PurchasePolicyType != CommonStr.PurchasePolicyTypes.CompundPurchasePolicy) { throw new Exception("should be compund purchase type!"); } List <PurchasePolicy> childrens = new List <PurchasePolicy>(); List <DbPurchasePolicy> dbChildrens = purchasePolicyGraph[node]; foreach (DbPurchasePolicy dbchild in dbChildrens) { childrens.Add(ConstrucuctPurchasePolicyRecursive(purchasePolicyGraph, dbchild)); } return(new CompundPurchasePolicy((int)node.MergeType, childrens)); }