where('id', $sourceId)->first(); if (! $existing) { $existing = DB::table('dns_manager_domain')->where('domain_name', $domainName)->first(); } $targetId = $existing?->id ?? $sourceId; $idMap[$sourceId] = (int) $targetId; $payload = [ 'domain_name' => $domainName, 'dns_manager_server_id' => $this->resolveDnsServerId((int) ($row['dns_manager_server_id'] ?? 0)), 'is_created' => (int) ($row['is_created'] ?? 0), 'plesk_id' => $this->nullableInt($row['plesk_id'] ?? null), 'created_at' => $this->normalizeDateTime($row['created_at'] ?? null) ?? now(), 'updated_at' => $this->normalizeDateTime($row['updated_at'] ?? null) ?? now(), ]; if ($preview) { $summary['dns_domains'][$existing ? 'updated' : 'created']++; continue; } if ($existing) { DB::table('dns_manager_domain')->where('id', $existing->id)->update($payload); $summary['dns_domains']['updated']++; continue; } $payload['id'] = $sourceId; DB::table('dns_manager_domain')->insert($payload); $summary['dns_domains']['created']++; } return $idMap; } private function importDnsRecords(array $rows, array $domainIdMap, array &$summary, bool $preview): void { foreach ($rows as $row) { $sourceId = (int) ($row['id'] ?? 0); $sourceDomainId = (int) ($row['dns_manager_domain_id'] ?? 0); $targetDomainId = $domainIdMap[$sourceDomainId] ?? null; if ($sourceId <= 0 || ! $targetDomainId) { $summary['dns_records']['skipped']++; continue; } $existing = DB::table('dns_manager_ns')->where('id', $sourceId)->first(); $payload = [ 'dns_manager_domain_id' => $targetDomainId, 'name' => (string) ($row['name'] ?? ''), 'type' => (string) ($row['type'] ?? 'A'), 'priority' => $this->nullableString($row['priority'] ?? null, '0'), 'weight' => $this->nullableString($row['weight'] ?? null, '0'), 'port' => $this->nullableString($row['port'] ?? null, '0'), 'service_name' => $this->nullableString($row['service_name'] ?? null), 'protocol' => $this->nullableString($row['protocol'] ?? null), 'value' => $this->nullableString($row['value'] ?? null), 'plesk_id' => $this->nullableInt($row['plesk_id'] ?? null), 'ttl' => (int) ($row['ttl'] ?? 300), 'created_at' => $this->normalizeDateTime($row['created_at'] ?? null) ?? now(), 'updated_at' => $this->normalizeDateTime($row['updated_at'] ?? null) ?? now(), ]; if (Schema::hasColumn('dns_manager_ns', 'type_valid')) { $payload['type_valid'] = $existing?->type_valid ?? (string) ($row['type'] ?? 'A'); } if ($preview) { $summary['dns_records'][$existing ? 'updated' : 'created']++; continue; } if ($existing) { DB::table('dns_manager_ns')->where('id', $sourceId)->update($payload); $summary['dns_records']['updated']++; continue; } $payload['id'] = $sourceId; DB::table('dns_manager_ns')->insert($payload); $summary['dns_records']['created']++; } } private function importDnsCustomerLinks( array $rows, array $customerIdMap, array $domainIdMap, array &$summary, bool $preview, ): void { foreach ($rows as $row) { $domainId = $domainIdMap[(int) ($row['domain_id'] ?? 0)] ?? null; $customerId = $customerIdMap[(int) ($row['user_id'] ?? 0)] ?? null; if (! $domainId || ! $customerId) { $summary['dns_links']['skipped']++; continue; } $existing = DB::table('dns_domain_user') ->where('domain_id', $domainId) ->where('user_id', $customerId) ->first(); $payload = [ 'domain_id' => $domainId, 'user_id' => $customerId, 'created_at' => $this->normalizeDateTime($row['created_at'] ?? null) ?? now(), 'updated_at' => $this->normalizeDateTime($row['updated_at'] ?? null) ?? now(), ]; if ($preview) { $summary['dns_links'][$existing ? 'updated' : 'created']++; continue; } if ($existing) { DB::table('dns_domain_user') ->where('id', $existing->id) ->update(['updated_at' => $payload['updated_at']]); $summary['dns_links']['updated']++; continue; } DB::table('dns_domain_user')->insert($payload); $summary['dns_links']['created']++; } } private function resolveDnsServerId(int $sourceServerId): int { if ($sourceServerId > 0 && DB::table('dns_manager_servers')->where('id', $sourceServerId)->exists()) { return $sourceServerId; } if ($this->canonicalServerId !== null) { return $this->canonicalServerId; } $server = DB::table('dns_manager_servers') ->select('id') ->orderByRaw('CASE WHEN id = 1 THEN 0 ELSE 1 END') ->orderBy('id') ->first(); if (! $server) { throw new RuntimeException('No dns_manager_servers available to map imported domains.'); } return $this->canonicalServerId = (int) $server->id; } }