6
0
Fork 0

调整运费service

release
vine_liutk 2021-12-13 17:52:29 +08:00
parent af83424a01
commit 2973e703d5
1 changed files with 5 additions and 18 deletions

View File

@ -11,16 +11,19 @@ class ShippingService
* 运费模板根据地址,重量, 商品金额 计算运费; 重量传g金额传分
*
* @param integer $weight
* @param integer $totalAmount
* @param integer $templateId
* @return void
* @param integer $zoneId
* @return integer
*/
public function countShippingAmount(int $weight, int $totalAmount, int $templateId, int $zoneId)
public function countShippingAmount(int $weight, int $totalAmount, int $templateId, int $zoneId): int
{
$shipping_amount = 0;
$canShipping = true;//是否支持寄送
//如果该模板下无运费规则,则直接包邮
if (ShippingRule::where('template_id', $templateId)->count() > 0) {
$canShipping = false;
//只拿有这个地区配置的模板规则减少sql查询市区数量
$rules = ShippingRule::with('zones')->where('template_id', $templateId)->whereHas('zones', function ($q) use ($zoneId) {
return $q->where('zones.id', $zoneId);
@ -28,10 +31,7 @@ class ShippingService
//按包邮/计重
$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)) {//在包邮价格范围内
@ -51,25 +51,12 @@ class ShippingService
break;
}
}
// }
}
}
if (!$canShipping) {
throw new BizException('当前选择的地址有部分商品不支持配送');
}
return $this->returnAmount($shipping_amount);
}
/**
* 统一返回运费格式
*
* @param [type] $shipping_amount
* @return void
*/
protected function returnAmount($shipping_amount)
{
return $shipping_amount;
// return bcdiv($shipping_amount, 100, 2);
}
}