Ejemplo n.º 1
0
        /// <summary>
        /// 取用户在门店的购物车数量
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="branchIds"></param>
        /// <returns></returns>
        public static Dictionary <long, int> GetShopBranchCartItemCount(long userId, List <long> branchIds)
        {
            Dictionary <long, int> result = new Dictionary <long, int>();

            if (userId == 0)
            {
                return(result);
            }
            Himall.Entities.ShoppingCartInfo memberCartInfo = new Himall.Entities.ShoppingCartInfo();
            List <DTO.Product.Product>       onSaleProducts = new List <DTO.Product.Product>();

            if (userId > 0)
            {//如果已登陆取购物车数据
                memberCartInfo = CartApplication.GetShopBranchCart(userId);
                if (memberCartInfo != null)
                {
                    onSaleProducts = ProductManagerApplication.GetAllStatusProductByIds(memberCartInfo.Items.Select(e => e.ProductId)).ToList();
                }
            }

            Dictionary <long, long> buyedCounts = null;

            if (userId > 0)
            {
                buyedCounts = new Dictionary <long, long>();
                buyedCounts = OrderApplication.GetProductBuyCount(userId, memberCartInfo.Items.Select(x => x.ProductId));
            }
            var shopBranchSkuList = Service.GetSkusByBranchIds(branchIds, skuids: memberCartInfo.Items.Select(s => s.SkuId).ToList());

            foreach (var id in branchIds)
            {
                //var cartQuantity = memberCartInfo.Items.Where(c => c.ShopBranchId.HasValue && c.ShopBranchId.Value == query.shopBranchId).Sum(c => c.Quantity);
                //过滤购物车 无效商品
                var cartQuantity = memberCartInfo.Items.Where(c => c.ShopBranchId == id).Select(item =>
                {
                    var product       = onSaleProducts.FirstOrDefault(p => p.Id == item.ProductId);
                    var shopbranchsku = shopBranchSkuList.FirstOrDefault(x => x.ShopBranchId == id && x.SkuId == item.SkuId);
                    long stock        = shopbranchsku == null ? 0 : shopbranchsku.Stock;

                    if (stock > product.MaxBuyCount && product.MaxBuyCount != 0)
                    {
                        stock = product.MaxBuyCount;
                    }
                    if (product.MaxBuyCount > 0 && buyedCounts != null && buyedCounts.ContainsKey(item.ProductId))
                    {
                        long buynum = buyedCounts[item.ProductId];
                        stock       = stock - buynum;
                    }
                    var status = product.IsOpenLadder ? 1 : (shopbranchsku == null ? 1 : (shopbranchsku.Status == ShopBranchSkuStatus.Normal) ? (item.Quantity > stock ? 2 : 0) : 1);//0:正常;1:冻结;2:库存不足
                    if (status == 0)
                    {
                        return(item.Quantity);
                    }
                    else
                    {
                        return(0);
                    }
                }).Sum(count => count);
                if (cartQuantity > 0)
                {
                    result.Add(id, cartQuantity);
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        public static bool BranchCanBuy(long userId, long productId, int count, string skuId, long shopBranchId, out int reason)
        {
            var product = Service.GetProduct(productId);

            if (product.SaleStatus != Entities.ProductInfo.ProductSaleStatus.OnSale || product.AuditStatus != Entities.ProductInfo.ProductAuditStatus.Audited)
            {
                //商城商品下架,但是门店的商品状态销售中,允许用户购买。
                //商城商品下架后,销售状态-仓库中,审核状态-待审核
                if (product.SaleStatus != Entities.ProductInfo.ProductSaleStatus.InStock && product.AuditStatus != Entities.ProductInfo.ProductAuditStatus.WaitForAuditing)
                {
                    reason = 1;
                    return(false);
                }
            }
            var sku = ProductManagerApplication.GetSKU(skuId);

            if (sku == null)
            {
                reason = 2;
                return(false);
            }
            var shopBranch = ShopBranchApplication.GetShopBranchById(shopBranchId);

            if (shopBranch == null)
            {
                reason = 4;
                return(false);
            }
            var shopBranchSkuList = ShopBranchApplication.GetSkusByIds(shopBranchId, new List <string> {
                skuId
            });

            if (shopBranchSkuList == null || shopBranchSkuList.Count == 0 || shopBranchSkuList[0].Status == ShopBranchSkuStatus.InStock)
            {
                reason = 2;
                return(false);
            }
            var sbsku = shopBranchSkuList.FirstOrDefault();

            if (sbsku.Stock < count)
            {
                reason = 9;
                return(false);
            }
            if (product.IsDeleted)
            {
                reason = 2;
                return(false);
            }

            if (product.MaxBuyCount <= 0)
            {
                reason = 0;
                return(true);
            }

            var buyedCounts = OrderApplication.GetProductBuyCount(userId, new long[] { productId });

            if (product.MaxBuyCount < count + (buyedCounts.ContainsKey(productId) ? buyedCounts[productId] : 0))
            {
                reason = 3;
                return(false);
            }
            reason = 0;
            return(true);
        }
Ejemplo n.º 3
0
        public static List <CartItem> GetCartItems(long memberId)
        {
            var cart = GetShoppingCart(memberId);
            //会员折扣
            var discount = MemberApplication.GetMemberDiscount(memberId);

            var skuIds   = cart.Items.Select(p => p.SkuId).ToList();
            var skus     = GetService <IProductService>().GetSKUs(skuIds);
            var products = GetService <IProductService>().GetProducts(skus.Select(p => p.ProductId).ToList());
            var shops    = GetService <IShopService>().GetShops(products.Select(p => p.ShopId).ToList());
            var types    = TypeApplication.GetTypes();
            //限时购活动
            var limitProducts = LimitTimeApplication.GetPriceByProducrIds(products.Select(p => p.Id).ToList());
            //阶梯价商品
            var allLadders = GetService <IProductLadderPriceService>().GetLadderPricesByProductIds(products.Where(p => p.IsOpenLadder).Select(p => p.Id).ToList());

            var result = new List <CartItem>();

            foreach (var item in cart.Items)
            {
                var sku     = skus.FirstOrDefault(p => p.Id == item.SkuId);
                var product = products.FirstOrDefault(p => p.Id == item.ProductId);
                if (product == null)
                {
                    continue;                 //忽略异常数据
                }
                var shop = shops.FirstOrDefault(p => p.Id == product.ShopId);
                var type = types.FirstOrDefault(p => p.Id == product.TypeId);
                if (sku == null || shop == null)
                {
                    continue;                             //忽略异常数据
                }
                #region 价格覆盖
                var minMach = 0;
                //阶梯价格
                if (product.IsOpenLadder)
                {
                    var ladders  = allLadders.Where(p => p.ProductId == item.ProductId).ToList();
                    var quantity = cart.Items.Where(c => c.ProductId == item.ProductId && c.ShopBranchId == 0).ToList().Sum(c => c.Quantity);
                    sku.SalePrice = ProductManagerApplication.GetProductLadderPrice(ladders, quantity);
                    minMach       = ladders.Min(p => p.MinBath);
                }

                var price = -1M;//其他价格
                //限时购价格
                var limit = limitProducts.FirstOrDefault(p => p.ProductId == item.ProductId);
                if (limit != null)
                {
                    price = limit.MinPrice;
                }
                //会员折扣
                if (price < 0 && shop.IsSelf)
                {
                    price = sku.SalePrice * discount;
                }

                if (price >= 0)
                {
                    sku.SalePrice = price;
                }
                #endregion

                //填充SKU别名
                ProductManagerApplication.FillSkuAlias(sku, product, type);
                //是否限时购
                var isLimit = limit != null;



                result.Add(new CartItem
                {
                    ItemId     = item.Id,
                    Quantity   = item.Quantity,
                    AddTime    = item.AddTime,
                    Product    = product,
                    Shop       = shop,
                    Sku        = sku,
                    ShowStatus = isLimit ? 1 : ProductManagerApplication.GetProductShowStatus(product, sku.Map <DTO.SKU>(), 1),
                    LimitId    = isLimit?limit.Id:0, //参与限时购活动ID
                    IsLimit    = isLimit,            //是否参加限时购
                    MinMach    = minMach,
                });
            }
            return(result);
        }