Example #1
0
        /// <summary>
        ///     Counts the fee by weight.
        ///     根据重量计算区域运费
        /// </summary>
        /// <param name="regionFee">The delivery fee.</param>
        /// <param name="weight">The weight.</param>
        private decimal CountFeeByWeight(RegionTemplateFee regionFee, decimal weight)
        {
            //基础费用为首重
            var result = regionFee.StartFee;

            //判断是否超过首重
            if (weight > regionFee.StartStandard)
            {
                //续重重量(整数)
                var nextWeight = weight - regionFee.StartStandard;
                //nextWeight = Math.Ceiling(nextWeight); // 向上取整
                //排除续费为0,不需要运费的
                if (regionFee.EndStandard != 0)
                {
                    //加上续重费用,以续重为基础,加上超过其倍数的费用
                    var nextWeightFee = Math.Ceiling(nextWeight / regionFee.EndStandard);
                    result += nextWeightFee * regionFee.EndFee;
                    //如果超过首重的部分不能被续费重量整除,则向上取整,需要再加入单位续重
                    //if (nextWeight % regionFee.EndStandard != 0) {
                    //    result += regionFee.EndFee;
                    //}
                }
            }

            return(result);
        }
Example #2
0
        /// <summary>
        ///     Counts the express fee.
        ///     根据客户选择的物流方式,商品重量,计算快递费用
        /// </summary>
        /// <param name="storeId">The stroe identifier.</param>
        /// <param name="templId">The express identifier.</param>
        /// <param name="userAddress">用户地址</param>
        /// <param name="weight">The weight.</param>
        public Tuple <ServiceResult, decimal> CountExpressFee(ObjectId storeId, ObjectId templId, UserAddress userAddress, decimal weight)
        {
            var expressFee = 0m; // 快递费用
            var express    = Resolve <IDeliveryTemplateService>().GetSingle(t => t.StoreId == storeId && t.Id == templId);

            if (express == null)
            {
                return(Tuple.Create(ServiceResult.FailedWithMessage("运费模板未找到"), expressFee));
            }
            //卖家承担运费的模版,运费为0
            if (express.TemplateType == DeliveryTemplateType.SellerBearTheFreight)
            {
                return(Tuple.Create(ServiceResult.Success, expressFee));
            }
            //先检查区县,如果区县运费找到,则直接返回
            var regionTemplateFee = GetTemplateFeeByRegionId(express, userAddress.RegionId);

            if (regionTemplateFee != null)
            {
                expressFee = CountFeeByWeight(regionTemplateFee, weight);
                return(Tuple.Create(ServiceResult.Success, expressFee));
            }

            // 如果区县未运费找到,再检查城市,如果城市运费找到,则直接返回
            regionTemplateFee = GetTemplateFeeByRegionId(express, userAddress.City);
            if (regionTemplateFee != null)
            {
                expressFee = CountFeeByWeight(regionTemplateFee, weight);
                return(Tuple.Create(ServiceResult.Success, expressFee));
            }

            // 如果城市未运费找到,再检查省份,如果省份运费找到,则直接返回
            regionTemplateFee = GetTemplateFeeByRegionId(express, userAddress.Province);
            if (regionTemplateFee != null)
            {
                expressFee = CountFeeByWeight(regionTemplateFee, weight);
                return(Tuple.Create(ServiceResult.Success, expressFee));
            }

            // 如果省份未运费找到,默认数据配置
            var regionFee = new RegionTemplateFee
            {
                StartFee      = express.StartFee,
                EndFee        = express.EndFee,
                StartStandard = express.StartStandard,
                EndStandard   = express.EndStandard
            };

            expressFee = CountFeeByWeight(regionFee, weight);
            return(Tuple.Create(ServiceResult.Success, expressFee));
        }
Example #3
0
        /// <summary>
        ///     Counts the express fee.
        ///     根据客户选择的物流方式,商品重量,计算快递费用
        /// </summary>
        /// <param name="storeId">The stroe identifier.</param>
        /// <param name="userAddress">用户地址</param>
        /// <param name="productSkuItems">The weight.</param>
        public Tuple <ServiceResult, decimal> CountExpressFee(ObjectId storeId, UserAddress userAddress, IList <ProductSkuItem> productSkuItems)
        {
            //total
            var totalExpressFee = 0m;
            //get all delivery template
            var deliveryTemplates = Resolve <IDeliveryTemplateService>().GetList(d => d.StoreId == storeId).ToList();
            var productGroups     = productSkuItems.GroupBy(p => p.ProductId).ToList();

            productGroups.ForEach(product => {
                var tempDelivery = product.FirstOrDefault();
                var template     = deliveryTemplates.FirstOrDefault(d => d.Id.ToString() == tempDelivery?.DeliveryTemplateId);
                if (template == null || template.TemplateType == DeliveryTemplateType.SellerBearTheFreight)
                {
                    return;
                }

                //delivery freight calculate type
                var value = 0M;
                if (template.CalculateType == DeliveryFreightCalculateType.ByCount)
                {
                    value = product.Sum(p => p.BuyCount);
                }
                if (template.CalculateType == DeliveryFreightCalculateType.ByWeight)
                {
                    value = product.Sum(p => p.IsFreeShipping ? 0 : (p.BuyCount * p.Weight) / 1000);
                }

                //region (现在用户只存了县级id (440105),县级找不到截前四位)
                var tempExpressFee = CountFeeByRegion(template, userAddress.RegionId, value);
                if (tempExpressFee < 0)
                {
                    //city
                    var cityId = userAddress.RegionId.ToString().Substring(0, 4).ToLong();
                    //if (userAddress.City > 0)
                    //{
                    //    cityId = userAddress.City;
                    //}
                    tempExpressFee = CountFeeByRegion(template, cityId, value);

                    if (tempExpressFee < 0)
                    {
                        //province
                        var provinceId = userAddress.RegionId.ToString().Substring(0, 2).ToLong();
                        //if (userAddress.Province > 0)
                        //{
                        //    provinceId = userAddress.Province;
                        //}
                        tempExpressFee = CountFeeByRegion(template, provinceId, value);
                    }
                }
                if (tempExpressFee < 0)
                {
                    //default
                    var regionFee = new RegionTemplateFee {
                        StartFee      = template.StartFee,
                        EndFee        = template.EndFee,
                        StartStandard = template.StartStandard,
                        EndStandard   = template.EndStandard
                    };
                    totalExpressFee += CountFeeByWeight(regionFee, value);
                }
                else
                {
                    totalExpressFee += tempExpressFee;
                }
            });

            return(Tuple.Create(ServiceResult.Success, totalExpressFee));
        }