getPdo(); $insAccount = $pdo->prepare( "INSERT INTO social_accounts (owner_type, owner_id, platform, account_id, profile_url, remark, is_primary, is_active) VALUES (?,?,?,?,?,?,?,1)" ); $insAttr = $pdo->prepare( "INSERT INTO media_commercial_attributes (social_account_id, account_level, content_categories, follower_count, avg_read_count, certification_type) VALUES (?,?,?,?,?,?)" ); $inserted = 0; $failed = 0; while (($row = fgetcsv($handle)) !== false) { $row = array_map('trim', $row); $rec = []; foreach ($header as $idx => $col) { $col = trim($col); if (isset($row[$idx])) { $rec[$col] = $row[$idx]; } } if (empty($rec['platform']) || empty($rec['account_id']) || empty($rec['owner_type'])) { $failed++; continue; } $ownerType = $rec['owner_type'] === 'company' ? 'company' : 'person'; $ownerId = (int)($rec['owner_id'] ?? 0); if ($ownerId <= 0) { // 按名称解析 $nameCol = $ownerType === 'company' ? 'display_name' : 'full_name'; $tbl = $ownerType === 'company' ? 'companies' : 'persons'; $find = $pdo->prepare("SELECT id FROM $tbl WHERE $nameCol = ? LIMIT 1"); $find->execute([$rec['owner_name'] ?? $rec['owner_id'] ?? '']); $ownerId = (int)$find->fetchColumn(); } if ($ownerId <= 0) { $failed++; continue; } try { $chk = $pdo->prepare("SELECT COUNT(*) FROM social_accounts WHERE platform = ? AND account_id = ?"); $chk->execute([$rec['platform'], $rec['account_id']]); if ((int)$chk->fetchColumn() > 0) { $failed++; continue; } $insAccount->execute([ $ownerType, $ownerId, $rec['platform'], $rec['account_id'], $rec['profile_url'] ?? null, $rec['remark'] ?? null, !empty($rec['is_primary']) ? 1 : 0, ]); $newId = (int)$pdo->lastInsertId(); if (!empty($rec['account_level']) || !empty($rec['certification_type'])) { $insAttr->execute([ $newId, $rec['account_level'] ?? null, $rec['content_categories'] ?? null, (int)($rec['follower_count'] ?? 0), (int)($rec['avg_read_count'] ?? 0), $rec['certification_type'] ?? null, ]); } $inserted++; } catch (Exception $e) { $failed++; } } fclose($handle); logCurrent('import', 'media', 'social_accounts', null, ['inserted' => $inserted, 'failed' => $failed]); Response::success(['inserted' => $inserted, 'failed' => $failed], "导入完成:成功 $inserted 条,失败 $failed 条");