commit 865c93c811b18715acf1163b81b3ac6962be7d1c Author: Tian jianyong <11429339@qq.com> Date: Fri Nov 8 23:43:57 2024 +0800 增加一些文件 diff --git a/.cursorrules b/.cursorrules new file mode 100644 index 0000000..f9d19c8 --- /dev/null +++ b/.cursorrules @@ -0,0 +1,82 @@ +# 开发流程 + +First ensure basic functionality works +Implement core functionality using the simplest direct approach +Ensure data flow is working correctly +Verify results are accurate +Then gradually add additional features +Add error handling +Add data validation +Add format conversion +Add logging +Improve user experience +Avoid premature optimization +Don't do complex data validation at the start +Don't worry about performance optimization early +Don't over-engineer +This development flow: +Quickly validates if core functionality works +Identifies and fixes fundamental issues early +Avoids wasting time on unnecessary optimizations +Makes code easier to maintain and debug +These principles should guide all code responses, focusing on getting the basics working first before adding complexity. + +# 代码修改最佳实践 + +1. 修改前的准备 + +- 检查相关文件和依赖关系 +- 确保命名一致性 +- 添加必要的日志记录 +- 准备回滚方案 + +2. 修改过程中 + +- 遵循统一的代码风格 +- 添加适当的错误处理 +- 保持代码的可读性 +- 避免重复代码 + +3. 修改后的验证 + +- 验证主要功能 +- 测试边界条件 +- 检查错误处理 +- 验证性能影响 + +4. 文档更新 + +- 更新相关文档 +- 添加注释说明 +- 记录重要修改 +- 更新调试信息 + +5. 代码审查要点 + +- 检查命名规范 +- 验证错误处理 +- 确认日志完整性 +- 评估代码质量 + +6. 调试建议 + +- 添加详细日志 +- 使用断点调试 +- 验证数据流 +- 检查状态变化 + +7. 性能考虑 + +- 避免过早优化 +- 关注关键路径 +- 合理使用缓存 +- 优化数据库查询 + +8. 安全性检查 + +- 验证输入数据 +- 处理异常情况 +- 保护敏感信息 +- 添加访问控制 + +These practices help maintain code quality and reduce potential issues. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e04c50 --- /dev/null +++ b/.gitignore @@ -0,0 +1,27 @@ +.DS_Store +node_modules +/dist +/models +/logs +/uploads +/data + +# local env files +.env.local +.env.*.local + +# Log files +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* + +# Editor directories and files +.idea +.vscode +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? +/frontend/node_modules diff --git a/README.md b/README.md new file mode 100644 index 0000000..b04ef07 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# 数据库配置说明 + +本系统使用 MySQL 8.0+ 作为数据库。在安装 MySQL 后,需要: + +1. 创建数据库用户 + +```sql +CREATE USER 'equipment_user'@'localhost' IDENTIFIED BY 'your_password'; +GRANT ALL PRIVILEGES ON equipment_cost_db.* TO 'equipment_user'@'localhost'; +FLUSH PRIVILEGES; +``` + +2. 配置数据库字符集 +确保 MySQL 配置文件(my.cnf 或 my.ini)包含以下设置: + +```ini +[mysqld] +character-set-server=utf8mb4 +collation-server=utf8mb4_unicode_ci + +[client] +default-character-set=utf8mb4 +``` diff --git a/app.py b/app.py new file mode 100644 index 0000000..da4dbaf --- /dev/null +++ b/app.py @@ -0,0 +1,8 @@ +import logging + +# 配置日志 +logging.basicConfig( + filename='logs/api.log', + level=logging.INFO, + format='%(asctime)s - %(levelname)s - %(message)s' +) \ No newline at end of file diff --git a/config.py b/config.py new file mode 100644 index 0000000..ee008e5 --- /dev/null +++ b/config.py @@ -0,0 +1,32 @@ +import os +import secrets + +# 数据库配置 +DATABASE_URI = "mysql+pymysql://root:123456@localhost:3306/equipment_cost_db" + +# 安全密钥配置(自动生成随机密钥) +SECRET_KEY = secrets.token_hex(16) + +# 环境配置 +DEBUG = True +ENV = 'development' + +# 文件上传配置 +UPLOAD_FOLDER = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'uploads') +ALLOWED_EXTENSIONS = {'csv', 'xlsx', 'xls', 'json'} +MAX_CONTENT_LENGTH = 16 * 1024 * 1024 # 16MB 最大上传限制 + +# API配置 +API_VERSION = 'v1' +API_PREFIX = f'/api/{API_VERSION}' + +# 跨域配置 +CORS_ORIGINS = [ + "http://localhost:8080", + "http://127.0.0.1:8080", +] + +# 日志配置 +LOG_LEVEL = 'DEBUG' +LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' +LOG_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'logs/app.log') \ No newline at end of file diff --git a/data/.DS_Store b/data/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/data/.DS_Store differ diff --git a/data/equipment_data_20241108.xlsx b/data/equipment_data_20241108.xlsx new file mode 100644 index 0000000..c84b6cc Binary files /dev/null and b/data/equipment_data_20241108.xlsx differ diff --git a/data/equipment_data_20241108_training.xlsx b/data/equipment_data_20241108_training.xlsx new file mode 100644 index 0000000..30f7dcf Binary files /dev/null and b/data/equipment_data_20241108_training.xlsx differ diff --git a/data/equipment_data_20241108_verify.xlsx b/data/equipment_data_20241108_verify.xlsx new file mode 100644 index 0000000..92a527a Binary files /dev/null and b/data/equipment_data_20241108_verify.xlsx differ diff --git a/docs/debug.md b/docs/debug.md new file mode 100644 index 0000000..0d78604 --- /dev/null +++ b/docs/debug.md @@ -0,0 +1,616 @@ +# 调试记录 + +## 特殊参数显示问题 + +### 问题描述 + +在数据管理页面中,装备详情对话框的特殊参数部分显示为空行或不显示。 + +### 调试步骤 + +1. 后端数据查询 + +```sql +# 测试特殊参数查询 +SELECT equipment_id, param_name, param_value, param_unit +FROM custom_params +WHERE param_name IS NOT NULL +AND param_value IS NOT NULL +LIMIT 5 +``` + +2. 日志记录 + +```python +logging.info(f"Getting details for equipment ID: {id}") +logging.info(f"Equipment type: {equipment_type}") +logging.info(f"Found equipment details: {result['name']}") +logging.info(f"Custom params: {result.get('custom_params')}") +``` + +3. 前端调试 + +```javascript +console.log('Requesting details for row:', row) +console.log('Details response:', response.data) +console.log('Custom params:', response.data.custom_params) +console.log('Selected data:', selectedData.value) +``` + +### 关键发现 + +1. 数据库查询 + +- 特殊参数表中有数据 +- JSON_ARRAYAGG 返回的格式需要处理 +- 需要过滤掉 NULL 值 + +2. 数据格式 + +- 后端返回的特殊参数是 JSON 字符串 +- 需要在前端解析为数组 +- 确保数组不为空 + +3. 前端渲染 + +- 条件判断需要更严格 +- 需要确保数据类型正确 +- 需要正确格式化显示值 + +### 解决方案 + +1. 后端查询优化 + +```sql +( + SELECT JSON_ARRAYAGG( + JSON_OBJECT( + 'id', csp.id, + 'param_name', csp.param_name, + 'param_value', csp.param_value, + 'param_unit', csp.param_unit, + 'description', csp.description + ) + ) + FROM custom_params csp + WHERE csp.equipment_id = e.id + AND csp.param_name IS NOT NULL + AND csp.param_value IS NOT NULL +) as custom_params +``` + +2. 前端数据处理 + +```javascript +// 确保 custom_params 是数组 +if (typeof response.data.custom_params === 'string') { + response.data.custom_params = JSON.parse(response.data.custom_params) +} +``` + +3. 渲染条件优化 + +```vue +