コード例 #1
0
        private async Task <List <Voucher> > GetVouchers(string id)
        {
            string sql = MSSqlFunctions.getQueryVoucher(id);

            using (IDbConnection con = Connection)
            {
                con.Open();
                var voucherDictionary         = new Dictionary <string, Voucher>();
                var productDictionary         = new Dictionary <int, Product>();
                var productCategoryDictionary = new Dictionary <int, ProductCategory>();

                var list = await con.QueryAsync <dynamic, Product, ProductCategory, Voucher>(
                    sql,
                    (voucher_, product, productCategory) =>
                {
                    Voucher voucherConverted = VoucherRepository.VoucherConvert(voucher_);
                    Voucher voucherEntry;
                    Product productEntry;
                    ProductCategory productCategoryEntry;
                    if (!voucherDictionary.TryGetValue(voucherConverted.Id, out voucherEntry))
                    {
                        voucherEntry = voucherConverted;
                        voucherDictionary.Add(voucherConverted.Id, voucherEntry);
                    }
                    if (product != null)
                    {
                        if (!productDictionary.TryGetValue(product.Id, out productEntry))
                        {
                            productEntry            = product;
                            product.productCategory = productCategory;
                            productDictionary.Add(productEntry.Id, productEntry);
                        }
                        voucherEntry.validProducts.Add(product);
                    }
                    if (productCategory != null)
                    {
                        if (!productCategoryDictionary.TryGetValue(productCategory.Id, out productCategoryEntry))
                        {
                            productCategoryEntry = productCategory;
                            productCategoryDictionary.Add(productCategoryEntry.Id, productCategoryEntry);
                        }
                        voucherEntry.validCategorys.Add(productCategory);
                    }
                    return(voucherEntry);
                },
                    splitOn : "Id,Id,Id");

                return(list.ToList());
            }
        }
コード例 #2
0
        public IList <Cart> getCarts(Nullable <int> id)
        {
            string sql = MSSqlFunctions.getQueryCart(id);

            using (IDbConnection con = Connection)
            {
                con.Open();
                var cartDictionary            = new Dictionary <int, Cart>();
                var cartItemDictionary        = new Dictionary <int, CartItem>();
                var productDictionary         = new Dictionary <int, Product>();
                var productCategoryDictionary = new Dictionary <int, ProductCategory>();
                var cartVoucherDictionary     = new Dictionary <string, Voucher>();

                return(con.Query <Cart, CartItem, Product, ProductCategory, dynamic, Cart>(
                           sql,
                           (cart, cartItem, product, productCategory, cartVoucher) =>
                {
                    Cart cartEntry;
                    CartItem cartItemEntry = null;
                    Product productEntry;
                    ProductCategory productCategoryEntry;

                    if (cartItem != null)
                    {
                        if (!productCategoryDictionary.TryGetValue(productCategory.Id, out productCategoryEntry))
                        {
                            productCategoryEntry = productCategory;
                            productCategoryDictionary.Add(productCategoryEntry.Id, productCategoryEntry);
                        }
                        if (!productDictionary.TryGetValue(product.Id, out productEntry))
                        {
                            productEntry = product;
                            productDictionary.Add(productEntry.Id, productEntry);
                        }
                        productEntry.productCategory = productCategoryEntry;
                        if (cartItem != null)
                        {
                            if (!cartItemDictionary.TryGetValue(cartItem.Id, out cartItemEntry))
                            {
                                cartItemEntry = cartItem;
                                cartItemEntry.Id = cartItem.Id;
                                cartItemEntry.Product = productEntry;
                                cartItemDictionary.Add(cartItemEntry.Id, cartItemEntry);
                            }
                        }
                    }
                    Voucher cartVoucher_ = VoucherRepository.VoucherConvert(cartVoucher);
                    Voucher cartVoucherEntry;

                    if (!cartDictionary.TryGetValue(cart.Id, out cartEntry))
                    {
                        cartEntry = cart;
                        cartDictionary.Add(cartEntry.Id, cartEntry);
                    }
                    if (cartVoucher_ != null)
                    {
                        if (!cartVoucherDictionary.TryGetValue(cartVoucher_.Id, out cartVoucherEntry))
                        {
                            cartVoucherEntry = cartVoucher_;
                            cartVoucherDictionary.Add(cartVoucherEntry.Id, cartVoucher_);
                        }
                        cartEntry.addVoucher(cartVoucherEntry);
                    }
                    if (cartItemEntry != null)
                    {
                        cartEntry.Items.Add(cartItemEntry);
                    }
                    return cartEntry;
                },
                           splitOn: "Id,Id,Id,Id,Id")
                       .Distinct()
                       .ToList());
            }
        }