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); }
/// <summary> /// 计算运费 /// </summary> /// <param name="store"></param> /// <returns></returns> private int GetFreight(QiyeStore store, List <QiyeGoodsCart> cartList, int getWay, string goodCarIdStr, int aid, WxAddress address, ref string msg) { int friPrice = 0; QiyeStoreSwitchModel config = JsonConvert.DeserializeObject <QiyeStoreSwitchModel>(store.SwitchConfig); int qtySum = cartList.Sum(x => x.Count); switch (getWay) { case (int)miniAppOrderGetWay.到店自取: break; case (int)miniAppOrderGetWay.商家配送: DeliveryFeeResult deliueryResult = DeliveryTemplateBLL.SingleModel.GetQiyeFee(goodCarIdStr, aid, address.provinceName, address.cityName, ref msg); if (msg.Length > 0) { return(0); } friPrice = deliueryResult.Fee; break; } return(friPrice); }
public DeliveryFeeResult GetFreightInfo(PinGoods good, int buyCount, string province, string city) { PinStore store = PinStoreBLL.SingleModel.GetModelByAid_Id(good.aId, good.storeId); if (store == null) { return(new DeliveryFeeResult { InRange = true, Message = "店铺不存在" }); } if (!store.setting.freightSwitch) { return(new DeliveryFeeResult { InRange = true, Message = "未开启运费模板功能,默认零元运费" }); } DeliveryFeeSumMethond sumMethod; if (!Enum.TryParse(store.setting.freightSumRule.ToString(), out sumMethod)) { return(new DeliveryFeeResult { Message = "运费规则设置异常" }); } DeliveryProduct product = new DeliveryProduct { Count = buyCount, Id = good.id, Name = good.name, TemplateId = good.FreightTemplate, Weight = good.GetAttrbute().Weight, }; DeliveryFeeResult result = GetDeliveryFeeCommon(new List <DeliveryProduct> { product }, province, city, sumMethod); if (result.Fee > 0) { result.Message = $"[{sumMethod}]{result.Message}"; } return(result); }
/// <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)); }
/// <summary> /// 获取运费信息 /// </summary> /// <returns></returns> public ActionResult GetFreightFee(string appId = null, string openId = null) { returnObj = new Return_Msg_APP(); int userId = Context.GetRequestInt("userid", 0); int storeId = Context.GetRequestInt("storeid", 0); string province = Context.GetRequest("province", ""); string city = Context.GetRequest("city", ""); string goodCartIds = Context.GetRequest("goodcartids", ""); if (userId <= 0) { returnObj.Msg = "参数错误"; return(Json(returnObj)); } if (string.IsNullOrWhiteSpace(goodCartIds)) { returnObj.Msg = "购物车参数出错"; return(Json(returnObj)); } C_UserInfo usrInfo = C_UserInfoBLL.SingleModel.GetModel(userId); if (usrInfo == null) { returnObj.Msg = "无效用户"; return(Json(returnObj)); } XcxAppAccountRelation model = _xcxAppAccountRelationBLL.GetModelByAppid(usrInfo.appId); if (model == null) { returnObj.Msg = "无效模板"; return(Json(returnObj)); } PlatStore platStore = new PlatStore(); if (storeId > 0) { platStore = PlatStoreBLL.SingleModel.GetModel(storeId); } else { platStore = PlatStoreBLL.SingleModel.GetModelByAId(model.Id); } if (platStore == null) { returnObj.Msg = "没有找到店铺"; return(Json(returnObj)); } string errorMsg = ""; DeliveryFeeResult deliueryResult = DeliveryTemplateBLL.SingleModel.GetPlatFee(goodCartIds, platStore.Aid, province, city, ref errorMsg); if (errorMsg.Length > 0) { returnObj.Msg = errorMsg; return(Json(returnObj)); } returnObj.Msg = "获取成功"; returnObj.isok = true; returnObj.dataObj = new { deliueryResult = deliueryResult, storeaddress = platStore.Location }; return(Json(returnObj)); }
/// <summary> /// 企业智推版运费 /// </summary> /// <param name="goodcartids"></param> /// <param name="aid"></param> /// <param name="province"></param> /// <param name="city"></param> /// <param name="msg"></param> /// <returns></returns> public DeliveryFeeResult GetQiyeFee(string goodcartids, int aid, string province, string city, ref string msg) { DeliveryFeeResult deliueryResult = new DeliveryFeeResult(); //购物车 List <QiyeGoodsCart> cartList = QiyeGoodsCartBLL.SingleModel.GetListByIds(goodcartids); if (cartList == null || cartList.Count <= 0) { msg = "运费:购物车数据为空"; return(deliueryResult); } //商品 string goodsIds = string.Join(",", cartList.Select(s => s.GoodsId)); List <QiyeGoods> goodsList = QiyeGoodsBLL.SingleModel.GetListByIds(goodsIds); if (goodsList == null || goodsList.Count <= 0) { msg = "运费:找不到商品数据"; return(deliueryResult); } QiyeStore store = QiyeStoreBLL.SingleModel.GetModelByAId(aid); if (store == null) { msg = "运费:无效店铺"; return(deliueryResult); } DeliveryFeeSumMethond sumMethod; PlatStoreSwitchModel config = string.IsNullOrWhiteSpace(store.SwitchConfig) ? new PlatStoreSwitchModel() : JsonConvert.DeserializeObject <PlatStoreSwitchModel>(store.SwitchConfig); if (config.enableDeliveryTemplate) { if (!Enum.TryParse(config.deliveryFeeSumMethond.ToString(), out sumMethod)) { msg = "运费:无效运费模板"; return(deliueryResult); } List <DeliveryProduct> productInfo = new List <DeliveryProduct>(); foreach (QiyeGoods goodItem in goodsList) { List <QiyeGoodsCart> tempCartList = cartList.Where(w => w.GoodsId == goodItem.Id).ToList(); if (tempCartList == null || tempCartList.Count <= 0) { msg = "运费:无效购物车数据"; return(deliueryResult); } productInfo.Add(new DeliveryProduct { TemplateId = goodItem.TemplateId, Name = goodItem.Name, Count = tempCartList.Sum(s => s.Count), }); } deliueryResult = GetDeliveryFeeCommon(productInfo, province, city, sumMethod); if (deliueryResult == null) { msg = "运费:无效运费模板"; } else if (!deliueryResult.InRange) { msg = deliueryResult.Message; } } return(deliueryResult); }
/// <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); }