# Changelog: 剧本场景统计更新修复 **日期**: 2026-02-09 **类型**: Bug Fix **影响范围**: 剧本解析服务 **严重程度**: High ## 📋 问题描述 ### 症状 剧本 AI 解析完成后,`scene_count` 字段始终为 0,导致: 1. 前端无法正确显示场景数量统计 2. 分镜生成依赖场景数据,可能受影响 3. 数据统计不准确 ### 根本原因 在 `ScreenplayService.store_parsed_elements()` 方法中,更新剧本统计时**遗漏了 `scene_count` 字段的更新**: ```python # ❌ 修复前:只更新了 character_count await self.repository.update(screenplay_id, { 'character_count': len(character_id_map), 'parsing_status': ParsingStatus.COMPLETED }) ``` 虽然场景数据(locations)已正确存储到 `project_locations` 表,但剧本表的统计字段未同步更新。 ## 🔧 修复方案 ### 代码变更 **文件**: `server/app/services/screenplay_service.py` ```python # ✅ 修复后:同时更新 character_count 和 scene_count await self.repository.update(screenplay_id, { 'character_count': len(character_id_map), 'scene_count': len(location_id_map), # 新增 'parsing_status': ParsingStatus.COMPLETED }) ``` ### 修复逻辑 - `location_id_map` 包含所有已创建的场景(locations) - 使用 `len(location_id_map)` 获取场景总数 - 与 `character_count` 一起更新到剧本表 ## ✅ 验证方法 ### 1. 重新解析现有剧本 ```bash curl 'http://localhost:6160/api/v1/screenplays/{screenplay_id}/parse' \ -H 'Authorization: Bearer {token}' \ -H 'Content-Type: application/json' \ --data-raw '{"custom_requirements":""}' ``` ### 2. 检查统计字段 ```bash curl 'http://localhost:6160/api/v1/screenplays/{screenplay_id}' \ -H 'Authorization: Bearer {token}' \ -s | jq '.data | {scene_count, character_count}' ``` **期望结果**: ```json { "scene_count": 124, // ✅ 不再是 0 "character_count": 15 } ``` ### 3. 数据库验证 ```sql SELECT screenplay_id, name, scene_count, character_count FROM screenplays WHERE screenplay_id = '019c4179-3dc6-7423-a30a-e965a7df0e09'; ``` ## 📊 影响范围 ### 受影响的功能 1. ✅ **剧本详情页统计** - 场景数量显示 2. ✅ **项目仪表板** - 剧本统计汇总 3. ✅ **分镜生成** - 依赖场景数据的功能 ### 不受影响的功能 - ❌ 场景数据本身(`project_locations` 表数据正常) - ❌ 分镜创建逻辑(使用 `location_id_map`,不依赖 `scene_count`) ## 🔄 后续建议 ### 1. 数据修复脚本(可选) 如果需要修复历史数据: ```python # server/scripts/fix_screenplay_scene_count.py async def fix_scene_count(): """修复历史剧本的 scene_count 字段""" screenplays = await screenplay_repo.get_all() for screenplay in screenplays: # 统计该剧本关联的场景数量 locations = await location_repo.get_by_project(screenplay.project_id) scene_count = len(locations) # 更新统计 await screenplay_repo.update(screenplay.screenplay_id, { 'scene_count': scene_count }) ``` ### 2. 单元测试增强 建议在 `test_screenplay_service.py` 中添加测试: ```python async def test_store_parsed_elements_updates_scene_count(): """测试:存储解析元素时正确更新 scene_count""" parsed_data = { 'locations': [ {'name': '海边', 'description': '...'}, {'name': '教室', 'description': '...'} ], 'characters': [...] } result = await service.store_parsed_elements(screenplay_id, parsed_data) screenplay = await service.get_by_id(screenplay_id) assert screenplay.scene_count == 2 # ✅ 验证场景统计 assert screenplay.character_count > 0 ``` ## 📝 相关文档 - [剧本服务架构](../guides/backend-architecture.md#screenplay-service) - [AI 解析流程](../../guides/ai-prompts-guide.md) - [数据模型设计](../../requirements/backend/03-models/screenplay-models.md) ## 🏷️ 标签 `bug-fix` `screenplay` `statistics` `ai-parsing` `high-priority`