修复导入问题

This commit is contained in:
2026-04-21 16:40:35 +08:00
parent fcf143593a
commit 194c076456
2 changed files with 76 additions and 45 deletions

View File

@@ -72,62 +72,81 @@ class AdminService:
operator_id: int, operator_id: int,
initial_points: int = 60 initial_points: int = 60
) -> Dict[str, Any]: ) -> Dict[str, Any]:
"""批量导入学生""" """批量导入学生(优化版:预查重 + 批量操作)"""
results = [] results = []
success_count = 0 success_count = 0
# 预查重:一次性获取所有已存在的学号和手机号
existing_students = await StudentModel.get_all()
existing_student_nos = {s["student_no"] for s in existing_students}
all_users = await execute_query("SELECT username FROM users WHERE status = 1")
existing_usernames = {u["username"] for u in all_users}
for student in students: for student in students:
student_no = student.get("student_no", "").strip() try:
name = student.get("name", "").strip() student_no = student.get("student_no", "").strip()
parent_phone = student.get("parent_phone", "").strip() name = student.get("name", "").strip()
password = student.get("password", "").strip() parent_phone = student.get("parent_phone", "").strip()
password = student.get("password", "").strip()
if not student_no or not name: if not student_no or not name:
results.append({"student_no": student_no, "success": False, "error": "学号或姓名不能为空"}) results.append({"student_no": student_no, "success": False, "error": "学号或姓名不能为空"})
continue continue
if not security.validate_student_no(student_no): if not security.validate_student_no(student_no):
results.append({"student_no": student_no, "success": False, "error": "学号格式错误"}) results.append({"student_no": student_no, "success": False, "error": "学号格式错误"})
continue continue
if parent_phone and not security.validate_phone(parent_phone): if parent_phone and not security.validate_phone(parent_phone):
results.append({"student_no": student_no, "success": False, "error": "手机号格式错误"}) results.append({"student_no": student_no, "success": False, "error": "手机号格式错误"})
continue continue
existing = await StudentModel.get_by_student_no(student_no) if student_no in existing_student_nos:
if existing: results.append({"student_no": student_no, "success": False, "error": "学号已存在"})
results.append({"student_no": student_no, "success": False, "error": "学号已存在"}) continue
continue
init_password = password if password else "123456" init_password = password if password else "123456"
student_id = await StudentModel.create( # 创建学生记录
student_no=student_no, student_id = await StudentModel.create(
name=name, student_no=student_no,
parent_phone=parent_phone if parent_phone else None, name=name,
initial_points=initial_points parent_phone=parent_phone if parent_phone else None,
) initial_points=initial_points
)
existing_student_nos.add(student_no)
await UserModel.create_student( # 创建学生登录账号
username=student_no, await UserModel.create_student(
password=init_password, username=student_no,
real_name=name, password=init_password,
student_id=student_id real_name=name,
) student_id=student_id
)
existing_usernames.add(student_no)
if parent_phone: # 创建家长账号(如果手机号存在且未被注册)
parent_exists = await UserModel.get_by_username(parent_phone) if parent_phone and parent_phone not in existing_usernames:
if not parent_exists:
await UserModel.create_parent( await UserModel.create_parent(
username=parent_phone, username=parent_phone,
password=init_password, password=init_password,
real_name=f"{name}家长", real_name=f"{name}家长",
student_id=student_id student_id=student_id
) )
existing_usernames.add(parent_phone)
results.append({"student_no": student_no, "success": True, "student_id": student_id}) results.append({"student_no": student_no, "success": True, "student_id": student_id})
success_count += 1 success_count += 1
logger.info(f"用户[{operator_id}] 导入学生: {student_no} - {name}") logger.info(f"用户[{operator_id}] 导入学生: {student_no} - {name}")
except Exception as e:
logger.error(f"导入学生失败: {student.get('student_no', '?')} - {str(e)}")
results.append({
"student_no": student.get("student_no", ""),
"success": False,
"error": f"导入异常: {str(e)}"
})
return { return {
"success": True, "success": True,

View File

@@ -134,6 +134,18 @@ async function doImport() {
showToast(result.message); showToast(result.message);
closeModal('importModal'); closeModal('importModal');
loadStudents(); loadStudents();
// 显示详细导入结果
if (result.data && result.data.results) {
const failedList = result.data.results.filter(r => !r.success);
if (failedList.length > 0) {
let detail = '失败详情:\n';
failedList.forEach(r => {
detail += `${r.student_no || '未知'}: ${r.error}\n`;
});
alert(detail);
}
}
} else { } else {
showToast(result.message, 'error'); showToast(result.message, 'error');
} }