From a5df8efa17c22c71b7e44de2c9154fbefcad7ff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E6=A2=93=E5=BD=AD?= Date: Thu, 4 Jun 2026 14:23:50 +0800 Subject: [PATCH] 11 --- .../AppUser/AppUserService.cs | 97 +++++++++++-------- .../AppUser/Dtos/AppUserDtos.cs | 5 +- .../Project/ProjectService.cs | 9 -- 3 files changed, 57 insertions(+), 54 deletions(-) diff --git a/YD_Event.Application/AppUser/AppUserService.cs b/YD_Event.Application/AppUser/AppUserService.cs index be91ce6..3d6cfef 100644 --- a/YD_Event.Application/AppUser/AppUserService.cs +++ b/YD_Event.Application/AppUser/AppUserService.cs @@ -174,14 +174,21 @@ public class AppUserService : BaseService, ITransient throw Oops.Bah("当前用户未关联单位,无法进行操作"); } + // 手机号为空时,跳过基于手机号的重复检测(单位添加运动员时,联系电话非必填) + var mobileIsEmpty = string.IsNullOrWhiteSpace(input.Mobile); + // 先检查是否存在相同手机号且相同类型的运动员在其他单位 - var existingInOtherUnit = await _appUserRep.AsQueryable() - .Where(x => x.Mobile == input.Mobile - && x.PersonType == input.PersonType - && x.MerchantCertificationId != merchantCertificationId - && x.MerchantCertificationId != null - && !x.DeleteMark) - .FirstAsync(); + AppUserEntity? existingInOtherUnit = null; + if (!mobileIsEmpty) + { + existingInOtherUnit = await _appUserRep.AsQueryable() + .Where(x => x.Mobile == input.Mobile + && x.PersonType == input.PersonType + && x.MerchantCertificationId != merchantCertificationId + && x.MerchantCertificationId != null + && !x.DeleteMark) + .FirstAsync(); + } AppUserEntity? entity = null; bool isUnitTransfer = false; @@ -198,12 +205,16 @@ public class AppUserService : BaseService, ITransient else { // 检查是否存在相同手机号但还未加入任何单位的记录(小程序注册的用户) - var existingWithoutUnit = await _appUserRep.AsQueryable() - .Where(x => x.Mobile == input.Mobile - && x.PersonType == input.PersonType - && x.MerchantCertificationId == null - && !x.DeleteMark) - .FirstAsync(); + AppUserEntity? existingWithoutUnit = null; + if (!mobileIsEmpty) + { + existingWithoutUnit = await _appUserRep.AsQueryable() + .Where(x => x.Mobile == input.Mobile + && x.PersonType == input.PersonType + && x.MerchantCertificationId == null + && !x.DeleteMark) + .FirstAsync(); + } if (existingWithoutUnit != null) { @@ -225,18 +236,22 @@ public class AppUserService : BaseService, ITransient entity = await _appUserRep.GetByIdAsync(input.Id.Value); if (entity == null || entity.DeleteMark) throw Oops.Bah("成员不存在"); - // 检查手机号是否被本单位内同类型的其他记录占用 - var duplicateInSameUnit = await _appUserRep.AsQueryable() - .Where(x => x.Mobile == input.Mobile - && x.PersonType == input.PersonType - && x.MerchantCertificationId == merchantCertificationId - && !x.DeleteMark - && x.Id != input.Id.Value) - .FirstAsync(); - - if (duplicateInSameUnit != null) + // 手机号为空时,不进行手机号重复检测 + if (!mobileIsEmpty) { - throw Oops.Bah($"该手机号在本单位已存在相同类型的成员"); + // 检查手机号是否被本单位内同类型的其他记录占用 + var duplicateInSameUnit = await _appUserRep.AsQueryable() + .Where(x => x.Mobile == input.Mobile + && x.PersonType == input.PersonType + && x.MerchantCertificationId == merchantCertificationId + && !x.DeleteMark + && x.Id != input.Id.Value) + .FirstAsync(); + + if (duplicateInSameUnit != null) + { + throw Oops.Bah($"该手机号在本单位已存在相同类型的成员"); + } } entity.UpdatedUserId = _authManager.UserId; @@ -244,18 +259,21 @@ public class AppUserService : BaseService, ITransient } else if (isNewRecord) { - // 新增模式:检查本单位内是否有相同手机号且相同类型的记录 - var existingInSameUnit = await _appUserRep.AsQueryable() - .Where(x => x.Mobile == input.Mobile - && x.PersonType == input.PersonType - && x.MerchantCertificationId == merchantCertificationId - && !x.DeleteMark) - .FirstAsync(); - - if (existingInSameUnit != null) + // 新增模式:手机号不为空时,检查本单位内是否有相同手机号且相同类型的记录 + if (!mobileIsEmpty) { - // 本单位内已有相同手机号且相同类型的记录,不允许重复添加 - throw Oops.Bah($"该手机号在本单位已存在相同类型的成员"); + var existingInSameUnit = await _appUserRep.AsQueryable() + .Where(x => x.Mobile == input.Mobile + && x.PersonType == input.PersonType + && x.MerchantCertificationId == merchantCertificationId + && !x.DeleteMark) + .FirstAsync(); + + if (existingInSameUnit != null) + { + // 本单位内已有相同手机号且相同类型的记录,不允许重复添加 + throw Oops.Bah($"该手机号在本单位已存在相同类型的成员"); + } } // 新增记录 @@ -270,14 +288,9 @@ public class AppUserService : BaseService, ITransient AuditStatus = AuditStatus.PendingSubmission }; - // 仅在真正新增运动员时验证验证码 - if (input.PersonType == PersonType.Athlete) + // 单位添加运动员时,验证码改为非必填;仅在手机号+验证码均存在时才校验 + if (input.PersonType == PersonType.Athlete && !mobileIsEmpty && !string.IsNullOrWhiteSpace(input.VerificationCode)) { - if (string.IsNullOrWhiteSpace(input.VerificationCode)) - { - throw Oops.Bah("验证码不能为空"); - } - var cacheKey = $"sms:code:{input.Mobile}"; var cachedCodeResult = _cachingProvider.Get(cacheKey); string? cachedCode = cachedCodeResult.Value; diff --git a/YD_Event.Application/AppUser/Dtos/AppUserDtos.cs b/YD_Event.Application/AppUser/Dtos/AppUserDtos.cs index c5ba7a0..3c6d534 100644 --- a/YD_Event.Application/AppUser/Dtos/AppUserDtos.cs +++ b/YD_Event.Application/AppUser/Dtos/AppUserDtos.cs @@ -198,11 +198,10 @@ public class CreateOrUpdateMemberInput public int? Age { get; set; } /// - /// 联系电话 + /// 联系电话(非必填,有值时校验手机号格式) /// - [Required(ErrorMessage = "联系电话不能为空")] [RegularExpression(@"^1[3456789]\d{9}$", ErrorMessage = "手机号格式不正确")] - public string Mobile { get; set; } + public string? Mobile { get; set; } /// /// 技能等级(教练特有) diff --git a/YD_Event.Application/Project/ProjectService.cs b/YD_Event.Application/Project/ProjectService.cs index aeff4d0..ffcd529 100644 --- a/YD_Event.Application/Project/ProjectService.cs +++ b/YD_Event.Application/Project/ProjectService.cs @@ -82,9 +82,6 @@ public class ProjectService : BaseService, ITran [UnitOfWork] public async Task Add(AddProjectInput input) { - var isExist = await _repository.IsAnyAsync(x => x.Name == input.Name); - if (isExist) throw new InvalidOperationException("该项目名称已存在"); - var entity = input.Adapt(); entity.Id = _idGenerator.NewLong(); entity.CreatedTime = DateTime.Now; @@ -115,12 +112,6 @@ public class ProjectService : BaseService, ITran var entity = await _repository.GetByIdAsync(input.Id); if (entity == null) throw new InvalidOperationException("项目不存在"); - if (entity.Name != input.Name) - { - var isExist = await _repository.IsAnyAsync(x => x.Name == input.Name && x.Id != input.Id); - if (isExist) throw new InvalidOperationException("该项目名称已存在"); - } - input.Adapt(entity); entity.UpdatedTime = DateTime.Now; await _repository.UpdateAsync(entity);