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) {