| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- import { Injectable } from '@angular/core';
- import { FmodeParse } from 'fmode-ng/parse';
- import { ProjectToCaseService } from './project-to-case.service';
- const Parse = FmodeParse.with('nova');
- /**
- * 测试项目完成服务
- * 用于将"10.28 测试"项目推进到售后归档阶段并验证案例创建
- */
- @Injectable({
- providedIn: 'root'
- })
- export class TestProjectCompleteService {
- private companyId = localStorage.getItem("company")!;
- constructor(private projectToCaseService: ProjectToCaseService) {}
- /**
- * 完成测试项目 - 将"10.28 测试"项目推进到售后归档
- */
- async completeTestProject(): Promise<{
- success: boolean;
- projectId?: string;
- caseId?: string;
- message: string;
- }> {
- try {
- console.log('🚀 开始处理测试项目...');
- // 1. 查找"10.28 测试"项目
- const project = await this.findTestProject();
- if (!project) {
- return {
- success: false,
- message: '未找到"10.28 测试"或"10.28 测试项目"'
- };
- }
- const projectId = project.id;
- const projectTitle = project.get('title');
- console.log(`✅ 找到项目: ${projectTitle} (${projectId})`);
- // 2. 填充项目必要信息
- await this.fillProjectData(project);
- console.log('✅ 项目数据已填充');
- // 3. 创建/更新产品数据
- await this.createProducts(project);
- console.log('✅ 产品数据已创建');
- // 4. 更新项目阶段为"售后归档"
- project.set('currentStage', '售后归档');
- project.set('stage', '售后归档');
- project.set('status', '已完成');
- await project.save();
- console.log('✅ 项目阶段已更新为: 售后归档');
- // 5. 触发自动创建案例
- console.log('📦 触发案例自动创建...');
- await this.projectToCaseService.onProjectStageChanged(projectId, '售后归档');
- // 6. 验证案例是否创建
- const caseObj = await this.findProjectCase(projectId);
- if (caseObj) {
- console.log(`✅ 案例创建成功! 案例ID: ${caseObj.id}`);
- console.log(`📋 案例名称: ${caseObj.get('name')}`);
- console.log(`🎨 封面图片: ${caseObj.get('coverImage') ? '已设置' : '未设置'}`);
- console.log(`📸 图片数量: ${(caseObj.get('images') || []).length}`);
- console.log(`💰 项目总额: ${caseObj.get('totalPrice')}`);
- console.log(`🏷️ 标签: ${(caseObj.get('tag') || []).join(', ')}`);
- return {
- success: true,
- projectId: projectId,
- caseId: caseObj.id,
- message: `测试成功!项目"${projectTitle}"已完成售后归档,案例已自动创建 (案例ID: ${caseObj.id})`
- };
- } else {
- return {
- success: false,
- projectId: projectId,
- message: '项目已更新为售后归档,但案例创建失败或需要稍后查看'
- };
- }
- } catch (error: any) {
- console.error('❌ 测试失败:', error);
- return {
- success: false,
- message: `测试失败: ${error.message || error}`
- };
- }
- }
- /**
- * 查找测试项目
- */
- private async findTestProject(): Promise<any> {
- const query = new Parse.Query('Project');
- query.equalTo('company', this.getCompanyPointer());
-
- // 尝试查找"10.28 测试"或"10.28 测试项目"
- const titleQuery1 = new Parse.Query('Project');
- titleQuery1.equalTo('company', this.getCompanyPointer());
- titleQuery1.equalTo('title', '10.28 测试');
-
- const titleQuery2 = new Parse.Query('Project');
- titleQuery2.equalTo('company', this.getCompanyPointer());
- titleQuery2.equalTo('title', '10.28 测试项目');
-
- const combinedQuery = Parse.Query.or(titleQuery1, titleQuery2);
- combinedQuery.descending('createdAt');
-
- try {
- const projects = await combinedQuery.find();
- return projects.length > 0 ? projects[0] : null;
- } catch (error) {
- console.error('查找项目失败:', error);
- return null;
- }
- }
- /**
- * 填充项目数据
- */
- private async fillProjectData(project: any): Promise<void> {
- const projectData = project.get('data') || {};
- // 填充基础信息
- if (!projectData.area) {
- projectData.area = 120; // 120平米
- }
- if (!projectData.projectType) {
- projectData.projectType = '家装';
- }
- if (!projectData.roomType) {
- projectData.roomType = '三居室';
- }
- if (!projectData.spaceType) {
- projectData.spaceType = '平层';
- }
- if (!projectData.renderingLevel) {
- projectData.renderingLevel = '高端';
- }
- // 填充预算信息
- if (!projectData.budget) {
- projectData.budget = {
- total: 350000,
- designFee: 50000,
- constructionFee: 200000,
- softDecorFee: 100000
- };
- }
- // 填充时间线
- if (!projectData.timeline) {
- projectData.timeline = {
- startDate: '2024-10-01',
- completionDate: '2024-10-28',
- duration: 27,
- milestones: [
- { stage: '订单分配', date: '2024-10-01', status: 'completed' },
- { stage: '需求沟通', date: '2024-10-05', status: 'completed' },
- { stage: '方案确认', date: '2024-10-10', status: 'completed' },
- { stage: '建模', date: '2024-10-15', status: 'completed' },
- { stage: '渲染', date: '2024-10-25', status: 'completed' },
- { stage: '售后归档', date: '2024-10-28', status: 'in_progress' }
- ]
- };
- }
- // 填充设计亮点
- if (!projectData.highlights) {
- projectData.highlights = [
- '现代简约风格设计',
- '智能家居系统集成',
- '开放式厨房与客厅设计',
- '采用环保材料',
- '充分利用自然采光'
- ];
- }
- // 填充设计特色
- if (!projectData.features) {
- projectData.features = [
- '流畅的空间动线',
- '中性色调搭配',
- '隐藏式储物空间',
- '多功能家具设计'
- ];
- }
- // 填充材料信息
- if (!projectData.materials) {
- projectData.materials = {
- floor: '实木复合地板',
- wall: '乳胶漆+硅藻泥',
- ceiling: '石膏板吊顶',
- door: '实木复合门',
- window: '断桥铝合金窗'
- };
- }
- // 填充客户信息
- if (!projectData.clientInfo) {
- projectData.clientInfo = {
- familyMembers: 4,
- ageRange: '30-45岁',
- lifestyle: '现代都市家庭',
- specialNeeds: ['儿童房设计', '老人房无障碍设计'],
- satisfactionScore: 95
- };
- }
- // 填充标签
- if (!projectData.tags || projectData.tags.length === 0) {
- projectData.tags = ['现代简约', '北欧风', '家装', '三居室'];
- }
- // 保存数据
- project.set('data', projectData);
- // 确保项目有必要的关联字段
- if (!project.get('totalAmount') && !project.get('orderAmount')) {
- project.set('totalAmount', 350000);
- }
- // 确保有设计师和团队
- if (!project.get('assignee') && !project.get('designer')) {
- // 查找一个设计师
- const designerQuery = new Parse.Query('Profile');
- designerQuery.equalTo('company', this.getCompanyPointer());
- designerQuery.equalTo('identyType', 'designer');
- designerQuery.limit(1);
-
- try {
- const designers = await designerQuery.find();
- if (designers.length > 0) {
- project.set('assignee', designers[0]);
- project.set('designer', designers[0]);
- }
- } catch (error) {
- console.warn('未找到设计师,使用默认值');
- }
- }
- if (!project.get('team') && !project.get('department')) {
- // 查找一个团队
- const teamQuery = new Parse.Query('Department');
- teamQuery.equalTo('company', this.getCompanyPointer());
- teamQuery.limit(1);
-
- try {
- const teams = await teamQuery.find();
- if (teams.length > 0) {
- project.set('team', teams[0]);
- project.set('department', teams[0]);
- }
- } catch (error) {
- console.warn('未找到团队,使用默认值');
- }
- }
- await project.save();
- }
- /**
- * 创建产品数据
- */
- private async createProducts(project: any): Promise<void> {
- // 查询已有产品
- const existingQuery = new Parse.Query('Product');
- existingQuery.equalTo('project', project);
- const existingProducts = await existingQuery.find();
- // 如果已有产品,跳过
- if (existingProducts.length > 0) {
- console.log(`已有 ${existingProducts.length} 个产品,跳过创建`);
- return;
- }
- // 创建示例产品
- const products = [
- {
- name: '主卧效果图',
- spaceArea: '主卧',
- stage: '渲染',
- status: 'completed',
- description: '现代简约风格主卧设计',
- images: [
- 'https://via.placeholder.com/800x600/667eea/ffffff?text=Master+Bedroom+1',
- 'https://via.placeholder.com/800x600/764ba2/ffffff?text=Master+Bedroom+2'
- ]
- },
- {
- name: '客厅效果图',
- spaceArea: '客厅',
- stage: '渲染',
- status: 'completed',
- description: '开放式客厅与餐厅设计',
- images: [
- 'https://via.placeholder.com/800x600/f093fb/ffffff?text=Living+Room+1',
- 'https://via.placeholder.com/800x600/4facfe/ffffff?text=Living+Room+2'
- ]
- },
- {
- name: '厨房效果图',
- spaceArea: '厨房',
- stage: '渲染',
- status: 'completed',
- description: '现代化开放式厨房',
- images: [
- 'https://via.placeholder.com/800x600/43e97b/ffffff?text=Kitchen'
- ]
- }
- ];
- const ProductClass = Parse.Object.extend('Product');
- for (const productData of products) {
- const product = new ProductClass();
- product.set('company', this.getCompanyPointer());
- product.set('project', project);
- product.set('name', productData.name);
- product.set('spaceArea', productData.spaceArea);
- product.set('stage', productData.stage);
- product.set('status', productData.status);
- product.set('description', productData.description);
- // 创建附件
- const attachments = [];
- for (const imageUrl of productData.images) {
- const AttachmentClass = Parse.Object.extend('Attachment');
- const attachment = new AttachmentClass();
- attachment.set('url', imageUrl);
- attachment.set('type', 'image');
- attachment.set('name', `${productData.spaceArea}_${Date.now()}.jpg`);
- await attachment.save();
- attachments.push(attachment);
- }
- product.set('attachments', attachments);
- await product.save();
- }
- console.log(`✅ 创建了 ${products.length} 个产品`);
- }
- /**
- * 查找项目关联的案例
- */
- private async findProjectCase(projectId: string): Promise<any> {
- // 等待2秒让案例创建完成
- await new Promise(resolve => setTimeout(resolve, 2000));
- const query = new Parse.Query('Case');
- query.equalTo('company', this.getCompanyPointer());
- query.equalTo('project', {
- __type: 'Pointer',
- className: 'Project',
- objectId: projectId
- });
- query.notEqualTo('isDeleted', true);
- query.descending('createdAt');
- try {
- const cases = await query.find();
- return cases.length > 0 ? cases[0] : null;
- } catch (error) {
- console.error('查找案例失败:', error);
- return null;
- }
- }
- /**
- * 获取公司指针
- */
- private getCompanyPointer(): any {
- return {
- __type: 'Pointer',
- className: 'Company',
- objectId: this.companyId
- };
- }
- }
|