diff --git a/VOL.Business/IServices/School/IS_StudentService.cs b/VOL.Business/IServices/School/IS_StudentService.cs
index a649615..2a4b248 100644
--- a/VOL.Business/IServices/School/IS_StudentService.cs
+++ b/VOL.Business/IServices/School/IS_StudentService.cs
@@ -91,7 +91,7 @@ namespace VOL.Business.IServices.School
///
///
///
- Task UploadPhoto(IFormFile file, int gradeId, int classId, string studentNo, string studentName);
+ Task UploadPhoto(IFormFile file, int gradeId, int classId, string studentNo, string? studentName);
///
/// 批量导入学生头像
diff --git a/VOL.Business/Services/School/S_StudentService.cs b/VOL.Business/Services/School/S_StudentService.cs
index 8602f10..94f4e7f 100644
--- a/VOL.Business/Services/School/S_StudentService.cs
+++ b/VOL.Business/Services/School/S_StudentService.cs
@@ -294,13 +294,20 @@ namespace VOL.Business.Services.School
public async Task AddStudent(AddStudentParam paramDto)
{
- var stuExists = await _studentRepository.ExistsAsync(x => x.StudentNo == paramDto.StudentNo);
+ if (paramDto == null || string.IsNullOrWhiteSpace(paramDto.StudentNo))
+ throw new ArgumentNullException("学生学号必填");
+
+ var tenantId = UserContext.Current.TenantId;
+ paramDto.StudentNo = paramDto.StudentNo.Trim();
+
+ var stuExists = await _studentRepository.ExistsAsync(x =>
+ x.SchoolCode == tenantId && x.StudentNo == paramDto.StudentNo);
if (stuExists)
throw new Exception($"学号{paramDto.StudentNo}的学生已存在!");
var studentEntity = _mapper.Map(paramDto);
studentEntity.StudentStatus = StudentStatus.Normal;
- studentEntity.SchoolCode = UserContext.Current.TenantId;
+ studentEntity.SchoolCode = tenantId;
studentEntity.Creator = UserContext.Current.UserId;
studentEntity.CreateDate = DateTime.Now;
@@ -324,16 +331,23 @@ namespace VOL.Business.Services.School
}
public async Task ModifyStudent(AddStudentParam paramDto)
{
- if (paramDto == null || string.IsNullOrEmpty(paramDto.StudentNo))
+ if (paramDto == null || string.IsNullOrWhiteSpace(paramDto.StudentNo))
{
throw new ArgumentNullException("学生学号必填");
}
- var student = await _studentRepository.FindAsyncFirst(x => x.Id == paramDto.Id);
+ var tenantId = UserContext.Current.TenantId;
+ paramDto.StudentNo = paramDto.StudentNo.Trim();
+
+ var student = await _studentRepository.FindAsyncFirst(x =>
+ x.Id == paramDto.Id && x.SchoolCode == tenantId);
if (student == null)
throw new ArgumentNullException("未找到要更新的数据");
- var stuExists = await _studentRepository.ExistsAsync(x => x.StudentNo == paramDto.StudentNo && x.Id != student.Id);
+ var stuExists = await _studentRepository.ExistsAsync(x =>
+ x.SchoolCode == tenantId &&
+ x.StudentNo == paramDto.StudentNo &&
+ x.Id != student.Id);
if (stuExists)
throw new Exception($"学号{paramDto.StudentNo}的学生已存在!");
@@ -676,7 +690,7 @@ namespace VOL.Business.Services.School
///
/// 上传学生头像
///
- public async Task UploadPhoto(IFormFile file, int gradeId, int classId, string studentNo, string studentName)
+ public async Task UploadPhoto(IFormFile file, int gradeId, int classId, string studentNo, string? studentName)
{
const long maxFileSize = 2 * 1024 * 1024;
@@ -686,6 +700,17 @@ namespace VOL.Business.Services.School
throw new Exception("单张人脸照片大小不能超过2MB");
if (string.IsNullOrWhiteSpace(studentNo))
throw new Exception("学生学号不能为空");
+
+ // 编辑时上传组件可能只传文件和学号,此时使用当前学校已有的学生姓名。
+ // 新增学生尚未入库,仍需要客户端显式传入姓名。
+ if (string.IsNullOrWhiteSpace(studentName))
+ {
+ studentName = await _studentRepository.DbContext.Set()
+ .AsNoTracking()
+ .Where(x => x.SchoolCode == UserContext.Current.TenantId && x.StudentNo == studentNo)
+ .Select(x => x.StudentName)
+ .FirstOrDefaultAsync();
+ }
if (string.IsNullOrWhiteSpace(studentName))
throw new Exception("学生姓名不能为空");
diff --git a/VOL.WebApi/Controllers/Business/StudentController.cs b/VOL.WebApi/Controllers/Business/StudentController.cs
index 1d1a7ba..8fb19b9 100644
--- a/VOL.WebApi/Controllers/Business/StudentController.cs
+++ b/VOL.WebApi/Controllers/Business/StudentController.cs
@@ -179,6 +179,7 @@ namespace VOL.WebApi.Controllers.Business
/// 年级Id
/// 班级Id
/// 学生学号
+ /// 学生姓名;编辑已有学生时可不传
///
[HttpPost(nameof(UploadPhoto))]