Exemplo n.º 1
0
 public DeliveryFeeSumBLL(DeliveryFeeSumMethond SumFeeMethod)
 {
     this._SumFeeMethod = SumFeeMethod;
 }
Exemplo n.º 2
0
        /// <summary>
        /// 获取商品运费(专业版:通过购物车商品)
        /// </summary>
        /// <param name="goodCarts"></param>
        /// <param name="sumMethod"></param>
        /// <returns></returns>
        public DeliveryFeeResult GetDeliveryFeeSum(List <EntGoodsCart> goodCarts, string provinces, string city, DeliveryFeeSumMethond sumMethod)
        {
            DeliveryFeeResult result = new DeliveryFeeResult();

            if (string.IsNullOrWhiteSpace(provinces) || string.IsNullOrWhiteSpace(city))
            {
                result.InRange = false;
                result.Message = "无效配送地址";
                return(result);
            }

            //获取商品
            string goodsId = string.Join(",", goodCarts.Select(good => good.FoodGoodsId));

            if (string.IsNullOrWhiteSpace(goodsId))
            {
                result.InRange = false;
                return(result);
            }

            List <EntGoods>        goods           = EntGoodsBLL.SingleModel.GetListByIds(goodsId);
            List <DeliveryProduct> computedProduct = goodCarts.Where(item => goods.FindIndex(good => good.id == item.FoodGoodsId) > -1).Select(item => {
                //商品
                EntGoods good = goods.Find(thisGood => thisGood.id == item.FoodGoodsId);
                return(new DeliveryProduct
                {
                    Name = good.name,
                    Count = item.Count,
                    TemplateId = good.TemplateId,
                    Weight = good.Weight,
                    Amount = item.Price,
                    Id = good.id
                });
            }).ToList();

            return(GetDeliveryFeeCommon(productInfo: computedProduct, provinces: provinces, city: city, sumMethod: sumMethod));
        }
Exemplo n.º 3
0
        public DeliveryFeeResult GetDeliveryFee(BargainUser bargainOrder, string province, string city, DeliveryFeeSumMethond sumMethod, DeliveryConfig config = null)
        {
            if (config == null)
            {
                config = DeliveryConfigBLL.SingleModel.GetConfig(new Bargain {
                    Id = bargainOrder.BId
                });
            }
            DeliveryProduct formatProduct = new DeliveryProduct
            {
                Count      = 1,
                Name       = bargainOrder.BName,
                TemplateId = config.Attr.DeliveryTemplateId,
                Weight     = config.Attr.Weight,
                Amount     = bargainOrder.CurrentPrice,
            };

            DeliveryFeeResult result = GetDeliveryFeeCommon(new List <DeliveryProduct> {
                formatProduct
            }, province, city, sumMethod);

            if (result.InRange)
            {
                DeliveryDiscount discount = GetDiscount(bargainOrder.aid, bargainOrder.CurrentPrice);
                result.Fee     = discount.HasDiscount ? discount.DiscountPrice : result.Fee;
                result.Message = discount.HasDiscount ? discount.DiscountInfo : result.Message;
            }
            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 获取商品运费(通用业务)
        /// </summary>
        /// <param name="productInfo">产品信息</param>
        /// <param name="provinces">配送省份</param>
        /// <param name="city">配送市区</param>
        /// <param name="sumMethod">运费结算规则</param>
        /// <returns></returns>
        public DeliveryFeeResult GetDeliveryFeeCommon(List <DeliveryProduct> productInfo, string provinces, string city, DeliveryFeeSumMethond sumMethod)
        {
            DeliveryFeeResult result = new DeliveryFeeResult();

            if (string.IsNullOrWhiteSpace(provinces) || string.IsNullOrWhiteSpace(city))
            {
                result.Message = "无效配送地址";
                return(result);
            }

            //获取商品
            if (productInfo == null || productInfo.Count == 0)
            {
                result.Message = "无效产品信息";
                return(result);
            }

            //获取运费模板ID
            string templateIds = string.Join(",", productInfo.Select(product => product.TemplateId).Where(templateId => templateId > 0));

            if (string.IsNullOrWhiteSpace(templateIds))
            {
                //没有选择运费模板,默认零元
                result.InRange = true;
                result.Message = "没有产品使用运费模板,返回零元";
                return(result);
            }

            List <DeliveryTemplate> productTemplates = GetTemplateByIds(templateIds);

            if (productTemplates == null || productTemplates.Count == 0)
            {
                //没运费模板数据,已删除,默认零元
                result.InRange = true;
                result.Message = "查无运费模板,或许已删除,默认返回零元";
                return(result);
            }

            DeliveryFeeSumBLL sumFeeMethod = new DeliveryFeeSumBLL(sumMethod);

            foreach (DeliveryTemplate template in productTemplates)
            {
                List <DeliveryProduct> products = productInfo.FindAll(item => item.TemplateId == template.Id);
                if (template.IsFree == 1)
                {
                    //全国包邮
                    result.Fee      = 0;
                    result.InRange  = true;
                    result.Message += $"[{template.Name}]模板,设置为全国包邮,[{string.Join(",",products.Select(item => item.Name))}]商品运费为零;";
                    continue;
                }

                DeliveryTemplate region = MatchDeliveryRegion(template, provinces, city);
                if (region == null)
                {
                    //检查超出配送区域
                    result.ErrorId = products.First().Id;
                    result.Message = $"[{products.First().Name}]不在配送区域内";
                    return(result);
                }
                if (region.IsFree == 1)
                {
                    //部分区域设置包邮
                    result.Fee      = 0;
                    result.InRange  = true;
                    result.Message += $"[{string.Join(",", products.Select(item => item.Name))}]配送到[{provinces}][{city}]为包邮地区;";
                    continue;
                }

                //购买单位
                int    unit     = 0;
                int    padUnit  = 0;
                string unitName = string.Empty;
                switch (template.UnitType)
                {
                case (int)DeliveryUnit.件数:
                    unitName = "件";
                    unit     = products.Sum(item => item.Count);
                    break;

                case (int)DeliveryUnit.重量:
                    unitName = "g";
                    //购买重量 = 数量 x 单件重量
                    unit = products.Sum(item => item.Count * item.Weight);
                    //补余,如:购买300g,不足1kg,追加700kg
                    int extraUnit = unit - region.Base;
                    if (extraUnit > 0 && region.Extra > 0 && region.ExtraCost > 0)
                    {
                        padUnit = (int)Math.Ceiling((double)extraUnit / region.Extra) * region.Extra - extraUnit;
                    }
                    break;

                default:
                    continue;
                }

                //新建运费
                DeliveryFeeSum sum = new DeliveryFeeSum
                {
                    Base      = region.Base,
                    BaseCost  = region.BaseCost,
                    Extra     = region.Extra,
                    ExtraCost = region.ExtraCost,
                    BuyUnit   = unit,
                    PadUnit   = padUnit,
                    UnitType  = template.UnitType
                };

                //叠加运费
                sumFeeMethod.AddTotal(sum);
                result.Message += string.Join(";", products.Select(item =>
                {
                    int buyUnit = template.UnitType == (int)DeliveryUnit.件数 ? item.Count : item.Count * item.Weight;
                    return(string.Join("", new string[]
                    {
                        $"[{item.Name}](购买{buyUnit}{unitName}):",
                        $"{sum.Base}{unitName} 内 {sum.BaseCost * 0.01}元,",
                        $"运费增加{sum.ExtraCost * 0.01}元;",
                    }));
                }));
            }

            int totalFee = 0;

            result.InRange = true;
            try
            {
                totalFee = sumFeeMethod.GetTotal();
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.InRange = false;
            }
            result.Fee = totalFee;
            return(result);
        }