处理价格不在区间范围内但是又有设置的商品匹配返回

This commit is contained in:
codeliu 2024-03-23 10:54:53 +08:00
parent aac0774b9c
commit f59249a6ba

View File

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