From e69668cbbb7335f9639d7e5e6837f664b0be253f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8D=E6=BD=9C?= <1620556043@qq.com> Date: Mon, 24 Mar 2025 14:57:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=80=83=E7=94=9F=E5=AF=BC?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dtos/ExamPaper/CreateExamPaperDto.cs | 1 + .../Dtos/Student/StudentBatchImportDto.cs | 10 +------ .../Implementations/StudentService.cs | 28 ++++++++++--------- .../EventHandlers/UserCreatedEventHandler.cs | 7 +++-- .../Services/IUserService.cs | 4 ++- .../Services/UserService.cs | 10 ++++--- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Src/CodeSpirit.ExamApi/Dtos/ExamPaper/CreateExamPaperDto.cs b/Src/CodeSpirit.ExamApi/Dtos/ExamPaper/CreateExamPaperDto.cs index aed6866..4d7f1df 100644 --- a/Src/CodeSpirit.ExamApi/Dtos/ExamPaper/CreateExamPaperDto.cs +++ b/Src/CodeSpirit.ExamApi/Dtos/ExamPaper/CreateExamPaperDto.cs @@ -69,5 +69,6 @@ public class CreateExamPaperDto Required = true, Placeholder = "请选择题目" )] + [DisplayName("题目列表")] public List QuestionIds { get; set; } } diff --git a/Src/CodeSpirit.ExamApi/Dtos/Student/StudentBatchImportDto.cs b/Src/CodeSpirit.ExamApi/Dtos/Student/StudentBatchImportDto.cs index 0fa9bf7..be3bc72 100644 --- a/Src/CodeSpirit.ExamApi/Dtos/Student/StudentBatchImportDto.cs +++ b/Src/CodeSpirit.ExamApi/Dtos/Student/StudentBatchImportDto.cs @@ -11,14 +11,6 @@ namespace CodeSpirit.ExamApi.Dtos.Student; /// public class StudentBatchImportDto { - /// - /// UserId - /// - [Required(ErrorMessage = "UserId不能为空")] - [DisplayName("UserId")] - [JsonProperty("UserId")] - public long UserId { get; set; } - /// /// 学生姓名 /// @@ -60,7 +52,7 @@ public class StudentBatchImportDto /// /// 准考证 /// - [Required(ErrorMessage = "准考证不能为空")] + //[Required(ErrorMessage = "准考证不能为空")] [StringLength(20, ErrorMessage = "准考证长度不能超过20个字符")] [DisplayName("准考证")] [JsonProperty("准考证")] diff --git a/Src/CodeSpirit.ExamApi/Services/Implementations/StudentService.cs b/Src/CodeSpirit.ExamApi/Services/Implementations/StudentService.cs index 31f1587..ca2d501 100644 --- a/Src/CodeSpirit.ExamApi/Services/Implementations/StudentService.cs +++ b/Src/CodeSpirit.ExamApi/Services/Implementations/StudentService.cs @@ -16,6 +16,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using System.Linq; using System.Linq.Expressions; +using CodeSpirit.Core.Extensions; namespace CodeSpirit.ExamApi.Services.Implementations; @@ -246,12 +247,13 @@ public class StudentService : BaseCRUDIService protected override string GetImportItemId(StudentBatchImportDto importDto) { - return importDto.StudentNumber; + return importDto.IdNo; } @@ -354,22 +356,21 @@ public class StudentService : BaseCRUDIService(); var failedItems = new List(); - var studentNumberRepetition = importList.GroupBy(x => x.StudentNumber).Select(s => new { studentNumber = s.Key, count = s.Count() }); + var studentNumberRepetition = importList.Where(s => !s.StudentNumber.IsNullOrWhiteSpace()).GroupBy(x => x.StudentNumber).Select(s => new { studentNumber = s.Key, count = s.Count() }); if (studentNumberRepetition.Any(x => x.count > 1)) { var error = string.Join(",", studentNumberRepetition.Where(x => x.count > 1).Select(x => x.studentNumber)); failedItems.Add($"导入数据中出现重复的学号:{error}"); } - var idNoRepetition = importList.GroupBy(x => x.IdNo).Select(s => new { idNo = s.Key, count = s.Count() }); + var idNoRepetition = importList.Where(s => !s.IdNo.IsNullOrWhiteSpace()).GroupBy(x => x.IdNo).Select(s => new { idNo = s.Key, count = s.Count() }); if (idNoRepetition.Any(x => x.count > 1)) { var error = string.Join(",", idNoRepetition.Where(x => x.count > 1).Select(x => x.idNo)); failedItems.Add($"导入数据中出现重复的身份证:{error}"); } - var admissionTicketRepetition = importList.GroupBy(x => x.AdmissionTicket).Select(s => new { admissionTicket = s.Key, count = s.Count() }); + var admissionTicketRepetition = importList.Where(s => !s.AdmissionTicket.IsNullOrWhiteSpace()).GroupBy(x => x.AdmissionTicket).Select(s => new { admissionTicket = s.Key, count = s.Count() }); if (admissionTicketRepetition.Any(x => x.count > 1)) { var error = string.Join(",", admissionTicketRepetition.Where(x => x.count > 1).Select(x => x.admissionTicket)); @@ -385,17 +386,17 @@ public class StudentService : BaseCRUDIService x.IdNo == item.IdNo)) + if (!item.IdNo.IsNullOrWhiteSpace() && checkDatas.Any(x => x.IdNo == item.IdNo)) { failedItems.Add($"{item.IdNo}「传入的身份证'{item.IdNo}'已存在"); continue; } - if (checkDatas.Any(x => x.AdmissionTicket == item.AdmissionTicket)) + if (!item.AdmissionTicket.IsNullOrWhiteSpace() && checkDatas.Any(x => x.AdmissionTicket == item.AdmissionTicket)) { failedItems.Add($"{item.AdmissionTicket}「传入的准考证'{item.AdmissionTicket}'已存在"); continue; } - if (checkDatas.Any(x => x.StudentNumber == item.StudentNumber)) + if (!item.StudentNumber.IsNullOrWhiteSpace() && checkDatas.Any(x => x.StudentNumber == item.StudentNumber)) { failedItems.Add($"{item.StudentNumber}「传入的学工号'{item.StudentNumber}'已存在"); continue; @@ -418,11 +419,12 @@ public class StudentService : BaseCRUDIService { // 创建新用户 _logger.LogInformation("用户创建事件:创建新用户, 姓名: {@Name}", @event.Name); - + _logger.LogError("用户创建事件:创建新用户, 姓名: {Name}", @event.Name); var createUserDto = new CreateUserDto { UserName = @event.UserName, @@ -67,8 +68,8 @@ public class UserCreatedEventHandler : IEventHandler Email = @event.Email, IdNo = @event.IdNo, }; - - await _userService.CreateAdvancedUserAsync(createUserDto, "123456", -1); + var pwd = @event.IdNo.@IsNullOrWhiteSpace() || @event.IdNo.Length < 6 ? "123456" : @event.IdNo[^6..]; + await _userService.CreateAdvancedUserAsync(createUserDto, pwd.ToUpper(), -1, @event.UserId); } } catch (Exception ex) diff --git a/Src/CodeSpirit.IdentityApi/Services/IUserService.cs b/Src/CodeSpirit.IdentityApi/Services/IUserService.cs index a6b72b5..df67de7 100644 --- a/Src/CodeSpirit.IdentityApi/Services/IUserService.cs +++ b/Src/CodeSpirit.IdentityApi/Services/IUserService.cs @@ -76,10 +76,12 @@ public interface IUserService : IBaseCRUDIService用户创建数据传输对象 /// 指定的用户密码,如为null则自动生成随机密码 /// 创建者ID + /// userId不为空时,将userId作为新创建的用户的Id,否则将自动生成Id /// 创建者名称 /// 创建的用户数据传输对象 Task CreateAdvancedUserAsync( CreateUserDto createDto, string password = null, - long? creatorId = null); + long? creatorId = null, + long? userId = null); } \ No newline at end of file diff --git a/Src/CodeSpirit.IdentityApi/Services/UserService.cs b/Src/CodeSpirit.IdentityApi/Services/UserService.cs index a9ff18a..b2347bb 100644 --- a/Src/CodeSpirit.IdentityApi/Services/UserService.cs +++ b/Src/CodeSpirit.IdentityApi/Services/UserService.cs @@ -776,18 +776,20 @@ public class UserService : BaseCRUDIService指定的用户密码,如为null则自动生成随机密码 /// 创建者ID /// 是否发送密码邮件 + /// userId不为空时,将userId作为新创建的用户的Id,否则将自动生成Id /// 创建的用户数据传输对象 public async Task CreateAdvancedUserAsync( - CreateUserDto createDto, - string password = null, - long? creatorId = null) + CreateUserDto createDto, + string password = null, + long? creatorId = null, + long? userId = null) { // 验证用户名和邮箱 await ValidateCreateDto(createDto); // 创建用户实体 var user = Mapper.Map(createDto); - user.Id = _idGenerator.NewId(); + user.Id = userId.HasValue && userId.Value != default ? userId.Value : _idGenerator.NewId(); // 记录创建者信息 if (creatorId.HasValue) -- Gitee