db = DatabaseManager::getInstance(); } /** * Obter todos os criativos com filtros */ public function getAll(array $filters = []): array { $sql = "SELECT c.*, b.name as brand_name, lt.name as landing_type_name FROM advertising_creatives c JOIN advertising_brands b ON c.brand_id = b.id LEFT JOIN advertising_brand_landing_types lt ON c.landing_type_id = lt.id WHERE 1=1"; $params = []; if (!empty($filters['brand_id'])) { $sql .= " AND c.brand_id = :brand_id"; $params[':brand_id'] = $filters['brand_id']; } if (!empty($filters['landing_type_id'])) { $sql .= " AND c.landing_type_id = :landing_type_id"; $params[':landing_type_id'] = $filters['landing_type_id']; } if (!empty($filters['type'])) { $sql .= " AND c.type = :type"; $params[':type'] = $filters['type']; } if (isset($filters['is_active'])) { $sql .= " AND c.is_active = :is_active"; $params[':is_active'] = $filters['is_active']; } if (isset($filters['is_default'])) { $sql .= " AND c.is_default = :is_default"; $params[':is_default'] = $filters['is_default']; } $sql .= " ORDER BY c.sort_order ASC, c.created_at DESC"; return $this->db->fetchAll($sql, $params) ?: []; } /** * Obter criativo por ID */ public function getById(int $id): ?array { $sql = "SELECT c.*, b.name as brand_name, lt.name as landing_type_name FROM advertising_creatives c JOIN advertising_brands b ON c.brand_id = b.id LEFT JOIN advertising_brand_landing_types lt ON c.landing_type_id = lt.id WHERE c.id = :id"; return $this->db->fetch($sql, [':id' => $id]); } /** * Obter criativos para um tracker específico * Hierarquia: criativo específico do tracker > criativo do landing type > criativo padrão da marca */ public function getForTracker(int $brandId, ?int $landingTypeId = null, ?int $creativeId = null): array { // Se tem criativo específico, retornar apenas ele if ($creativeId) { $creative = $this->getById($creativeId); return $creative ? [$creative] : []; } // Buscar criativos do landing type ou padrão da marca $sql = "SELECT c.*, b.name as brand_name, lt.name as landing_type_name FROM advertising_creatives c JOIN advertising_brands b ON c.brand_id = b.id LEFT JOIN advertising_brand_landing_types lt ON c.landing_type_id = lt.id WHERE c.brand_id = :brand_id AND c.is_active = 1"; $params = [':brand_id' => $brandId]; if ($landingTypeId) { // Buscar criativos do landing type OU padrão da marca $sql .= " AND (c.landing_type_id = :landing_type_id OR c.landing_type_id IS NULL)"; $params[':landing_type_id'] = $landingTypeId; } else { // Apenas criativos padrão da marca $sql .= " AND c.landing_type_id IS NULL"; } $sql .= " ORDER BY c.landing_type_id DESC, c.is_default DESC, c.sort_order ASC"; return $this->db->fetchAll($sql, $params) ?: []; } /** * Criar novo criativo */ public function create(array $data): int { $cuid = $this->generateCuid(); $sql = "INSERT INTO advertising_creatives ( cuid, brand_id, landing_type_id, name, type, file_type, file_path, file_url, cdn_url, file_size, width, height, duration, thumbnail_url, metadata, tags, is_active, is_default, sort_order ) VALUES ( :cuid, :brand_id, :landing_type_id, :name, :type, :file_type, :file_path, :file_url, :cdn_url, :file_size, :width, :height, :duration, :thumbnail_url, :metadata, :tags, :is_active, :is_default, :sort_order )"; $params = [ ':cuid' => $cuid, ':brand_id' => $data['brand_id'], ':landing_type_id' => $data['landing_type_id'] ?? null, ':name' => $data['name'], ':type' => $data['type'] ?? 'image', ':file_type' => $data['file_type'], ':file_path' => $data['file_path'], ':file_url' => $data['file_url'] ?? null, ':cdn_url' => $data['cdn_url'] ?? null, ':file_size' => $data['file_size'] ?? null, ':width' => $data['width'] ?? null, ':height' => $data['height'] ?? null, ':duration' => $data['duration'] ?? null, ':thumbnail_url' => $data['thumbnail_url'] ?? null, ':metadata' => !empty($data['metadata']) ? json_encode($data['metadata']) : null, ':tags' => $data['tags'] ?? null, ':is_active' => $data['is_active'] ?? 1, ':is_default' => $data['is_default'] ?? 0, ':sort_order' => $data['sort_order'] ?? 0 ]; $this->db->query($sql, $params); return $this->db->lastInsertId(); } /** * Atualizar criativo */ public function update(int $id, array $data): bool { $sql = "UPDATE advertising_creatives SET name = :name, type = :type, file_type = :file_type, file_path = :file_path, file_url = :file_url, cdn_url = :cdn_url, file_size = :file_size, width = :width, height = :height, duration = :duration, thumbnail_url = :thumbnail_url, metadata = :metadata, tags = :tags, is_active = :is_active, is_default = :is_default, sort_order = :sort_order, updated_at = CURRENT_TIMESTAMP WHERE id = :id"; return $this->db->query($sql, [ ':id' => $id, ':name' => $data['name'], ':type' => $data['type'] ?? 'image', ':file_type' => $data['file_type'] ?? 'image', ':file_path' => $data['file_path'] ?? '', ':file_url' => $data['file_url'] ?? null, ':cdn_url' => $data['cdn_url'] ?? null, ':file_size' => $data['file_size'] ?? null, ':width' => $data['width'] ?? null, ':height' => $data['height'] ?? null, ':duration' => $data['duration'] ?? null, ':thumbnail_url' => $data['thumbnail_url'] ?? null, ':metadata' => !empty($data['metadata']) ? json_encode($data['metadata']) : null, ':tags' => $data['tags'] ?? null, ':is_active' => $data['is_active'] ?? 1, ':is_default' => $data['is_default'] ?? 0, ':sort_order' => $data['sort_order'] ?? 0 ]) > 0; } /** * Soft delete (desativar) criativo */ public function delete(int $id): bool { $sql = "UPDATE advertising_creatives SET status = -1, updated_at = NOW() WHERE id = ?"; return $this->db->query($sql, [$id]) > 0; } /** * Obter estatísticas gerais dos criativos */ public function getStats(): array { $sql = " SELECT COUNT(*) as total_creatives, SUM(IF(status = 1, 1, 0)) as active_creatives, SUM(IF(status = 0, 1, 0)) as inactive_creatives, COUNT(DISTINCT campaign_id) as total_campaigns, COUNT(DISTINCT type) as total_types FROM advertising_creatives WHERE status >= 0 "; return $this->db->fetch($sql) ?: []; } /** * Adicionar variação de criativo por país/idioma */ public function addVariation(int $creativeId, array $data): int { $sql = "INSERT INTO advertising_creative_variations ( creative_id, country_code, language_code, file_path, file_url, cdn_url, is_active ) VALUES ( :creative_id, :country_code, :language_code, :file_path, :file_url, :cdn_url, :is_active ) ON DUPLICATE KEY UPDATE file_path = VALUES(file_path), file_url = VALUES(file_url), cdn_url = VALUES(cdn_url), is_active = VALUES(is_active)"; $params = [ ':creative_id' => $creativeId, ':country_code' => $data['country_code'] ?? null, ':language_code' => $data['language_code'] ?? null, ':file_path' => $data['file_path'], ':file_url' => $data['file_url'] ?? null, ':cdn_url' => $data['cdn_url'] ?? null, ':is_active' => $data['is_active'] ?? 1 ]; $this->db->query($sql, $params); return $this->db->lastInsertId(); } /** * Obter variações de um criativo */ public function getVariations(int $creativeId): array { $sql = "SELECT * FROM advertising_creative_variations WHERE creative_id = :creative_id AND is_active = 1 ORDER BY country_code, language_code"; return $this->db->fetchAll($sql, [':creative_id' => $creativeId]) ?: []; } /** * Obter criativo com variação específica */ public function getWithVariation(int $creativeId, ?string $countryCode = null, ?string $languageCode = null): ?array { $creative = $this->getById($creativeId); if (!$creative) return null; // Buscar variação específica if ($countryCode || $languageCode) { $sql = "SELECT * FROM advertising_creative_variations WHERE creative_id = :creative_id AND is_active = 1"; $params = [':creative_id' => $creativeId]; if ($countryCode) { $sql .= " AND country_code = :country_code"; $params[':country_code'] = $countryCode; } if ($languageCode) { $sql .= " AND language_code = :language_code"; $params[':language_code'] = $languageCode; } $variation = $this->db->fetch($sql, $params); if ($variation) { // Sobrescrever URLs com a variação $creative['file_path'] = $variation['file_path']; $creative['file_url'] = $variation['file_url']; $creative['cdn_url'] = $variation['cdn_url']; $creative['variation'] = $variation; } } return $creative; } /** * Definir criativo como padrão */ public function setAsDefault(int $id): bool { $creative = $this->getById($id); if (!$creative) return false; // Remover flag de padrão de outros criativos do mesmo tipo $sql = "UPDATE advertising_creatives SET is_default = 0 WHERE brand_id = :brand_id AND type = :type"; $params = [ ':brand_id' => $creative['brand_id'], ':type' => $creative['type'] ]; if ($creative['landing_type_id']) { $sql .= " AND landing_type_id = :landing_type_id"; $params[':landing_type_id'] = $creative['landing_type_id']; } else { $sql .= " AND landing_type_id IS NULL"; } $this->db->query($sql, $params); // Definir este como padrão $sql = "UPDATE advertising_creatives SET is_default = 1 WHERE id = :id"; return $this->db->query($sql, [':id' => $id]) > 0; } /** * Reordenar criativos */ public function reorder(array $order): bool { $sql = "UPDATE advertising_creatives SET sort_order = ? WHERE id = ?"; foreach ($order as $sortOrder => $id) { $this->db->query($sql, [$sortOrder, $id]); } return true; } private function generateCuid(): string { return (string) new \Visus\Cuid2\Cuid2(); } public function getDb(): DatabaseManager { return $this->db; } }