Fix LESS import error and refactor project structure
This commit is contained in:
@@ -4,17 +4,11 @@ namespace App\Models;
|
||||
|
||||
use App\Core\Model;
|
||||
|
||||
/**
|
||||
* Product - модель товара
|
||||
*/
|
||||
class Product extends Model
|
||||
{
|
||||
protected string $table = 'products';
|
||||
protected string $primaryKey = 'product_id';
|
||||
|
||||
/**
|
||||
* Получить товар с категорией
|
||||
*/
|
||||
public function findWithCategory(int $id): ?array
|
||||
{
|
||||
$sql = "SELECT p.*, c.name as category_name, c.slug as category_slug
|
||||
@@ -24,9 +18,6 @@ class Product extends Model
|
||||
return $this->queryOne($sql, [$id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить доступные товары
|
||||
*/
|
||||
public function getAvailable(array $filters = [], int $limit = 50): array
|
||||
{
|
||||
$sql = "SELECT p.*, c.name as category_name
|
||||
@@ -35,13 +26,11 @@ class Product extends Model
|
||||
WHERE p.is_available = TRUE";
|
||||
$params = [];
|
||||
|
||||
// Фильтр по категории
|
||||
if (!empty($filters['category_id'])) {
|
||||
$sql .= " AND p.category_id = ?";
|
||||
$params[] = $filters['category_id'];
|
||||
}
|
||||
|
||||
// Фильтр по цене
|
||||
if (!empty($filters['min_price'])) {
|
||||
$sql .= " AND p.price >= ?";
|
||||
$params[] = $filters['min_price'];
|
||||
@@ -51,21 +40,18 @@ class Product extends Model
|
||||
$params[] = $filters['max_price'];
|
||||
}
|
||||
|
||||
// Фильтр по цветам
|
||||
if (!empty($filters['colors']) && is_array($filters['colors'])) {
|
||||
$placeholders = implode(',', array_fill(0, count($filters['colors']), '?'));
|
||||
$sql .= " AND p.color IN ({$placeholders})";
|
||||
$params = array_merge($params, $filters['colors']);
|
||||
}
|
||||
|
||||
// Фильтр по материалам
|
||||
if (!empty($filters['materials']) && is_array($filters['materials'])) {
|
||||
$placeholders = implode(',', array_fill(0, count($filters['materials']), '?'));
|
||||
$sql .= " AND p.material IN ({$placeholders})";
|
||||
$params = array_merge($params, $filters['materials']);
|
||||
}
|
||||
|
||||
// Поиск по названию/описанию
|
||||
if (!empty($filters['search'])) {
|
||||
$sql .= " AND (p.name ILIKE ? OR p.description ILIKE ?)";
|
||||
$search = '%' . $filters['search'] . '%';
|
||||
@@ -79,9 +65,6 @@ class Product extends Model
|
||||
return $this->query($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить все товары (включая недоступные) для админки
|
||||
*/
|
||||
public function getAllForAdmin(bool $showAll = true): array
|
||||
{
|
||||
$sql = "SELECT p.*, c.name as category_name
|
||||
@@ -97,9 +80,6 @@ class Product extends Model
|
||||
return $this->query($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить похожие товары
|
||||
*/
|
||||
public function getSimilar(int $productId, int $categoryId, int $limit = 3): array
|
||||
{
|
||||
$sql = "SELECT * FROM {$this->table}
|
||||
@@ -111,9 +91,6 @@ class Product extends Model
|
||||
return $this->query($sql, [$categoryId, $productId, $limit]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить доступные цвета
|
||||
*/
|
||||
public function getAvailableColors(): array
|
||||
{
|
||||
$sql = "SELECT DISTINCT color FROM {$this->table}
|
||||
@@ -123,9 +100,6 @@ class Product extends Model
|
||||
return array_column($result, 'color');
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить доступные материалы
|
||||
*/
|
||||
public function getAvailableMaterials(): array
|
||||
{
|
||||
$sql = "SELECT DISTINCT material FROM {$this->table}
|
||||
@@ -135,9 +109,6 @@ class Product extends Model
|
||||
return array_column($result, 'material');
|
||||
}
|
||||
|
||||
/**
|
||||
* Создать товар
|
||||
*/
|
||||
public function createProduct(array $data): ?int
|
||||
{
|
||||
$slug = $this->generateSlug($data['name']);
|
||||
@@ -161,9 +132,6 @@ class Product extends Model
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Обновить товар
|
||||
*/
|
||||
public function updateProduct(int $id, array $data): bool
|
||||
{
|
||||
$updateData = [
|
||||
@@ -183,9 +151,6 @@ class Product extends Model
|
||||
return $this->update($id, $updateData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Уменьшить количество товара на складе
|
||||
*/
|
||||
public function decreaseStock(int $productId, int $quantity): bool
|
||||
{
|
||||
$sql = "UPDATE {$this->table}
|
||||
@@ -195,18 +160,12 @@ class Product extends Model
|
||||
return $this->execute($sql, [$quantity, $productId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверить наличие товара
|
||||
*/
|
||||
public function checkStock(int $productId, int $quantity): bool
|
||||
{
|
||||
$product = $this->find($productId);
|
||||
return $product && $product['is_available'] && $product['stock_quantity'] >= $quantity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Генерация slug
|
||||
*/
|
||||
private function generateSlug(string $name): string
|
||||
{
|
||||
$slug = mb_strtolower($name);
|
||||
@@ -227,13 +186,9 @@ class Product extends Model
|
||||
return trim($slug, '-');
|
||||
}
|
||||
|
||||
/**
|
||||
* Генерация артикула
|
||||
*/
|
||||
private function generateSku(string $name): string
|
||||
{
|
||||
$prefix = strtoupper(substr(preg_replace('/[^a-zA-Z0-9]/', '', $name), 0, 6));
|
||||
return 'PROD-' . $prefix . '-' . rand(100, 999);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user