优化产品名称读取

This commit is contained in:
Edward Yang 2022-08-15 13:00:14 +08:00
parent 1b8e643880
commit 4027bb3631
1 changed files with 42 additions and 4 deletions

View File

@ -20,6 +20,9 @@ use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
class ProductRepo
{
private static $allProductsWithName;
/**
* 获取产品详情
*/
@ -122,7 +125,7 @@ class ProductRepo
}
public static function list($data=[])
public static function list($data = [])
{
return self::getBuilder($data)->paginate($data['per_page'] ?? 20);
}
@ -146,14 +149,14 @@ class ProductRepo
}
/**
* 获取商品名称
* 获取商品ID获取单个商品名称
*
* @param $id
* @return HigherOrderBuilderProxy|mixed|string
*/
public static function getName($id)
public static function getNameById($id)
{
$product = Product::query()->find($id);
if ($product) {
return $product->description->name;
}
@ -161,6 +164,41 @@ class ProductRepo
}
/**
* 通过产品ID获取商品名称
* @param $id
* @return mixed|string
*/
public static function getName($id)
{
$categories = self::getAllProductsWithName();
return $categories[$id]['name'] ?? '';
}
/**
* 获取所有商品ID和名称列表
*
* @return array|null
*/
public static function getAllProductsWithName(): ?array
{
if (self::$allProductsWithName !== null) {
return self::$allProductsWithName;
}
$items = [];
$products = self::getBuilder()->select('id')->get();
foreach ($products as $product) {
$items[$product->id] = [
'id' => $product->id,
'name' => $product->description->name ?? '',
];
}
return self::$allProductsWithName = $items;
}
/**
* @param $productIds
* @return array