107 lines
3.7 KiB
PHP
107 lines
3.7 KiB
PHP
<?php
|
||
|
||
namespace Beike\Models;
|
||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||
|
||
class Logistics extends Base{
|
||
use HasFactory;
|
||
use SoftDeletes;
|
||
|
||
protected $fillable = [
|
||
'name',
|
||
'warehouse_name',
|
||
'country_id',
|
||
'country_ids',
|
||
'type',
|
||
'first_weight',
|
||
'first_weight_fee',
|
||
'continuation_weight_max',
|
||
'add_weight',
|
||
'continuation_weight_fee',
|
||
'throwing_ratio',
|
||
'num_fee',
|
||
'day_min',
|
||
'day_max',
|
||
'position'
|
||
];
|
||
/**
|
||
* Common: 根据国家ID 获取所有关联的物流列表
|
||
* Author: wu-hui
|
||
* Time: 2023/08/22 10:31
|
||
* @param int $countryId
|
||
* @param string[] $field
|
||
* @return array
|
||
*/
|
||
public static function getAll(int $countryId,$field = ['id','name','warehouse_name','type','day_min','day_max']){
|
||
$list = self::select($field)
|
||
->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){
|
||
// 计算体积重 并且取数值大的作为重量计算
|
||
$volumeWeight = $productItem['volume_weight'] / $logisticsItem['throwing_ratio'];
|
||
$sumWeight = $productItem['sum_weight'] > $volumeWeight ? $productItem['sum_weight'] : $volumeWeight;
|
||
// 总重量 - 减首重 = 剩余重量
|
||
$surplusWeight = $sumWeight - $firstWeight;
|
||
// 判断:如果剩余重量小于等于0 则符合条件;否则计算续重区间
|
||
if($surplusWeight <= 0){
|
||
continue;
|
||
}else{
|
||
$eligibleCount = collect($weights)
|
||
->where('min','<=',$sumWeight)//$surplusWeight
|
||
->where('max','>',$sumWeight)//$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');
|
||
}
|
||
|
||
|
||
}
|