输入金额和商家id获取商品列表

This commit is contained in:
liu 2024-03-15 09:54:21 +08:00
parent 46b4ae139d
commit fce64220b9
3 changed files with 201 additions and 0 deletions

View File

@ -16,6 +16,7 @@ use app\common\dao\system\financial\FinancialRecordDao;
use app\common\model\store\order\StoreGroupOrder; use app\common\model\store\order\StoreGroupOrder;
use app\common\model\store\order\StoreOrder; use app\common\model\store\order\StoreOrder;
use app\common\model\store\order\StoreRefundOrder; use app\common\model\store\order\StoreRefundOrder;
use app\common\model\store\product\ProductAttrValue;
use app\common\model\system\merchant\FinancialRecord; use app\common\model\system\merchant\FinancialRecord;
use app\common\model\system\merchant\Merchant; use app\common\model\system\merchant\Merchant;
use app\common\model\user\User; use app\common\model\user\User;
@ -2553,6 +2554,183 @@ class StoreOrderRepository extends BaseRepository
} }
public function dealGoodsList($where,$money)
{
$data = ProductAttrValue::getDB()->alias('a')
->leftJoin('store_product p', 'a.product_id = p.product_id')
->where($where)
->where('a.stock', '>', 0)
->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');
$list = $data->select();
$count = $data->count();
$minMoney = bcsub($money, 600, 2);
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('不在区间情况中');
}
}
$deal = $this->dealArr($list->toArray());
//平均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 {
$list = array_merge($end, $end);//满足2条数据
}
} else {
//在1 或者 2区间
$newArray = [];
$list = $this->findNearestPriceProductDg($deal, $minMoney, $range, $newArray, $money, $money);
}
$count = count($list);
return compact('count', 'list');
} else {
return compact('count', 'list');
}
}
public function getRangeNumber($price, $ranges = [[0, 100], [100, 500], [500, PHP_INT_MAX]])
{
foreach ($ranges as $index => $range) {
if ($price >= $range[0] && $price <= $range[1]) {
return $index;
}
}
return -1;
}
public function dealArr($originalArray): array
{
$range1 = []; // 0-100元的商品
$range2 = []; // 100-500元的商品
$range3 = []; // 500元以上商品
$countRange1 = 0;
$countRange2 = 0;
$countRange3 = 0;
foreach ($originalArray as $item) {
$price = floatval($item['price']);
if ($price >= 0 && $price <= 100) {
$range1[] = $item;
$countRange1++;
} elseif ($price > 100 && $price <= 500) {
$range2[] = $item;
$countRange2++;
} else {
$range3[] = $item;
$countRange3++;
}
}
return [
['range' => '0-100元', 'items' => $range1, 'quantity' => $countRange1],
['range' => '100-500元', 'items' => $range2, 'quantity' => $countRange2],
['range' => '500元以上', 'items' => $range3, 'quantity' => $countRange3],
];
}
public function findNearestPriceProduct($array, $targetPrice)
{
$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;
}
}
return $nearestProduct;
}
//全部数组 假总金额 区间 新数组 总 拿去减少的
public function findNearestPriceProductDg($array, $targetPrice, $range, &$newArray, $trueMoney, $remarkMoney)
{
$minDiff = PHP_INT_MAX;
$nearestProduct = null;
foreach ($array[$range]['items'] as $product) {
$price = floatval($product['price']);
$diff = abs($targetPrice - $price);
if ($diff < $minDiff) {
$minDiff = $diff;
$nearestProduct = $product;
}
}
$newArray[] = $nearestProduct;
$remarkId = $nearestProduct['value_id'];
$totalPrice = array_sum(array_column($newArray, 'price')); //本次全部的价格之和
if ($totalPrice <= $remarkMoney) {
$next = $remarkMoney - $totalPrice;//剩下的
$tt = $remarkMoney - $totalPrice;
$range = $this->getRangeNumber($next);
$array = $this->dealReduce($array,$remarkId);
$this->findNearestPriceProductDg($array['data'], $targetPrice, $range, $newArray, $tt, $remarkMoney);
}
return $newArray;
}
public function dealReduce($data, $targetValueId): array
{
$found = false;
foreach ($data as &$category) {
foreach ($category['items'] as $itemKey => $item) {
if ($item['value_id'] === $targetValueId) {
unset($category['items'][$itemKey]);
if($category['quantity'] == 0){
break 2;
}
$category['quantity'] -= 1;
$found = true;
break 2;
}
}
}
return [
'status' => $found,
'data' => $data
];
}
} }

View File

@ -303,4 +303,26 @@ class StoreOrder extends BaseController
$res = $orderRepository->show($id, $this->request->uid()); $res = $orderRepository->show($id, $this->request->uid());
return app('json')->success($res); return app('json')->success($res);
} }
public function getOrder()
{
//商户id 金额
$merId = $this->request->param('mer_id');
$money = $this->request->param('money');
if(empty($money) || empty($merId)){
return app('json')->fail('参数缺失');
}
$where = [
"p.is_show" => 1,
"p.status" => 1,
"p.product_type" => 0,
"p.is_gift_bag" => 0
];
$where['a.mer_id'] = $merId;
return app('json')->success($this->repository->dealGoodsList($where,$money));
}
} }

View File

@ -65,6 +65,7 @@ Route::group('api/', function () {
}); });
//订单 //订单
Route::any('order_mix', 'api.store.order.StoreOrder/getOrder');//商户获取商品
Route::group('order', function () { Route::group('order', function () {
Route::post('check', '/checkOrder'); Route::post('check', '/checkOrder');
Route::post('create', '/createOrder'); Route::post('create', '/createOrder');