whereRaw(\DB::raw('FIND_IN_SET('.$countryId.',country_ids)')) ->with(['weights']) ->orderBy('position','ASC') ->orderBy('id','ASC') ->get(); return $list ? $list->toArray() : []; } /** * Common: 物流过滤 - 根据商品和物流列表过滤 仅返回有效的物流 * Author: wu-hui * Time: 2023/08/29 10:02 * @param $logisticsList * @param $productsList * @return array|array[] */ public static function LogisticsFiltering($logisticsList,$productsList){ $eligibleList = array_map(function($logisticsItem) use ($productsList){ // 判断:物流是否按照重量计算 weight:按重量计费,num:按数量计费,free:卖家包邮 if($logisticsItem['type'] == 'weight'){ $weights = (array)$logisticsItem['weights'];// 续重区间列表 $firstWeight = (float)$logisticsItem['first_weight'];// 首重 if(!$weights) { $logisticsItem = []; }else{ // 循环判断:只要有一个商品不符合条件 则当前物流不可用; foreach($productsList as $productItem){ $surplusWeight = $productItem['sum_weight'] - $firstWeight;// 总重量 - 减首重 = 剩余重量 // 判断:如果剩余重量小于等于0 则符合条件;否则计算续重区间 if($surplusWeight <= 0){ continue; }else{ $eligibleCount = collect($weights) ->where('min','<=',$surplusWeight) ->where('max','>',$surplusWeight) ->count(); if($eligibleCount <= 0) { $logisticsItem = []; break; } } } } } return $logisticsItem; },$logisticsList); return array_filter($eligibleList); } public function country(){ return $this->belongsTo(Country::class,'country_id','id'); } public function weights(){ return $this->hasMany(LogisticsWeight::class,'logistics_id','id'); } }