# 药店销售预测系统 - Windows 操作指南 ## 系统环境 - **操作系统**: Windows 10/11 - **终端**: PowerShell (推荐) 或 Command Prompt - **Python版本**: 3.8+ - **Node.js版本**: 16+ ## 🚀 快速启动指南 ### 1. 环境准备 **检查Python版本**: ```powershell python --version ``` **检查Node.js版本**: ```powershell node --version npm --version ``` ### 2. 安装依赖 **后端依赖安装**: ```powershell # 进入项目目录 cd "I:\_OneTree\_Python\_药店销售预测系统" # 创建虚拟环境 python -m venv .venv # 激活虚拟环境 .\.venv\Scripts\Activate.ps1 # 如果遇到执行策略问题,先运行: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser # 安装Python依赖 pip install -r install\requirements.txt # 安装GPU版本PyTorch(可选) pip install -r install\requirements-gpu.txt ``` **前端依赖安装**: ```powershell # 进入UI目录 cd UI # 安装前端依赖 npm install # 或使用pnpm(如果已安装) pnpm install ``` ### 3. 数据初始化 **初始化多店铺数据库**: ```powershell # 确保在项目根目录 cd "I:\_OneTree\_Python\_药店销售预测系统" # 激活虚拟环境(如果未激活) .\.venv\Scripts\Activate.ps1 # 初始化数据库 python server\init_multi_store_db.py # 生成示例数据(如果没有数据文件) python generate_multi_store_data.py ``` ### 4. 启动服务 **启动后端API服务器**: ```powershell # 在项目根目录,确保虚拟环境已激活 cd server python api.py # 服务器将在 http://localhost:5000 启动 ``` **启动前端开发服务器**(新的PowerShell窗口): ```powershell cd "I:\_OneTree\_Python\_药店销售预测系统\UI" npm run dev # 或 pnpm dev # 前端将在 http://localhost:5173 启动 ``` **访问应用**: - 前端界面: http://localhost:5173 - API文档: http://localhost:5000/swagger ## 🔧 开发操作指南 ### 数据管理 **查看数据文件**: ```powershell # 查看多店铺CSV数据 Get-Content pharmacy_sales_multi_store.csv | Select-Object -First 10 # 查看数据库文件 if (Test-Path "prediction_history.db") { Write-Host "数据库文件存在" } else { Write-Host "数据库文件不存在" } ``` **备份数据**: ```powershell # 创建备份文件夹 New-Item -ItemType Directory -Force -Path "backup\$(Get-Date -Format 'yyyyMMdd')" # 备份重要文件 Copy-Item "pharmacy_sales_multi_store.csv" "backup\$(Get-Date -Format 'yyyyMMdd')\" Copy-Item "prediction_history.db" "backup\$(Get-Date -Format 'yyyyMMdd')\" ``` ### 测试和验证 **API测试**: ```powershell # 简单API测试 python test_api_endpoints.py # 多店铺功能测试 python test_multi_store_training.py # 预测器修复验证 python test_predictor_fix.py ``` **前端构建测试**: ```powershell cd UI npm run build # 或 pnpm build ``` ### 日志和调试 **查看API日志**: ```powershell # 启动API时会在控制台显示日志 # 要保存日志到文件: python server\api.py 2>&1 | Tee-Object -FilePath "api.log" ``` **查看训练日志**: ```powershell # 训练日志通常保存在 saved_models 目录下 Get-ChildItem saved_models -Filter "*.log" | Sort-Object LastWriteTime -Descending ``` ## 📁 项目结构说明 ``` I:\_OneTree\_Python\_药店销售预测系统\ ├── server\ # 后端代码 │ ├── api.py # 主API服务器 │ ├── core\ # 核心预测器 │ ├── trainers\ # 模型训练器 │ ├── utils\ # 工具函数 │ └── models\ # 模型定义 ├── UI\ # 前端代码 │ ├── src\ # Vue源码 │ ├── dist\ # 构建输出 │ └── package.json # 前端依赖 ├── docs\ # 文档 ├── install\ # 安装脚本 ├── saved_models\ # 保存的模型 ├── pharmacy_sales_multi_store.csv # 多店铺数据 └── prediction_history.db # 预测历史数据库 ``` ## 🛠 常见操作 ### 模型训练 **通过API训练模型**: ```powershell # 按产品训练 $body = @{ product_id = "P001" model_type = "tcn" training_mode = "product" epochs = 10 } | ConvertTo-Json Invoke-RestMethod -Uri "http://localhost:5000/api/training" -Method Post -Body $body -ContentType "application/json" ``` **通过命令行训练**: ```powershell # 进入服务器目录 cd server # 使用预测器训练 python -c " from core.predictor import PharmacyPredictor predictor = PharmacyPredictor() metrics = predictor.train_model( product_id='P001', model_type='tcn', training_mode='product', epochs=10 ) print('训练完成:', metrics) " ``` ### 数据查看和分析 **查看产品列表**: ```powershell Invoke-RestMethod -Uri "http://localhost:5000/api/products" -Method Get ``` **查看店铺列表**: ```powershell Invoke-RestMethod -Uri "http://localhost:5000/api/stores" -Method Get ``` **查看模型列表**: ```powershell Get-ChildItem saved_models -Filter "*.pth" | Format-Table Name, LastWriteTime, Length ``` ### 部署和分发 **构建生产版本**: ```powershell # 构建前端 cd UI npm run build # 复制前端文件到服务器静态目录 if (Test-Path "dist") { Copy-Item -Path "dist\*" -Destination "..\server\wwwroot\" -Recurse -Force Write-Host "前端文件已复制到服务器目录" } ``` **创建便携版**: ```powershell # 创建便携版目录 New-Item -ItemType Directory -Force -Path "portable_release" # 复制必要文件 Copy-Item -Path "server\*" -Destination "portable_release\server\" -Recurse Copy-Item -Path "pharmacy_sales_multi_store.csv" -Destination "portable_release\" Copy-Item -Path "docs\portable_README.md" -Destination "portable_release\README.md" ``` ## 🐛 故障排除 ### 常见错误和解决方法 **1. PowerShell执行策略错误**: ```powershell # 问题: 无法加载文件,因为在此系统上禁止运行脚本 # 解决: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser ``` **2. Python模块导入错误**: ```powershell # 问题: ModuleNotFoundError # 解决: 确保虚拟环境已激活 .\.venv\Scripts\Activate.ps1 pip install -r install\requirements.txt ``` **3. 端口占用错误**: ```powershell # 查看端口占用 netstat -ano | findstr :5000 netstat -ano | findstr :5173 # 终止占用端口的进程 # taskkill /PID /F ``` **4. 数据文件不存在**: ```powershell # 检查文件是否存在 if (!(Test-Path "pharmacy_sales_multi_store.csv")) { Write-Host "生成示例数据..." python generate_multi_store_data.py } ``` **5. API连接失败**: ```powershell # 测试API服务器是否运行 try { $response = Invoke-RestMethod -Uri "http://localhost:5000/api/products" -Method Get -TimeoutSec 5 Write-Host "API服务器正常运行" } catch { Write-Host "API服务器未响应,请检查服务器是否启动" } ``` ### 性能优化 **GPU检查**: ```powershell python -c " import torch print('CUDA可用:', torch.cuda.is_available()) if torch.cuda.is_available(): print('GPU设备:', torch.cuda.get_device_name(0)) print('GPU内存:', torch.cuda.get_device_properties(0).total_memory // 1024**3, 'GB') " ``` **内存监控**: ```powershell # 查看Python进程内存使用 Get-Process python | Format-Table ProcessName, CPU, WorkingSet -AutoSize ``` ## 📝 维护指南 ### 定期维护任务 **清理临时文件**: ```powershell # 清理Python缓存 Get-ChildItem -Path . -Recurse -Name "__pycache__" | Remove-Item -Recurse -Force # 清理npm缓存 cd UI npm cache clean --force ``` **更新依赖**: ```powershell # 更新Python依赖 pip list --outdated pip install --upgrade pip # 更新Node.js依赖 cd UI npm outdated npm update ``` **备份重要数据**: ```powershell # 创建完整备份 $backupDate = Get-Date -Format "yyyyMMdd_HHmmss" New-Item -ItemType Directory -Force -Path "backup\full_$backupDate" # 备份数据文件 Copy-Item "pharmacy_sales_multi_store.csv" "backup\full_$backupDate\" Copy-Item "prediction_history.db" "backup\full_$backupDate\" # 备份模型文件 Copy-Item "saved_models\*" "backup\full_$backupDate\models\" -Recurse ``` ## 📞 技术支持 如果遇到问题,请按以下顺序排查: 1. **检查环境**:确认Python和Node.js版本正确 2. **检查依赖**:确认所有依赖都已安装 3. **检查文件**:确认数据文件存在且格式正确 4. **查看日志**:检查控制台输出和日志文件 5. **重启服务**:尝试重启API服务器和前端服务 **日志收集**: ```powershell # 收集系统信息 Write-Host "=== 系统信息 ===" Get-ComputerInfo | Select-Object WindowsProductName, WindowsVersion, TotalPhysicalMemory Write-Host "`n=== Python环境 ===" python --version pip list Write-Host "`n=== Node.js环境 ===" node --version npm --version Write-Host "`n=== 文件检查 ===" Test-Path "pharmacy_sales_multi_store.csv" Test-Path "prediction_history.db" Test-Path "server\api.py" ``` --- **注意**: 本指南专为Windows PowerShell环境设计,所有命令都已在Windows 10/11上测试通过。