public Discount getProductDiscount(int storeId, int productId) { try { //SqlConnection connection = Connector.getInstance().getSQLConnection(); lock (connection) { connection.Open(); var discountEntry = connection.Query <DiscountEntry>("SELECT * FROM [dbo].[Discount] WHERE storeId=@storeId AND productId=@productId", new { storeId = storeId, productId = productId }).First(); connection.Close(); DiscountEntry d = (DiscountEntry)discountEntry; DiscountComponent dis = getDiscountByID(d.getId()); if (dis != null) { return((Discount)dis); } int discountId = d.getId(); DiscountComponentEntry component = (DiscountComponentEntry)connection.Query <DiscountComponentEntry>("SELECT * FROM [dbo].[DiscountComponent] WHERE id=@id", new { id = discountId }).First(); bool isPartOfComplex = false; if (component.getIsPartOfComplex() == 1) { isPartOfComplex = true; } if (d.getType() == "Visible") { VisibleDiscount v = new VisibleDiscount(component.getId(), isPartOfComplex, component.getPercentage(), component.getDuration(), d.getVisibleType(), component.getStoreId()); Product p = DBProduct.getInstance().getProductByID(d.getProductId()); v.setProduct(p); discounts.AddFirst(v); connection.Close(); return(v); } else { int productID = d.getProductId(); ReliantDiscount r = null; if (d.getReliantType() == "sameProduct") { Product p = DBProduct.getInstance().getProductByID(productID); r = new ReliantDiscount(component.getId(), isPartOfComplex, component.getPercentage(), component.getDuration(), d.getNumOfProducts(), p, component.getStoreId()); discounts.AddFirst(r); } return(r); } } } catch (Exception) { if (connection.State != ConnectionState.Closed) { connection.Close(); } return(null); } }
public LinkedList <DiscountComponent> getStoreDiscountsList(int storeId) { try { //SqlConnection connection = Connector.getInstance().getSQLConnection(); LinkedList <DiscountComponent> storeDiscounts = new LinkedList <DiscountComponent>(); lock (connection) { connection.Open(); var c = connection.Query <DiscountComponentEntry>("SELECT * FROM [dbo].[DiscountComponent] WHERE storeId=@storeId AND type=@type", new { storeId = storeId, type = "Discount" }).ToList <DiscountComponentEntry>(); connection.Close(); List <DiscountComponentEntry> discountList = (List <DiscountComponentEntry>)c; foreach (DiscountComponentEntry d in discountList) { connection.Open(); var discountEntry = connection.Query <DiscountEntry>("SELECT * FROM [dbo].[Discount] WHERE id=@id", new { id = d.getId() }).First(); connection.Close(); DiscountEntry de = (DiscountEntry)discountEntry; if (de.getProductId() != -1)//productDiscount { Discount dis = getProductDiscount(d.getStoreId(), de.getProductId()); if (dis.getIsPartOfComplex() == false) //add to store discounts only if it is not part of complex discount { storeDiscounts.AddFirst(dis); } } else//StoreDiscount { Discount dis = getStoreDiscount(d.getId(), d.getStoreId()); if (dis.getIsPartOfComplex() == false) { storeDiscounts.AddFirst(dis); } discounts.AddFirst(dis); } } connection.Open(); var compositeEntryList = connection.Query <DiscountComponentEntry>("SELECT * FROM [dbo].[DiscountComponent] WHERE storeId=@storeId AND type=@type", new { storeId = storeId, type = "Composite" }).ToList <DiscountComponentEntry>(); List <DiscountComponentEntry> compositeEntryL = (List <DiscountComponentEntry>)compositeEntryList; int i = 0; while (compositeEntryL.Count != 0) { DiscountComponentEntry di = compositeEntryL.ElementAt(i); List <DiscountComponent> children = new List <DiscountComponent>(); var discountChildList = connection.Query <DiscountCompositeEntry>("SELECT * FROM [dbo].[DiscountComposite] WHERE id=@id", new { id = di.getId() }).ToList <DiscountCompositeEntry>(); connection.Close(); List <DiscountCompositeEntry> de = (List <DiscountCompositeEntry>)discountChildList; string type = de.ElementAt(0).getType(); bool childrenPulledFromDB = true; foreach (DiscountCompositeEntry en in de) { if (getDiscountByID(en.getchildid()) == null) { childrenPulledFromDB = false; break; } } if (childrenPulledFromDB) { foreach (DiscountCompositeEntry en in de) { DiscountComponent disc = getDiscountByID(en.getchildid()); children.Add(disc); } bool isPartOfComplex = false; if (di.getIsPartOfComplex() == 1) { isPartOfComplex = true; } DiscountComposite compos = new DiscountComposite(di.getId(), children, type, di.getPercentage(), di.getDuration(), di.getStoreId(), isPartOfComplex); discounts.AddFirst(compos); storeDiscounts.AddFirst(compos); compositeEntryL.Remove(di); } if (compositeEntryL.Count != 0) { i = (i + 1) % compositeEntryL.Count; } } connection.Close(); return(storeDiscounts); } } catch (Exception) { if (connection.State != ConnectionState.Closed) { connection.Close(); } return(new LinkedList <DiscountComponent>()); } }