server_name; // Chỉ lưu dữ liệu khi có server_name if (!$serverName) { Log::info('ℹ️ Bỏ qua lưu dữ liệu VM vì không có server_name trong order item', [ 'order_item_id' => $orderItem->id, 'vmware_id' => $vmwareId ]); return [ 'success' => true, 'message' => 'Bỏ qua lưu dữ liệu VM vì không có server_name trong order item', 'skipped' => true ]; } try { DB::beginTransaction(); $product = $orderItem->getProductAttribute(); $customer = $orderItem->order->customer; $host = $product->host; if (!$product || !$customer || !$host) { throw new \Exception('Thiếu thông tin sản phẩm, khách hàng hoặc host'); } // Tạo mới IP address với server_name $ipAddress = $this->createIpAddress($orderItem, $host, $ipAddresses, $serverName); // Tạo mới VM với server_name $vm = $this->createVM($orderItem, $vmwareId, $ipAddress, $customer, $serverName); DB::commit(); Log::info('✅ Lưu dữ liệu VM thành công với server_name từ order item', [ 'vm_id' => $vm->id, 'vmware_id' => $vm->vmware_id, 'ip_address_id' => $ipAddress->id, 'order_item_id' => $orderItem->id, 'customer_id' => $customer->id, 'server_name' => $serverName ]); return [ 'success' => true, 'vm' => $vm, 'ip_address' => $ipAddress, 'message' => 'Lưu dữ liệu VM thành công với server_name từ order item' ]; } catch (\Exception $e) { DB::rollBack(); Log::error('❌ Lỗi khi lưu dữ liệu VM', [ 'order_item_id' => $orderItem->id, 'vmware_id' => $vmwareId, 'server_name' => $serverName, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return [ 'success' => false, 'message' => 'Lỗi khi lưu dữ liệu VM: ' . $e->getMessage() ]; } } /** * Tạo mới IP address với server_name */ private function createIpAddress(OrderItem $orderItem, $host, array $ipAddresses, string $serverName): ModuleIpAddress { $product = $orderItem->getProductAttribute(); // Lấy IP đầu tiên từ danh sách IP addresses $primaryIp = null; if (!empty($ipAddresses)) { $primaryIp = $ipAddresses[0]['ip'] ?? null; } // Nếu không có IP từ vCenter, sử dụng IP từ sản phẩm if (!$primaryIp && $product->ipAddress) { $primaryIp = $product->ipAddress->ip; } $ipData = [ 'vm_name' => $serverName, 'module_host_id' => $host->id, 'module_network_id' => $product->ipAddress?->module_network_id ?? 1, // Default network 'ip' => $primaryIp ?? '0.0.0.0', 'gateway' => $product->ipAddress?->gateway ?? '0.0.0.0', // Default gateway 'netmask' => $product->ipAddress?->netmask ?? '255.255.255.0', 'dns' => $product->ipAddress?->dns ?? '8.8.8.8', 'status' => $primaryIp ? 1 : 0 // 1 = Assigned, 0 = Free ]; $newIp = ModuleIpAddress::create($ipData); Log::info('✅ Tạo mới IP address với server_name', [ 'ip_id' => $newIp->id, 'ip' => $ipData['ip'], 'vm_name' => $ipData['vm_name'], 'server_name' => $serverName ]); return $newIp; } /** * Tạo mới VM với server_name */ private function createVM(OrderItem $orderItem, string $vmwareId, ModuleIpAddress $ipAddress, $customer, string $serverName): ModuleVmware { $product = $orderItem->getProductAttribute(); // Xử lý payment fields với type casting $firstPayment = $orderItem->order->first_payment; $nextPayment = $orderItem->order->next_payment; $nextInvoice = $orderItem->order->next_invoice; $vmData = [ 'name' => $serverName, 'module_ip_address_id' => $ipAddress->id, 'port' => 1, // Default port 'order_id' => $orderItem->order->id, 'first_payment' => is_numeric($firstPayment) ? (int) $firstPayment : 0, // Ensure integer 'next_payment' => is_numeric($nextPayment) ? (int) $nextPayment : 0, // Ensure integer 'next_due_date' => $orderItem->order->next_due_date ?? null, 'next_invoice' => is_numeric($nextInvoice) ? (int) $nextInvoice : 0, // Ensure integer 'user' => 'admin', // Default user 'password' => '', // Will be set later 'hostname' => $serverName, // Sử dụng server_name cho hostname 'vmware_id' => $vmwareId, 'os_id' => $orderItem->os_id ?? 0, 'customer_id' => $customer->id, 'status' => 1 // Active ]; $newVM = ModuleVmware::create($vmData); Log::info('✅ Tạo mới VM với server_name', [ 'vm_id' => $newVM->id, 'vmware_id' => $vmwareId, 'name' => $serverName, 'server_name' => $serverName ]); return $newVM; } /** * Cập nhật trạng thái VM */ public function updateVMStatus(string $vmwareId, int $status): bool { try { $vm = ModuleVmware::where('vmware_id', $vmwareId)->first(); if (!$vm) { Log::warning('⚠️ Không tìm thấy VM để cập nhật trạng thái', [ 'vmware_id' => $vmwareId, 'status' => $status ]); return false; } $vm->update(['status' => $status]); Log::info('🔄 Cập nhật trạng thái VM', [ 'vm_id' => $vm->id, 'vmware_id' => $vmwareId, 'old_status' => $vm->getOriginal('status'), 'new_status' => $status ]); return true; } catch (\Exception $e) { Log::error('❌ Lỗi khi cập nhật trạng thái VM', [ 'vmware_id' => $vmwareId, 'status' => $status, 'error' => $e->getMessage() ]); return false; } } /** * Cập nhật IP address cho VM */ public function updateVMIPAddress(string $vmwareId, string $ipAddress): bool { try { $vm = ModuleVmware::where('vmware_id', $vmwareId)->first(); if (!$vm || !$vm->ipAddress) { Log::warning('⚠️ Không tìm thấy VM hoặc IP address để cập nhật', [ 'vmware_id' => $vmwareId, 'ip' => $ipAddress ]); return false; } $vm->ipAddress->update([ 'ip' => $ipAddress, 'status' => 1 // Assigned ]); Log::info('🔄 Cập nhật IP address cho VM', [ 'vm_id' => $vm->id, 'vmware_id' => $vmwareId, 'ip' => $ipAddress ]); return true; } catch (\Exception $e) { Log::error('❌ Lỗi khi cập nhật IP address cho VM', [ 'vmware_id' => $vmwareId, 'ip' => $ipAddress, 'error' => $e->getMessage() ]); return false; } } }