> */ public function popularPreview(int $limit = 10): array { $limit = max(1, min($limit, 30)); $sql = $this->previewSelectSql( 'ORDER BY p.sales_count DESC, RAND()', $limit ); $statement = $this->pdo()->query($sql); return $this->preparePreviewProducts($statement->fetchAll()); } /** * @return array> */ public function catalogPreview(int $limit = 24, int $offset = 0, bool $includeHidden = false): array { $limit = max(1, min($limit, 5000)); $offset = max(0, $offset); $sql = $this->previewSelectSql( 'ORDER BY (p.is_published = 0 OR p.is_available = 0), p.sort_order, p.name', $limit, $offset, $includeHidden ); $statement = $this->pdo()->query($sql); return $this->preparePreviewProducts($statement->fetchAll()); } public function catalogTotal(bool $includeHidden = false): int { $statusSql = $includeHidden ? '' : ' AND p.is_published = 1 AND p.is_available = 1'; $statement = $this->pdo()->query('SELECT COUNT(*) FROM products p WHERE 1=1' . $statusSql); return (int) $statement->fetchColumn(); } /** * @return array{products: array>, total: int} */ public function searchCatalog(string $query, int $limit = 48, int $offset = 0, bool $includeHidden = false): array { $limit = max(1, min($limit, 5000)); $offset = max(0, $offset); $products = $this->catalogPreview(5000, 0, $includeHidden); $rankedProducts = (new SmartSearchService())->rankProducts($query, $products); return [ 'products' => array_slice($rankedProducts, $offset, $limit), 'total' => count($rankedProducts), ]; } /** * @return array> */ public function forCategory(int $categoryId, int $limit = 48, int $offset = 0, bool $includeHidden = false): array { $limit = max(1, min($limit, 5000)); $offset = max(0, $offset); $categoryIds = (new CategoryRepository())->descendantIds($categoryId); $categoryParents = $this->categoryParentMap(); $requestedRootId = $this->rootCategoryId($categoryId, $categoryParents); $isRootCategory = ($categoryParents[$categoryId] ?? null) === null; $placeholders = implode(',', array_fill(0, count($categoryIds), '?')); $sql = $this->previewSelectSql( 'AND ( EXISTS ( SELECT 1 FROM product_category_links pcl WHERE pcl.product_id = p.id AND pcl.category_id IN (' . $placeholders . ') ) OR ( p.category_id IN (' . $placeholders . ') AND (p.legacy_path IS NULL OR p.legacy_path NOT LIKE \'/katalog/product/view/1/%\') AND NOT EXISTS ( SELECT 1 FROM product_category_links pcl_any WHERE pcl_any.product_id = p.id ) ) ) ORDER BY (p.is_published = 0 OR p.is_available = 0), p.sort_order, p.name', 5000, 0, $includeHidden ); $statement = $this->pdo()->prepare($sql); $statement->execute(array_merge($categoryIds, $categoryIds)); $categoryLookup = array_flip($categoryIds); $products = $this->preparePreviewProducts($statement->fetchAll()); $filtered = []; foreach ($products as $product) { $leafCategoryIds = $product['category_display_ids'] ?? []; $leafRootIds = $product['category_display_root_ids'] ?? []; if ($leafCategoryIds === []) { if (isset($categoryLookup[(int) ($product['category_id'] ?? 0)])) { $filtered[] = $product; } } elseif ( array_intersect($leafCategoryIds, $categoryIds) !== [] && (!$isRootCategory || array_values(array_unique($leafRootIds)) === [$requestedRootId]) ) { $filtered[] = $product; } } usort($filtered, static function (array $left, array $right): int { $leftUnavailable = ((int) ($left['is_published'] ?? 1) === 0 || (int) ($left['is_available'] ?? 1) === 0) ? 1 : 0; $rightUnavailable = ((int) ($right['is_published'] ?? 1) === 0 || (int) ($right['is_available'] ?? 1) === 0) ? 1 : 0; return [$leftUnavailable, (int) ($left['sort_order'] ?? 0), (string) ($left['name'] ?? '')] <=> [$rightUnavailable, (int) ($right['sort_order'] ?? 0), (string) ($right['name'] ?? '')]; }); return array_slice($filtered, $offset, $limit); } public function categoryTotal(int $categoryId, bool $includeHidden = false): int { return count($this->forCategory($categoryId, 5000, 0, $includeHidden)); } /** * @return array|null */ public function findBySlug(string $slug): ?array { $statement = $this->pdo()->prepare( 'SELECT p.*, COALESCE(linked_c.id, c.id) AS display_category_id, COALESCE(linked_c.name, c.name) AS category_name, COALESCE(linked_c.slug, c.slug) AS category_slug FROM products p LEFT JOIN categories c ON c.id = p.category_id LEFT JOIN categories linked_c ON linked_c.id = ( SELECT pcl.category_id FROM product_category_links pcl INNER JOIN categories lc ON lc.id = pcl.category_id WHERE pcl.product_id = p.id AND lc.is_active = 1 ORDER BY lc.parent_id IS NULL, pcl.sort_order, pcl.category_id LIMIT 1 ) WHERE p.slug = :slug AND p.is_published = 1 AND p.is_available = 1 LIMIT 1' ); $statement->execute(['slug' => $slug]); $product = $statement->fetch(); return $product ?: null; } /** * @return array> */ public function variantsForProduct(int $productId): array { $statement = $this->pdo()->prepare( 'SELECT id, product_id, name, unit, package_quantity, step_quantity, price_per_unit, package_price, old_package_price, is_default FROM product_variants WHERE product_id = :product_id AND is_published = 1 AND is_available = 1 ORDER BY is_default DESC, sort_order, id' ); $statement->execute(['product_id' => $productId]); return $statement->fetchAll(); } /** * @return array> */ public function imagesForProduct(int $productId): array { $statement = $this->pdo()->prepare( 'SELECT id, path, alt, sort_order FROM product_images WHERE product_id = :product_id ORDER BY sort_order, id' ); $statement->execute(['product_id' => $productId]); return $statement->fetchAll(); } /** * @return array> */ public function adminLatest(int $limit = 30): array { $limit = max(1, min($limit, 100)); $statement = $this->pdo()->query( 'SELECT p.id, p.name, p.slug, p.is_published, c.name AS category_name, p.created_at FROM products p LEFT JOIN categories c ON c.id = p.category_id ORDER BY p.created_at DESC, p.id DESC LIMIT ' . $limit ); return $statement->fetchAll(); } /** * @param array $data */ public function createWithVariant(array $data): int { if (($data['name'] ?? '') === '' || ($data['slug'] ?? '') === '') { throw new \InvalidArgumentException('Название и slug обязательны.'); } $pdo = $this->pdo(); $pdo->beginTransaction(); try { $statement = $pdo->prepare( 'INSERT INTO products (category_id, name, slug, short_description, seo_title, seo_description, seo_keywords, base_unit, is_published, is_available, published_at) VALUES (:category_id, :name, :slug, :short_description, :seo_title, :seo_description, :seo_keywords, :base_unit, :is_published, 1, :published_at)' ); $statement->execute([ 'category_id' => $data['category_id'], 'name' => $data['name'], 'slug' => $data['slug'], 'short_description' => $data['short_description'] ?: null, 'seo_title' => $data['seo_title'] ?: null, 'seo_description' => $data['seo_description'] ?: null, 'seo_keywords' => $data['seo_keywords'] ?: null, 'base_unit' => $data['base_unit'] ?: 'kg', 'is_published' => $data['is_published'] ?? 0, 'published_at' => !empty($data['is_published']) ? date('Y-m-d H:i:s') : null, ]); $productId = (int) $pdo->lastInsertId(); if (($data['variant_name'] ?? '') !== '' && (float) ($data['package_price'] ?? 0) > 0) { $variantStatement = $pdo->prepare( 'INSERT INTO product_variants (product_id, name, unit, package_quantity, step_quantity, price_per_unit, package_price, old_package_price, discount_type, discount_percent, is_default, is_published, is_available) VALUES (:product_id, :name, :unit, :package_quantity, :step_quantity, :price_per_unit, :package_price, :old_package_price, :discount_type, :discount_percent, 1, 1, 1)' ); $variantStatement->execute([ 'product_id' => $productId, 'name' => $data['variant_name'], 'unit' => $data['base_unit'] ?: 'kg', 'package_quantity' => $data['package_quantity'] ?: 1, 'step_quantity' => $data['step_quantity'] ?: ($data['package_quantity'] ?: 1), 'price_per_unit' => $data['price_per_unit'], 'package_price' => $data['package_price'], 'old_package_price' => $data['old_package_price'] ?? null, 'discount_type' => $data['discount_type'] ?? 'none', 'discount_percent' => $data['discount_percent'] ?? null, ]); } $pdo->commit(); return $productId; } catch (\Throwable $exception) { $pdo->rollBack(); throw $exception; } } private function previewSelectSql(string $tailSql, int $limit, int $offset = 0, bool $includeHidden = false): string { return 'SELECT p.id, p.name, p.slug, p.legacy_path, p.h1, p.seo_title, p.seo_description, p.seo_keywords, p.category_id, p.is_published, p.is_available, c.name AS category_name, c.slug AS category_slug, p.main_image_path, p.base_unit, p.package_display_mode, p.sales_count, p.sort_order, v.id AS variant_id, v.name AS variant_name, v.unit, v.package_quantity, v.price_per_unit, v.package_price, v.old_package_price, v.discount_type, v.discount_percent FROM products p LEFT JOIN categories c ON c.id = p.category_id LEFT JOIN product_variants v ON v.id = ( SELECT pv.id FROM product_variants pv WHERE pv.product_id = p.id AND pv.is_published = 1 AND pv.is_available = 1 ORDER BY pv.is_default DESC, pv.sort_order, pv.id LIMIT 1 ) WHERE 1=1 ' . ($includeHidden ? '' : 'AND p.is_published = 1 AND p.is_available = 1') . ' ' . $tailSql . ' LIMIT ' . $limit . ' OFFSET ' . $offset; } /** * @param array> $products * @return array> */ private function preparePreviewProducts(array $products): array { return $this->attachPreviewVariants($this->attachDisplayCategories($this->attachPriceTiers($products))); } /** * @param array> $products * @return array> */ private function attachPreviewVariants(array $products): array { if ($products === []) { return []; } $productIds = array_map(static fn (array $product): int => (int) $product['id'], $products); $placeholders = implode(',', array_fill(0, count($productIds), '?')); $statement = $this->pdo()->prepare( 'SELECT id, product_id, name, unit, package_quantity, step_quantity, price_per_unit, package_price, old_package_price, is_default, sort_order FROM product_variants WHERE product_id IN (' . $placeholders . ') AND is_published = 1 AND is_available = 1 ORDER BY product_id, is_default DESC, sort_order, id' ); $statement->execute($productIds); $variantsByProduct = []; foreach ($statement->fetchAll() as $variant) { $productId = (int) $variant['product_id']; if (count($variantsByProduct[$productId] ?? []) >= 6) { continue; } $variantsByProduct[$productId][] = $variant; } foreach ($products as &$product) { $product['preview_variants'] = $variantsByProduct[(int) $product['id']] ?? []; } unset($product); return $products; } /** * @param array> $products * @return array> */ private function attachDisplayCategories(array $products): array { if ($products === []) { return []; } $productIds = array_map(static fn (array $product): int => (int) $product['id'], $products); $placeholders = implode(',', array_fill(0, count($productIds), '?')); $statement = $this->pdo()->prepare( 'SELECT pcl.product_id, c.id, c.parent_id, c.name, c.sort_order FROM product_category_links pcl INNER JOIN categories c ON c.id = pcl.category_id WHERE pcl.product_id IN (' . $placeholders . ') AND c.is_active = 1 ORDER BY pcl.product_id, c.sort_order, c.name' ); $statement->execute($productIds); $categoriesByProduct = []; foreach ($statement->fetchAll() as $row) { $categoriesByProduct[(int) $row['product_id']][(int) $row['id']] = [ 'id' => (int) $row['id'], 'parent_id' => $row['parent_id'] === null ? null : (int) $row['parent_id'], 'name' => (string) $row['name'], 'sort_order' => (int) $row['sort_order'], ]; } $categoryParents = $this->categoryParentMap(); $categoryRoots = $this->categoryRootMap($categoryParents); foreach ($products as &$product) { $linkedCategories = $categoriesByProduct[(int) $product['id']] ?? []; if ($linkedCategories === [] && !empty($product['category_name'])) { $product['category_display_name'] = $product['category_name']; continue; } $leafCategories = []; foreach ($linkedCategories as $categoryId => $category) { if ($this->hasLinkedDescendant($categoryId, array_keys($linkedCategories), $categoryParents)) { continue; } $leafCategories[] = $category; } usort($leafCategories, static function (array $left, array $right): int { return [$left['sort_order'], $left['name']] <=> [$right['sort_order'], $right['name']]; }); $names = array_values(array_unique(array_map(static fn (array $category): string => $category['name'], $leafCategories))); $product['category_display_ids'] = array_values(array_map(static fn (array $category): int => (int) $category['id'], $leafCategories)); $product['category_display_root_ids'] = array_values(array_unique(array_map( static fn (array $category): int => (int) ($categoryRoots[(int) $category['id']] ?? $category['id']), $leafCategories ))); $product['category_display_name'] = $names === [] ? ($product['category_name'] ?? 'Каталог') : implode(' / ', $names); } unset($product); return $products; } /** * @return array */ private function categoryParentMap(): array { $statement = $this->pdo()->query('SELECT id, parent_id FROM categories WHERE is_active = 1'); $parents = []; foreach ($statement->fetchAll() as $category) { $parents[(int) $category['id']] = $category['parent_id'] === null ? null : (int) $category['parent_id']; } return $parents; } /** * @param array $categoryParents * @return array */ private function categoryRootMap(array $categoryParents): array { $roots = []; foreach (array_keys($categoryParents) as $categoryId) { $roots[$categoryId] = $this->rootCategoryId((int) $categoryId, $categoryParents); } return $roots; } /** * @param array $categoryParents */ private function rootCategoryId(int $categoryId, array $categoryParents): int { $currentId = $categoryId; $parentId = $categoryParents[$currentId] ?? null; while ($parentId !== null) { $currentId = $parentId; $parentId = $categoryParents[$currentId] ?? null; } return $currentId; } /** * @param array $linkedCategoryIds * @param array $categoryParents */ private function hasLinkedDescendant(int $categoryId, array $linkedCategoryIds, array $categoryParents): bool { $linkedLookup = array_flip($linkedCategoryIds); foreach ($linkedCategoryIds as $linkedCategoryId) { if ($linkedCategoryId === $categoryId) { continue; } $parentId = $categoryParents[$linkedCategoryId] ?? null; while ($parentId !== null) { if ($parentId === $categoryId && isset($linkedLookup[$linkedCategoryId])) { return true; } $parentId = $categoryParents[$parentId] ?? null; } } return false; } /** * @param array> $products * @return array> */ private function attachPriceTiers(array $products): array { $priceTierService = new PriceTierService(); foreach ($products as &$product) { $basePrice = $product['price_per_unit'] ?? $product['package_price'] ?? null; $product['price_tiers'] = $basePrice === null ? [] : $priceTierService->forAmount((float) $basePrice); } unset($product); return $products; } }