修复导入问题

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,11 +72,19 @@ 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:
try:
student_no = student.get("student_no", "").strip() student_no = student.get("student_no", "").strip()
name = student.get("name", "").strip() name = student.get("name", "").strip()
parent_phone = student.get("parent_phone", "").strip() parent_phone = student.get("parent_phone", "").strip()
@@ -94,41 +102,52 @@ class AdminService:
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_id = await StudentModel.create(
student_no=student_no, student_no=student_no,
name=name, name=name,
parent_phone=parent_phone if parent_phone else None, parent_phone=parent_phone if parent_phone else None,
initial_points=initial_points initial_points=initial_points
) )
existing_student_nos.add(student_no)
# 创建学生登录账号
await UserModel.create_student( await UserModel.create_student(
username=student_no, username=student_no,
password=init_password, password=init_password,
real_name=name, real_name=name,
student_id=student_id 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,
"total": len(students), "total": len(students),

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');
} }