提交文件

This commit is contained in:
2026-02-28 15:16:15 +08:00
parent 1a4e50e0a4
commit 44f250f58e
159 changed files with 61268 additions and 0 deletions

View File

@@ -0,0 +1,433 @@
# 系统用户表
<cite>
**本文档引用的文件**
- [models.py](file://backend/app/models/models.py)
- [auth.py](file://backend/app/api/v1/auth.py)
- [security.py](file://backend/app/core/security.py)
- [schemas.py](file://backend/app/schemas/schemas.py)
- [config.py](file://backend/app/core/config.py)
- [001_initial.py](file://backend/alembic/versions/001_initial.py)
- [init_db.py](file://backend/app/core/init_db.py)
- [database.md](file://docs/database.md)
</cite>
## 目录
1. [简介](#简介)
2. [项目结构](#项目结构)
3. [核心组件](#核心组件)
4. [架构概览](#架构概览)
5. [详细组件分析](#详细组件分析)
6. [依赖关系分析](#依赖关系分析)
7. [性能考虑](#性能考虑)
8. [故障排除指南](#故障排除指南)
9. [结论](#结论)
## 简介
系统用户表(users)是医院绩效考核管理系统的核心数据表,负责存储系统用户的认证和授权信息。该表实现了完整的用户身份管理功能,包括用户名密码管理、角色权限控制、员工关联关系、状态管理和登录认证流程。
本系统采用现代化的安全架构使用bcrypt进行密码哈希加密JWT令牌进行无状态认证并通过角色权限体系实现细粒度的访问控制。用户表与员工表建立了关联关系支持将系统用户与实际员工身份进行绑定。
## 项目结构
系统用户表位于后端应用的数据模型层采用SQLAlchemy ORM框架进行数据库映射。整个用户管理系统由以下核心组件构成
```mermaid
graph TB
subgraph "数据模型层"
User[User模型]
Staff[Staff模型]
Department[Department模型]
end
subgraph "认证服务层"
Security[Security模块]
AuthAPI[Auth API路由]
Schemas[Schemas定义]
end
subgraph "配置管理层"
Config[Config配置]
InitDB[初始化脚本]
end
User --> Security
AuthAPI --> Security
Security --> Config
InitDB --> User
User --> Staff
Staff --> Department
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L244-L261)
- [security.py](file://backend/app/core/security.py#L1-L110)
- [auth.py](file://backend/app/api/v1/auth.py#L1-L74)
**章节来源**
- [models.py](file://backend/app/models/models.py#L244-L261)
- [auth.py](file://backend/app/api/v1/auth.py#L1-L74)
- [security.py](file://backend/app/core/security.py#L1-L110)
## 核心组件
### 用户表结构定义
系统用户表(users)采用完整的字段定义,确保用户管理的完整性和安全性:
| 字段名 | 类型 | 约束 | 描述 | 默认值 |
|--------|------|------|------|--------|
| id | Integer | 主键, 自增 | 用户唯一标识符 | 无 |
| username | String(50) | 非空, 唯一 | 用户登录名 | 无 |
| password_hash | String(255) | 非空 | 密码哈希值 | 无 |
| staff_id | Integer | 外键, 可空 | 关联员工ID | NULL |
| role | String(20) | 非空 | 用户角色 | "staff" |
| is_active | Boolean | 非空 | 账户启用状态 | TRUE |
| last_login | DateTime | 可空 | 最后登录时间 | NULL |
| created_at | DateTime | 非空 | 创建时间 | 当前时间 |
| updated_at | DateTime | 非空 | 更新时间 | 当前时间 |
### 角色权限体系
系统实现了三层角色权限控制机制:
```mermaid
graph LR
subgraph "用户角色层次"
Admin[管理员<br/>admin]
Manager[经理<br/>manager]
Staff[普通员工<br/>staff]
end
subgraph "权限级别"
Level1[完全访问权限]
Level2[部分管理权限]
Level3[基础操作权限]
end
Admin --> Level1
Manager --> Level2
Staff --> Level3
Level1 --> Admin
Level2 --> Manager
Level3 --> Staff
```
**图表来源**
- [schemas.py](file://backend/app/schemas/schemas.py#L321-L327)
- [security.py](file://backend/app/core/security.py#L94-L109)
**章节来源**
- [schemas.py](file://backend/app/schemas/schemas.py#L321-L327)
- [security.py](file://backend/app/core/security.py#L94-L109)
## 架构概览
系统用户管理采用分层架构设计,确保安全性和可维护性:
```mermaid
sequenceDiagram
participant Client as 客户端
participant AuthAPI as 认证API
participant Security as 安全模块
participant DB as 数据库
participant JWT as JWT服务
Client->>AuthAPI : POST /auth/login
AuthAPI->>DB : 查询用户信息
DB-->>AuthAPI : 返回用户记录
AuthAPI->>Security : 验证密码
Security->>Security : bcrypt校验
Security-->>AuthAPI : 验证结果
AuthAPI->>JWT : 创建访问令牌
JWT-->>AuthAPI : 返回JWT令牌
AuthAPI-->>Client : 返回访问令牌
Note over Client,JWT : 用户认证完成
```
**图表来源**
- [auth.py](file://backend/app/api/v1/auth.py#L17-L37)
- [security.py](file://backend/app/core/security.py#L24-L43)
## 详细组件分析
### 用户模型类分析
用户模型类实现了完整的用户实体定义,包含字段映射、约束条件和索引配置:
```mermaid
classDiagram
class User {
+int id
+string username
+string password_hash
+int staff_id
+string role
+boolean is_active
+datetime last_login
+datetime created_at
+datetime updated_at
+__tablename__ = "users"
+__table_args__
+Index(idx_user_username)
+ForeignKey(staff_id -> staff.id)
}
class Staff {
+int id
+string employee_id
+string name
+int department_id
+string position
+string title
+float base_salary
+float performance_ratio
+string status
+datetime hire_date
+datetime created_at
+datetime updated_at
}
User --> Staff : "关联员工"
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L244-L261)
- [models.py](file://backend/app/models/models.py#L88-L114)
### 登录认证流程
系统采用OAuth2密码模式结合JWT令牌的认证机制
```mermaid
flowchart TD
Start([用户发起登录]) --> ValidateInput["验证用户名密码格式"]
ValidateInput --> InputValid{"输入有效?"}
InputValid --> |否| ReturnError["返回错误信息"]
InputValid --> |是| QueryUser["查询用户信息"]
QueryUser --> UserExists{"用户存在?"}
UserExists --> |否| ReturnError
UserExists --> |是| VerifyPassword["验证密码"]
VerifyPassword --> PasswordValid{"密码正确?"}
PasswordValid --> |否| ReturnError
PasswordValid --> |是| CheckActive["检查账户状态"]
CheckActive --> IsActive{"账户启用?"}
IsActive --> |否| ReturnDisabled["账户已禁用"]
IsActive --> |是| CreateToken["创建JWT访问令牌"]
CreateToken --> SetLastLogin["更新最后登录时间"]
SetLastLogin --> ReturnToken["返回访问令牌"]
ReturnError --> End([结束])
ReturnDisabled --> End
ReturnToken --> End
```
**图表来源**
- [auth.py](file://backend/app/api/v1/auth.py#L17-L37)
- [security.py](file://backend/app/core/security.py#L24-L31)
### 密码安全管理
系统采用bcrypt算法进行密码哈希处理确保密码存储的安全性
| 安全特性 | 实现方式 | 说明 |
|----------|----------|------|
| 密码哈希 | bcrypt | 使用bcrypt.gensalt()生成盐值 |
| 哈希验证 | bcrypt.checkpw() | 验证原始密码与哈希值匹配 |
| 令牌加密 | HS256算法 | 使用SECRET_KEY进行JWT签名 |
| 过期时间 | 8小时 | ACCESS_TOKEN_EXPIRE_MINUTES=480 |
| 传输安全 | HTTPS | 生产环境建议启用HTTPS |
**章节来源**
- [security.py](file://backend/app/core/security.py#L18-L43)
- [config.py](file://backend/app/core/config.py#L23-L26)
### 用户状态管理
系统实现了灵活的用户状态管理机制:
```mermaid
stateDiagram-v2
[*] --> Active : 创建用户时默认激活
Active --> Disabled : 管理员禁用账户
Disabled --> Active : 管理员重新激活
Active --> [*] : 账户删除
note right of Active
可正常登录
可访问系统功能
end note
note right of Disabled
无法登录系统
访问被拒绝
end note
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L252-L254)
- [security.py](file://backend/app/core/security.py#L85-L91)
**章节来源**
- [models.py](file://backend/app/models/models.py#L252-L254)
- [security.py](file://backend/app/core/security.py#L85-L91)
### 员工关联关系
用户表与员工表建立了可选的一对一关联关系:
```mermaid
erDiagram
STAFF {
int id PK
string employee_id UK
string name
int department_id FK
string position
string title
float base_salary
float performance_ratio
string status
datetime hire_date
datetime created_at
datetime updated_at
}
USERS {
int id PK
string username UK
string password_hash
int staff_id FK
string role
boolean is_active
datetime last_login
datetime created_at
datetime updated_at
}
STAFF ||--o| USERS : "关联"
note for STAFF "员工表"
note for USERS "用户表"
note for USERS "staff_id -> staff.id"
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L88-L114)
- [models.py](file://backend/app/models/models.py#L244-L261)
**章节来源**
- [models.py](file://backend/app/models/models.py#L88-L114)
- [models.py](file://backend/app/models/models.py#L244-L261)
## 依赖关系分析
系统用户管理模块的依赖关系如下:
```mermaid
graph TB
subgraph "外部依赖"
SQLAlchemy[SQLAlchemy ORM]
Bcrypt[bcrypt加密库]
JWT[jose JWT库]
FastAPI[FastAPI框架]
end
subgraph "内部模块"
UserModel[User模型]
SecurityModule[安全模块]
AuthAPI[认证API]
ConfigModule[配置模块]
end
subgraph "数据库层"
UsersTable[users表]
StaffTable[staff表]
end
SQLAlchemy --> UserModel
Bcrypt --> SecurityModule
JWT --> SecurityModule
FastAPI --> AuthAPI
UserModel --> UsersTable
UserModel --> StaffTable
SecurityModule --> UserModel
AuthAPI --> SecurityModule
ConfigModule --> SecurityModule
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L1-L13)
- [security.py](file://backend/app/core/security.py#L1-L16)
- [auth.py](file://backend/app/api/v1/auth.py#L1-L14)
**章节来源**
- [models.py](file://backend/app/models/models.py#L1-L13)
- [security.py](file://backend/app/core/security.py#L1-L16)
- [auth.py](file://backend/app/api/v1/auth.py#L1-L14)
## 性能考虑
### 数据库性能优化
1. **索引策略**
- 用户名唯一索引:支持快速用户名查找
- 复合索引:用于常用查询条件组合
2. **连接池配置**
- 数据库连接池大小20
- 最大溢出连接10
- 异步数据库连接:提升并发性能
3. **缓存策略**
- JWT令牌本地缓存减少数据库查询
- 用户权限缓存:避免重复权限验证
### 安全性能平衡
1. **密码哈希成本**
- bcrypt默认成本因子提供良好安全性
- 可根据硬件性能调整成本因子
2. **令牌过期策略**
- 8小时有效期平衡安全性与用户体验
- 可配置的过期时间
## 故障排除指南
### 常见问题及解决方案
| 问题类型 | 症状 | 可能原因 | 解决方案 |
|----------|------|----------|----------|
| 登录失败 | 401未授权错误 | 用户名或密码错误 | 检查用户名密码,确认大小写 |
| 账户禁用 | 403禁止访问 | 用户被管理员禁用 | 联系管理员重新激活 |
| 密码错误 | 验证失败 | bcrypt哈希不匹配 | 确认密码正确性 |
| 令牌过期 | 401未授权 | JWT过期 | 重新登录获取新令牌 |
| 数据库连接 | 连接超时 | 连接池耗尽 | 增加连接池大小 |
### 调试工具和方法
1. **日志分析**
- 查看认证API日志
- 监控数据库查询性能
- 跟踪JWT令牌生命周期
2. **数据库检查**
```sql
-- 检查用户表结构
SELECT * FROM users LIMIT 1;
-- 验证用户存在性
SELECT COUNT(*) FROM users WHERE username = 'admin';
-- 检查密码哈希
SELECT username, password_hash FROM users WHERE username = 'admin';
```
3. **配置验证**
- 检查SECRET_KEY配置
- 验证ACCESS_TOKEN_EXPIRE_MINUTES设置
- 确认CORS配置正确
**章节来源**
- [auth.py](file://backend/app/api/v1/auth.py#L30-L34)
- [security.py](file://backend/app/core/security.py#L55-L82)
## 结论
系统用户表(users)作为医院绩效考核管理系统的核心组件实现了完整的用户身份管理功能。通过采用bcrypt密码哈希、JWT令牌认证和三层角色权限控制系统确保了用户管理的安全性和灵活性。
用户表的设计充分考虑了医院管理的实际需求,支持与员工表的关联管理,实现了用户身份与实际工作职责的绑定。同时,系统的异步数据库连接和连接池配置保证了良好的性能表现。
建议在生产环境中:
1. 定期更新SECRET_KEY并妥善保管
2. 配置HTTPS确保传输安全
3. 设置合理的密码复杂度要求
4. 定期审查用户权限分配
5. 监控用户登录活动日志
通过这些措施,可以确保系统用户管理的安全性和可靠性,为医院绩效管理提供坚实的技术支撑。

View File

@@ -0,0 +1,394 @@
# 系统菜单表
<cite>
**本文档引用的文件**
- [models.py](file://backend/app/models/models.py)
- [menus.py](file://backend/app/api/v1/menus.py)
- [menu_service.py](file://backend/app/services/menu_service.py)
- [schemas.py](file://backend/app/schemas/schemas.py)
- [Menus.vue](file://frontend/src/views/system/Menus.vue)
- [menu.js](file://frontend/src/api/menu.js)
- [security.py](file://backend/app/core/security.py)
- [config.py](file://backend/app/core/config.py)
- [001_initial.py](file://backend/alembic/versions/001_initial.py)
- [002_template.py](file://backend/alembic/versions/002_template.py)
- [create_menu_tables.py](file://backend/create_menu_tables.py)
</cite>
## 目录
1. [简介](#简介)
2. [项目结构](#项目结构)
3. [核心组件](#核心组件)
4. [架构概览](#架构概览)
5. [详细组件分析](#详细组件分析)
6. [依赖关系分析](#依赖关系分析)
7. [性能考虑](#性能考虑)
8. [故障排除指南](#故障排除指南)
9. [结论](#结论)
## 简介
系统菜单表(menus)是医院绩效考核管理系统的核心导航组件,负责管理整个系统的菜单结构、权限控制和用户界面导航。该表采用自引用的树形结构设计,支持多层级菜单组织,并通过权限标识实现细粒度的访问控制。
本系统采用前后端分离架构后端使用FastAPI + SQLAlchemy前端使用Vue.js + Element Plus实现了完整的菜单管理功能包括菜单树形展示、权限控制、动态路由生成等特性。
## 项目结构
系统菜单表位于后端应用的models模块中与API接口、服务层和前端界面形成完整的菜单管理生态系统
```mermaid
graph TB
subgraph "后端架构"
Models[数据模型层<br/>models.py]
API[API接口层<br/>menus.py]
Service[服务层<br/>menu_service.py]
Schemas[数据模式<br/>schemas.py]
Security[安全认证<br/>security.py]
end
subgraph "前端架构"
Vue[Vue组件<br/>Menus.vue]
Api[API调用<br/>menu.js]
end
subgraph "数据库层"
DB[(PostgreSQL数据库)]
MenusTable[menus表]
end
Vue --> Api
Api --> API
API --> Service
Service --> Models
Models --> DB
DB --> MenusTable
Security --> API
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L347-L373)
- [menus.py](file://backend/app/api/v1/menus.py#L1-L164)
- [menu_service.py](file://backend/app/services/menu_service.py#L1-L137)
**章节来源**
- [models.py](file://backend/app/models/models.py#L347-L373)
- [menus.py](file://backend/app/api/v1/menus.py#L1-L164)
- [menu_service.py](file://backend/app/services/menu_service.py#L1-L137)
## 核心组件
### 数据模型定义
系统菜单表采用SQLAlchemy ORM映射定义了完整的菜单结构和业务规则
| 字段名称 | 数据类型 | 约束条件 | 描述 | 默认值 |
|---------|---------|---------|------|--------|
| id | Integer | 主键, 自增 | 菜单唯一标识符 | - |
| parent_id | Integer | 外键(menus.id), 可空 | 父菜单ID | NULL |
| menu_type | Enum | 必填, 菜单类型 | 菜单类型(MENU/BUTTON) | MENU |
| menu_name | String(100) | 必填, 非空 | 菜单显示名称 | - |
| menu_icon | String(50) | 可空 | Element Plus图标名称 | NULL |
| path | String(200) | 必填, 非空 | Vue Router路由路径 | - |
| component | String(200) | 可空 | 组件路径 | NULL |
| permission | String(100) | 可空 | 权限标识符 | NULL |
| sort_order | Integer | 必填, ≥0 | 排序权重 | 0 |
| is_visible | Boolean | 必填 | 是否对用户可见 | TRUE |
| is_active | Boolean | 必填 | 是否启用 | TRUE |
| created_at | DateTime | 必填 | 创建时间 | 当前时间 |
| updated_at | DateTime | 必填 | 更新时间 | 当前时间 |
### 菜单类型定义
系统支持两种菜单类型,用于不同的导航需求:
```mermaid
classDiagram
class MenuType {
<<enumeration>>
MENU : "menu"
BUTTON : "button"
}
class Menu {
+int id
+int parent_id
+MenuType menu_type
+string menu_name
+string menu_icon
+string path
+string component
+string permission
+int sort_order
+bool is_visible
+bool is_active
+datetime created_at
+datetime updated_at
}
Menu --> MenuType : uses
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L341-L345)
- [models.py](file://backend/app/models/models.py#L347-L373)
**章节来源**
- [models.py](file://backend/app/models/models.py#L341-L373)
## 架构概览
系统采用分层架构设计,确保菜单管理功能的可维护性和扩展性:
```mermaid
sequenceDiagram
participant Frontend as 前端界面
participant API as API接口
participant Service as 服务层
participant Model as 数据模型
participant DB as 数据库
Frontend->>API : GET /menus/tree
API->>Service : get_tree(visible_only)
Service->>Model : 查询菜单树
Model->>DB : SQL查询
DB-->>Model : 返回结果集
Model-->>Service : 菜单对象列表
Service->>Service : 转换为字典格式
Service-->>API : 菜单树数据
API-->>Frontend : JSON响应
Note over Frontend,DB : 用户权限验证由安全中间件处理
```
**图表来源**
- [menus.py](file://backend/app/api/v1/menus.py#L17-L29)
- [menu_service.py](file://backend/app/services/menu_service.py#L16-L29)
**章节来源**
- [menus.py](file://backend/app/api/v1/menus.py#L1-L164)
- [menu_service.py](file://backend/app/services/menu_service.py#L1-L137)
## 详细组件分析
### 后端API接口
系统提供了完整的菜单管理RESTful API支持CRUD操作和菜单树查询
#### 菜单树查询接口
```mermaid
flowchart TD
GetTree[GET /menus/tree] --> ValidateParams[验证请求参数]
ValidateParams --> CheckPermissions[检查用户权限]
CheckPermissions --> BuildQuery[构建查询条件]
BuildQuery --> ApplyFilters[应用过滤器]
ApplyFilters --> ExecuteQuery[执行数据库查询]
ExecuteQuery --> TransformData[转换数据格式]
TransformData --> ReturnResponse[返回响应]
CheckPermissions --> |用户未认证| Unauthorized[401 未授权]
CheckPermissions --> |用户被禁用| Forbidden[403 禁止访问]
```
**图表来源**
- [menus.py](file://backend/app/api/v1/menus.py#L17-L29)
- [security.py](file://backend/app/core/security.py#L85-L91)
#### 菜单管理操作
系统支持以下主要操作:
- **创建菜单**: POST `/menus` - 需要管理员或经理权限
- **更新菜单**: PUT `/menus/{menu_id}` - 需要管理员或经理权限
- **删除菜单**: DELETE `/menus/{menu_id}` - 需要管理员或经理权限
- **初始化菜单**: POST `/menus/init` - 需要管理员权限
**章节来源**
- [menus.py](file://backend/app/api/v1/menus.py#L98-L164)
### 服务层实现
服务层负责业务逻辑处理和数据访问:
```mermaid
classDiagram
class MenuService {
+get_tree(db, visible_only) List[Dict]
+get_list(db, menu_type, is_visible) List[Menu]
+get_by_id(db, menu_id) Menu
+create(db, menu_data) Menu
+update(db, menu_id, menu_data) Menu
+delete(db, menu_id) bool
+init_default_menus(db) void
-_menu_to_dict(menu) Dict
}
class MenuRepository {
+find_all() List[Menu]
+find_by_id(id) Menu
+save(menu) Menu
+delete(menu) void
}
MenuService --> MenuRepository : uses
```
**图表来源**
- [menu_service.py](file://backend/app/services/menu_service.py#L12-L137)
**章节来源**
- [menu_service.py](file://backend/app/services/menu_service.py#L1-L137)
### 前端界面实现
前端使用Vue.js实现菜单管理界面提供直观的可视化操作
#### 菜单管理界面功能
- **菜单树形展示**: 使用Element Plus Tree组件展示层级关系
- **表单编辑**: 支持菜单属性的增删改查
- **权限控制**: 基于用户角色的界面元素显示
- **实时预览**: 修改后的菜单效果即时反映
**章节来源**
- [Menus.vue](file://frontend/src/views/system/Menus.vue#L1-L265)
### 权限控制系统
系统通过多种机制实现菜单权限控制:
```mermaid
flowchart LR
subgraph "权限层次"
Role[用户角色<br/>admin/manager/staff]
Permission[权限标识<br/>system:menu:*]
Menu[菜单权限<br/>menu.permission]
end
subgraph "验证流程"
Request[请求到达]
CheckRole[检查角色]
CheckPermission[检查权限]
CheckMenu[检查菜单权限]
Allow[允许访问]
Deny[拒绝访问]
end
Request --> CheckRole
CheckRole --> CheckPermission
CheckPermission --> CheckMenu
CheckMenu --> |满足条件| Allow
CheckMenu --> |不满足| Deny
```
**图表来源**
- [security.py](file://backend/app/core/security.py#L94-L109)
- [models.py](file://backend/app/models/models.py#L358-L358)
**章节来源**
- [security.py](file://backend/app/core/security.py#L1-L110)
- [schemas.py](file://backend/app/schemas/schemas.py#L584-L638)
## 依赖关系分析
系统菜单表与其他组件的依赖关系如下:
```mermaid
graph TB
subgraph "核心依赖"
MenuModel[Menu模型<br/>models.py]
MenuAPI[菜单API<br/>menus.py]
MenuService[菜单服务<br/>menu_service.py]
MenuSchema[菜单模式<br/>schemas.py]
end
subgraph "安全依赖"
Security[安全模块<br/>security.py]
User[用户模型<br/>models.py]
end
subgraph "前端依赖"
MenuView[菜单视图<br/>Menus.vue]
MenuAPIJS[菜单API<br/>menu.js]
end
subgraph "数据库依赖"
Database[(PostgreSQL)]
Migration[数据库迁移<br/>alembic]
end
MenuAPI --> MenuService
MenuService --> MenuModel
MenuAPI --> Security
Security --> User
MenuView --> MenuAPIJS
MenuAPIJS --> MenuAPI
MenuModel --> Database
Migration --> Database
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L347-L373)
- [menus.py](file://backend/app/api/v1/menus.py#L1-L164)
- [menu_service.py](file://backend/app/services/menu_service.py#L1-L137)
**章节来源**
- [models.py](file://backend/app/models/models.py#L1-L438)
- [001_initial.py](file://backend/alembic/versions/001_initial.py#L1-L183)
## 性能考虑
### 查询优化策略
1. **索引设计**: 菜单表建立了多个复合索引以优化查询性能
2. **懒加载**: 使用selectinload优化N+1查询问题
3. **条件过滤**: 支持按类型和可见性进行快速过滤
4. **缓存策略**: 可考虑在应用层实现菜单树缓存
### 数据库设计优化
```mermaid
erDiagram
MENUS {
integer id PK
integer parent_id FK
string menu_type
string menu_name
string menu_icon
string path
string component
string permission
integer sort_order
boolean is_visible
boolean is_active
datetime created_at
datetime updated_at
}
MENUS }o--|| MENUS : "parent_id -> id"
INDEXES {
idx_menu_parent parent_id
idx_menu_type menu_type
idx_menu_visible is_visible
}
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L368-L372)
**章节来源**
- [models.py](file://backend/app/models/models.py#L368-L372)
## 故障排除指南
### 常见问题及解决方案
#### 菜单权限问题
- **问题**: 用户无法看到某些菜单
- **原因**: 菜单is_visible=false或用户权限不足
- **解决**: 检查菜单的is_visible状态和用户的role字段
#### 菜单树加载失败
- **问题**: 菜单树无法正常显示
- **原因**: 数据库连接问题或查询条件错误
- **解决**: 检查数据库连接配置和索引完整性
#### 权限验证失败
- **问题**: API请求返回403错误
- **原因**: 用户角色不是admin或manager
- **解决**: 确认用户角色配置和权限分配
**章节来源**
- [security.py](file://backend/app/core/security.py#L94-L109)
- [menus.py](file://backend/app/api/v1/menus.py#L98-L164)
## 结论
系统菜单表设计合理,实现了完整的菜单管理功能。通过清晰的分层架构、完善的权限控制和良好的扩展性,为医院绩效考核管理系统提供了可靠的导航基础。
关键优势包括:
- **灵活的树形结构**: 支持任意层级的菜单组织
- **细粒度权限控制**: 通过权限标识实现精确的访问控制
- **前后端协同**: 前端提供直观的操作界面,后端保证数据一致性
- **可扩展性**: 易于添加新的菜单类型和权限规则
建议在生产环境中重点关注数据库性能优化和权限配置的安全性,确保系统的稳定运行。

View File

@@ -0,0 +1,388 @@
# 绩效计划表
<cite>
**本文档引用的文件**
- [models.py](file://backend/app/models/models.py)
- [schemas.py](file://backend/app/schemas/schemas.py)
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py)
- [performance_plan.js](file://frontend/src/api/performance_plan.js)
- [Plans.vue](file://frontend/src/views/plan/Plans.vue)
- [create_plan_tables.py](file://backend/create_plan_tables.py)
- [详细设计.md](file://docs/详细设计.md)
</cite>
## 目录
1. [简介](#简介)
2. [项目结构](#项目结构)
3. [核心组件](#核心组件)
4. [架构总览](#架构总览)
5. [详细组件分析](#详细组件分析)
6. [依赖关系分析](#依赖关系分析)
7. [性能考虑](#性能考虑)
8. [故障排查指南](#故障排查指南)
9. [结论](#结论)
10. [附录](#附录)
## 简介
本文件面向“绩效计划表”(performance_plans)的完整设计与实现,涵盖字段定义、层级设计理念、状态流转机制、审批流程、版本控制与激活管理、以及与科室、员工、用户的关联关系。同时提供创建、审批、执行的最佳实践与流程规范,帮助读者快速理解并正确使用该模块。
## 项目结构
后端采用分层架构:
- 数据模型层:定义实体与关系
- 模式层:定义请求/响应数据结构
- API 层:暴露 REST 接口
- 服务层:封装业务逻辑
- 前端:调用 API 并呈现交互
```mermaid
graph TB
subgraph "前端"
FE_API["performance_plan.js<br/>前端API封装"]
FE_VIEW["Plans.vue<br/>计划管理视图"]
end
subgraph "后端"
API["performance_plans.py<br/>API路由"]
SVC["performance_plan_service.py<br/>服务层"]
MODELS["models.py<br/>数据模型"]
SCHEMAS["schemas.py<br/>数据模式"]
end
FE_API --> API
FE_VIEW --> FE_API
API --> SVC
SVC --> MODELS
API --> SCHEMAS
SVC --> SCHEMAS
```
图表来源
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L1-L310)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L1-L342)
- [models.py](file://backend/app/models/models.py#L270-L338)
- [schemas.py](file://backend/app/schemas/schemas.py#L519-L570)
- [performance_plan.js](file://frontend/src/api/performance_plan.js#L1-L67)
- [Plans.vue](file://frontend/src/views/plan/Plans.vue#L1-L742)
章节来源
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L1-L310)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L1-L342)
- [models.py](file://backend/app/models/models.py#L270-L338)
- [schemas.py](file://backend/app/schemas/schemas.py#L519-L570)
- [performance_plan.js](file://frontend/src/api/performance_plan.js#L1-L67)
- [Plans.vue](file://frontend/src/views/plan/Plans.vue#L1-L742)
## 核心组件
- 绩效计划表:存储计划基本信息、层级、年度/月份、状态、审批信息、版本与激活状态等
- 计划指标关联表:将计划与指标绑定,记录目标值、权重、评分方法与参数
- 相关枚举:计划层级、计划状态、用户、科室类型等
- API/服务:提供 CRUD、提交、审批、激活、树形结构、统计等能力
- 前端:提供列表、树形、统计卡片、表单编辑与操作按钮
章节来源
- [models.py](file://backend/app/models/models.py#L270-L338)
- [schemas.py](file://backend/app/schemas/schemas.py#L519-L570)
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L1-L310)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L1-L342)
- [performance_plan.js](file://frontend/src/api/performance_plan.js#L1-L67)
- [Plans.vue](file://frontend/src/views/plan/Plans.vue#L1-L742)
## 架构总览
系统围绕“计划-指标-执行-结果”的闭环展开,前端通过 API 进行交互,后端服务层负责状态机与业务规则,模型层映射到数据库表。
```mermaid
sequenceDiagram
participant U as "用户"
participant FE as "前端"
participant API as "API路由"
participant SVC as "服务层"
participant DB as "数据库"
U->>FE : 打开计划管理页面
FE->>API : GET /plans 列表查询
API->>SVC : get_list()
SVC->>DB : 查询计划+关联
DB-->>SVC : 计划列表
SVC-->>API : 返回数据
API-->>FE : 渲染列表
U->>FE : 新建/编辑计划
FE->>API : POST/PUT /plans
API->>SVC : create/update
SVC->>DB : 写入计划/更新
DB-->>SVC : 提交成功
SVC-->>API : 返回结果
API-->>FE : 成功提示
U->>FE : 提交/审批/激活
FE->>API : POST /{id}/submit | approve | activate
API->>SVC : submit/approve/activate
SVC->>DB : 更新状态/时间/审批人
DB-->>SVC : 提交成功
SVC-->>API : 返回结果
API-->>FE : 成功提示
```
图表来源
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L161-L241)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L131-L182)
- [performance_plan.js](file://frontend/src/api/performance_plan.js#L24-L46)
- [Plans.vue](file://frontend/src/views/plan/Plans.vue#L567-L608)
## 详细组件分析
### 数据模型与字段定义
- 绩效计划表字段
- 计划名称、编码、层级(hospital/department/individual)、年度、月份(月度计划可选)、计划类型(annual/monthly)
- 所属科室、责任人、上级计划(父子关系)
- 描述、战略目标、关键举措
- 状态(draft/pending/approved/rejected/active/completed/cancelled)
- 提交人、提交时间、审批人、审批时间、审批意见
- 版本号、激活状态、创建/更新时间
- 计划指标关联表字段
- 计划ID、指标ID、目标值、目标单位、权重、评分方法、评分参数(JSON)、备注
- 关联关系
- 计划与科室、员工、用户(提交人/审批人)的多对一
- 计划与指标的多对多(通过关联表)
- 计划的父子层级(自关联)
```mermaid
erDiagram
PERFORMANCE_PLANS {
int id PK
string plan_name
string plan_code UK
enum plan_level
int plan_year
int plan_month
string plan_type
int department_id FK
int staff_id FK
int parent_plan_id FK
text description
text strategic_goals
text key_initiatives
enum status
int submitter_id FK
datetime submit_time
int approver_id FK
datetime approve_time
text approve_remark
int version
bool is_active
datetime created_at
datetime updated_at
}
PLAN_KPI_RELATIONS {
int id PK
int plan_id FK
int indicator_id FK
float target_value
string target_unit
float weight
string scoring_method
text scoring_params
text remark
datetime created_at
datetime updated_at
}
DEPARTMENTS ||--o{ PERFORMANCE_PLANS : "所属科室"
STAFF ||--o{ PERFORMANCE_PLANS : "责任人"
USERS ||--o{ PERFORMANCE_PLANS : "提交人/审批人"
PERFORMANCE_PLANS ||--o{ PLAN_KPI_RELATIONS : "包含"
INDICATORS ||--o{ PLAN_KPI_RELATIONS : "关联指标"
PERFORMANCE_PLANS ||--o{ PERFORMANCE_PLANS : "父子层级"
```
图表来源
- [models.py](file://backend/app/models/models.py#L270-L338)
章节来源
- [models.py](file://backend/app/models/models.py#L270-L338)
### 计划层级设计理念与使用场景
- 医院级:由院领导制定年度战略目标与关键举措,作为全院计划的顶层目标
- 科室级各科室依据医院级计划分解为本科室的KPI与目标值形成可执行的中期计划
- 个人级:员工依据科室计划制定个人绩效计划,承接关键举措与目标
- 层级关系:支持父子计划,便于逐级分解与汇总
章节来源
- [models.py](file://backend/app/models/models.py#L263-L268)
- [详细设计.md](file://docs/详细设计.md#L59-L67)
### 计划状态管理与流转机制
状态机如下:
- 草稿(draft) → 待审批(pending):提交
- 待审批(pending) → 已批准(approved)/已驳回(rejected):审批
- 已批准(approved) → 执行中(active):激活
- 执行中(active) → 已完成(completed)/已取消(cancelled):根据业务需要变更
```mermaid
stateDiagram-v2
[*] --> 草稿
草稿 --> 待审批 : "提交"
待审批 --> 已批准 : "审批通过"
待审批 --> 已驳回 : "审批驳回"
已批准 --> 执行中 : "激活"
执行中 --> 已完成 : "计划完成"
执行中 --> 已取消 : "计划取消"
```
图表来源
- [models.py](file://backend/app/models/models.py#L233-L241)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L131-L182)
章节来源
- [models.py](file://backend/app/models/models.py#L233-L241)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L131-L182)
### 计划与科室、员工、用户的关联关系
- 所属科室:通过外键关联科室表,支持层级树形展示
- 责任人:通过外键关联员工表,体现计划执行主体
- 提交人/审批人:通过外键关联用户表,记录流程责任人
- 计划指标:通过关联表与指标表建立多对多关系,支持权重与评分参数配置
章节来源
- [models.py](file://backend/app/models/models.py#L299-L304)
- [models.py](file://backend/app/models/models.py#L314-L338)
### 计划审批流程、版本控制与激活管理
- 审批流程
- 提交:将状态从草稿转为待审批,并记录提交时间
- 审批:仅允许处于待审批状态的计划进行审批,通过则进入已批准,否则进入已驳回
- 激活:仅已批准的计划可激活,进入执行中并标记为启用
- 版本控制
- 计划表包含版本号字段,支持后续扩展为版本分支/历史版本追踪
- 激活状态管理
- is_active 字段与状态共同决定计划是否处于执行中
章节来源
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L194-L241)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L131-L182)
- [models.py](file://backend/app/models/models.py#L293-L294)
### 前端交互与最佳实践
- 列表与树形:支持按层级、年度、状态筛选,树形结构展示父子关系
- 统计卡片:直观展示各状态数量,便于管理层监控
- 表单编辑:支持计划层级、年度/月份、所属科室、责任人、上级计划、KPI关联等
- 最佳实践
- 新建计划时先完善KPI关联再提交审批
- 审批前确保KPI权重与目标值合理避免反复退回
- 激活前完成所有前置准备(如指标模板、数据源配置)
章节来源
- [performance_plan.js](file://frontend/src/api/performance_plan.js#L1-L67)
- [Plans.vue](file://frontend/src/views/plan/Plans.vue#L1-L742)
## 依赖关系分析
- API 依赖服务层,服务层依赖模型层与模式层
- 前端通过 API 封装调用后端接口
- 计划与指标通过关联表解耦,支持灵活扩展
```mermaid
graph LR
FE["前端视图/接口"] --> API["API路由"]
API --> SVC["服务层"]
SVC --> MODELS["数据模型"]
API --> SCHEMAS["数据模式"]
SVC --> SCHEMAS
```
图表来源
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L1-L310)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L1-L342)
- [models.py](file://backend/app/models/models.py#L270-L338)
- [schemas.py](file://backend/app/schemas/schemas.py#L519-L570)
章节来源
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L1-L310)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L1-L342)
- [models.py](file://backend/app/models/models.py#L270-L338)
- [schemas.py](file://backend/app/schemas/schemas.py#L519-L570)
## 性能考虑
- 查询优化:列表接口支持按层级、年度、状态、科室过滤,配合索引提升查询效率
- 关联加载:详情接口按需加载科室、员工、指标关联,避免 N+1 查询
- 分页与统计:提供分页与统计接口,降低大数据量下的前端渲染压力
- 状态机幂等:服务层对状态转换进行边界检查,避免重复提交导致的状态错乱
章节来源
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L19-L59)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L62-L73)
## 故障排查指南
- 提交失败:检查计划状态是否为草稿,若非草稿则无法提交
- 审批失败:检查计划状态是否为待审批,若非待审批则无法审批
- 激活失败:检查计划状态是否为已批准,若非已批准则无法激活
- 删除失败:确认计划是否存在且满足删除条件
- 前端无数据:检查筛选条件(层级/年度/状态/科室)是否过于严格
章节来源
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L131-L182)
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L194-L257)
## 结论
绩效计划表模块通过清晰的层级设计、严谨的状态机与完善的审批流程实现了从战略到执行的闭环管理。结合前端可视化与后端服务层的边界控制能够有效支撑医院级、科室级与个人级的绩效计划制定与执行。建议在实际使用中遵循“先完善KPI、再提交审批、再激活执行”的流程确保计划的可执行性与可追溯性。
## 附录
### 字段对照表(计划表)
- 计划名称:字符串,必填
- 计划编码:字符串,唯一,必填
- 计划层级:枚举(hospital/department/individual),必填
- 计划年度:整数,必填
- 计划月份:整数(月度计划可选)
- 计划类型:字符串(annual/monthly)默认annual
- 所属科室ID整数(可选)
- 责任人ID整数(可选)
- 上级计划ID整数(可选)
- 描述:文本(可选)
- 战略目标:文本(可选)
- 关键举措:文本(可选)
- 状态:枚举(draft/pending/approved/rejected/active/completed/cancelled)默认draft
- 提交人ID整数(可选)
- 提交时间:时间戳(可选)
- 审批人ID整数(可选)
- 审批时间:时间戳(可选)
- 审批意见:文本(可选)
- 版本号整数默认1
- 是否启用布尔默认true
- 创建/更新时间:时间戳
章节来源
- [models.py](file://backend/app/models/models.py#L270-L296)
### 字段对照表(计划指标关联表)
- 计划ID整数必填
- 指标ID整数必填
- 目标值:数值(可选)
- 目标单位:字符串(可选)
- 权重数值默认1.0
- 评分方法:字符串(可选)
- 评分参数JSON(可选)
- 备注:文本(可选)
- 创建/更新时间:时间戳
章节来源
- [models.py](file://backend/app/models/models.py#L314-L328)
### API 接口一览
- GET /plans获取计划列表支持层级/年度/科室/状态筛选)
- GET /plans/tree获取计划树形结构
- GET /plans/stats获取计划统计
- GET /plans/{id}获取计划详情含KPI关联
- POST /plans创建计划支持KPI关联
- PUT /plans/{id}:更新计划
- POST /{id}/submit提交计划
- POST /{id}/approve审批计划通过/驳回)
- POST /{id}/activate激活计划
- DELETE /{id}:删除计划
- POST /{plan_id}/kpi-relations添加KPI关联
- PUT /kpi-relations/{relation_id}更新KPI关联
- DELETE /kpi-relations/{relation_id}删除KPI关联
章节来源
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L21-L310)
- [performance_plan.js](file://frontend/src/api/performance_plan.js#L1-L67)
### 数据库初始化
- 使用脚本创建计划相关表,确保表结构与模型一致
章节来源
- [create_plan_tables.py](file://backend/create_plan_tables.py#L1-L20)

View File

@@ -0,0 +1,418 @@
# 考核指标模板表
<cite>
**本文档引用的文件**
- [models.py](file://backend/app/models/models.py)
- [002_template.py](file://backend/alembic/versions/002_template.py)
- [init_templates.py](file://backend/app/scripts/init_templates.py)
- [init_indicator_templates.py](file://backend/init_indicator_templates.py)
- [templates.py](file://backend/app/api/v1/templates.py)
- [template_service.py](file://backend/app/services/template_service.py)
- [Templates.vue](file://frontend/src/views/basic/Templates.vue)
- [template.js](file://frontend/src/api/template.js)
- [database.md](file://docs/database.md)
</cite>
## 目录
1. [简介](#简介)
2. [项目结构](#项目结构)
3. [核心组件](#核心组件)
4. [架构概览](#架构概览)
5. [详细组件分析](#详细组件分析)
6. [依赖关系分析](#依赖关系分析)
7. [性能考虑](#性能考虑)
8. [故障排除指南](#故障排除指南)
9. [结论](#结论)
10. [附录](#附录)
## 简介
本文件详细说明考核指标模板表indicator_templates的设计与实现涵盖模板表的完整字段定义、模板类型设计理念与适用场景、维度权重配置、模板激活状态管理、模板与指标的关联关系以及模板指标关联表的作用。同时提供模板创建、版本管理、模板应用的最佳实践和使用指南并给出模板设计、维护、应用的规范和注意事项。
## 项目结构
本项目采用前后端分离架构后端使用FastAPI + SQLAlchemy前端使用Vue.js + Element Plus。模板功能涉及以下关键文件
- 数据模型:定义模板表、指标表、模板指标关联表及其关系
- 迁移脚本:创建模板表及关联字段
- 初始化脚本:提供预设模板数据
- API路由提供模板的增删改查接口
- 服务层:封装模板业务逻辑
- 前端页面:提供模板管理界面
```mermaid
graph TB
subgraph "后端"
Models["models.py<br/>数据模型"]
Migration["002_template.py<br/>数据库迁移"]
InitScript["init_templates.py<br/>模板初始化脚本"]
APITemplates["templates.py<br/>模板API路由"]
Service["template_service.py<br/>模板服务层"]
end
subgraph "前端"
View["Templates.vue<br/>模板管理视图"]
API["template.js<br/>前端API封装"]
end
Models --> Migration
Models --> APITemplates
APITemplates --> Service
Service --> Models
View --> API
API --> APITemplates
InitScript --> Models
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L387-L438)
- [002_template.py](file://backend/alembic/versions/002_template.py#L21-L96)
- [init_templates.py](file://backend/app/scripts/init_templates.py#L189-L276)
- [templates.py](file://backend/app/api/v1/templates.py#L1-L272)
- [template_service.py](file://backend/app/services/template_service.py#L20-L293)
- [Templates.vue](file://frontend/src/views/basic/Templates.vue#L1-L638)
- [template.js](file://frontend/src/api/template.js#L1-L62)
**章节来源**
- [models.py](file://backend/app/models/models.py#L387-L438)
- [002_template.py](file://backend/alembic/versions/002_template.py#L21-L96)
- [init_templates.py](file://backend/app/scripts/init_templates.py#L189-L276)
- [templates.py](file://backend/app/api/v1/templates.py#L1-L272)
- [template_service.py](file://backend/app/services/template_service.py#L20-L293)
- [Templates.vue](file://frontend/src/views/basic/Templates.vue#L1-L638)
- [template.js](file://frontend/src/api/template.js#L1-L62)
## 核心组件
本节详细说明模板表的字段定义、模板类型、维度权重配置、激活状态管理,以及模板与指标的关联关系。
### 模板表字段定义
- id主键自增整数
- template_name模板名称字符串必填
- template_code模板编码字符串唯一且必填
- template_type模板类型枚举必填
- description模板描述文本可选
- dimension_weights维度权重JSON文本可选
- assessment_cycle考核周期字符串默认"monthly"
- is_active是否启用布尔默认True
- created_at创建时间日期时间
- updated_at更新时间日期时间
索引:
- idx_template_type按模板类型索引
- idx_template_active按启用状态索引
**章节来源**
- [models.py](file://backend/app/models/models.py#L387-L408)
### 模板类型与适用场景
模板类型枚举包括:
- general通用模板适用于全院各科室
- surgical手术临床科室适用于外科、妇科、眼科等手术科室
- nonsurgical_ward非手术有病房科室
- nonsurgical_noward非手术无病房科室
- medical_tech医技科室如检验科、放射科、超声科、药剂科等
- nursing护理单元
- admin行政科室
- logistics后勤科室
每种模板类型对应不同的权重分配和指标组合,确保考核内容与科室职责相匹配。
**章节来源**
- [models.py](file://backend/app/models/models.py#L375-L385)
- [init_templates.py](file://backend/app/scripts/init_templates.py#L82-L186)
### 维度权重配置
维度权重用于平衡计分卡的四个维度:
- financial财务管理范围30%-40%
- customer顾客服务范围25%-35%
- internal_process内部流程范围20%-30%
- learning_growth学习与成长范围5%-15%
权重以JSON格式存储在dimension_weights字段中便于前端展示和用户调整。
**章节来源**
- [templates.py](file://backend/app/api/v1/templates.py#L63-L74)
- [init_templates.py](file://backend/app/scripts/init_templates.py#L88-L186)
### 激活状态管理
- is_active字段控制模板的启用状态
- 前端提供开关控件,支持一键启用/停用
- 后端API提供状态更新接口
**章节来源**
- [models.py](file://backend/app/models/models.py#L398-L398)
- [Templates.vue](file://frontend/src/views/basic/Templates.vue#L25-L28)
- [templates.py](file://backend/app/api/v1/templates.py#L459-L462)
### 模板与指标的关联关系
模板通过模板指标关联表template_indicators与指标建立多对多关系
- template_id外键指向模板表
- indicator_id外键指向指标表
- category指标分类二级指标
- target_value/target_unit目标值及单位
- weight指标权重
- scoring_method/scoring_params评分方法及参数
- sort_order排序
- remark备注
关联表提供灵活的指标组合能力,支持不同模板包含不同指标及其权重。
**章节来源**
- [models.py](file://backend/app/models/models.py#L411-L438)
- [init_templates.py](file://backend/app/scripts/init_templates.py#L90-L186)
## 架构概览
模板系统的整体架构由数据模型、API路由、服务层和前端界面组成形成完整的CRUD闭环。
```mermaid
sequenceDiagram
participant User as "用户"
participant Frontend as "前端界面"
participant API as "模板API"
participant Service as "模板服务"
participant DB as "数据库"
User->>Frontend : 访问模板管理页面
Frontend->>API : GET /templates
API->>Service : 查询模板列表
Service->>DB : 查询模板数据
DB-->>Service : 返回模板列表
Service-->>API : 返回模板数据
API-->>Frontend : 渲染模板列表
User->>Frontend : 新增模板
Frontend->>API : POST /templates
API->>Service : 创建模板
Service->>DB : 插入模板记录
DB-->>Service : 返回模板ID
Service-->>API : 返回创建结果
API-->>Frontend : 显示成功消息
```
**图表来源**
- [templates.py](file://backend/app/api/v1/templates.py#L22-L42)
- [template_service.py](file://backend/app/services/template_service.py#L92-L128)
- [Templates.vue](file://frontend/src/views/basic/Templates.vue#L356-L369)
**章节来源**
- [templates.py](file://backend/app/api/v1/templates.py#L1-L272)
- [template_service.py](file://backend/app/services/template_service.py#L20-L293)
- [Templates.vue](file://frontend/src/views/basic/Templates.vue#L1-L638)
## 详细组件分析
### 数据模型类图
```mermaid
classDiagram
class IndicatorTemplate {
+int id
+string template_name
+string template_code
+TemplateType template_type
+string description
+string dimension_weights
+string assessment_cycle
+boolean is_active
+datetime created_at
+datetime updated_at
+TemplateIndicator[] indicators
}
class TemplateIndicator {
+int id
+int template_id
+int indicator_id
+string category
+float target_value
+string target_unit
+float weight
+string scoring_method
+string scoring_params
+int sort_order
+string remark
+datetime created_at
+datetime updated_at
+IndicatorTemplate template
+Indicator indicator
}
class Indicator {
+int id
+string name
+string code
+IndicatorType indicator_type
+BSCDimension bs_dimension
+float weight
+float max_score
+float target_value
+string target_unit
+string calculation_method
+string assessment_method
+string deduction_standard
+string data_source
+string applicable_dept_types
+boolean is_veto
+boolean is_active
+datetime created_at
+datetime updated_at
+AssessmentDetail[] assessment_details
+TemplateIndicator[] template_relations
}
IndicatorTemplate "1" --> "many" TemplateIndicator : "包含"
TemplateIndicator "many" --> "1" Indicator : "关联"
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L387-L438)
**章节来源**
- [models.py](file://backend/app/models/models.py#L387-L438)
### 模板创建流程
```mermaid
flowchart TD
Start([开始创建模板]) --> Validate["验证模板编码唯一性"]
Validate --> Valid{"编码有效?"}
Valid --> |否| Error["返回错误:编码已存在"]
Valid --> |是| CreateTemplate["创建模板记录"]
CreateTemplate --> AddIndicators["批量添加模板指标"]
AddIndicators --> SetSortOrder["设置排序序号"]
SetSortOrder --> Commit["提交事务"]
Commit --> Success["创建成功"]
Error --> End([结束])
Success --> End
```
**图表来源**
- [templates.py](file://backend/app/api/v1/templates.py#L129-L143)
- [template_service.py](file://backend/app/services/template_service.py#L92-L128)
**章节来源**
- [templates.py](file://backend/app/api/v1/templates.py#L129-L143)
- [template_service.py](file://backend/app/services/template_service.py#L92-L128)
### 模板指标管理流程
```mermaid
flowchart TD
Start([模板指标管理]) --> LoadTemplate["加载模板详情"]
LoadTemplate --> ShowIndicators["显示模板指标列表"]
ShowIndicators --> AddIndicator["添加指标"]
ShowIndicators --> EditIndicator["编辑指标"]
ShowIndicators --> RemoveIndicator["移除指标"]
AddIndicator --> CheckExists["检查指标是否已存在"]
CheckExists --> Exists{"已存在?"}
Exists --> |是| ShowError["显示错误:指标已存在"]
Exists --> |否| InsertIndicator["插入新指标记录"]
InsertIndicator --> RefreshTemplate["刷新模板详情"]
EditIndicator --> UpdateIndicator["更新指标记录"]
UpdateIndicator --> RefreshTemplate
RemoveIndicator --> DeleteIndicator["删除指标记录"]
DeleteIndicator --> RefreshTemplate
RefreshTemplate --> ShowIndicators
ShowError --> ShowIndicators
RefreshTemplate --> End([结束])
```
**图表来源**
- [templates.py](file://backend/app/api/v1/templates.py#L174-L272)
- [template_service.py](file://backend/app/services/template_service.py#L160-L253)
**章节来源**
- [templates.py](file://backend/app/api/v1/templates.py#L174-L272)
- [template_service.py](file://backend/app/services/template_service.py#L160-L253)
### 前端交互流程
```mermaid
sequenceDiagram
participant User as "用户"
participant View as "Templates.vue"
participant API as "template.js"
participant Backend as "模板API"
User->>View : 点击"新增模板"
View->>API : 调用createTemplate()
API->>Backend : POST /templates
Backend-->>API : 返回创建结果
API-->>View : 显示成功消息
View->>View : 刷新模板列表
User->>View : 点击"添加指标"
View->>API : 调用addTemplateIndicator()
API->>Backend : POST /templates/{id}/indicators
Backend-->>API : 返回添加结果
API-->>View : 显示成功消息
View->>View : 刷新指标列表
```
**图表来源**
- [Templates.vue](file://frontend/src/views/basic/Templates.vue#L402-L491)
- [template.js](file://frontend/src/api/template.js#L23-L56)
**章节来源**
- [Templates.vue](file://frontend/src/views/basic/Templates.vue#L1-L638)
- [template.js](file://frontend/src/api/template.js#L1-L62)
## 依赖关系分析
模板系统的关键依赖关系如下:
```mermaid
graph TB
subgraph "数据层"
IndicatorModel["Indicator模型"]
TemplateModel["IndicatorTemplate模型"]
TemplateIndicatorModel["TemplateIndicator模型"]
end
subgraph "服务层"
TemplateService["TemplateService"]
end
subgraph "接口层"
TemplateAPI["Template API路由"]
end
subgraph "前端"
TemplatesView["Templates.vue"]
TemplateAPIJS["template.js"]
end
TemplateAPI --> TemplateService
TemplateService --> TemplateModel
TemplateService --> TemplateIndicatorModel
TemplateService --> IndicatorModel
TemplatesView --> TemplateAPIJS
TemplateAPIJS --> TemplateAPI
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L117-L438)
- [template_service.py](file://backend/app/services/template_service.py#L10-L17)
- [templates.py](file://backend/app/api/v1/templates.py#L1-L18)
- [Templates.vue](file://frontend/src/views/basic/Templates.vue#L236-L244)
**章节来源**
- [models.py](file://backend/app/models/models.py#L117-L438)
- [template_service.py](file://backend/app/services/template_service.py#L10-L17)
- [templates.py](file://backend/app/api/v1/templates.py#L1-L18)
- [Templates.vue](file://frontend/src/views/basic/Templates.vue#L236-L244)
## 性能考虑
- 数据库索引:模板表按模板类型和启用状态建立索引,提升查询性能
- 关联查询使用selectinload优化N+1查询问题
- 分页查询API支持分页参数避免一次性加载大量数据
- JSON存储维度权重以JSON格式存储便于前端直接解析使用
## 故障排除指南
常见问题及解决方案:
- 模板编码重复:创建模板时检查编码唯一性,避免重复
- 指标重复添加:添加指标前检查是否已存在于模板中
- 权重配置错误:前端提供权重范围校验,确保数值在合理范围内
- 数据迁移问题使用Alembic进行数据库版本管理确保表结构一致
**章节来源**
- [templates.py](file://backend/app/api/v1/templates.py#L136-L142)
- [template_service.py](file://backend/app/services/template_service.py#L174-L182)
## 结论
考核指标模板表通过清晰的数据模型设计、完善的API接口、友好的前端界面实现了灵活的模板管理和指标配置能力。系统支持多种模板类型、维度权重配置、指标组合管理等功能能够满足不同科室的考核需求。建议在实际使用中遵循最佳实践定期维护模板数据确保考核体系的有效性和准确性。
## 附录
### 模板初始化数据
系统提供了预设的模板数据,包括通用模板和各类科室专用模板,涵盖平衡计分卡的四个维度和相应的指标组合。
**章节来源**
- [init_templates.py](file://backend/app/scripts/init_templates.py#L82-L186)
- [init_indicator_templates.py](file://backend/init_indicator_templates.py#L11-L371)
### 数据库迁移
使用Alembic进行数据库版本管理确保模板表结构的一致性和可追溯性。
**章节来源**
- [002_template.py](file://backend/alembic/versions/002_template.py#L21-L96)
- [database.md](file://docs/database.md#L272-L286)

View File

@@ -0,0 +1,421 @@
# 考核指标表
<cite>
**本文引用的文件**
- [models.py](file://backend/app/models/models.py)
- [schemas.py](file://backend/app/schemas/schemas.py)
- [indicators.py](file://backend/app/api/v1/indicators.py)
- [indicator_service.py](file://backend/app/services/indicator_service.py)
- [init_indicator_templates.py](file://backend/init_indicator_templates.py)
- [migrate_indicators.py](file://backend/migrate_indicators.py)
- [Indicators.vue](file://frontend/src/views/basic/Indicators.vue)
- [indicator.js](file://frontend/src/api/indicator.js)
- [stats_service.py](file://backend/app/services/stats_service.py)
- [001_initial.py](file://backend/alembic/versions/001_initial.py)
</cite>
## 目录
1. [简介](#简介)
2. [项目结构](#项目结构)
3. [核心组件](#核心组件)
4. [架构概览](#架构概览)
5. [详细组件分析](#详细组件分析)
6. [依赖关系分析](#依赖关系分析)
7. [性能考虑](#性能考虑)
8. [故障排查指南](#故障排查指南)
9. [结论](#结论)
10. [附录](#附录)
## 简介
本文件面向“考核指标表indicators”的完整文档化需求涵盖字段定义、枚举类型、业务规则计算方法、考核方法、扣分标准、适用科室类型配置、一票否决机制、激活状态管理以及创建/修改/删除的最佳实践与数据验证规则。文档同时结合后端模型、服务层、API 层与前端界面,形成从数据模型到用户界面的闭环说明。
## 项目结构
- 后端采用 FastAPI + SQLAlchemy 异步 ORM指标模型位于数据模型层API 路由与服务层分别提供查询、创建、更新、删除能力,并支持模板导入。
- 前端提供指标列表展示、搜索筛选、新增/编辑/删除、状态切换等交互。
```mermaid
graph TB
FE["前端界面<br/>Indicators.vue"] --> API["API 路由<br/>indicators.py"]
API --> SVC["服务层<br/>indicator_service.py"]
SVC --> DB["数据库模型<br/>models.py 中的 Indicator 表"]
FE --> APIClient["API 客户端<br/>indicator.js"]
DB --> Alembic["迁移脚本<br/>migrate_indicators.py / 001_initial.py"]
DB --> Templates["模板初始化<br/>init_indicator_templates.py"]
```
图表来源
- [indicators.py](file://backend/app/api/v1/indicators.py#L1-L142)
- [indicator_service.py](file://backend/app/services/indicator_service.py#L1-L197)
- [models.py](file://backend/app/models/models.py#L117-L146)
- [Indicators.vue](file://frontend/src/views/basic/Indicators.vue#L1-L296)
- [indicator.js](file://frontend/src/api/indicator.js#L1-L32)
- [migrate_indicators.py](file://backend/migrate_indicators.py#L1-L46)
- [001_initial.py](file://backend/alembic/versions/001_initial.py#L66-L85)
- [init_indicator_templates.py](file://backend/init_indicator_templates.py#L1-L375)
章节来源
- [indicators.py](file://backend/app/api/v1/indicators.py#L1-L142)
- [indicator_service.py](file://backend/app/services/indicator_service.py#L1-L197)
- [models.py](file://backend/app/models/models.py#L117-L146)
- [Indicators.vue](file://frontend/src/views/basic/Indicators.vue#L1-L296)
- [indicator.js](file://frontend/src/api/indicator.js#L1-L32)
- [migrate_indicators.py](file://backend/migrate_indicators.py#L1-L46)
- [001_initial.py](file://backend/alembic/versions/001_initial.py#L66-L85)
- [init_indicator_templates.py](file://backend/init_indicator_templates.py#L1-L375)
## 核心组件
- 指标实体Indicator承载指标的全部元数据与业务规则字段。
- 枚举类型:
- 指标类型IndicatorType质量、数量、效率、服务、成本。
- 平衡计分卡维度BSCDimension财务、客户、内部流程、学习与成长。
- 科室类型DeptType用于限制指标适用范围。
- API 层:提供列表、详情、创建、更新、删除、模板导入等接口。
- 服务层:封装查询、创建、更新、删除、模板导入逻辑。
- 前端界面:提供指标列表、搜索、新增/编辑/删除、状态切换。
章节来源
- [models.py](file://backend/app/models/models.py#L54-L61)
- [models.py](file://backend/app/models/models.py#L29-L35)
- [models.py](file://backend/app/models/models.py#L16-L27)
- [schemas.py](file://backend/app/schemas/schemas.py#L39-L44)
- [schemas.py](file://backend/app/schemas/schemas.py#L12-L22)
## 架构概览
指标系统围绕“指标模型 + API + 服务 + 前端”的分层架构运行,支持通过模板批量导入指标,支持按科室类型过滤适用指标,支持一票否决与激活状态控制。
```mermaid
sequenceDiagram
participant U as "用户"
participant FE as "前端界面"
participant API as "API 路由"
participant SVC as "服务层"
participant DB as "数据库模型"
U->>FE : 打开指标页面
FE->>API : GET /indicators?...分页/筛选
API->>SVC : 查询指标列表
SVC->>DB : 构造查询类型/维度/状态
DB-->>SVC : 返回指标数据
SVC-->>API : 返回结果
API-->>FE : 渲染表格
U->>FE : 新增/编辑指标
FE->>API : POST/PUT /indicators
API->>SVC : 创建/更新指标
SVC->>DB : 写入/更新指标
DB-->>SVC : 提交成功
SVC-->>API : 返回结果
API-->>FE : 刷新列表
```
图表来源
- [indicators.py](file://backend/app/api/v1/indicators.py#L20-L112)
- [indicator_service.py](file://backend/app/services/indicator_service.py#L16-L93)
- [models.py](file://backend/app/models/models.py#L117-L146)
## 详细组件分析
### 指标实体与字段定义
- 字段清单与含义
- id主键自增
- name指标名称必填
- code指标编码唯一、必填
- indicator_type指标类型必填枚举
- bs_dimension平衡计分卡维度必填枚举
- weight权重>0数值型
- max_score最高分值默认100
- target_value目标值可选
- target_unit目标值单位可选
- calculation_method计算方法/公式(可选)
- assessment_method考核方法可选
- deduction_standard扣分标准可选
- data_source数据来源可选
- applicable_dept_types适用科室类型JSON数组字符串可选
- is_veto是否一票否决布尔
- is_active是否启用布尔默认启用
- created_at/updated_at时间戳
- 约束与索引
- 唯一约束code
- 约束weight > 0
- 索引indicator_type
- 关系
- 与考核明细AssessmentDetail一对多
章节来源
- [models.py](file://backend/app/models/models.py#L117-L146)
- [001_initial.py](file://backend/alembic/versions/001_initial.py#L66-L85)
### 枚举类型与适用场景
- 指标类型IndicatorType
- 质量quality关注结果与过程质量如病历合格率、院感达标率
- 数量quantity关注产出数量如门诊/住院人次
- 效率efficiency关注资源利用效率如报告及时率
- 服务service关注服务体验与满意度如患者满意度
- 成本cost关注成本控制如药品比例、医保扣款
- 平衡计分卡维度BSCDimension
- 财务financial收入增长、成本控制、资产效率
- 客户customer患者满意度、投诉管理、服务可及性
- 内部流程internal_process医疗质量、安全、合规、流程执行率
- 学习与成长learning_growth培训参与、继续教育、科研教学
- 科室类型DeptType
- 临床手术、非手术有病房、非手术无病房、医技、医疗辅助、护理、行政、财务、后勤等
章节来源
- [models.py](file://backend/app/models/models.py#L54-L61)
- [models.py](file://backend/app/models/models.py#L29-L35)
- [models.py](file://backend/app/models/models.py#L16-L27)
- [schemas.py](file://backend/app/schemas/schemas.py#L39-L44)
- [schemas.py](file://backend/app/schemas/schemas.py#L12-L22)
### 业务规则与计算方法
- 计算方法calculation_method用于定义指标的实际计算公式或口径例如“本期收入 - 同期收入)/ 同期收入 × 100%”
- 考核方法assessment_method用于说明数据采集与评估方式例如“统计报表”、“问卷调查”、“系统统计”
- 扣分标准deduction_standard用于明确未达标时的扣分规则例如“每降低1%扣2分”、“每例未处理扣5分”
- 适用科室类型配置applicable_dept_types
- 以 JSON 数组字符串形式存储,用于限制指标在特定科室类型下的生效范围
- 模板导入时会自动写入对应科室类型的 dept_type
- 一票否决is_veto
- 当某指标被标记为一票否决时,在考核过程中若该项未达标,将直接判定为不通过
- 示例:院感控制达标率(示例来自模板数据)
- 激活状态管理is_active
- 控制指标是否参与当前考核周期
- 前端提供开关式状态变更入口
章节来源
- [models.py](file://backend/app/models/models.py#L129-L135)
- [init_indicator_templates.py](file://backend/init_indicator_templates.py#L22-L232)
- [init_indicator_templates.py](file://backend/init_indicator_templates.py#L309-L362)
- [Indicators.vue](file://frontend/src/views/basic/Indicators.vue#L31-L35)
### API 与服务层行为
- 列表查询
- 支持按指标类型、BSC 维度、是否启用进行筛选
- 支持分页与总数统计
- 详情查询
- 根据 ID 获取指标详情
- 创建/更新/删除
- 创建前校验编码唯一性
- 更新支持部分字段更新
- 删除返回布尔结果
- 模板导入
- 支持按模板类型批量导入指标
- 支持覆盖策略overwrite
章节来源
- [indicators.py](file://backend/app/api/v1/indicators.py#L20-L142)
- [indicator_service.py](file://backend/app/services/indicator_service.py#L16-L154)
### 前端交互与数据验证
- 列表展示
- 支持按指标类型筛选、关键词搜索(名称/编码)
- 展示编码、名称、类型、权重、最高分值、目标值、单位、状态等
- 新增/编辑
- 必填字段:编码、名称、指标类型
- 权重范围≥0.1,支持一位小数
- 最高分值范围≥0最大1000
- 目标值:数值型,保留两位小数
- 计算方法与描述:文本域输入
- 状态切换
- 通过开关直接更新 is_active
章节来源
- [Indicators.vue](file://frontend/src/views/basic/Indicators.vue#L1-L296)
- [indicator.js](file://frontend/src/api/indicator.js#L1-L32)
- [schemas.py](file://backend/app/schemas/schemas.py#L153-L192)
### 模板与迁移
- 模板初始化
- 提供多类科室模板(手术临床、非手术有病房、非手术无病房、医技、行政、后勤等)
- 模板中包含指标名称、编码、类型、维度、权重、目标值、计算方法、考核方法、扣分标准、数据来源、适用科室类型、是否一票否决、是否启用等
- 迁移脚本
- 为历史表添加 bs_dimension、target_unit、assessment_method、deduction_standard、data_source、applicable_dept_types、is_veto 等列
章节来源
- [init_indicator_templates.py](file://backend/init_indicator_templates.py#L1-L375)
- [migrate_indicators.py](file://backend/migrate_indicators.py#L1-L46)
## 依赖关系分析
- 指标实体依赖枚举类型IndicatorType、BSCDimension、DeptType
- API 层依赖服务层;服务层依赖模型层;模型层依赖数据库
- 前端通过 API 客户端调用后端接口
```mermaid
classDiagram
class Indicator {
+int id
+string name
+string code
+IndicatorType indicator_type
+BSCDimension bs_dimension
+float weight
+float max_score
+float target_value
+string target_unit
+string calculation_method
+string assessment_method
+string deduction_standard
+string data_source
+string applicable_dept_types
+bool is_veto
+bool is_active
}
class IndicatorService {
+get_list(...)
+get_by_id(...)
+get_active_indicators(...)
+create(...)
+update(...)
+delete(...)
+import_template(...)
+get_templates()
}
class API_Indicators {
+get_indicators(...)
+get_active_indicators(...)
+get_indicator(...)
+create_indicator(...)
+update_indicator(...)
+delete_indicator(...)
+get_indicator_templates(...)
+import_indicator_template(...)
}
API_Indicators --> IndicatorService : "调用"
IndicatorService --> Indicator : "读写"
```
图表来源
- [models.py](file://backend/app/models/models.py#L117-L146)
- [indicator_service.py](file://backend/app/services/indicator_service.py#L13-L197)
- [indicators.py](file://backend/app/api/v1/indicators.py#L1-L142)
## 性能考虑
- 查询优化
- 列表查询支持按类型、维度、状态过滤,建议在高频查询上保持索引有效
- 分页查询避免一次性加载过多数据
- 数据一致性
- 编码唯一性约束确保指标识别稳定
- 权重大于0的约束保证权重语义正确
- 模板导入
- 批量导入时建议开启事务,减少多次往返
- 覆盖策略需谨慎使用,避免误更新
[本节为通用指导,无需特定文件来源]
## 故障排查指南
- 创建失败:指标编码重复
- 现象:创建接口返回错误
- 处理:更换唯一编码后重试
- 更新失败:指标不存在
- 现象更新接口返回404
- 处理确认指标ID是否存在
- 权重异常权重≤0
- 现象:数据库约束报错
- 处理:调整权重至>0
- 模板导入未生效
- 现象:导入后指标未出现或未覆盖
- 处理:检查 overwrite 参数、dept_type 是否匹配、数据库迁移是否完成
章节来源
- [indicators.py](file://backend/app/api/v1/indicators.py#L78-L81)
- [indicators.py](file://backend/app/api/v1/indicators.py#L95-L98)
- [models.py](file://backend/app/models/models.py#L145)
- [migrate_indicators.py](file://backend/migrate_indicators.py#L9-L41)
## 结论
考核指标表通过清晰的字段定义、严谨的枚举约束与完善的业务规则,实现了对不同科室类型的差异化考核。配合模板导入与前端交互,能够高效地完成指标的创建、维护与应用。建议在实际使用中严格遵循数据验证规则与最佳实践,确保指标体系的稳定性与可扩展性。
[本节为总结性内容,无需特定文件来源]
## 附录
### 字段与规则对照表
- 字段名name
- 类型:字符串
- 必填:是
- 用途:指标名称
- 字段名code
- 类型:字符串
- 必填:是
- 唯一性:是
- 用途:指标唯一标识
- 字段名indicator_type
- 类型:枚举(质量/数量/效率/服务/成本)
- 必填:是
- 用途:区分指标类别
- 字段名bs_dimension
- 类型:枚举(财务/客户/内部流程/学习与成长)
- 必填:是
- 用途:平衡计分卡维度归属
- 字段名weight
- 类型:数值
- 必填:是
- 约束:>0
- 用途:权重,影响加权得分
- 字段名max_score
- 类型:数值
- 默认值100
- 用途:指标最高分值
- 字段名target_value
- 类型:数值
- 可选:是
- 用途:目标值
- 字段名target_unit
- 类型:字符串
- 可选:是
- 用途:目标值单位
- 字段名calculation_method
- 类型:文本
- 可选:是
- 用途:计算方法/公式
- 字段名assessment_method
- 类型:文本
- 可选:是
- 用途:考核方法
- 字段名deduction_standard
- 类型:文本
- 可选:是
- 用途:扣分标准
- 字段名data_source
- 类型:字符串
- 可选:是
- 用途:数据来源
- 字段名applicable_dept_types
- 类型JSON数组字符串
- 可选:是
- 用途:适用科室类型
- 字段名is_veto
- 类型:布尔
- 默认值false
- 用途:一票否决标志
- 字段名is_active
- 类型:布尔
- 默认值true
- 用途:启用/禁用控制
章节来源
- [models.py](file://backend/app/models/models.py#L117-L146)
- [schemas.py](file://backend/app/schemas/schemas.py#L153-L192)
### 最佳实践与数据验证规则
- 创建/修改
- 编码必须唯一且必填
- 指标类型与平衡计分卡维度必须选择
- 权重必须大于0
- 最高分值建议合理设置如100避免过大或过小
- 目标值与单位应配套填写
- 计算方法、考核方法、扣分标准应明确可执行
- 适用科室类型应与模板一致,避免跨科室误用
- 激活状态
- 新建指标默认启用;停用时需谨慎评估影响范围
- 一票否决
- 仅在关键质量/安全指标上启用,避免滥用
- 模板导入
- 导入前确认 dept_type 与模板类型一致
- 使用覆盖模式时注意备份与审计
[本节为通用指导,无需特定文件来源]

View File

@@ -0,0 +1,357 @@
# 计划指标关联表
<cite>
**本文档引用的文件**
- [models.py](file://backend/app/models/models.py)
- [schemas.py](file://backend/app/schemas/schemas.py)
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py)
- [database.md](file://docs/database.md)
- [002_template.py](file://backend/alembic/versions/002_template.py)
- [create_plan_tables.py](file://backend/create_plan_tables.py)
</cite>
## 目录
1. [简介](#简介)
2. [项目结构](#项目结构)
3. [核心组件](#核心组件)
4. [架构概览](#架构概览)
5. [详细组件分析](#详细组件分析)
6. [依赖关系分析](#依赖关系分析)
7. [性能考虑](#性能考虑)
8. [故障排除指南](#故障排除指南)
9. [结论](#结论)
10. [附录](#附录)
## 简介
计划指标关联表plan_kpi_relations是医院绩效管理系统中的核心数据表用于建立绩效计划与考核指标之间的多对多关联关系。该表实现了计划层面的指标配置功能支持权重分配、目标值设定、评分方法配置等关键业务需求。
本表采用多对多关联设计,通过外键约束确保数据完整性,支持灵活的指标组合配置,为不同层级、不同类型的绩效计划提供个性化的指标体系。
## 项目结构
基于代码库分析,计划指标关联表涉及以下核心模块:
```mermaid
graph TB
subgraph "后端架构"
API[API层<br/>performance_plans.py]
Service[服务层<br/>performance_plan_service.py]
Model[模型层<br/>models.py]
Schema[Schema层<br/>schemas.py]
end
subgraph "数据库层"
PlanKpi[plan_kpi_relations<br/>关联表]
PerformancePlan[performance_plans<br/>计划表]
Indicator[indicators<br/>指标表]
end
subgraph "前端界面"
PlanUI[计划管理界面]
KPIConfig[KPI配置组件]
end
API --> Service
Service --> Model
Model --> PlanKpi
PlanKpi --> PerformancePlan
PlanKpi --> Indicator
PlanUI --> API
KPIConfig --> API
```
**图表来源**
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L260-L310)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L195-L249)
- [models.py](file://backend/app/models/models.py#L314-L338)
**章节来源**
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L260-L310)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L195-L249)
- [models.py](file://backend/app/models/models.py#L314-L338)
## 核心组件
### 数据表结构定义
计划指标关联表采用完整的字段定义,支持全面的业务配置需求:
| 字段名 | 数据类型 | 约束条件 | 说明 |
|--------|----------|----------|------|
| id | Integer | 主键, 自增 | 关联关系唯一标识 |
| plan_id | Integer | 外键, 非空 | 关联的绩效计划ID |
| indicator_id | Integer | 外键, 非空 | 关联的指标ID |
| target_value | Numeric(10,2) | 可空 | 指标的计划目标值 |
| target_unit | String(50) | 可空 | 目标值计量单位 |
| weight | Numeric(5,2) | 默认1.0 | 指标权重值 |
| scoring_method | String(50) | 可空 | 评分方法类型 |
| scoring_params | Text | 可空 | 评分参数JSON格式 |
| remark | Text | 可空 | 备注说明 |
| created_at | DateTime | 默认当前时间 | 记录创建时间 |
| updated_at | DateTime | 默认当前时间, 更新时自动更新 | 记录最后更新时间 |
### 索引与约束
```mermaid
graph LR
subgraph "索引设计"
IDX1[idx_relation_plan<br/>plan_id索引]
IDX2[idx_relation_indicator<br/>indicator_id索引]
IDX3[idx_relation_unique<br/>plan_id+indicator_id唯一索引]
end
subgraph "约束设计"
FK1[FK_performance_plans<br/>plan_id → performance_plans.id]
FK2[FK_indicators<br/>indicator_id → indicators.id]
CK1[CK_weight_range<br/>weight ∈ [0,100]]
end
IDX1 --> FK1
IDX2 --> FK2
IDX3 --> Unique[唯一约束]
CK1 --> Weight[权重检查]
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L334-L338)
**章节来源**
- [models.py](file://backend/app/models/models.py#L314-L338)
## 架构概览
系统采用分层架构设计,实现清晰的职责分离:
```mermaid
sequenceDiagram
participant Client as 客户端
participant API as API接口
participant Service as 服务层
participant DB as 数据库
Client->>API : 添加KPI关联请求
API->>Service : add_kpi_relation()
Service->>DB : 查询计划是否存在
DB-->>Service : 计划存在
Service->>DB : 创建PlanKpiRelation记录
DB-->>Service : 返回新记录
Service-->>API : 返回关联关系
API-->>Client : 返回结果
Note over Client,DB : 数据一致性通过外键约束保证
```
**图表来源**
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L260-L275)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L195-L213)
**章节来源**
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L260-L275)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L195-L213)
## 详细组件分析
### 数据模型设计
```mermaid
classDiagram
class PlanKpiRelation {
+int id
+int plan_id
+int indicator_id
+float target_value
+string target_unit
+float weight
+string scoring_method
+string scoring_params
+string remark
+datetime created_at
+datetime updated_at
+plan : PerformancePlan
+indicator : Indicator
}
class PerformancePlan {
+int id
+string plan_name
+string plan_code
+int plan_year
+string plan_level
+kpi_relations : List[PlanKpiRelation]
}
class Indicator {
+int id
+string name
+string code
+float weight
+plan_relations : List[PlanKpiRelation]
}
PlanKpiRelation --> PerformancePlan : "多对一"
PlanKpiRelation --> Indicator : "多对一"
PerformancePlan --> PlanKpiRelation : "一对多"
Indicator --> PlanKpiRelation : "一对多"
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L314-L332)
### API接口设计
系统提供完整的CRUD操作接口
| 接口方法 | URL路径 | 功能描述 | 权限要求 |
|----------|---------|----------|----------|
| POST | /plans/{plan_id}/kpi-relations | 添加计划指标关联 | 管理员/经理 |
| PUT | /plans/kpi-relations/{relation_id} | 更新计划指标关联 | 管理员/经理 |
| DELETE | /plans/kpi-relations/{relation_id} | 删除计划指标关联 | 管理员/经理 |
### 业务逻辑处理
```mermaid
flowchart TD
Start([开始]) --> ValidatePlan["验证计划存在性"]
ValidatePlan --> PlanExists{"计划存在?"}
PlanExists --> |否| ReturnError["返回错误"]
PlanExists --> |是| CreateRelation["创建关联关系"]
CreateRelation --> SaveToDB["保存到数据库"]
SaveToDB --> RefreshData["刷新数据"]
RefreshData --> Success["返回成功"]
ReturnError --> End([结束])
Success --> End
```
**图表来源**
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L195-L213)
**章节来源**
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L260-L310)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L195-L249)
## 依赖关系分析
### 外部依赖
```mermaid
graph TB
subgraph "核心依赖"
SQLAlchemy[SQLAlchemy ORM]
FastAPI[FastAPI框架]
Pydantic[Pydantic验证]
end
subgraph "数据库依赖"
PostgreSQL[PostgreSQL引擎]
Alembic[数据库迁移]
end
subgraph "前端依赖"
Vue3[Vue3框架]
ElementPlus[ElementPlus组件库]
end
SQLAlchemy --> Alembic
FastAPI --> Pydantic
Vue3 --> ElementPlus
```
### 内部模块依赖
```mermaid
graph LR
API --> Service
Service --> Model
Model --> Schema
Model --> Database
Service --> Database
API --> Database
```
**图表来源**
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L1-L20)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L1-L20)
**章节来源**
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L1-L20)
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L1-L20)
## 性能考虑
### 查询优化策略
1. **索引优化**
- plan_id索引加速按计划查询关联关系
- indicator_id索引加速按指标查询关联关系
- 唯一索引:防止重复的计划-指标关联
2. **连接查询优化**
- 使用selectinload优化N+1查询问题
- 合理使用join减少查询次数
3. **缓存策略**
- 对常用查询结果进行缓存
- 利用数据库连接池提高并发性能
### 数据一致性保证
```mermaid
stateDiagram-v2
[*] --> Draft : 创建关联
Draft --> Active : 提交/激活
Active --> Completed : 完成计划
Active --> Cancelled : 取消关联
Completed --> [*]
Cancelled --> [*]
note right of Active
数据一致性通过
外键约束和事务保证
end note
```
## 故障排除指南
### 常见问题及解决方案
1. **关联关系创建失败**
- 检查计划ID是否存在
- 验证指标ID的有效性
- 确认唯一性约束未被违反
2. **更新操作异常**
- 确认关联关系ID正确
- 检查字段范围限制权重0-100
- 验证JSON参数格式
3. **删除操作失败**
- 确认关联关系存在
- 检查是否有依赖关系
**章节来源**
- [performance_plan_service.py](file://backend/app/services/performance_plan_service.py#L215-L249)
## 结论
计划指标关联表作为医院绩效管理系统的核心组件,通过精心设计的数据模型和完善的业务逻辑,实现了灵活的指标配置功能。系统采用分层架构设计,确保了良好的可维护性和扩展性。
关键优势包括:
- 完整的多对多关联支持
- 灵活的权重和评分配置
- 强大的数据一致性保证
- 清晰的权限控制机制
## 附录
### 数据库迁移历史
系统通过Alembic进行数据库版本管理支持指标模板相关字段的添加和维护。
**章节来源**
- [002_template.py](file://backend/alembic/versions/002_template.py#L1-L96)
- [create_plan_tables.py](file://backend/create_plan_tables.py#L1-L20)
### 最佳实践建议
1. **配置管理**
- 建立标准化的指标配置流程
- 定期审查和优化权重分配
- 维护评分方法的一致性
2. **数据维护**
- 定期清理无效的关联关系
- 监控数据完整性
- 建立备份和恢复机制
3. **性能优化**
- 合理使用索引
- 优化查询语句
- 监控数据库性能指标

View File

@@ -0,0 +1,700 @@
# 配置管理表
<cite>
**本文档引用的文件**
- [models.py](file://backend/app/models/models.py)
- [001_initial.py](file://backend/alembic/versions/001_initial.py)
- [002_template.py](file://backend/alembic/versions/002_template.py)
- [schemas.py](file://backend/app/schemas/schemas.py)
- [security.py](file://backend/app/core/security.py)
- [indicators.py](file://backend/app/api/v1/indicators.py)
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py)
- [menus.py](file://backend/app/api/v1/menus.py)
- [templates.py](file://backend/app/api/v1/templates.py)
- [indicator_service.py](file://backend/app/services/indicator_service.py)
- [template_service.py](file://backend/app/services/template_service.py)
- [database.py](file://backend/app/core/database.py)
- [config.py](file://backend/app/core/config.py)
</cite>
## 目录
1. [简介](#简介)
2. [项目结构](#项目结构)
3. [核心组件](#核心组件)
4. [架构概览](#架构概览)
5. [详细组件分析](#详细组件分析)
6. [依赖关系分析](#依赖关系分析)
7. [性能考虑](#性能考虑)
8. [故障排除指南](#故障排除指南)
9. [结论](#结论)
## 简介
本文档详细介绍了医院绩效考核管理系统的配置管理表结构,重点关注以下核心配置表:
- 考核指标表indicators
- 系统用户表users
- 绩效计划表performance_plans
- 计划指标关联表plan_kpi_relations
- 系统菜单表menus
- 考核指标模板表indicator_templates
该系统采用现代化的后端架构使用FastAPI框架、SQLAlchemy ORM、PostgreSQL数据库并实现了完整的权限控制和模板化管理机制。
## 项目结构
系统采用分层架构设计,主要分为以下几个层次:
```mermaid
graph TB
subgraph "表现层"
API[API路由层]
Frontend[前端界面]
end
subgraph "业务逻辑层"
Services[服务层]
Schemas[数据模式层]
end
subgraph "数据访问层"
Models[ORM模型层]
Database[数据库层]
end
subgraph "基础设施层"
Security[安全认证]
Config[配置管理]
end
Frontend --> API
API --> Services
Services --> Models
Models --> Database
API --> Schemas
Services --> Schemas
Security --> API
Config --> Database
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L1-L438)
- [schemas.py](file://backend/app/schemas/schemas.py#L1-L743)
- [security.py](file://backend/app/core/security.py#L1-L110)
**章节来源**
- [models.py](file://backend/app/models/models.py#L1-L438)
- [schemas.py](file://backend/app/schemas/schemas.py#L1-L743)
- [database.py](file://backend/app/core/database.py#L1-L39)
## 核心组件
### 枚举类型系统
系统实现了完整的枚举类型体系,确保数据的一致性和完整性:
#### 指标类型枚举
- 质量指标quality
- 数量指标quantity
- 效率指标efficiency
- 服务指标service
- 成本指标cost
#### 平衡计分卡维度
- 财务维度financial
- 客户维度customer
- 内部流程维度internal_process
- 学习与成长维度learning_growth
#### 用户权限级别
- 普通员工staff
- 部门经理manager
- 系统管理员admin
#### 计划状态管理
- 草稿draft
- 待审批pending
- 已批准approved
- 已驳回rejected
- 执行中active
- 已完成completed
- 已取消cancelled
**章节来源**
- [models.py](file://backend/app/models/models.py#L16-L61)
- [schemas.py](file://backend/app/schemas/schemas.py#L39-L44)
- [schemas.py](file://backend/app/schemas/schemas.py#L24-L28)
- [schemas.py](file://backend/app/schemas/schemas.py#L470-L479)
### 数据模型架构
系统采用SQLAlchemy ORM进行数据库建模所有配置表都遵循统一的设计规范
```mermaid
classDiagram
class Indicator {
+int id
+string name
+string code
+IndicatorType indicator_type
+float weight
+float max_score
+float target_value
+string target_unit
+string calculation_method
+string assessment_method
+string deduction_standard
+string data_source
+string applicable_dept_types
+bool is_veto
+bool is_active
+datetime created_at
+datetime updated_at
}
class User {
+int id
+string username
+string password_hash
+int staff_id
+string role
+bool is_active
+datetime last_login
+datetime created_at
+datetime updated_at
}
class PerformancePlan {
+int id
+string plan_name
+string plan_code
+PlanLevel plan_level
+int plan_year
+int plan_month
+string plan_type
+int department_id
+int staff_id
+int parent_plan_id
+string description
+string strategic_goals
+string key_initiatives
+PlanStatus status
+int submitter_id
+datetime submit_time
+int approver_id
+datetime approve_time
+string approve_remark
+int version
+bool is_active
+datetime created_at
+datetime updated_at
}
class PlanKpiRelation {
+int id
+int plan_id
+int indicator_id
+float target_value
+string target_unit
+float weight
+string scoring_method
+string scoring_params
+string remark
+datetime created_at
+datetime updated_at
}
class Menu {
+int id
+int parent_id
+MenuType menu_type
+string menu_name
+string menu_icon
+string path
+string component
+string permission
+int sort_order
+bool is_visible
+bool is_active
+datetime created_at
+datetime updated_at
}
class IndicatorTemplate {
+int id
+string template_name
+string template_code
+TemplateType template_type
+string description
+string dimension_weights
+string assessment_cycle
+bool is_active
+datetime created_at
+datetime updated_at
}
class TemplateIndicator {
+int id
+int template_id
+int indicator_id
+string category
+float target_value
+string target_unit
+float weight
+string scoring_method
+string scoring_params
+int sort_order
+string remark
+datetime created_at
+datetime updated_at
}
IndicatorTemplate "1" -- "many" TemplateIndicator : "has"
PerformancePlan "1" -- "many" PlanKpiRelation : "contains"
Indicator "many" -- "many" PlanKpiRelation : "related_to"
User "1" -- "many" PerformancePlan : "submits"
Menu "1" -- "many" Menu : "parent_child"
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L117-L147)
- [models.py](file://backend/app/models/models.py#L244-L261)
- [models.py](file://backend/app/models/models.py#L270-L312)
- [models.py](file://backend/app/models/models.py#L314-L339)
- [models.py](file://backend/app/models/models.py#L347-L373)
- [models.py](file://backend/app/models/models.py#L387-L409)
- [models.py](file://backend/app/models/models.py#L411-L438)
**章节来源**
- [models.py](file://backend/app/models/models.py#L117-L438)
## 架构概览
系统采用RESTful API架构所有配置管理操作都通过标准化的HTTP接口进行
```mermaid
sequenceDiagram
participant Client as "客户端"
participant API as "API路由层"
participant Service as "服务层"
participant Model as "ORM模型层"
participant DB as "数据库"
Client->>API : HTTP请求
API->>Service : 调用业务逻辑
Service->>Model : 数据模型操作
Model->>DB : SQL查询/更新
DB-->>Model : 查询结果
Model-->>Service : 处理后的数据
Service-->>API : 业务结果
API-->>Client : JSON响应
Note over Client,DB : 完整的CRUD操作流程
```
**图表来源**
- [indicators.py](file://backend/app/api/v1/indicators.py#L1-L142)
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L1-L310)
- [templates.py](file://backend/app/api/v1/templates.py#L1-L272)
**章节来源**
- [indicators.py](file://backend/app/api/v1/indicators.py#L1-L142)
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L1-L310)
- [templates.py](file://backend/app/api/v1/templates.py#L1-L272)
## 详细组件分析
### 考核指标表indicators
#### 字段定义
| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | Integer | 主键, 自增 | 指标唯一标识符 |
| name | String(100) | 非空 | 指标名称 |
| code | String(20) | 唯一, 非空 | 指标编码 |
| indicator_type | Enum | 非空 | 指标类型(质量/数量/效率/服务/成本) |
| bs_dimension | Enum | 非空 | 平衡计分卡维度 |
| weight | Numeric(5,2) | 默认1.0, >0 | 权重 |
| max_score | Numeric(5,2) | 默认100.0 | 最高分值 |
| target_value | Numeric(10,2) | 可选 | 目标值 |
| target_unit | String(50) | 可选 | 目标值单位 |
| calculation_method | Text | 可选 | 计算方法/公式 |
| assessment_method | Text | 可选 | 考核方法 |
| deduction_standard | Text | 可选 | 扣分标准 |
| data_source | String(100) | 可选 | 数据来源 |
| applicable_dept_types | Text | 可选 | 适用科室类型JSON数组 |
| is_veto | Boolean | 默认False | 是否一票否决指标 |
| is_active | Boolean | 默认True | 是否启用 |
| created_at | DateTime | 默认当前时间 | 创建时间 |
| updated_at | DateTime | 更新时自动更新 | 更新时间 |
#### 业务规则
1. **权重约束**权重必须大于0通过数据库检查约束保证
2. **适用科室**支持JSON格式存储适用科室类型数组
3. **模板集成**:与指标模板系统无缝集成
4. **状态管理**:支持启用/停用控制
#### 数据一致性保证
```mermaid
flowchart TD
Start([创建指标]) --> Validate["验证输入参数"]
Validate --> CheckCode{"检查编码唯一性"}
CheckCode --> |重复| Error["返回错误"]
CheckCode --> |唯一| Create["创建指标记录"]
Create --> SetDefaults["设置默认值"]
SetDefaults --> Save["保存到数据库"]
Save --> Success["创建成功"]
Error --> End([结束])
Success --> End
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L117-L147)
- [indicators.py](file://backend/app/api/v1/indicators.py#L71-L85)
**章节来源**
- [models.py](file://backend/app/models/models.py#L117-L147)
- [indicators.py](file://backend/app/api/v1/indicators.py#L1-L142)
### 系统用户表users
#### 字段定义
| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | Integer | 主键, 自增 | 用户唯一标识符 |
| username | String(50) | 唯一, 非空 | 用户名 |
| password_hash | String(255) | 非空 | 密码哈希值 |
| staff_id | Integer | 外键, 可选 | 关联员工ID |
| role | String(20) | 默认"staff" | 角色权限 |
| is_active | Boolean | 默认True | 是否启用 |
| last_login | DateTime | 可选 | 最后登录时间 |
| created_at | DateTime | 默认当前时间 | 创建时间 |
| updated_at | DateTime | 更新时自动更新 | 更新时间 |
#### 权限控制机制
系统实现了多层级权限控制:
```mermaid
graph LR
subgraph "权限层级"
Admin[系统管理员<br/>拥有所有权限]
Manager[部门经理<br/>管理权限]
Staff[普通员工<br/>查看权限]
end
subgraph "受保护操作"
Create[创建操作]
Update[更新操作]
Delete[删除操作]
Approve[审批操作]
end
Admin --> Create
Admin --> Update
Admin --> Delete
Admin --> Approve
Manager --> Create
Manager --> Update
Manager --> Approve
Staff -.-> Create
Staff -.-> Update
Staff -.-> Delete
Staff -.-> Approve
```
**图表来源**
- [security.py](file://backend/app/core/security.py#L94-L110)
- [models.py](file://backend/app/models/models.py#L244-L261)
**章节来源**
- [models.py](file://backend/app/models/models.py#L244-L261)
- [security.py](file://backend/app/core/security.py#L1-L110)
### 绩效计划表performance_plans
#### 字段定义
| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | Integer | 主键, 自增 | 计划唯一标识符 |
| plan_name | String(200) | 非空 | 计划名称 |
| plan_code | String(50) | 唯一, 非空 | 计划编码 |
| plan_level | Enum | 非空 | 计划层级(医院/科室/个人) |
| plan_year | Integer | 非空 | 计划年度 |
| plan_month | Integer | 可选 | 计划月份(月度计划) |
| plan_type | String(20) | 默认"annual" | 计划类型annual/monthly |
| department_id | Integer | 外键, 可选 | 所属科室ID |
| staff_id | Integer | 外键, 可选 | 责任人ID |
| parent_plan_id | Integer | 外键, 可选 | 上级计划ID |
| description | Text | 可选 | 计划描述 |
| strategic_goals | Text | 可选 | 战略目标 |
| key_initiatives | Text | 可选 | 关键举措 |
| status | Enum | 默认"DRAFT" | 状态(草稿/待审批/已批准/执行中/完成/取消) |
| submitter_id | Integer | 外键, 可选 | 提交人ID |
| submit_time | DateTime | 可选 | 提交时间 |
| approver_id | Integer | 外键, 可选 | 审批人ID |
| approve_time | DateTime | 可选 | 审批时间 |
| approve_remark | Text | 可选 | 审批意见 |
| version | Integer | 默认1 | 版本号 |
| is_active | Boolean | 默认True | 是否启用 |
| created_at | DateTime | 默认当前时间 | 创建时间 |
| updated_at | DateTime | 更新时自动更新 | 更新时间 |
#### 计划状态流转
```mermaid
stateDiagram-v2
[*] --> 草稿
草稿 --> 待审批 : "提交申请"
待审批 --> 已批准 : "审批通过"
待审批 --> 已驳回 : "审批驳回"
已批准 --> 执行中 : "激活计划"
执行中 --> 已完成 : "计划完成"
执行中 --> 已取消 : "取消计划"
已完成 --> [*]
已取消 --> [*]
已驳回 --> 草稿 : "修改后重新提交"
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L270-L312)
- [schemas.py](file://backend/app/schemas/schemas.py#L470-L479)
**章节来源**
- [models.py](file://backend/app/models/models.py#L270-L312)
- [performance_plans.py](file://backend/app/api/v1/performance_plans.py#L1-L310)
### 计划指标关联表plan_kpi_relations
#### 字段定义
| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | Integer | 主键, 自增 | 关联唯一标识符 |
| plan_id | Integer | 外键, 非空 | 计划ID |
| indicator_id | Integer | 外键, 非空 | 指标ID |
| target_value | Numeric(10,2) | 可选 | 目标值 |
| target_unit | String(50) | 可选 | 目标值单位 |
| weight | Numeric(5,2) | 默认1.0 | 权重 |
| scoring_method | String(50) | 可选 | 评分方法 |
| scoring_params | Text | 可选 | 评分参数JSON |
| remark | Text | 可选 | 备注 |
| created_at | DateTime | 默认当前时间 | 创建时间 |
| updated_at | DateTime | 更新时自动更新 | 更新时间 |
#### 关联关系特性
1. **唯一性约束**:同一计划下的指标只能关联一次
2. **权重管理**:支持为每个关联设置独立权重
3. **评分定制**:支持为特定关联配置评分方法和参数
**章节来源**
- [models.py](file://backend/app/models/models.py#L314-L339)
### 系统菜单表menus
#### 字段定义
| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | Integer | 主键, 自增 | 菜单唯一标识符 |
| parent_id | Integer | 外键, 可选 | 父菜单ID |
| menu_type | Enum | 默认"menu" | 菜单类型(菜单/按钮) |
| menu_name | String(100) | 非空 | 菜单名称 |
| menu_icon | String(50) | 可选 | 菜单图标 |
| path | String(200) | 非空 | 路由路径 |
| component | String(200) | 可选 | 组件路径 |
| permission | String(100) | 可选 | 权限标识 |
| sort_order | Integer | 默认0 | 排序 |
| is_visible | Boolean | 默认True | 是否可见 |
| is_active | Boolean | 默认True | 是否启用 |
| created_at | DateTime | 默认当前时间 | 创建时间 |
| updated_at | DateTime | 更新时自动更新 | 更新时间 |
#### 菜单树形结构
系统支持无限级菜单嵌套通过parent_id字段实现父子关系
```mermaid
graph TD
Root[根菜单] --> Parent1[一级菜单1]
Root --> Parent2[一级菜单2]
Parent1 --> Child11[二级菜单1-1]
Parent1 --> Child12[二级菜单1-2]
Parent2 --> Child21[二级菜单2-1]
Child11 --> GrandChild111[三级菜单1-1-1]
```
**图表来源**
- [models.py](file://backend/app/models/models.py#L347-L373)
**章节来源**
- [models.py](file://backend/app/models/models.py#L347-L373)
- [menus.py](file://backend/app/api/v1/menus.py#L1-L164)
### 考核指标模板表indicator_templates
#### 字段定义
| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | Integer | 主键, 自增 | 模板唯一标识符 |
| template_name | String(200) | 非空 | 模板名称 |
| template_code | String(50) | 唯一, 非空 | 模板编码 |
| template_type | Enum | 非空 | 模板类型(通用/手术/非手术/医技/护理/行政/后勤) |
| description | Text | 可选 | 模板描述 |
| dimension_weights | Text | 可选 | 维度权重JSON |
| assessment_cycle | String(20) | 默认"monthly" | 考核周期 |
| is_active | Boolean | 默认True | 是否启用 |
| created_at | DateTime | 默认当前时间 | 创建时间 |
| updated_at | DateTime | 更新时自动更新 | 更新时间 |
#### 模板类型体系
| 模板类型 | 适用科室 | 描述 |
|----------|----------|------|
| general | 通用 | 适用于所有科室的基础模板 |
| surgical | 手术临床科室 | 适用于外科系统各手术科室 |
| nonsurgical_ward | 非手术有病房科室 | 适用于内科系统等有病房科室 |
| nonsurgical_noward | 非手术无病房科室 | 适用于门诊科室 |
| medical_tech | 医技科室 | 适用于检验科、放射科等医技科室 |
| nursing | 护理单元 | 适用于各护理单元 |
| admin | 行政科室 | 适用于党办、财务科、医保办等行政科室 |
| logistics | 后勤保障科室 | 适用于总务科、采购科、基建科 |
#### 模板指标关联表template_indicators
| 字段名 | 类型 | 约束 | 描述 |
|--------|------|------|------|
| id | Integer | 主键, 自增 | 关联唯一标识符 |
| template_id | Integer | 外键, 非空 | 模板ID |
| indicator_id | Integer | 外键, 非空 | 指标ID |
| category | String(100) | 可选 | 指标分类(二级指标) |
| target_value | Numeric(10,2) | 可选 | 目标值 |
| target_unit | String(50) | 可选 | 目标值单位 |
| weight | Numeric(5,2) | 默认1.0 | 权重 |
| scoring_method | String(50) | 可选 | 评分方法 |
| scoring_params | Text | 可选 | 评分参数JSON |
| sort_order | Integer | 默认0 | 排序 |
| remark | Text | 可选 | 备注 |
| created_at | DateTime | 默认当前时间 | 创建时间 |
| updated_at | DateTime | 更新时自动更新 | 更新时间 |
**章节来源**
- [models.py](file://backend/app/models/models.py#L387-L438)
- [templates.py](file://backend/app/api/v1/templates.py#L1-L272)
## 依赖关系分析
### 数据库迁移历史
系统通过Alembic进行数据库版本管理主要迁移包括
```mermaid
graph LR
Initial[初始版本] --> Template[模板版本]
Initial["初始版本<br/>- 创建科室表<br/>- 创建员工表<br/>- 创建指标表<br/>- 创建考核记录表<br/>- 创建考核明细表<br/>- 创建工资记录表<br/>- 创建用户表"]
Template["模板版本<br/>- 创建指标模板表<br/>- 创建模板指标关联表<br/>- 为指标表添加BSC维度字段"]
```
**图表来源**
- [001_initial.py](file://backend/alembic/versions/001_initial.py#L21-L183)
- [002_template.py](file://backend/alembic/versions/002_template.py#L21-L96)
### 外部依赖关系
系统依赖的关键外部组件:
```mermaid
graph TB
subgraph "核心依赖"
FastAPI[FastAPI Web框架]
SQLAlchemy[SQLAlchemy ORM]
Postgres[PostgreSQL数据库]
Alembic[数据库迁移工具]
end
subgraph "安全依赖"
JWT[JWT令牌]
Bcrypt[密码加密]
OAuth2[OAuth2密码模式]
end
subgraph "配置依赖"
Pydantic[数据验证]
Settings[环境配置]
end
FastAPI --> SQLAlchemy
SQLAlchemy --> Postgres
FastAPI --> JWT
JWT --> Bcrypt
FastAPI --> Pydantic
Pydantic --> Settings
Alembic --> SQLAlchemy
```
**图表来源**
- [database.py](file://backend/app/core/database.py#L1-L39)
- [security.py](file://backend/app/core/security.py#L1-L110)
- [config.py](file://backend/app/core/config.py#L1-L47)
**章节来源**
- [001_initial.py](file://backend/alembic/versions/001_initial.py#L1-L183)
- [002_template.py](file://backend/alembic/versions/002_template.py#L1-L96)
- [database.py](file://backend/app/core/database.py#L1-L39)
## 性能考虑
### 数据库索引优化
系统为关键查询字段建立了适当的索引:
- **indicators表**:按指标类型建立索引,支持快速筛选
- **users表**:按用户名建立唯一索引,确保用户唯一性
- **performance_plans表**:按计划层级、年度、状态建立复合索引
- **menus表**:按父节点、类型、可见性建立索引
- **plan_kpi_relations表**按计划ID、指标ID建立唯一索引
### 缓存策略
建议实现的缓存策略:
1. **枚举类型缓存**:缓存所有枚举定义,避免重复查询
2. **模板数据缓存**:缓存常用模板数据,减少数据库访问
3. **用户权限缓存**:缓存用户权限信息,提高权限检查效率
### 异步处理
系统采用异步数据库连接,支持高并发场景:
- 使用SQLAlchemy AsyncEngine进行异步数据库操作
- 通过AsyncSession管理数据库会话
- 支持异步API路由处理
## 故障排除指南
### 常见问题及解决方案
#### 数据库连接问题
- **症状**:应用启动时报数据库连接错误
- **原因**DATABASE_URL配置不正确
- **解决**:检查.env文件中的数据库连接字符串
#### 权限验证失败
- **症状**API调用返回401或403错误
- **原因**JWT令牌无效或用户权限不足
- **解决**:重新登录获取有效令牌,检查用户角色配置
#### 数据唯一性冲突
- **症状**:创建记录时报唯一约束冲突
- **原因**:重复的编码或用户名
- **解决**:检查输入数据的唯一性约束
#### 模板导入失败
- **症状**:指标模板导入过程中断
- **原因**:模板数据格式不正确或依赖关系缺失
- **解决**验证模板JSON格式确保指标已存在
**章节来源**
- [security.py](file://backend/app/core/security.py#L55-L83)
- [indicators.py](file://backend/app/api/v1/indicators.py#L78-L84)
## 结论
本配置管理表系统通过精心设计的数据模型和完善的权限控制机制,为医院绩效考核管理提供了强大的基础支撑。系统的主要优势包括:
1. **完整的配置管理**:涵盖指标、计划、模板、菜单等核心配置
2. **灵活的权限控制**:多层级权限体系满足不同角色需求
3. **模板化管理**:支持指标模板的创建、管理和复用
4. **数据一致性**:通过外键约束和检查约束保证数据完整性
5. **扩展性强**:模块化设计便于功能扩展和维护
建议在实际部署中重点关注数据库性能优化、缓存策略实施和监控告警机制建设,以确保系统的稳定运行和高性能表现。