khanegi admin changes
This commit is contained in:
@@ -8,13 +8,19 @@ use Illuminate\Support\Collection;
|
||||
|
||||
class CategoryService
|
||||
{
|
||||
/** دستههای پیشفرض — باید با سلکت افزودن محصول و تب فیلتر اپ یکسان باشند */
|
||||
/**
|
||||
* دستههای پایه — ترتیب نمایش در ادمین و سلکت محصول.
|
||||
* دستههای اضافی فقط از group_type محصولات اضافه میشوند.
|
||||
*/
|
||||
public const DEFAULT_CATEGORY_NAMES = [
|
||||
'آبشده',
|
||||
'مکعب',
|
||||
'گوی',
|
||||
'سکه',
|
||||
'سکه پارسیان',
|
||||
'شمش',
|
||||
'شمش ونوس',
|
||||
'شمش برنا',
|
||||
'مس',
|
||||
'نقره',
|
||||
];
|
||||
@@ -63,7 +69,8 @@ class CategoryService
|
||||
->values();
|
||||
}
|
||||
|
||||
public function syncFromProducts(): int
|
||||
/** @return Collection<int, string> */
|
||||
public function collectTargetCategoryNames(): Collection
|
||||
{
|
||||
$fromProducts = Product::query()
|
||||
->whereNotNull('group_type')
|
||||
@@ -71,22 +78,39 @@ class CategoryService
|
||||
->distinct()
|
||||
->pluck('group_type');
|
||||
|
||||
$names = collect(self::DEFAULT_CATEGORY_NAMES)
|
||||
->merge($fromProducts)
|
||||
$defaults = collect(self::DEFAULT_CATEGORY_NAMES);
|
||||
$extras = $fromProducts
|
||||
->map(static fn ($name) => trim((string) $name))
|
||||
->filter()
|
||||
->unique()
|
||||
->diff($defaults)
|
||||
->sort()
|
||||
->values();
|
||||
|
||||
return $defaults
|
||||
->merge($extras)
|
||||
->unique()
|
||||
->values();
|
||||
}
|
||||
|
||||
public function syncFromProducts(): int
|
||||
{
|
||||
$names = $this->collectTargetCategoryNames();
|
||||
$created = 0;
|
||||
$order = (int) Category::max('sort_order');
|
||||
$order = 0;
|
||||
|
||||
foreach ($names as $name) {
|
||||
if (Category::where('name', $name)->exists()) {
|
||||
$order++;
|
||||
$existing = Category::where('name', $name)->first();
|
||||
|
||||
if ($existing) {
|
||||
if ((int) $existing->sort_order !== $order) {
|
||||
$existing->sort_order = $order;
|
||||
$existing->save();
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$order++;
|
||||
Category::create([
|
||||
'name' => $name,
|
||||
'active' => 1,
|
||||
|
||||
Reference in New Issue
Block a user