修改扫码商品获取

This commit is contained in:
luofei 2024-03-20 15:29:48 +08:00
parent 160eb5fbc2
commit 373cd3a5dc

View File

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