fixed product list

This commit is contained in:
Edward Yang 2022-06-23 14:53:13 +08:00
parent ca1ab01624
commit 4d408c2d50
6 changed files with 105 additions and 37 deletions

View File

@ -2,6 +2,7 @@
namespace Beike\Admin\Repositories;
use Beike\Models\Category;
use Illuminate\Support\Facades\DB;
class CategoryRepo
@ -18,4 +19,21 @@ class CategoryRepo
return $results;
}
/**
* @param $categoryData
*/
public static function create($categoryData)
{
// Category::query()->create($categoryData);
$category = new Category();
$category->parent_id = 12;
}
public static function update(Category $category)
{
$category->update([
]);
}
}

View File

@ -4,6 +4,7 @@ namespace Beike\Shop\Http\Controllers;
use Beike\Models\Category;
use Beike\Shop\Repositories\CategoryRepo;
use Beike\Shop\Repositories\ProductRepo;
use Plugin\Guangda\Seller\Models\Product;
class HomeController extends Controller
@ -12,6 +13,7 @@ class HomeController extends Controller
{
$data = [
'categories' => CategoryRepo::getTwoLevelCategories(),
'category_products' => ProductRepo::getProductsByCategories([100002,100003]),
];
return view('home', $data);

View File

@ -4,7 +4,7 @@ namespace Beike\Shop\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class CategoryItem extends JsonResource
class CategoryList extends JsonResource
{
/**
* Transform the resource into an array.
@ -19,7 +19,7 @@ class CategoryItem extends JsonResource
'name' => $this->description->name ?? '',
];
if ($this->children) {
if ($this->children->count() > 0) {
$item['children'] = self::collection($this->children);
}
return $item;

View File

@ -0,0 +1,26 @@
<?php
/**
* ProductList.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-06-23 11:33:06
* @modified 2022-06-23 11:33:06
*/
namespace Beike\Shop\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductList extends JsonResource
{
public function toArray($request): array
{
return [
'id' => $this->id,
'name' => $this->description->name ?? '',
'category_id' => $this->category_id ?? null,
];
}
}

View File

@ -12,6 +12,7 @@
namespace Beike\Shop\Repositories;
use Beike\Models\Category;
use Beike\Shop\Http\Resources\CategoryList;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
@ -22,30 +23,14 @@ class CategoryRepo
*/
public static function getTwoLevelCategories()
{
$items = [];
$topCategories = Category::query()
->from('categories as c')
->with(['description', 'children.description'])
->where('parent_id', 0)
->get();
foreach ($topCategories as $index => $topCategory) {
$items[$index] = [
'id' => $topCategory->id,
'name' => $topCategory->description->name
];
$children = $topCategory->children;
if ($children->count() > 0) {
foreach ($children as $itemIndex => $item) {
$items[$index]['children'][$itemIndex] = [
'id' => $item->id,
'name' => $item->description->name
];
}
}
}
return $items;
$categoryList = CategoryList::collection($topCategories);
return json_decode($categoryList->toJson(), true);
}
@ -65,23 +50,6 @@ class CategoryRepo
return $builder->get();
}
/**
* @param $categoryData
*/
public static function create($categoryData)
{
// Category::query()->create($categoryData);
$category = new Category();
$category->parent_id = 12;
}
public static function update(Category $category)
{
$category->update([
]);
}
public static function updateViewNumber()
{

View File

@ -0,0 +1,54 @@
<?php
/**
* ProductRepo.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-06-23 11:19:23
* @modified 2022-06-23 11:19:23
*/
namespace Beike\Shop\Repositories;
use Beike\Models\Product;
use Beike\Shop\Http\Resources\ProductList;
use Illuminate\Support\Collection;
class ProductRepo
{
/**
* 通过多个产品分类获取产品列表
*
* @param $categoryIds
* @return Collection
*/
public static function getProductsByCategories($categoryIds): Collection
{
$products = self::getProductsByCategory($categoryIds);
$items = collect($products)->groupBy('category_id');
return $items;
}
/**
* 通过单个产品分类获取产品列表
*
* @param $categoryId
* @return array
*/
public static function getProductsByCategory($categoryId): array
{
if (is_int($categoryId)) {
$categoryId[] = $categoryId;
}
$products = Product::query()
->select(['products.*', 'pc.category_id'])
->join('product_categories as pc', 'products.id', '=', 'pc.product_id')
->join('categories as c', 'pc.category_id', '=', 'c.id')
->whereIn('c.id', $categoryId)
->get();
return ProductList::collection($products)->jsonSerialize();
}
}