import { Component } from '@angular/core'; import { CommonModule } from '@angular/common'; import { BriefUploadComponent } from '../brief-upload/brief-upload.component'; import { TaskProgressComponent } from '../task-progress/task-progress.component'; import { ExportPanelComponent } from '../export-panel/export-panel.component'; import { WorkspaceApiService } from '../../services/workspace-api.service'; import { TaskStep, TaskStatus } from '../../models/task.model'; @Component({ selector: 'app-workspace-flow', standalone: true, imports: [CommonModule, BriefUploadComponent, TaskProgressComponent, ExportPanelComponent], templateUrl: './workspace-flow.component.html', styleUrl: './workspace-flow.component.scss', }) export class WorkspaceFlowComponent { currentTaskId: string | null = null; taskStatus: TaskStatus = 'pending'; steps: TaskStep[] = []; candidateCount = 0; exporting = false; errorMessage = ''; libraryUploading = false; libraryMessage = ''; stopPolling: (() => void) | null = null; constructor(private api: WorkspaceApiService) {} get isProcessing(): boolean { return ['uploading', 'analyzing', 'searching', 'processing'].includes(this.taskStatus); } get isCompleted(): boolean { return this.taskStatus === 'completed'; } get showProgress(): boolean { return this.isProcessing || this.isCompleted || this.taskStatus === 'failed'; } async onFileSelected(file: File): Promise { this.errorMessage = ''; this.taskStatus = 'uploading'; this.steps = []; this.candidateCount = 0; try { const response = await this.api.uploadBrief(file); this.currentTaskId = response.taskId; this.taskStatus = 'analyzing'; // Start the task processing await this.api.startTask(response.taskId); // Poll for progress this.startPolling(response.taskId); } catch (error: unknown) { this.taskStatus = 'failed'; this.errorMessage = error instanceof Error ? error.message : '上传失败'; } } async onCreatorLibraryInput(event: Event): Promise { const input = event.target as HTMLInputElement; const file = input.files?.[0]; if (!file) return; this.libraryUploading = true; this.libraryMessage = ''; this.errorMessage = ''; try { const result = await this.api.uploadCreatorLibrary(file); this.libraryMessage = `已入库 ${result.localCreatorCount} 位达人:${result.fileName}`; } catch (error: unknown) { this.errorMessage = error instanceof Error ? error.message : '达人资料库上传失败'; } finally { this.libraryUploading = false; input.value = ''; } } private startPolling(taskId: string): void { if (this.stopPolling) { this.stopPolling(); } this.stopPolling = this.api.pollTaskProgress(taskId, (event) => { const stepIndex = this.steps.findIndex((s) => s.id === event.step); const stepData: TaskStep = { id: event.step, label: this.getStepLabel(event.step), status: event.status, progress: event.progress, detail: event.detail, }; if (stepIndex >= 0) { this.steps[stepIndex] = stepData; } else { this.steps.push(stepData); } // Update overall status if (event.data && typeof event.data === 'object') { const data = event.data as Record; if (data['candidates'] && Array.isArray(data['candidates'])) { this.candidateCount = data['candidates'].length; } if (data['totalPool']) { this.candidateCount = data['totalPool'] as number; } } const allDone = this.steps.length > 0 && this.steps.every((s) => s.status === 'done'); const anyError = this.steps.some((s) => s.status === 'error'); if (allDone) { this.taskStatus = 'completed'; } else if (anyError) { this.taskStatus = 'failed'; this.errorMessage = this.steps.find((s) => s.status === 'error')?.detail || '处理失败'; } else { this.taskStatus = 'processing'; } }); } async onExport(): Promise { if (!this.currentTaskId) return; this.exporting = true; try { const blob = await this.api.exportRecommendation(this.currentTaskId); const url = URL.createObjectURL(blob); const anchor = document.createElement('a'); anchor.href = url; anchor.download = `推荐表_${new Date().toISOString().slice(0, 10)}.xlsx`; anchor.click(); URL.revokeObjectURL(url); } catch (error: unknown) { this.errorMessage = error instanceof Error ? error.message : '导出失败'; } finally { this.exporting = false; } } private getStepLabel(stepId: string): string { const labels: Record = { upload: '上传文件', analyze: 'AI 解析 Brief 需求', rules: '确定筛选规则', search: '搜索达人资源', recommend: '生成推荐名单', export: '准备导出', }; return labels[stepId] || stepId; } ngOnDestroy(): void { if (this.stopPolling) { this.stopPolling(); } } }