/// <summary> /// 累计运费模型 /// </summary> /// <param name="totalSum">现有计费</param> /// <param name="feeModel">新增计费</param> /// <param name="sumMethod">计算规则</param> /// <returns></returns> public bool AddTotal(DeliveryFeeSum feeModel) { //寻找的相同运费模板设置 int infoIndex = _totalSum.FindIndex(info => info.Base == feeModel.Base && info.BaseCost == feeModel.BaseCost && info.Extra == feeModel.Extra && info.ExtraCost == feeModel.ExtraCost && info.UnitType == feeModel.UnitType); bool isHasSame = infoIndex > -1; if (isHasSame) { //合并购买单位,不重复计算 _totalSum[infoIndex].BuyUnit += feeModel.BuyUnit; return(true); } //有赞运费计费规则:累计不同模板运费 if (_SumFeeMethod == DeliveryFeeSumMethond.赞) { //累计运费 _totalSum.Add(feeModel); return(true); } //淘宝运费计费规则:只算一个最高的首运费 if (_SumFeeMethod == DeliveryFeeSumMethond.淘宝) { //log4net.LogHelper.WriteInfo(this.GetType(), $"714{JsonConvert.SerializeObject(feeModel)}"); //log4net.LogHelper.WriteInfo(this.GetType(), $"715{JsonConvert.SerializeObject(_totalSum)}"); //累计运费 if (_totalSum.Count == 0) { _totalSum.Add(feeModel); return(true); } //获取最高的首运费 DeliveryFeeSum highestBaseCost = _totalSum.OrderByDescending(total => total.BaseCost).First(); //log4net.LogHelper.WriteInfo(this.GetType(), $"720:{JsonConvert.SerializeObject(highestBaseCost)}"); if (highestBaseCost.BaseCost >= feeModel.BaseCost) { //低于最高首运费,每件以续费价格计算 feeModel.BaseCost = feeModel.ExtraCost; feeModel.Base = 0; } else { //高于最高首运费,覆盖当前最高首运费 _totalSum.ForEach(total => total.BaseCost = total.ExtraCost); } _totalSum.Add(feeModel); } return(false); }
/// <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); }