Exemple #1
0
        /// <summary>
        /// 取商品销售分类统计
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public static IEnumerable <ProductCategoryStatisticModel> GetProductCategorySales(DateTime startDate, DateTime endDate, long shopId = 0)
        {
            ProductStatisticQuery query = new ProductStatisticQuery()
            {
                StartDate = startDate,
                EndDate   = endDate
            };

            if (shopId > 0)
            {//如果不等于0,增加店铺ID条件
                query.ShopId = shopId;
            }
            //销售记录
            var productSales = _StatisticsService.GetProductAllVisits(query);
            var pids         = productSales.Select(e => e.ProductId).Distinct();
            //商品信息,分类
            var products  = ProductManagerApplication.GetAllStatusProductByIds(pids);
            var categorys = CategoryApplication.GetMainCategory();
            //补充分类
            var productCateInfo = productSales.Select(e =>
            {
                var mainCategoryId = products.FirstOrDefault(item => item.Id == e.ProductId).CategoryTopId;
                var mainCategory   = categorys.FirstOrDefault(c => c.Id == mainCategoryId);
                return(new
                {
                    CategoryId = mainCategoryId,
                    CategoryName = mainCategory == null?string.Empty:mainCategory.Name,
                    ProductId = e.ProductId,
                    ShopId = e.ShopId,
                    SaleAmounts = e.SaleAmounts,
                    SaleCounts = e.SaleCounts
                });
            });
            var totalAmount = productCateInfo.Sum(e => e.SaleAmounts);
            var totalCount  = productCateInfo.Sum(e => e.SaleCounts);
            //根据分类统计
            var productCateStat = productCateInfo.GroupBy(e => e.CategoryId).Select(e => new ProductCategoryStatisticModel
            {
                CategoryId   = e.Key,
                CategoryName = e.FirstOrDefault().CategoryName,
                SaleAmounts  = e.Sum(item => item.SaleAmounts),
                SaleCounts   = e.Sum(item => item.SaleCounts),
                EndDate      = endDate.ToString("yyyy-MM-dd"),
                StartDate    = startDate.ToString("yyyy-MM-dd"),
                AmountRate   = 0.0M,
                CountRate    = 0.0M
            });

            //计算比率
            totalAmount = totalAmount == 0 ? 1 : totalAmount;
            totalCount  = totalCount == 0 ? 1 : totalCount;
            foreach (var cate in productCateStat)
            {
                cate.AmountRate = (decimal)cate.SaleAmounts / totalAmount;
                cate.CountRate  = (decimal)cate.SaleCounts / totalCount;
            }
            return(productCateStat);
        }
Exemple #2
0
        /// <summary>
        /// 取商品统计
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static QueryPageModel <ProductStatisticModel> GetProductSales(ProductStatisticQuery query)
        {
            var productVisits = _StatisticsService.GetProductVisits(query);
            //补充商品名称
            var pids     = productVisits.Models.Select(e => e.ProductId);
            var products = ProductManagerApplication.GetAllStatusProductByIds(pids);

            foreach (var item in productVisits.Models)
            {
                var product = products.FirstOrDefault(e => e.Id == item.ProductId);
                if (product != null)
                {
                    item.ProductName = product.ProductName;
                }
            }
            return(productVisits);
        }
Exemple #3
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);
        }