isAutoSelectCoupon = 1; } /** * @return array * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ public function handle(): array { $this->siteId = (int)$this->request->input('site_id'); $this->couponId = (int)$this->request->input('coupon_id',0); $this->orderType = (int)$this->request->input('order_type'); $this->check(); $this->getCouponInfo(); $this->compute(); $this->autoSelectCoupon(); $this->reloadCompute(); return $this->return->success('success',$this->orderRes); } /** * @return void */ private function autoSelectCoupon(): void { if ($this->couponId != 0) return; $noUseCoupon = $this->couponTemplateModel->getNoUseCouponByUserId($this->userId); if ($noUseCoupon->isEmpty()) return; $noUseCoupon = $noUseCoupon->toArray(); $orderMaxPrice = max(array_column($this->orderRes['good'],'price')); $maxValidityEndTimeDate = max(array_column($noUseCoupon,'validity_end_time')); $filtered = array_filter($noUseCoupon, function($item) use($maxValidityEndTimeDate) { return $item['validity_end_time'] == $maxValidityEndTimeDate; }); $selectMaxPrice = 0; $selectCouponInfo = []; foreach ($filtered as &$value) { $value['amount'] = match ($value['coupon_type']) { CouponCode::COUPON_TYPE_INSTANT_REDUCTION => $value['amount'], CouponCode::COUPON_TYPE_DISCOUNT => bcmul(bcsub("1", (string)$value['ratio'],2),(string)$orderMaxPrice,2), default => 0 }; if ($value['amount'] < $selectMaxPrice) continue; $selectMaxPrice = $value['amount']; $selectCouponInfo = $value; } $this->couponId = $selectCouponInfo['id']; $this->orderRes['coupon'] = $selectCouponInfo; $this->orderRes['coupon_id'] = $selectCouponInfo['id']; } }