wyyl/beike/Models/Logistics.php

107 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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');
}
}