From 81a3bbeaece7b0f90975131127a612ce367b4ade Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Mon, 13 Dec 2021 13:20:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=BF=90=E8=B4=B9=E8=AE=A1?= =?UTF-8?q?=E7=AE=97sql=E6=9F=A5=E8=AF=A2=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/ShippingService.php | 47 +++++++++++++++++--------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/app/Services/ShippingService.php b/app/Services/ShippingService.php index ce93280b..3da9396c 100644 --- a/app/Services/ShippingService.php +++ b/app/Services/ShippingService.php @@ -17,38 +17,41 @@ class ShippingService public function countShippingAmount(int $weight, int $totalAmount, int $templateId, int $zoneId) { $shipping_amount = 0; - //获取运费规则; - $rules = ShippingRule::with('zones')->where('template_id', $templateId)->get(); $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');//优先计算包邮 foreach ($rules as $rule) { $canShipping = false; $_ruleInfo = json_decode($rule->info, true); - if (in_array($zoneId, $rule->zones->pluck('id')->toArray())) { - $canShipping = true; - if ($rule->type == ShippingRule::TYPE_FREE) { - if ($totalAmount >= bcmul($_ruleInfo['threshold'], 100)) {//在包邮价格范围内 - break;//跳出foreach循环, 直接包邮 - } - } elseif ($rule->type == ShippingRule::TYPE_WEIGHT) { - $_firstWeight = (int) bcmul($_ruleInfo['first_weight'], 1000); - $_firstWamount = (int) bcmul($_ruleInfo['first_w_amount'], 100); - $_contiueWeight = (int) bcmul($_ruleInfo['continue_weight'], 1000); - $_contiueWamount = (int) bcmul($_ruleInfo['continue_w_amount'], 100); - if ($weight <= $_firstWeight) { - $shipping_amount = $_firstWamount; - break; - } else {//计算续重价格 - $num = (int) bcdiv(($weight - $_firstWeight), $_contiueWeight); - $shipping_amount = (int) bcadd($_firstWamount, bcmul($num+1, $_contiueWamount)); - break; - } + // if (in_array($zoneId, $rule->zones->pluck('id')->toArray())) { + $canShipping = true; + if ($rule->type == ShippingRule::TYPE_FREE) { + if ($totalAmount >= bcmul($_ruleInfo['threshold'], 100)) {//在包邮价格范围内 + break;//跳出foreach循环, 直接包邮 + } + } elseif ($rule->type == ShippingRule::TYPE_WEIGHT) { + $_firstWeight = (int) bcmul($_ruleInfo['first_weight'], 1000); + $_firstWamount = (int) bcmul($_ruleInfo['first_w_amount'], 100); + $_contiueWeight = (int) bcmul($_ruleInfo['continue_weight'], 1000); + $_contiueWamount = (int) bcmul($_ruleInfo['continue_w_amount'], 100); + if ($weight <= $_firstWeight) { + $shipping_amount = $_firstWamount; + break; + } else {//计算续重价格 + $num = (int) bcdiv(($weight - $_firstWeight), $_contiueWeight); + $shipping_amount = (int) bcadd($_firstWamount, bcmul($num+1, $_contiueWamount)); + break; } } + // } } } if (!$canShipping) {