123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
- import { CommonModule } from '@angular/common';
- import { FormsModule } from '@angular/forms';
- import { TrainingReportsComponent } from './report-viewer.component';
- import { TrainingService } from './training.service';
- import { Report, Scene } from './report.interface';
- @Component({
- selector: 'page-crm-training',
- standalone: true,
- imports: [CommonModule, FormsModule, TrainingReportsComponent],
- templateUrl: './page-crm-training.html',
- styleUrls: ['./page-crm-training.scss']
- })
- export class PageCrmTraining implements OnInit {
- @ViewChild('chatMessagesContainer') chatMessagesContainer!: ElementRef;
- // 场景数据
- sceneLevels: { value: string; label: string }[] = [];
- customerTypes: { value: string; label: string }[] = [];
- allScenes: Scene[] = [];
- filteredScenes: Scene[] = [];
- selectedLevel: string = '';
- selectedCustomerType: string = '';
- selectedScene: Scene | null = null;
- // 对话数据
- chatMessages: Array<{
- type: 'user' | 'bot';
- content: string;
- timestamp: Date;
- }> = [];
- chatInput: string = '';
- currentRound: number = 1;
- maxRounds: number = 5;
- conversationScore: number | null = null;
- // 报告数据
- generatedReports: Report[] = [];
- selectedReport: Report | null = null;
- constructor(private trainingService: TrainingService) {}
- ngOnInit(): void {
- this.loadInitialData();
- }
- private loadInitialData(): void {
- this.trainingService.getSceneLevels().subscribe(levels => {
- this.sceneLevels = levels;
- });
- this.trainingService.getCustomerTypes().subscribe(types => {
- this.customerTypes = types;
- });
- this.trainingService.getAllScenes().subscribe(scenes => {
- this.allScenes = scenes;
- this.filteredScenes = [...scenes];
- });
- }
- filterScenes(): void {
- this.filteredScenes = this.allScenes.filter(scene => {
- const levelMatch = !this.selectedLevel || scene.level === this.selectedLevel;
- const typeMatch = !this.selectedCustomerType || scene.customerType === this.selectedCustomerType;
- return levelMatch && typeMatch;
- });
- }
- getLevelLabel(levelValue: string): string {
- const level = this.sceneLevels.find(l => l.value === levelValue);
- return level ? level.label : levelValue;
- }
- getCustomerLabel(typeValue: string): string {
- const type = this.customerTypes.find(t => t.value === typeValue);
- return type ? type.label : typeValue;
- }
- selectScene(scene: Scene): void {
- this.selectedScene = scene;
- this.currentRound = 1;
- this.conversationScore = null;
- this.chatMessages = [{
- type: 'bot',
- content: scene.initialMessage,
- timestamp: new Date()
- }];
- this.scrollToBottom();
- }
- sendMessage(): void {
- if (!this.chatInput.trim() || !this.selectedScene) return;
- // 添加用户消息
- const userMessage = {
- type: 'user' as const,
- content: this.chatInput,
- timestamp: new Date()
- };
- this.chatMessages.push(userMessage);
- this.chatInput = '';
- this.scrollToBottom();
- // 模拟AI回复
- setTimeout(() => {
- const botResponse = this.trainingService.generateResponse(
- this.selectedScene!.id,
- userMessage.content
- );
-
- this.chatMessages.push({
- type: 'bot' as const,
- content: botResponse,
- timestamp: new Date()
- });
- // 更新轮次和评分
- this.currentRound = Math.ceil(this.chatMessages.length / 2);
- if (this.currentRound <= this.maxRounds) {
- this.updateScore();
- }
- this.scrollToBottom();
- }, 800);
- }
- private updateScore(): void {
- const baseScore = 80;
- const roundBonus = (this.currentRound - 1) * 5;
- const randomVariation = Math.floor(Math.random() * 10);
-
- this.conversationScore = Math.min(baseScore + roundBonus + randomVariation, 100);
- // 如果完成所有轮次,生成报告
- if (this.currentRound === this.maxRounds) {
- this.generateReport();
- }
- }
- private generateReport(): void {
- if (!this.selectedScene) return;
- const newReport: Report = {
- id: `report-${Date.now()}`,
- name: this.selectedScene.name,
- sceneId: this.selectedScene.id,
- date: new Date().toISOString(),
- difficulty: this.selectedScene.level,
- customerRole: this.selectedScene.customerType,
- score: this.conversationScore || 0,
- evaluations: {
- responsiveness: Math.min(this.conversationScore! + Math.floor(Math.random() * 10) - 5, 100),
- 话术: Math.min((this.conversationScore || 80) + Math.floor(Math.random() * 8) - 4, 100),
- persuasion: Math.min((this.conversationScore || 75) + Math.floor(Math.random() * 12) - 6, 100),
- professionalism: Math.min((this.conversationScore || 85) + Math.floor(Math.random() * 6) - 3, 100)
- },
- summary: this.generateSummaryText()
- };
- this.generatedReports.unshift(newReport);
- }
- private generateSummaryText(): string {
- const strengths = [
- '展现了出色的沟通技巧',
- '对产品知识掌握扎实',
- '能够有效处理客户异议',
- '表现出了高度的专业性',
- '对话流畅自然',
- '反应迅速准确'
- ];
-
- const improvements = [
- '可以进一步提高反应速度',
- '建议丰富案例说明',
- '需要加强价格谈判技巧',
- '可优化话术结构',
- '应更关注客户需求',
- '可增加数据支持论点'
- ];
- return `在${this.selectedScene?.name}场景训练中,${
- strengths[Math.floor(Math.random() * strengths.length)]
- },${improvements[Math.floor(Math.random() * improvements.length)]}。`;
- }
- resetConversation(): void {
- if (this.selectedScene) {
- this.selectScene(this.selectedScene);
- }
- }
- viewReport(report: Report): void {
- this.selectedReport = report;
- console.log('查看完整报告:', report);
- }
- closeReport(): void {
- this.selectedReport = null;
- }
- private scrollToBottom(): void {
- setTimeout(() => {
- if (this.chatMessagesContainer) {
- this.chatMessagesContainer.nativeElement.scrollTop =
- this.chatMessagesContainer.nativeElement.scrollHeight;
- }
- }, 100);
- }
- }
|