Ejemplo n.º 1
0
 public DiscountPolicy ComposeDiscountPolicy(List <DbDiscountPolicy> dbDiscountPolicies)
 {
     if (dbDiscountPolicies.Count == 0)
     {
         return(new ConditionalBasketDiscount(0, new DiscountPreCondition(CommonStr.DiscountPreConditions.NoDiscount)));
     }
     else
     {
         Dictionary <DbDiscountPolicy, List <DbDiscountPolicy> > discountGraph = BuildDiscountPolicyGraph(dbDiscountPolicies);
         DbDiscountPolicy root = GetRoot(discountGraph);
         return(ConstrucuctDiscountRecursive(discountGraph, root));
     }
 }
Ejemplo n.º 2
0
        private Dictionary <DbDiscountPolicy, List <DbDiscountPolicy> > BuildDiscountPolicyGraph(List <DbDiscountPolicy> dbDiscountPolicies)
        {
            Dictionary <DbDiscountPolicy, List <DbDiscountPolicy> > graph = new Dictionary <DbDiscountPolicy, List <DbDiscountPolicy> >();

            foreach (DbDiscountPolicy dbDiscountPolicy in dbDiscountPolicies)
            {
                if (!graph.ContainsKey(dbDiscountPolicy))
                {
                    graph.Add(dbDiscountPolicy, new List <DbDiscountPolicy>());
                }

                if (dbDiscountPolicy.ParentId != null)
                {
                    DbDiscountPolicy parent = DbManager.Instance.getParentDiscount(dbDiscountPolicy);
                    if (!graph.ContainsKey(parent))
                    {
                        graph.Add(parent, new List <DbDiscountPolicy>());
                    }
                    graph[parent].Add(dbDiscountPolicy);
                }
            }
            return(graph);
        }
Ejemplo n.º 3
0
        private DiscountPolicy ConstrucuctDiscountRecursive(Dictionary <DbDiscountPolicy, List <DbDiscountPolicy> > discountGraph, DbDiscountPolicy node)
        {
            if (discountGraph[node].Count == 0)
            {
                if (node.DiscountType == CommonStr.DiscountPolicyTypes.RevealdDiscount)
                {
                    return(new RevealdDiscount((int)node.DiscountProductId, (int)node.Discount));
                }
                else if (node.DiscountType == CommonStr.DiscountPolicyTypes.ConditionalProductDiscount)
                {
                    int?preCondition = node.PreConditionNumber;
                    if (preCondition != null)
                    {
                        DiscountPreCondition preConditionObj = new DiscountPreCondition((int)preCondition);
                        if (preCondition == CommonStr.DiscountPreConditions.NumUnitsOfProductAboveEqX)
                        {
                            return(new ConditionalProductDiscount(preConditionObj, (double)node.Discount, (int)node.MinProductUnits, (int)node.DiscountProductId));
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
                else if (node.DiscountType == CommonStr.DiscountPolicyTypes.ConditionalBasketDiscount)
                {
                    int?preCondition = node.PreConditionNumber;
                    if (preCondition != null)
                    {
                        DiscountPreCondition preConditionObj = new DiscountPreCondition((int)preCondition);
                        if (preCondition == CommonStr.DiscountPreConditions.BasketPriceAboveX)
                        {
                            return(new ConditionalBasketDiscount(preConditionObj, (double)node.Discount, (double)node.MinBasketPrice));
                        }
                        else if (preCondition == CommonStr.DiscountPreConditions.BasketProductPriceAboveEqX)
                        {
                            return(new ConditionalBasketDiscount((double)node.MinProductPrice, preConditionObj));
                        }
                        else if (preCondition == CommonStr.DiscountPreConditions.NumUnitsInBasketAboveEqX)
                        {
                            return(new ConditionalBasketDiscount(preConditionObj, (double)node.Discount, (int)node.MinUnitsAtBasket));
                        }
                        else
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }

            if (node.DiscountType != CommonStr.DiscountPolicyTypes.CompundDiscount)
            {
                throw new Exception("should be compund discount type!");
            }

            List <DiscountPolicy>   childrens   = new List <DiscountPolicy>();
            List <DbDiscountPolicy> dbChildrens = discountGraph[node];

            foreach (DbDiscountPolicy dbchild in dbChildrens)
            {
                childrens.Add(ConstrucuctDiscountRecursive(discountGraph, dbchild));
            }
            return(new CompundDiscount((int)node.MergeType, childrens));
        }