diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index 3372e099..7ec6e83e 100644 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -2606,7 +2606,8 @@ class StoreOrderRepository extends BaseRepository ->field('a.value_id,a.product_id,a.mer_id,a.sku,a.stock,a.image,a.price, a.svip_price, a.unique,p.store_name,p.store_info') - ->limit(50)->select(); + ->order('a.price', 'asc') + ->limit(20)->select()->toArray(); return $data; } @@ -2623,50 +2624,27 @@ class StoreOrderRepository extends BaseRepository public function dealGoodsList($where,$money,$merId) { //拆分2种方式 大于该金额的 50 +小于该金额的50条 - $greaterWhere = function ($query) use ($money) { - $query->where('a.price', '>', $money); - }; - $maxData = $this->dealDataDiv($where,$greaterWhere); + $query = ProductAttrValue::alias('a') + ->leftJoin('store_product p', 'a.product_id = p.product_id') + ->where($where) + ->field('a.value_id,a.product_id,a.mer_id,a.sku,a.stock,a.image,a.price, + a.svip_price, + a.unique,p.store_name,p.store_info'); + $query2 = clone $query; + $list1 = $query->where('a.price', '>', $money)->order('a.price', 'asc')->limit(5)->select()->toArray(); + $list2 = $query2->where('a.price', '<=', $money)->order('a.price', 'desc')->limit(5)->select()->toArray(); + array_multisort(array_column($list2, 'price'), SORT_ASC, $list2); - $lessWhere = function ($query) use ($money) { - $query->where('a.price', '<=', $money); - }; - $mixData = $this->dealDataDiv($where,$lessWhere); - - $list = array_merge($maxData->toArray(), $mixData->toArray()); + $list = array_merge($list2, $list1); $count = count($list); - $minMoney = bcsub($money, 600, 2); $merInfo = Merchant::getDB()->where('mer_id',$merId)->value('mer_name'); if ($count) { - $range = $this->getRangeNumber($minMoney); //减少一般的区间 - //不存在--虚假的区间 - if ($range == -1) { - //拿实际的区间去处理 - $range = $this->getRangeNumber($money);//区间实际 - $minMoney = bcdiv($money, 2, 2); - if ($range == -1) { - return app('json')->fail('不在区间情况中'); - } - } + $range = $this->getRangeNumber($money); //减少一般的区间 $deal = $this->dealArr($list); //平均2个左右 0 1 2 阶梯往上查寻找 - $end = array($this->findNearestPriceProduct($deal[$range]['items'], $minMoney));//17jiji - if ($range == 0 && $deal[$range]['quantity'] = 1) { //第一个并且只有一条数据 - if ($money < $end[0]['price']) { - $list = $end; - } else { - $end[0]['num'] =2; - $list = $end;//满足2条数据 - } - } else { - //在1 或者 2区间 - $newArray = []; - $list = $this->findNearestPriceProductDg($deal, $minMoney, $range, $newArray, $money, $money); - - } - -// $count = array_sum(array_column($list, 'num')); //重复拿 + [$products, $minNum] = $this->getRange($deal, $range); + $list = $this->findNearestPriceProduct($products, $money, $minNum); $count = count($list);//计算数量的 return compact('count', 'list','merInfo'); @@ -2675,7 +2653,14 @@ class StoreOrderRepository extends BaseRepository } } - + public function getRange($array, $range) + { + if (!empty($array[$range]['items'])) { + return [$array[$range]['items'], $array[$range]['minNum']]; + } else { + return $this->getRange($array, $range - 1); + } + } //合并数组相同value_id public function mergeSameValueIds($array) { @@ -2735,33 +2720,34 @@ class StoreOrderRepository extends BaseRepository } return [ - ['range' => '0-100元', 'items' => $range1, 'quantity' => $countRange1], - ['range' => '100-500元', 'items' => $range2, 'quantity' => $countRange2], - ['range' => '500元以上', 'items' => $range3, 'quantity' => $countRange3], + ['range' => '0-100元', 'items' => $range1, 'quantity' => $countRange1, 'minNum' => 1], + ['range' => '100-500元', 'items' => $range2, 'quantity' => $countRange2, 'minNum' => 2], + ['range' => '500元以上', 'items' => $range3, 'quantity' => $countRange3, 'minNum' => 3], ]; } - public function findNearestPriceProduct($array, $targetPrice) + public function findNearestPriceProduct($products, $targetPrice, $minNum = 1) { - $minDiff = PHP_INT_MAX; - $nearestProduct = null; - - foreach ($array as $product) { - $price = floatval($product['price']); - $diff = abs($targetPrice - $price); - - if ($diff < $minDiff) { - $minDiff = $diff; - $nearestProduct = $product; + $nearestProduct = []; + $currentNum = 0; + $minPrice = current($products)['price'] ?? 0; + $minNum = ceil($targetPrice / $minPrice) <= $minNum ? ceil($targetPrice / $minPrice) : $minNum; + foreach ($products as $k => $product) { + if ($currentNum >= $minNum) { + break; } + if ($minNum == $k + 1) { + $product['num'] = (int)ceil($targetPrice / $product['price']); + } else { + $product['num'] = 1; + } + $targetPrice -= $product['price']; + $nearestProduct[] = $product; + $currentNum++; } - if($nearestProduct){ - $nearestProduct['num']=1; - } - return $nearestProduct; }