优化运费计算sql查询量
parent
1ac886306b
commit
81a3bbeaec
|
|
@ -17,38 +17,41 @@ class ShippingService
|
||||||
public function countShippingAmount(int $weight, int $totalAmount, int $templateId, int $zoneId)
|
public function countShippingAmount(int $weight, int $totalAmount, int $templateId, int $zoneId)
|
||||||
{
|
{
|
||||||
$shipping_amount = 0;
|
$shipping_amount = 0;
|
||||||
//获取运费规则;
|
|
||||||
$rules = ShippingRule::with('zones')->where('template_id', $templateId)->get();
|
|
||||||
$canShipping = true;//是否支持寄送
|
$canShipping = true;//是否支持寄送
|
||||||
|
|
||||||
//如果该模板下无运费规则,则直接包邮
|
//如果该模板下无运费规则,则直接包邮
|
||||||
if ($rules->count() > 0) {
|
if (ShippingRule::where('template_id', $templateId)->count() > 0) {
|
||||||
|
//只拿有这个地区配置的模板规则,减少sql查询市区数量
|
||||||
|
$rules = ShippingRule::with('zones')->where('template_id', $templateId)->whereHas('zones', function ($q) use ($zoneId) {
|
||||||
|
return $q->where('zones.id', $zoneId);
|
||||||
|
})->get();
|
||||||
//按包邮/计重
|
//按包邮/计重
|
||||||
$rules = $rules->sortBy('type');//优先计算包邮
|
$rules = $rules->sortBy('type');//优先计算包邮
|
||||||
foreach ($rules as $rule) {
|
foreach ($rules as $rule) {
|
||||||
$canShipping = false;
|
$canShipping = false;
|
||||||
$_ruleInfo = json_decode($rule->info, true);
|
$_ruleInfo = json_decode($rule->info, true);
|
||||||
|
|
||||||
if (in_array($zoneId, $rule->zones->pluck('id')->toArray())) {
|
// if (in_array($zoneId, $rule->zones->pluck('id')->toArray())) {
|
||||||
$canShipping = true;
|
$canShipping = true;
|
||||||
if ($rule->type == ShippingRule::TYPE_FREE) {
|
if ($rule->type == ShippingRule::TYPE_FREE) {
|
||||||
if ($totalAmount >= bcmul($_ruleInfo['threshold'], 100)) {//在包邮价格范围内
|
if ($totalAmount >= bcmul($_ruleInfo['threshold'], 100)) {//在包邮价格范围内
|
||||||
break;//跳出foreach循环, 直接包邮
|
break;//跳出foreach循环, 直接包邮
|
||||||
}
|
}
|
||||||
} elseif ($rule->type == ShippingRule::TYPE_WEIGHT) {
|
} elseif ($rule->type == ShippingRule::TYPE_WEIGHT) {
|
||||||
$_firstWeight = (int) bcmul($_ruleInfo['first_weight'], 1000);
|
$_firstWeight = (int) bcmul($_ruleInfo['first_weight'], 1000);
|
||||||
$_firstWamount = (int) bcmul($_ruleInfo['first_w_amount'], 100);
|
$_firstWamount = (int) bcmul($_ruleInfo['first_w_amount'], 100);
|
||||||
$_contiueWeight = (int) bcmul($_ruleInfo['continue_weight'], 1000);
|
$_contiueWeight = (int) bcmul($_ruleInfo['continue_weight'], 1000);
|
||||||
$_contiueWamount = (int) bcmul($_ruleInfo['continue_w_amount'], 100);
|
$_contiueWamount = (int) bcmul($_ruleInfo['continue_w_amount'], 100);
|
||||||
if ($weight <= $_firstWeight) {
|
if ($weight <= $_firstWeight) {
|
||||||
$shipping_amount = $_firstWamount;
|
$shipping_amount = $_firstWamount;
|
||||||
break;
|
break;
|
||||||
} else {//计算续重价格
|
} else {//计算续重价格
|
||||||
$num = (int) bcdiv(($weight - $_firstWeight), $_contiueWeight);
|
$num = (int) bcdiv(($weight - $_firstWeight), $_contiueWeight);
|
||||||
$shipping_amount = (int) bcadd($_firstWamount, bcmul($num+1, $_contiueWamount));
|
$shipping_amount = (int) bcadd($_firstWamount, bcmul($num+1, $_contiueWamount));
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!$canShipping) {
|
if (!$canShipping) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue