order = $order; $allGoods = $this->findDifferentExpressTypeGoods();/*获取不同运费计算模式的商品*/ $uniformPostage = $this->countGoodsExpress($allGoods['uniformPostageGoods'], Goods::EXPRESS_TYPE_UNIFORM_POSTAGE);/*计算统一运费模式下所有商品运费*/ $expressTemplate = $this->countGoodsExpress($allGoods['useTemplateGoods'], Goods::EXPRESS_TYPE_EXPRESS_TEMPLATE);/*计算运费模板模式下所有商品的运费*/ $amount = $uniformPostage > $expressTemplate ? $uniformPostage : $expressTemplate;/*比较两种不同运费计算模式下,取金额大的值为最终收取运费*/ return $amount / 100; /*将金额单位[分]转化为[元]*/ } /*--------------------------------------------------------------------------------------*/ /** * @return array * @throws NotFoundHttpException * 获取不同运费计算模式的商品 */ private function findDifferentExpressTypeGoods() { $uniformPostage = $useTemplate = []; $allGoods = OrderGoods::findAll(['order_id' => $this->order->id]); foreach ($allGoods as $orderGoods) { $goods = Goods::findOne(['id' => $orderGoods->goods_id, 'is_sale' => Goods::IS_SALE_YES, 'is_delete' => Goods::IS_DELETE_NO]); if (!$goods) { throw new NotFoundHttpException('商品未找到'); } $goodsArr['object'] = $goods; $goodsArr['count'] = $orderGoods->goods_count; $goodsArr['weight'] = $orderGoods->weight; if ($goods->is_express && $goods->express_type == Goods::EXPRESS_TYPE_UNIFORM_POSTAGE) { $uniformPostage[] = $goodsArr; } if ($goods->is_express && $goods->express_type == Goods::EXPRESS_TYPE_EXPRESS_TEMPLATE) { $useTemplate[] = $goodsArr; } } return ['uniformPostageGoods' => $uniformPostage, 'useTemplateGoods' => $useTemplate]; } /** * @param $allGoods * @param $type * @return float|int * @throws NotFoundHttpException * 根据不同计费模式计算商品运费 */ private function countGoodsExpress($allGoods, $type) { if ($type == Goods::EXPRESS_TYPE_UNIFORM_POSTAGE) { $amount = 0; foreach ($allGoods as $goods) { $amount = $goods['object']->uniform_postage > $amount ? $goods['object']->uniform_postage : $amount; } return $amount; } else { $extraPrice = 0; $areasIds = []; foreach ($allGoods as $goods) { $areasIds[] = $this->getAreaId($goods['object']->express_template); } /*获取首重运费和对应的计费规则id*/ $basic = $this->getBasicPriceAndAreaId($areasIds); foreach ($allGoods as $goods) { /*计算增重运费*/ $extraPrice += $this->countExtraAmount($goods['count'], $goods['weight'], $goods['object']->express_template, $basic['id']); } return ($basic['price'] + $extraPrice); } } /** * @param $areaIds * @return array * @throws NotFoundHttpException * 获取基础运费 */ private function getBasicPriceAndAreaId($areaIds) { $basePrice = $id = 0; foreach ($areaIds as $areasId) { $area = $this->findArea($areasId); if (!$area->basic_price > $basePrice) { continue; } $basePrice = $area->basic_price; $id = $areasId; } return ['price' => $basePrice, 'id' => $id]; } /** * @param $count * @param $weight * @param $templateId * @param $areaId * @return float|int * @throws NotFoundHttpException * 计算增重运费 */ private function countExtraAmount($count, $weight, $templateId, $areaId) { $area = $this->findArea($this->getAreaId($templateId)); $template = $this->findTemplate($templateId); if ($template->calculation_type == ExpressTemplate::CALCULATION_TYPE_WEIGHT) { if ($area->id == $areaId) { $amount = $weight - $area->basic_count > 0 ? $weight - $area->basic_count : 0; } else { $amount = $weight; } } else { if ($area->id == $areaId) { $amount = $count - $area->basic_count > 0 ? $count - $area->basic_count : 0; } else { $amount = $count; } } return $area->extra_price * ceil($amount / $area->extra_count); } /** * @param $templateId * @return int * @throws NotFoundHttpException * 根据运费模板和收货地址获取区域 */ private function getAreaId($templateId) { $areas = ExpressArea::findALl(['express_template' => $templateId]); $city = $this->order->city; foreach ($areas as $area) { $allCity = explode(',', $area->city); if (in_array($city, $allCity)) { return $area->id; } } throw new NotFoundHttpException('超出配送范围(未找到对应地区运费计算规则)'); } /** * @param $areaId * @return ExpressArea|null * @throws NotFoundHttpException * 获取对应区域 */ private function findArea($areaId) { $area = ExpressArea::findOne($areaId); if (!$area) { throw new NotFoundHttpException('模板地区未找到'); } return $area; } /** * @param $templateId * @return ExpressTemplate|null * @throws NotFoundHttpException * 获取运费模板 */ private function findTemplate($templateId) { $template = ExpressTemplate::findOne($templateId); if (!$template) { throw new NotFoundHttpException('运费模板未找到'); } return $template; } }