后台商品管理
This commit is contained in:
parent
16be2c27eb
commit
c4d73d4185
|
|
@ -8,6 +8,7 @@ use Beike\Models\Product;
|
||||||
use Beike\Models\ProductDescription;
|
use Beike\Models\ProductDescription;
|
||||||
use Beike\Models\ProductSku;
|
use Beike\Models\ProductSku;
|
||||||
use Beike\Admin\Services\ProductService;
|
use Beike\Admin\Services\ProductService;
|
||||||
|
use Beike\Repositories\ProductRepo;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class ProductController extends Controller
|
class ProductController extends Controller
|
||||||
|
|
@ -17,38 +18,7 @@ class ProductController extends Controller
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
if ($request->expectsJson()) {
|
if ($request->expectsJson()) {
|
||||||
$query = Product::query()
|
$products = ProductRepo::list($request->all());
|
||||||
->select('products.*')
|
|
||||||
->with('description', 'skus');
|
|
||||||
|
|
||||||
if ($request->sku) {
|
|
||||||
$query->whereHas('skus', function ($q) {
|
|
||||||
$q->where('sku', 'like', '%'.request('sku').'%');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 关键字搜索:名称
|
|
||||||
if ($request->keyword) {
|
|
||||||
$query->whereHas('description', function ($q) {
|
|
||||||
$q->where('name', 'like', '%'.request('keyword').'%');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
$query->when($request->active !== null, function ($q) {
|
|
||||||
$q->where('active', (int)request('active'));
|
|
||||||
});
|
|
||||||
|
|
||||||
// 回收站
|
|
||||||
if ($request->trashed) {
|
|
||||||
$query->onlyTrashed();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 排序
|
|
||||||
$orderBy = $request->order_by ?? 'products.id:desc';
|
|
||||||
$orderBy = explode(':', $orderBy);
|
|
||||||
$query->orderBy($orderBy[0], $orderBy[1] ?? 'desc');
|
|
||||||
|
|
||||||
$products = $query->paginate($request->per_page ?? 10);
|
|
||||||
|
|
||||||
return ProductResource::collection($products);
|
return ProductResource::collection($products);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,50 +32,63 @@ class ProductRepo
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过多个产品分类获取产品列表
|
* 通过单个或多个产品分类获取产品列表
|
||||||
*
|
|
||||||
* @param $categoryIds
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public static function getProductsByCategories($categoryIds)
|
|
||||||
{
|
|
||||||
$products = self::getProductsByCategory($categoryIds);
|
|
||||||
$items = $products->groupBy('category_id');
|
|
||||||
return $items;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 通过单个产品分类获取产品列表
|
|
||||||
*
|
*
|
||||||
* @param $categoryId
|
* @param $categoryId
|
||||||
* @return AnonymousResourceCollection
|
* @return AnonymousResourceCollection
|
||||||
*/
|
*/
|
||||||
public static function getProductsByCategory($categoryId): AnonymousResourceCollection
|
public static function getProductsByCategory($categoryId): AnonymousResourceCollection
|
||||||
{
|
{
|
||||||
$builder = self::getProductsBuilder($categoryId);
|
$builder = self::getBuilder(['category_id' => $categoryId]);
|
||||||
$products = $builder->get();
|
$products = $builder->get();
|
||||||
$items = ProductList::collection($products);
|
$items = ProductList::collection($products);
|
||||||
return $items;
|
return $items;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
public static function getBuilder($data = []) :Builder
|
||||||
* 获取 Builder
|
|
||||||
* @param $categoryId
|
|
||||||
* @return Builder
|
|
||||||
*/
|
|
||||||
public static function getProductsBuilder($categoryId): Builder
|
|
||||||
{
|
{
|
||||||
if (is_int($categoryId)) {
|
$builder = Product::query()->with('description', 'skus', 'master_sku');
|
||||||
$categoryId[] = $categoryId;
|
|
||||||
|
if (isset($data['category_id'])) {
|
||||||
|
$builder->whereHas('categories', function ($query) use ($data) {
|
||||||
|
if (is_array($data['category_id'])) {
|
||||||
|
$query->whereIn('category_id', $data['category_id']);
|
||||||
|
} else {
|
||||||
|
$query->where('category_id', $data['category_id']);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
$builder = Product::query()
|
if (isset($data['sku']) || isset($data['model'])) {
|
||||||
->select(['products.*', 'pc.category_id'])
|
$builder->whereHas('skus', function ($query) use ($data) {
|
||||||
->with(['description', 'skus', 'master_sku'])
|
if (isset($data['sku'])) {
|
||||||
->join('product_categories as pc', 'products.id', '=', 'pc.product_id')
|
$query->where('sku', 'like', "%{$data['sku']}%");
|
||||||
->join('categories as c', 'pc.category_id', '=', 'c.id')
|
}
|
||||||
->whereIn('c.id', $categoryId);
|
if (isset($data['model'])) {
|
||||||
|
$query->where('model', 'like', "%{$data['model']}%");
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (isset($data['model'])) {
|
||||||
|
$builder->whereHas('skus', function ($query) use ($data) {
|
||||||
|
$query->where('sku', 'like', "%{$data['sku']}%");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (isset($data['name'])) {
|
||||||
|
$builder->whereHas('description', function ($query) use ($data) {
|
||||||
|
$query->where('name', 'like', "%{$data['name']}%");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (isset($data['active'])) {
|
||||||
|
$builder->where('active', (int)$data['active']);
|
||||||
|
}
|
||||||
|
|
||||||
return $builder;
|
return $builder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function list($data)
|
||||||
|
{
|
||||||
|
return self::getBuilder($data)->paginate($data['per_page'] ?? 20);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,11 @@ class CategoryController extends Controller
|
||||||
|
|
||||||
public function show(Request $request, Category $category)
|
public function show(Request $request, Category $category)
|
||||||
{
|
{
|
||||||
$products = ProductRepo::getProductsBuilder($category)->paginate();
|
$products = ProductRepo::getProductsByCategory($category->id);
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
'category' => $category,
|
'category' => $category,
|
||||||
'products' => ProductList::collection($products),
|
'products' => $products,
|
||||||
];
|
];
|
||||||
|
|
||||||
return view('category', $data);
|
return view('category', $data);
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ class HomeController extends Controller
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$data = [
|
$data = [
|
||||||
'category_products' => ProductRepo::getProductsByCategories([100002, 100003, 100004, 100005]),
|
'category_products' => [],
|
||||||
];
|
];
|
||||||
|
|
||||||
$html = '';
|
$html = '';
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue