396 lines
8.0 KiB
Markdown
396 lines
8.0 KiB
Markdown
# 批量排班接口测试
|
|
|
|
## 接口信息
|
|
|
|
**URL**: `POST /adminapi/doctor.roster/batchSave`
|
|
|
|
**Content-Type**: `application/json`
|
|
|
|
## 测试用例
|
|
|
|
### 测试1:批量创建工作日排班
|
|
|
|
为2位医生创建一周工作日的排班(周一至周五,上午和下午)。
|
|
|
|
**请求体**:
|
|
```json
|
|
{
|
|
"rosters": [
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026-03-02",
|
|
"period": "morning",
|
|
"status": 1,
|
|
"quota": 20,
|
|
"max_patients": 30,
|
|
"remark": "工作日排班"
|
|
},
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026-03-02",
|
|
"period": "afternoon",
|
|
"status": 1,
|
|
"quota": 20,
|
|
"max_patients": 30,
|
|
"remark": "工作日排班"
|
|
},
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026-03-03",
|
|
"period": "morning",
|
|
"status": 1,
|
|
"quota": 20,
|
|
"max_patients": 30,
|
|
"remark": "工作日排班"
|
|
},
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026-03-03",
|
|
"period": "afternoon",
|
|
"status": 1,
|
|
"quota": 20,
|
|
"max_patients": 30,
|
|
"remark": "工作日排班"
|
|
},
|
|
{
|
|
"doctor_id": 2,
|
|
"date": "2026-03-02",
|
|
"period": "morning",
|
|
"status": 1,
|
|
"quota": 15,
|
|
"max_patients": 25,
|
|
"remark": "工作日排班"
|
|
},
|
|
{
|
|
"doctor_id": 2,
|
|
"date": "2026-03-02",
|
|
"period": "afternoon",
|
|
"status": 1,
|
|
"quota": 15,
|
|
"max_patients": 25,
|
|
"remark": "工作日排班"
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
**预期结果**:
|
|
```json
|
|
{
|
|
"code": 1,
|
|
"msg": "批量保存成功",
|
|
"data": true
|
|
}
|
|
```
|
|
|
|
### 测试2:批量设置周末休息
|
|
|
|
为所有医生设置周末休息。
|
|
|
|
**请求体**:
|
|
```json
|
|
{
|
|
"rosters": [
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026-03-07",
|
|
"period": "morning",
|
|
"status": 3,
|
|
"quota": 0,
|
|
"max_patients": 0,
|
|
"remark": "周末休息"
|
|
},
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026-03-07",
|
|
"period": "afternoon",
|
|
"status": 3,
|
|
"quota": 0,
|
|
"max_patients": 0,
|
|
"remark": "周末休息"
|
|
},
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026-03-08",
|
|
"period": "morning",
|
|
"status": 3,
|
|
"quota": 0,
|
|
"max_patients": 0,
|
|
"remark": "周末休息"
|
|
},
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026-03-08",
|
|
"period": "afternoon",
|
|
"status": 3,
|
|
"quota": 0,
|
|
"max_patients": 0,
|
|
"remark": "周末休息"
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
### 测试3:更新已存在的排班
|
|
|
|
如果某个排班已存在,应该更新而不是创建新记录。
|
|
|
|
**第一次请求**:
|
|
```json
|
|
{
|
|
"rosters": [
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026-03-04",
|
|
"period": "morning",
|
|
"status": 1,
|
|
"quota": 20,
|
|
"max_patients": 30
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
**第二次请求(更新)**:
|
|
```json
|
|
{
|
|
"rosters": [
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026-03-04",
|
|
"period": "morning",
|
|
"status": 2,
|
|
"quota": 0,
|
|
"max_patients": 0,
|
|
"remark": "临时停诊"
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
**预期结果**: 第二次请求应该更新第一次创建的记录,而不是创建新记录。
|
|
|
|
### 测试4:空数组
|
|
|
|
**请求体**:
|
|
```json
|
|
{
|
|
"rosters": []
|
|
}
|
|
```
|
|
|
|
**预期结果**: 应该返回成功,但不创建任何记录。
|
|
|
|
### 测试5:数据验证
|
|
|
|
**请求体(缺少必填字段)**:
|
|
```json
|
|
{
|
|
"rosters": [
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026-03-04"
|
|
// 缺少 period 和 status
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
**预期结果**: 应该返回验证错误。
|
|
|
|
## 使用 cURL 测试
|
|
|
|
### 基本测试
|
|
|
|
```bash
|
|
curl -X POST http://your-domain/adminapi/doctor.roster/batchSave \
|
|
-H "Content-Type: application/json" \
|
|
-H "token: your-token-here" \
|
|
-d '{
|
|
"rosters": [
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026-03-04",
|
|
"period": "morning",
|
|
"status": 1,
|
|
"quota": 20,
|
|
"max_patients": 30
|
|
}
|
|
]
|
|
}'
|
|
```
|
|
|
|
### 批量测试(10条记录)
|
|
|
|
```bash
|
|
curl -X POST http://your-domain/adminapi/doctor.roster/batchSave \
|
|
-H "Content-Type: application/json" \
|
|
-H "token: your-token-here" \
|
|
-d @batch_roster_data.json
|
|
```
|
|
|
|
**batch_roster_data.json**:
|
|
```json
|
|
{
|
|
"rosters": [
|
|
{"doctor_id": 1, "date": "2026-03-04", "period": "morning", "status": 1, "quota": 20, "max_patients": 30},
|
|
{"doctor_id": 1, "date": "2026-03-04", "period": "afternoon", "status": 1, "quota": 20, "max_patients": 30},
|
|
{"doctor_id": 1, "date": "2026-03-05", "period": "morning", "status": 1, "quota": 20, "max_patients": 30},
|
|
{"doctor_id": 1, "date": "2026-03-05", "period": "afternoon", "status": 1, "quota": 20, "max_patients": 30},
|
|
{"doctor_id": 1, "date": "2026-03-06", "period": "morning", "status": 1, "quota": 20, "max_patients": 30},
|
|
{"doctor_id": 1, "date": "2026-03-06", "period": "afternoon", "status": 1, "quota": 20, "max_patients": 30},
|
|
{"doctor_id": 1, "date": "2026-03-07", "period": "morning", "status": 3, "quota": 0, "max_patients": 0},
|
|
{"doctor_id": 1, "date": "2026-03-07", "period": "afternoon", "status": 3, "quota": 0, "max_patients": 0},
|
|
{"doctor_id": 1, "date": "2026-03-08", "period": "morning", "status": 3, "quota": 0, "max_patients": 0},
|
|
{"doctor_id": 1, "date": "2026-03-08", "period": "afternoon", "status": 3, "quota": 0, "max_patients": 0}
|
|
]
|
|
}
|
|
```
|
|
|
|
## 验证结果
|
|
|
|
### 1. 检查数据库
|
|
|
|
```sql
|
|
-- 查看批量创建的排班
|
|
SELECT * FROM la_doctor_roster
|
|
WHERE doctor_id = 1
|
|
AND date BETWEEN '2026-03-04' AND '2026-03-08'
|
|
ORDER BY date, period;
|
|
```
|
|
|
|
### 2. 检查唯一索引
|
|
|
|
```sql
|
|
-- 尝试插入重复记录,应该失败
|
|
INSERT INTO la_doctor_roster (doctor_id, date, period, status, create_time, update_time)
|
|
VALUES (1, '2026-03-04', 'morning', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP());
|
|
-- 错误: Duplicate entry '1-2026-03-04-morning' for key 'uk_doctor_date_period'
|
|
```
|
|
|
|
### 3. 检查更新逻辑
|
|
|
|
```sql
|
|
-- 第一次插入
|
|
-- 第二次用相同的 doctor_id, date, period 但不同的 status
|
|
-- 应该更新而不是插入新记录
|
|
SELECT COUNT(*) FROM la_doctor_roster
|
|
WHERE doctor_id = 1 AND date = '2026-03-04' AND period = 'morning';
|
|
-- 应该返回 1,不是 2
|
|
```
|
|
|
|
## 性能测试
|
|
|
|
### 测试100条记录
|
|
|
|
```bash
|
|
# 生成100条记录的JSON
|
|
php generate_test_data.php 100 > test_100.json
|
|
|
|
# 测试
|
|
time curl -X POST http://your-domain/adminapi/doctor.roster/batchSave \
|
|
-H "Content-Type: application/json" \
|
|
-H "token: your-token-here" \
|
|
-d @test_100.json
|
|
```
|
|
|
|
**预期**: 应该在2秒内完成
|
|
|
|
### 测试500条记录
|
|
|
|
```bash
|
|
php generate_test_data.php 500 > test_500.json
|
|
|
|
time curl -X POST http://your-domain/adminapi/doctor.roster/batchSave \
|
|
-H "Content-Type: application/json" \
|
|
-H "token: your-token-here" \
|
|
-d @test_500.json
|
|
```
|
|
|
|
**预期**: 应该在10秒内完成
|
|
|
|
## 错误处理测试
|
|
|
|
### 1. 事务回滚测试
|
|
|
|
在批量数据中插入一条错误数据,整个批量操作应该回滚。
|
|
|
|
```json
|
|
{
|
|
"rosters": [
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026-03-04",
|
|
"period": "morning",
|
|
"status": 1,
|
|
"quota": 20,
|
|
"max_patients": 30
|
|
},
|
|
{
|
|
"doctor_id": 999999,
|
|
"date": "2026-03-04",
|
|
"period": "morning",
|
|
"status": 1,
|
|
"quota": 20,
|
|
"max_patients": 30
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
**预期**: 如果第二条记录失败,第一条也不应该被保存。
|
|
|
|
### 2. 日期格式错误
|
|
|
|
```json
|
|
{
|
|
"rosters": [
|
|
{
|
|
"doctor_id": 1,
|
|
"date": "2026/03/04",
|
|
"period": "morning",
|
|
"status": 1,
|
|
"quota": 20,
|
|
"max_patients": 30
|
|
}
|
|
]
|
|
}
|
|
```
|
|
|
|
**预期**: 返回日期格式错误
|
|
|
|
## 常见问题排查
|
|
|
|
### 问题1: 批量保存失败
|
|
|
|
**检查**:
|
|
1. 数据库连接是否正常
|
|
2. 表是否存在
|
|
3. 字段类型是否匹配
|
|
4. 唯一索引是否正确
|
|
|
|
### 问题2: 部分记录保存失败
|
|
|
|
**检查**:
|
|
1. 查看错误日志
|
|
2. 检查是否有数据验证失败
|
|
3. 检查是否有外键约束
|
|
|
|
### 问题3: 性能问题
|
|
|
|
**优化**:
|
|
1. 减少单次批量的记录数
|
|
2. 添加数据库索引
|
|
3. 使用批量插入而不是循环插入
|
|
|
|
## 总结
|
|
|
|
批量排班接口已经完整实现,包括:
|
|
- ✅ 批量创建排班
|
|
- ✅ 批量更新排班
|
|
- ✅ 事务处理
|
|
- ✅ 数据验证
|
|
- ✅ 错误处理
|
|
|
|
可以正常使用了!
|