added wss tools 5
This commit is contained in:
@@ -5,6 +5,7 @@ namespace App\Services;
|
||||
use App\Models\Category;
|
||||
use App\Models\Product;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CategoryService
|
||||
{
|
||||
@@ -145,11 +146,56 @@ class CategoryService
|
||||
return collect();
|
||||
}
|
||||
|
||||
return Category::query()
|
||||
if (! Schema::hasTable('categories')) {
|
||||
return $this->fallbackClientCategories($groupsWithProducts);
|
||||
}
|
||||
|
||||
if (! Schema::hasColumn('categories', 'sort_order')) {
|
||||
return $this->fallbackClientCategories($groupsWithProducts);
|
||||
}
|
||||
|
||||
$categories = Category::query()
|
||||
->where('active', 1)
|
||||
->whereIn('name', $groupsWithProducts)
|
||||
->orderBy('sort_order')
|
||||
->orderBy('id')
|
||||
->get(['id', 'name', 'sort_order']);
|
||||
|
||||
if ($categories->isEmpty()) {
|
||||
$this->syncFromProducts();
|
||||
|
||||
return Category::query()
|
||||
->where('active', 1)
|
||||
->whereIn('name', $groupsWithProducts)
|
||||
->orderBy('sort_order')
|
||||
->orderBy('id')
|
||||
->get(['id', 'name', 'sort_order']);
|
||||
}
|
||||
|
||||
return $categories;
|
||||
}
|
||||
|
||||
/** وقتی جدول categories هنوز migrate نشده — از group_type محصولات */
|
||||
private function fallbackClientCategories(Collection $groupsWithProducts): Collection
|
||||
{
|
||||
$ordered = collect(self::DEFAULT_CATEGORY_NAMES)
|
||||
->merge($groupsWithProducts)
|
||||
->unique()
|
||||
->values();
|
||||
|
||||
$order = 0;
|
||||
|
||||
return $ordered
|
||||
->filter(static fn (string $name) => $groupsWithProducts->contains($name))
|
||||
->values()
|
||||
->map(static function (string $name) use (&$order) {
|
||||
$order++;
|
||||
|
||||
return (object) [
|
||||
'id' => $order,
|
||||
'name' => $name,
|
||||
'sort_order' => $order,
|
||||
];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user