66 lines
2.6 KiB
PHP
66 lines
2.6 KiB
PHP
<?php
|
||
|
||
namespace App\Api\Controllers;
|
||
|
||
use App\Api\Resources\GoodsParamsListResource;
|
||
use App\Api\Resources\SellerListResource;
|
||
use App\Models\GoodsParams;
|
||
use App\Models\Seller;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
class IndexController extends Controller
|
||
{
|
||
|
||
public function index(Request $request)
|
||
{
|
||
|
||
$goodsLists = GoodsParams::whereHas('goods', function ($query) {
|
||
return $query->where('status', 1);
|
||
})->where('status', 1)->where('stock', '>', 0)->where('score', '>', 0)->whereRaw('price > score')->select('goods_id', DB::raw('any_value(stock) as stock'), DB::raw('any_value(status) as status'), DB::raw('any_value(price) as price'), DB::raw('any_value(score) as score'), DB::raw('any_value(id) as id'))
|
||
->groupBy('goods_id')
|
||
->orderBy('price', 'asc')
|
||
->paginate(10);
|
||
|
||
|
||
|
||
$page = $request->page ?? 1; //获取当前页码
|
||
|
||
if ($page > 1) {
|
||
//页码大于1,AJAX调用分页
|
||
if ($goodsLists->count() > 0) {
|
||
return [
|
||
'data' => [
|
||
'goodsLists' => GoodsParamsListResource::collection($goodsLists),
|
||
],
|
||
'status' => 'SUCCESS',
|
||
'status_code' => 200,
|
||
];
|
||
} else {
|
||
//无内容提示到最后一页
|
||
return $this->failed('已经到最后一页');
|
||
}
|
||
}else{
|
||
$sellers = Seller::with(['storage'])->orderBy('sort', 'desc')->limit(12)->get();
|
||
$recommendSellerGoods = GoodsParams::whereHas('goods', function ($query) {
|
||
return $query->where('status', 1)->where('seller_id', 1);
|
||
})->where('status', 1)->where('stock', '>', 0)->where('score', '>', 0)->whereRaw('price > score')->select('goods_id', DB::raw('any_value(stock) as stock'), DB::raw('any_value(status) as status'), DB::raw('any_value(price) as price'), DB::raw('any_value(score) as score'), DB::raw('any_value(id) as id'))
|
||
->groupBy('goods_id')
|
||
->orderBy('price', 'asc')
|
||
->limit(8)->get();
|
||
|
||
return [
|
||
'data' => [
|
||
'goodsLists' => GoodsParamsListResource::collection($goodsLists),
|
||
'sellers' => SellerListResource::collection($sellers),
|
||
'recommendSellerGoods' => GoodsParamsListResource::collection($recommendSellerGoods)
|
||
],
|
||
'status' => 'SUCCESS',
|
||
'status_code' => 200,
|
||
];
|
||
}
|
||
|
||
|
||
}
|
||
}
|