TravelContentCreator/docs/ARCHITECTURE_REDESIGN.md

43 KiB
Raw Blame History

TravelContentCreator 架构重设计方案

一、现状架构图 (As-Is)

┌─────────────────────────────────────────────────────────────────────────────────────────┐
│                              zwy_picture (Java Spring Boot)                              │
│  ┌─────────────────────┐  ┌─────────────────────┐  ┌─────────────────────────────────┐  │
│  │ PosterGenerateQueue │  │ NoteCreatorQueue    │  │ KeywordParseQueueConsumer       │  │
│  │ Consumer/Producer   │  │ Consumer/Producer   │  │                                 │  │
│  └──────────┬──────────┘  └──────────┬──────────┘  └────────────────┬────────────────┘  │
│             │                        │                              │                    │
│             └────────────────────────┼──────────────────────────────┘                    │
│                                      │                                                   │
│                          ┌───────────▼───────────┐                                       │
│                          │ ExternalServiceClient │  HTTP Calls                           │
│                          └───────────┬───────────┘                                       │
└──────────────────────────────────────┼───────────────────────────────────────────────────┘
                                       │
                    ═══════════════════╪═══════════════════  Network Boundary
                                       │
┌──────────────────────────────────────▼───────────────────────────────────────────────────┐
│                        TravelContentCreator (Python FastAPI)                             │
│                                                                                          │
│  ┌────────────────────────────────────────────────────────────────────────────────────┐  │
│  │                              api/main.py (FastAPI App)                             │  │
│  │  ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐   │  │
│  │  │ /tweet/*    │ │ /poster/*   │ │ /prompt/*   │ │ /document/* │ │/integration/│   │  │
│  │  │ Router      │ │ Router      │ │ Router      │ │ Router      │ │ Router      │   │  │
│  │  └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘   │  │
│  └─────────┼───────────────┼───────────────┼───────────────┼───────────────┼──────────┘  │
│            │               │               │               │               │             │
│  ┌─────────▼───────────────▼───────────────▼───────────────▼───────────────▼──────────┐  │
│  │                           api/services/ (服务层 - 问题区域)                         │  │
│  │  ┌─────────────────────────────────────────────────────────────────────────────┐   │  │
│  │  │  tweet.py (757行)     poster.py (3032行!)    integration_service.py (796行) │   │  │
│  │  │  ┌─────────────────┐  ┌─────────────────┐    ┌─────────────────────────────┐│   │  │
│  │  │  │ TweetService    │  │ PosterService   │    │ IntegrationService          ││   │  │
│  │  │  │ - TopicGen      │  │ - 模板管理      │    │ - XHS搜索                   ││   │  │
│  │  │  │ - ContentGen    │  │ - 图片处理      │    │ - 文档整合                   ││   │  │
│  │  │  │ - ContentJudge  │  │ - LLM调用       │    │ - Cookie管理                 ││   │  │
│  │  │  │ - ID映射        │  │ - PSD生成       │    │                              ││   │  │
│  │  │  │                 │  │ - Fabric.js生成 │    │                              ││   │  │
│  │  │  │                 │  │ - 装饰图生成    │    │                              ││   │  │
│  │  │  └─────────────────┘  └─────────────────┘    └─────────────────────────────┘│   │  │
│  │  └─────────────────────────────────────────────────────────────────────────────┘   │  │
│  │                                                                                     │  │
│  │  ┌─────────────────────────────────────────────────────────────────────────────┐   │  │
│  │  │  database_service.py (39KB! - 所有表的CRUD混在一起)                          │   │  │
│  │  │  prompt_service.py (27KB)    prompt_builder.py (18KB)                        │   │  │
│  │  └─────────────────────────────────────────────────────────────────────────────┘   │  │
│  └────────────────────────────────────────────────────────────────────────────────────┘  │
│                                                                                          │
│  ┌────────────────────────────────────────────────────────────────────────────────────┐  │
│  │                              core/ (核心算法层)                                     │  │
│  │  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐  ┌──────────────────────┐    │  │
│  │  │ ai/          │  │ config/      │  │ document/    │  │ xhs_spider/          │    │  │
│  │  │ - AIAgent    │  │ - Manager    │  │ - Extractor  │  │ - Data_Spider        │    │  │
│  │  │              │  │ - Models     │  │ - Integrator │  │ - XHS_Apis           │    │  │
│  │  └──────────────┘  └──────────────┘  └──────────────┘  └──────────────────────┘    │  │
│  │                                                                                     │  │
│  │  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐                              │  │
│  │  │ xhs_adapter  │  │ doc_adapter  │  │ cookie_mgr   │                              │  │
│  │  └──────────────┘  └──────────────┘  └──────────────┘                              │  │
│  └────────────────────────────────────────────────────────────────────────────────────┘  │
│                                                                                          │
│  ┌────────────────────────────────────────────────────────────────────────────────────┐  │
│  │                              tweet/ (文案生成算法)                                  │  │
│  │  ┌──────────────────┐  ┌──────────────────┐  ┌──────────────────┐                  │  │
│  │  │ TopicGenerator   │  │ ContentGenerator │  │ ContentJudger    │                  │  │
│  │  │ (149行)          │  │ (142行)          │  │ (212行)          │                  │  │
│  │  └──────────────────┘  └──────────────────┘  └──────────────────┘                  │  │
│  └────────────────────────────────────────────────────────────────────────────────────┘  │
│                                                                                          │
│  ┌────────────────────────────────────────────────────────────────────────────────────┐  │
│  │                              poster/ (海报生成算法)                                 │  │
│  │  ┌──────────────────┐  ┌─────────────────────────────────────────────────────────┐ │  │
│  │  │ PosterGenerator  │  │ templates/                                              │ │  │
│  │  │ (121行)          │  │ ┌───────────────┐ ┌───────────────┐ ┌───────────────┐   │ │  │
│  │  │                  │  │ │VibrantTemplate│ │BusinessTemplate│ │CollageTemplate│   │ │  │
│  │  │                  │  │ │(1757行!)      │ │(16KB)          │ │(4KB)          │   │ │  │
│  │  └──────────────────┘  │ └───────────────┘ └───────────────┘ └───────────────┘   │ │  │
│  │                        └─────────────────────────────────────────────────────────┘ │  │
│  └────────────────────────────────────────────────────────────────────────────────────┘  │
│                                                                                          │
│  ┌────────────────────────────────────────────────────────────────────────────────────┐  │
│  │                              utils/ (工具层)                                        │  │
│  │  ┌──────────────────┐  ┌──────────────────┐  ┌──────────────────┐                  │  │
│  │  │ file_io.py       │  │ image_processor  │  │ prompts.py       │                  │  │
│  │  │ (7KB)            │  │ (11KB)           │  │ (18KB)           │                  │  │
│  │  └──────────────────┘  └──────────────────┘  └──────────────────┘                  │  │
│  └────────────────────────────────────────────────────────────────────────────────────┘  │
└──────────────────────────────────────────────────────────────────────────────────────────┘

二、核心问题诊断

2.1 架构问题

问题类型 具体表现 影响
God Object poster.py 3032行承担7+职责 难以维护、测试、扩展
职责混乱 api/services/ 同时包含业务逻辑和算法调用 层次不清
重复代码 tweet.pyposter.py 都有ID映射、数据库调用 DRY违反
依赖混乱 Service内部直接new DatabaseService 无法测试、无法替换
同步阻塞 async函数中调用同步DB操作 性能瓶颈
缺乏抽象 模板直接硬编码渲染逻辑 扩展困难

2.2 与 zwy_picture 对接问题

问题 现状 影响
无批量接口 Java端自己实现并发 网络开销大
无任务状态 长任务无法追踪 用户体验差
无重试机制 失败即终止 可靠性低
响应格式不统一 各接口格式不同 对接复杂

三、目标架构图 (To-Be)

┌─────────────────────────────────────────────────────────────────────────────────────────┐
│                              zwy_picture (Java Spring Boot)                              │
│  ┌─────────────────────────────────────────────────────────────────────────────────┐    │
│  │                         Task Queue Layer (已有,保持)                            │    │
│  │  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐                  │    │
│  │  │ PosterGenQueue  │  │ NoteCreatorQueue│  │ KeywordParseQueue│                  │    │
│  │  └────────┬────────┘  └────────┬────────┘  └────────┬────────┘                  │    │
│  └───────────┼────────────────────┼────────────────────┼────────────────────────────┘    │
│              └────────────────────┼────────────────────┘                                 │
│                                   │                                                      │
│                       ┌───────────▼───────────┐                                          │
│                       │ AIGCServiceClient     │  统一的AIGC服务客户端                     │
│                       │ - submitTask()        │  (新增:支持批量、状态查询)               │
│                       │ - getTaskStatus()     │                                          │
│                       │ - cancelTask()        │                                          │
│                       └───────────┬───────────┘                                          │
└───────────────────────────────────┼──────────────────────────────────────────────────────┘
                                    │
                 ═══════════════════╪═══════════════════  Network Boundary
                                    │
┌───────────────────────────────────▼──────────────────────────────────────────────────────┐
│                     TravelContentCreator (Python FastAPI) - 重构后                        │
│                                                                                           │
│  ┌─────────────────────────────────────────────────────────────────────────────────────┐ │
│  │                           API Gateway Layer (网关层)                                 │ │
│  │  ┌─────────────────────────────────────────────────────────────────────────────┐    │ │
│  │  │ api/main.py                                                                 │    │ │
│  │  │ - 统一异常处理中间件                                                         │    │ │
│  │  │ - 请求日志中间件                                                             │    │ │
│  │  │ - 认证中间件 (可选)                                                          │    │ │
│  │  └─────────────────────────────────────────────────────────────────────────────┘    │ │
│  │                                                                                      │ │
│  │  ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐   │ │
│  │  │ /v2/task/*  │ │ /v2/content/│ │ /v2/poster/*│ │ /v2/data/*  │ │ /v2/spider/*│   │ │
│  │  │ 任务管理    │ │ 内容生成    │ │ 海报生成    │ │ 基础数据    │ │ 爬虫服务    │   │ │
│  │  └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘ └──────┬──────┘   │ │
│  └─────────┼───────────────┼───────────────┼───────────────┼───────────────┼──────────┘ │
│            │               │               │               │               │            │
│  ┌─────────▼───────────────▼───────────────▼───────────────▼───────────────▼──────────┐ │
│  │                        Application Service Layer (应用服务层)                       │ │
│  │                                                                                      │ │
│  │  ┌──────────────────┐  ┌──────────────────┐  ┌──────────────────┐                   │ │
│  │  │ TaskOrchestrator │  │ ContentService   │  │ PosterService    │                   │ │
│  │  │ (新增)           │  │ (精简)           │  │ (精简)           │                   │ │
│  │  │ - 任务调度       │  │ - 协调内容生成   │  │ - 协调海报生成   │                   │ │
│  │  │ - 状态管理       │  │ - 调用算法层     │  │ - 调用算法层     │                   │ │
│  │  │ - 进度通知       │  │                  │  │                  │                   │ │
│  │  └──────────────────┘  └──────────────────┘  └──────────────────┘                   │ │
│  │                                                                                      │ │
│  │  ┌──────────────────┐  ┌──────────────────┐                                         │ │
│  │  │ SpiderService    │  │ DataService      │                                         │ │
│  │  │ - XHS搜索协调    │  │ - 基础数据CRUD   │                                         │ │
│  │  └──────────────────┘  └──────────────────┘                                         │ │
│  └─────────────────────────────────────────────────────────────────────────────────────┘ │
│                                          │                                               │
│  ┌───────────────────────────────────────▼─────────────────────────────────────────────┐ │
│  │                          Domain Layer (领域层 - 核心算法)                            │ │
│  │                                                                                      │ │
│  │  ┌─────────────────────────────────────────────────────────────────────────────┐    │ │
│  │  │                        content/ (内容生成领域)                               │    │ │
│  │  │  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐     │    │ │
│  │  │  │TopicEngine   │  │ContentEngine │  │JudgeEngine   │  │PromptEngine  │     │    │ │
│  │  │  │- 选题生成    │  │- 内容生成    │  │- 内容审核    │  │- 提示词构建  │     │    │ │
│  │  │  └──────────────┘  └──────────────┘  └──────────────┘  └──────────────┘     │    │ │
│  │  └─────────────────────────────────────────────────────────────────────────────┘    │ │
│  │                                                                                      │ │
│  │  ┌─────────────────────────────────────────────────────────────────────────────┐    │ │
│  │  │                        poster/ (海报生成领域)                                │    │ │
│  │  │  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐     │    │ │
│  │  │  │TemplateEngine│  │RenderEngine  │  │ExportEngine  │  │ImageEngine   │     │    │ │
│  │  │  │- 模板管理    │  │- PNG渲染     │  │- PSD导出     │  │- 图片处理    │     │    │ │
│  │  │  │- 模板选择    │  │- JSON渲染    │  │- Fabric导出  │  │- 尺寸调整    │     │    │ │
│  │  │  └──────────────┘  └──────────────┘  └──────────────┘  └──────────────┘     │    │ │
│  │  │                                                                              │    │ │
│  │  │  ┌─────────────────────────────────────────────────────────────────────┐    │    │ │
│  │  │  │                    templates/ (模板实现)                             │    │    │ │
│  │  │  │  ┌────────────┐  ┌────────────┐  ┌────────────┐  ┌────────────┐     │    │    │ │
│  │  │  │  │ Vibrant    │  │ Business   │  │ Collage    │  │ Custom...  │     │    │    │ │
│  │  │  │  │ (拆分后)   │  │ (拆分后)   │  │            │  │            │     │    │    │ │
│  │  │  │  └────────────┘  └────────────┘  └────────────┘  └────────────┘     │    │    │ │
│  │  │  └─────────────────────────────────────────────────────────────────────┘    │    │ │
│  │  └─────────────────────────────────────────────────────────────────────────────┘    │ │
│  │                                                                                      │ │
│  │  ┌─────────────────────────────────────────────────────────────────────────────┐    │ │
│  │  │                        spider/ (爬虫领域)                                    │    │ │
│  │  │  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐                       │    │ │
│  │  │  │ XHSCrawler   │  │ CookiePool   │  │ MediaDownload│                       │    │ │
│  │  │  │ - 搜索       │  │ - Cookie轮换 │  │ - 图片下载   │                       │    │ │
│  │  │  │ - 详情获取   │  │ - 有效性检测 │  │ - 视频下载   │                       │    │ │
│  │  │  └──────────────┘  └──────────────┘  └──────────────┘                       │    │ │
│  │  └─────────────────────────────────────────────────────────────────────────────┘    │ │
│  └─────────────────────────────────────────────────────────────────────────────────────┘ │
│                                          │                                               │
│  ┌───────────────────────────────────────▼─────────────────────────────────────────────┐ │
│  │                       Infrastructure Layer (基础设施层)                              │ │
│  │                                                                                      │ │
│  │  ┌──────────────────┐  ┌──────────────────┐  ┌──────────────────┐                   │ │
│  │  │ ai/              │  │ database/        │  │ storage/         │                   │ │
│  │  │ ┌──────────────┐ │  │ ┌──────────────┐ │  │ ┌──────────────┐ │                   │ │
│  │  │ │ LLMClient    │ │  │ │ AsyncDBPool  │ │  │ │ FileStorage  │ │                   │ │
│  │  │ │ (OpenAI等)   │ │  │ │ (aiomysql)   │ │  │ │ (本地/S3)    │ │                   │ │
│  │  │ └──────────────┘ │  │ └──────────────┘ │  │ └──────────────┘ │                   │ │
│  │  │ ┌──────────────┐ │  │ ┌──────────────┐ │  │ ┌──────────────┐ │                   │ │
│  │  │ │ TokenCounter │ │  │ │ Repositories │ │  │ │ CacheStorage │ │                   │ │
│  │  │ └──────────────┘ │  │ │ - ScenicSpot │ │  │ │ (Redis可选)  │ │                   │ │
│  │  └──────────────────┘ │ │ - Product    │ │  │ └──────────────┘ │                   │ │
│  │                        │ │ - Style      │ │  └──────────────────┘                   │ │
│  │  ┌──────────────────┐  │ │ - Template   │ │                                         │ │
│  │  │ config/          │  │ └──────────────┘ │  ┌──────────────────┐                   │ │
│  │  │ - ConfigLoader   │  └──────────────────┘  │ http/            │                   │ │
│  │  │ - EnvManager     │                        │ - HTTPClient     │                   │ │
│  │  └──────────────────┘                        │ - RetryPolicy    │                   │ │
│  │                                              └──────────────────┘                   │ │
│  └─────────────────────────────────────────────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────────────────────────────────────┘

四、详细模块设计

4.1 任务管理系统 (新增核心)

# domain/task/task_manager.py
from enum import Enum
from dataclasses import dataclass
from datetime import datetime
from typing import Optional, Dict, Any

class TaskStatus(Enum):
    PENDING = "pending"
    RUNNING = "running"
    COMPLETED = "completed"
    FAILED = "failed"
    CANCELLED = "cancelled"

class TaskType(Enum):
    TOPIC_GENERATION = "topic_generation"
    CONTENT_GENERATION = "content_generation"
    CONTENT_JUDGE = "content_judge"
    POSTER_GENERATION = "poster_generation"
    XHS_SEARCH = "xhs_search"
    CONTENT_INTEGRATION = "content_integration"

@dataclass
class Task:
    task_id: str
    task_type: TaskType
    status: TaskStatus
    progress: int  # 0-100
    params: Dict[str, Any]
    result: Optional[Dict[str, Any]] = None
    error_message: Optional[str] = None
    created_at: datetime = None
    updated_at: datetime = None
    
class TaskOrchestrator:
    """任务编排器 - 统一管理所有AIGC任务"""
    
    async def submit_task(self, task_type: TaskType, params: Dict) -> str:
        """提交任务返回task_id"""
        
    async def get_task_status(self, task_id: str) -> Task:
        """获取任务状态"""
        
    async def cancel_task(self, task_id: str) -> bool:
        """取消任务"""
        
    async def submit_batch(self, tasks: List[Dict]) -> List[str]:
        """批量提交任务"""

4.2 海报生成领域重构

poster/
├── __init__.py
├── engines/
│   ├── __init__.py
│   ├── template_engine.py      # 模板管理和选择
│   ├── render_engine.py        # PNG渲染引擎
│   ├── fabric_engine.py        # Fabric.js JSON生成
│   ├── psd_engine.py           # PSD导出引擎
│   └── image_engine.py         # 图片处理引擎
├── templates/
│   ├── __init__.py
│   ├── base.py                 # 模板基类
│   ├── vibrant/
│   │   ├── __init__.py
│   │   ├── layout.py           # 布局计算
│   │   ├── effects.py          # 特效(毛玻璃等)
│   │   └── renderer.py         # 渲染实现
│   ├── business/
│   │   └── ...
│   └── collage/
│       └── ...
├── models/
│   ├── __init__.py
│   ├── content.py              # 海报内容模型
│   ├── layout.py               # 布局模型
│   └── export.py               # 导出结果模型
└── poster_service.py           # 对外服务接口(精简)

4.3 内容生成领域重构

content/
├── __init__.py
├── engines/
│   ├── __init__.py
│   ├── topic_engine.py         # 选题生成引擎
│   ├── content_engine.py       # 内容生成引擎
│   ├── judge_engine.py         # 内容审核引擎
│   └── prompt_engine.py        # 提示词构建引擎
├── models/
│   ├── __init__.py
│   ├── topic.py                # 选题模型
│   ├── article.py              # 文章模型
│   └── prompt.py               # 提示词模型
├── parsers/
│   ├── __init__.py
│   ├── topic_parser.py         # 选题解析器
│   └── content_parser.py       # 内容解析器
└── content_service.py          # 对外服务接口(精简)

4.4 基础设施层重构

infrastructure/
├── __init__.py
├── ai/
│   ├── __init__.py
│   ├── llm_client.py           # LLM客户端抽象
│   ├── openai_client.py        # OpenAI实现
│   └── token_counter.py        # Token计数器
├── database/
│   ├── __init__.py
│   ├── connection.py           # 异步连接池
│   └── repositories/
│       ├── __init__.py
│       ├── base.py             # Repository基类
│       ├── scenic_spot_repo.py
│       ├── product_repo.py
│       ├── style_repo.py
│       ├── audience_repo.py
│       └── template_repo.py
├── storage/
│   ├── __init__.py
│   ├── file_storage.py         # 文件存储
│   └── cache_storage.py        # 缓存存储
└── config/
    ├── __init__.py
    ├── settings.py             # 配置加载
    └── constants.py            # 常量定义

五、工作拆分计划

Phase 1: 基础设施重构 (Week 1-2)

任务 优先级 预估工时 依赖
1.1 创建异步数据库连接池 P0 4h -
1.2 拆分DatabaseService为独立Repository P0 8h 1.1
1.3 统一异常处理中间件 P0 4h -
1.4 统一响应格式 P1 4h 1.3
1.5 配置管理重构 P1 4h -

Phase 2: 任务管理系统 (Week 2-3)

任务 优先级 预估工时 依赖
2.1 设计Task模型和状态机 P0 4h -
2.2 实现TaskOrchestrator P0 8h 2.1
2.3 实现任务状态持久化 P0 4h 2.2, 1.2
2.4 实现批量任务提交接口 P0 4h 2.2
2.5 实现任务进度回调机制 P1 4h 2.2

Phase 3: 海报生成领域重构 (Week 3-4)

任务 优先级 预估工时 依赖
3.1 提取ImageEngine P0 4h -
3.2 提取RenderEngine P0 8h 3.1
3.3 提取FabricEngine P0 8h 3.2
3.4 提取PSDEngine P1 4h 3.2
3.5 重构VibrantTemplate P0 8h 3.2, 3.3
3.6 精简PosterService P0 4h 3.1-3.5

Phase 4: 内容生成领域重构 (Week 4-5)

任务 优先级 预估工时 依赖
4.1 提取PromptEngine P0 4h -
4.2 重构TopicEngine P0 4h 4.1
4.3 重构ContentEngine P0 4h 4.1
4.4 重构JudgeEngine P1 4h 4.1
4.5 精简ContentService P0 4h 4.1-4.4

Phase 5: API层重构 (Week 5-6)

任务 优先级 预估工时 依赖
5.1 设计V2 API规范 P0 4h 2.1
5.2 实现/v2/task/* 接口 P0 8h 2.2, 5.1
5.3 实现/v2/poster/* 接口 P0 4h 3.6, 5.1
5.4 实现/v2/content/* 接口 P0 4h 4.5, 5.1
5.5 V1 API兼容层 P1 4h 5.2-5.4

Phase 6: Java端对接优化 (Week 6)

任务 优先级 预估工时 依赖
6.1 更新AIGCServiceClient P0 4h 5.2
6.2 适配批量任务提交 P0 4h 6.1
6.3 实现任务状态轮询 P0 4h 6.1
6.4 集成测试 P0 8h 6.1-6.3

六、关键接口设计

6.1 统一任务API

# POST /api/v2/task/submit
Request:
  task_type: "poster_generation" | "content_generation" | "topic_generation" | ...
  params:
    # 根据task_type不同而不同
  callback_url: "http://..."  # 可选,完成后回调

Response:
  code: 0
  data:
    task_id: "task_xxx"
    status: "pending"
  message: "success"

# GET /api/v2/task/{task_id}/status
Response:
  code: 0
  data:
    task_id: "task_xxx"
    status: "running" | "completed" | "failed"
    progress: 75
    result: {...}  # 完成时有值
    error: "..."   # 失败时有值
  message: "success"

# POST /api/v2/task/batch
Request:
  tasks:
    - task_type: "poster_generation"
      params: {...}
    - task_type: "poster_generation"
      params: {...}

Response:
  code: 0
  data:
    task_ids: ["task_1", "task_2", ...]
  message: "success"

6.2 统一响应格式

@dataclass
class ApiResponse(Generic[T]):
    code: int = 0           # 0=成功, 其他=错误码
    data: Optional[T] = None
    message: str = "success"
    request_id: str = ""    # 请求追踪ID
    timestamp: int = 0      # 时间戳

七、迁移策略

  1. 渐进式重构: 新旧代码并存通过Feature Flag切换
  2. V1/V2 API共存: V1保持兼容V2为新架构
  3. 数据库无迁移: Repository层只是代码重构不改表结构
  4. 测试先行: 每个Phase完成后进行集成测试

八、预期收益

指标 现状 目标
最大文件行数 3032行 <500行
单元测试覆盖率 ~0% >60%
API响应一致性 100%
任务可追踪性 100%
批量任务支持 支持
代码重复率 <10%