import { Component, OnInit } from '@angular/core'; import { CommonModule } from '@angular/common'; import { RouterModule } from '@angular/router'; import { ProjectService } from '../../../services/project.service'; import { Task, RenderProgress, CustomerFeedback } from '../../../models/project.model'; @Component({ selector: 'app-dashboard', imports: [CommonModule, RouterModule], templateUrl: './dashboard.html', styleUrl: './dashboard.scss' }) export class Dashboard implements OnInit { tasks: Task[] = []; overdueTasks: Task[] = []; urgentTasks: Task[] = []; pendingFeedbacks: {task: Task, feedback: CustomerFeedback}[] = []; reminderMessage: string = ''; feedbackProjectId: string = ''; countdowns: Map = new Map(); constructor(private projectService: ProjectService) {} ngOnInit(): void { this.loadTasks(); } loadTasks(): void { this.projectService.getTasks().subscribe(tasks => { // 按阶段优先级排序:建模 > 渲染 > 对图 > 反馈处理 > 后期 > 其他 this.tasks = tasks.sort((a, b) => { const stagePriority: Record = { '建模': 5, '渲染': 4, '对图': 3, '反馈处理': 2, '后期': 1, '完成': 0 }; const priorityA = stagePriority[a.stage] || 0; const priorityB = stagePriority[b.stage] || 0; if (priorityA !== priorityB) { return priorityB - priorityA; } // 优先级相同时,按截止日期排序 return a.deadline.getTime() - b.deadline.getTime(); }); // 筛选超期任务 this.overdueTasks = this.tasks.filter(task => task.isOverdue); // 筛选紧急任务(渲染超时预警,交付前3小时/1小时) this.urgentTasks = this.tasks.filter(task => { const now = new Date(); const diffHours = (task.deadline.getTime() - now.getTime()) / (1000 * 60 * 60); return diffHours <= 3 && diffHours > 0 && task.stage === '渲染'; }); // 设置反馈项目ID if (this.overdueTasks.length > 0) { this.feedbackProjectId = this.overdueTasks[0].projectId; } // 加载待处理反馈 this.loadPendingFeedbacks(); // 启动倒计时 this.startCountdowns(); }); } loadPendingFeedbacks(): void { this.pendingFeedbacks = []; // 模拟加载待处理反馈数据 this.tasks.forEach(task => { // 使用模拟数据代替API调用 const mockFeedbacks = [ { id: 'fb-' + task.id, projectId: task.projectId, content: '客户对色彩不满意,需要调整', isSatisfied: false, problemLocation: '色彩', expectedEffect: '更明亮的色调', referenceCase: '无', status: '待处理' as const, createdAt: new Date(Date.now() - 30 * 60 * 1000) // 30分钟前 }, { id: 'fb-' + task.id + '-2', projectId: task.projectId, content: '家具款式需要调整', isSatisfied: false, problemLocation: '家具', expectedEffect: '更现代的款式', referenceCase: '无', status: '待处理' as const, createdAt: new Date(Date.now() - 45 * 60 * 1000) // 45分钟前 } ]; const pending = mockFeedbacks.filter(feedback => feedback.status === '待处理' && !feedback.isSatisfied ); if (pending.length > 0) { this.pendingFeedbacks.push({task, feedback: pending[0]}); } }); } startCountdowns(): void { // 清除之前的定时器 this.countdowns.clear(); // 为渲染任务启动倒计时 this.tasks.forEach(task => { if (task.stage === '渲染') { this.updateCountdown(task.id, task.deadline); } }); // 定期更新倒计时 setInterval(() => { this.tasks.forEach(task => { if (task.stage === '渲染') { this.updateCountdown(task.id, task.deadline); } }); }, 60000); // 每分钟更新一次 } updateCountdown(taskId: string, deadline: Date): void { const now = new Date(); const diffMs = deadline.getTime() - now.getTime(); if (diffMs <= 0) { this.countdowns.set(taskId, '已超期'); return; } const diffHours = Math.floor(diffMs / (1000 * 60 * 60)); const diffMinutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60)); if (diffHours > 0) { this.countdowns.set(taskId, `${diffHours}小时${diffMinutes}分钟`); } else { this.countdowns.set(taskId, `${diffMinutes}分钟`); } } getTaskCountdown(taskId: string): string { return this.countdowns.get(taskId) || ''; } getTaskStageProgress(taskId: string): number { const task = this.tasks.find(t => t.id === taskId); if (!task) return 0; // 为不同阶段设置固定的模拟进度值 const stageProgressMap: Record = { '建模': 22, '渲染': 23, '对图': 50, '反馈处理': 80, '后期': 75, '完成': 100 }; // 对于渲染任务,如果有实际的渲染进度数据,使用它 if (task.stage === '渲染') { // 在实际应用中,这里会从服务中获取真实的进度 // this.projectService.getRenderProgress(task.projectId).subscribe(progress => { // if (progress) { // return progress.completionRate; // } // }); } return stageProgressMap[task.stage] || 0; } markTaskAsCompleted(taskId: string): void { this.projectService.markTaskAsCompleted(taskId).subscribe(() => { this.loadTasks(); // 重新加载任务列表 }); } handleFeedback(taskId: string): void { const task = this.tasks.find(t => t.id === taskId); if (task) { // 跳转到项目详情的反馈处理页面 window.location.href = `/designer/project-detail/${task.projectId}#feedback`; } } generateReminderMessage(): void { this.projectService.generateReminderMessage('overdue').subscribe(message => { this.reminderMessage = message; }); } clearReminder(): void { this.reminderMessage = ''; } }