From f59249a6ba96363b7ad1d47020594976f1287761 Mon Sep 17 00:00:00 2001 From: codeliu <1873441552@qq.com> Date: Sat, 23 Mar 2024 10:54:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=84=E7=90=86=E4=BB=B7=E6=A0=BC=E4=B8=8D?= =?UTF-8?q?=E5=9C=A8=E5=8C=BA=E9=97=B4=E8=8C=83=E5=9B=B4=E5=86=85=E4=BD=86?= =?UTF-8?q?=E6=98=AF=E5=8F=88=E6=9C=89=E8=AE=BE=E7=BD=AE=E7=9A=84=E5=95=86?= =?UTF-8?q?=E5=93=81=E5=8C=B9=E9=85=8D=E8=BF=94=E5=9B=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../store/order/StoreOrderRepository.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index 58dcb831..4f869529 100644 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -2690,6 +2690,7 @@ class StoreOrderRepository extends BaseRepository $merInfo = Merchant::getDB()->where('mer_id',$merId)->value('mer_name'); if ($count) { $range = $this->getRangeNumber($money); //减少一般的区间 + $baseList = $list; $deal = $this->dealArr($list); //平均2个左右 0 1 2 阶梯往上查寻找 [$products, $minNum] = $this->getRange($deal, $range); @@ -2697,6 +2698,10 @@ class StoreOrderRepository extends BaseRepository if (!empty($products)) { $list = $this->findNearestPriceProduct($products, $money, $minNum); } + if(empty($list) && $baseList){ + $list = $this->findProductByPrice($money,$baseList); + } + $count = count($list);//计算数量的 } return [ @@ -2705,6 +2710,36 @@ class StoreOrderRepository extends BaseRepository 'merInfo' => $merInfo, ]; } + public function findProductByPrice($inputPrice, $products) { + $totalNum = 0; + $matchedProduct = null; + + foreach ($products as $product) { + if ($product['price'] >= $inputPrice) { + $matchedProduct = $product; + $matchedProduct['num'] = 1; + break; + } else { + $totalNum += 1; + } + } + + if (!$matchedProduct) { // 如果没有找到满足条件的商品 + foreach ($products as &$product) { + + $product['num'] = (int)ceil($inputPrice / $product['price']); + $totalNum += $product['num']; + if ($totalNum >= 1) { + break; + } + } + } + + return $matchedProduct ?? reset($products); // 返回匹配到的商品或数组的第一个商品 + } + + + public function getRange($array, $range) {