using YD_Event.Application.AppUser.Dtos; using YD_Event.Application.Auth; using YD_Event.Core.Entities; using AppUserEntity = YD_Event.Core.Entities.AppUser; using YD_Event.Core.Infrastructure.Aliyun.Ocr; using YD_Event.Core.Enum; using Mapster; namespace YD_Event.Application.AppUser; /// /// 普通用户服务 /// public class AppUserService : BaseService, ITransient { private readonly ISqlSugarRepository _appUserRep; private readonly ISqlSugarClient _db; private readonly AuthManager _authManager; private readonly OcrService _ocrService; public AppUserService(ISqlSugarRepository appUserRep, ISqlSugarClient db, AuthManager authManager, OcrService ocrService) : base(appUserRep) { _appUserRep = appUserRep; _db = db; _authManager = authManager; _ocrService = ocrService; } /// /// 分页查询普通用户 /// /// /// [DisplayName("分页查询普通用户")] [HttpGet("page")] public async Task> Page([FromQuery] AppUserPageInput input) { var query = _appUserRep.AsQueryable() .Where(u => u.PersonType == PersonType.User) // 仅查询普通用户 .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name)) .WhereIF(input.Gender.HasValue, u => u.Gender == input.Gender) .WhereIF(!string.IsNullOrWhiteSpace(input.Mobile), u => u.Mobile.Contains(input.Mobile)) .WhereIF(input.AuditStatus.HasValue, u => u.AuditStatus == input.AuditStatus) .WhereIF(input.StartTime.HasValue, u => u.CreatedTime >= input.StartTime) .WhereIF(input.EndTime.HasValue, u => u.CreatedTime <= input.EndTime) .OrderByDescending(u => u.CreatedTime); return await query.Select().ToPagedListAsync(input); } /// /// 获取普通用户详情 /// /// /// [DisplayName("获取普通用户详情")] [HttpGet("{id}")] public async Task Detail([FromRoute] long id) { return await _appUserRep.AsQueryable() .Where(u => u.Id == id) .Select() .FirstAsync(); } /// /// 审核普通用户 /// /// /// [DisplayName("审核普通用户")] [HttpPost("audit")] [UnitOfWork] public async Task Audit([FromBody] AuditInput input) { if (input.Ids == null || !input.Ids.Any()) return; // 更新状态 await _appUserRep.UpdateAsync(u => new AppUserEntity { AuditStatus = input.Status, AuditRemark = input.Remark, UpdatedTime = DateTime.Now, UpdatedUserId = _authManager.UserId }, u => input.Ids.Contains(u.Id)); } #region 统一成员管理 (领队/教练/运动员) /// /// 统一分页查询成员 /// /// /// [HttpGet("member/page")] [DisplayName("统一分页查询成员")] public async Task> MemberPage([FromQuery] MemberQueryInput input) { var (userId, merchantId) = await GetCurrentUserAndMerchantId(); var query = _appUserRep.AsQueryable() .Where(x => x.PersonType == input.PersonType && !x.DeleteMark) .WhereIF(!string.IsNullOrWhiteSpace(input.Name), x => x.Name.Contains(input.Name)) .WhereIF(input.Gender.HasValue, x => x.Gender == input.Gender) .WhereIF(!string.IsNullOrWhiteSpace(input.Phone), x => x.Mobile.Contains(input.Phone)) .WhereIF(!string.IsNullOrWhiteSpace(input.IdCardNumber), u => u.IdCardNumber.Contains(input.IdCardNumber)) .WhereIF(input.PersonType == PersonType.Coach && !string.IsNullOrWhiteSpace(input.SkillLevel), x => x.SkillLevel == input.SkillLevel) .WhereIF(input.PersonType == PersonType.Coach && input.CertificationStatus.HasValue, x => x.CertificationStatus == input.CertificationStatus) .WhereIF(input.StartTime.HasValue, u => u.CreatedTime >= input.StartTime) .WhereIF(input.EndTime.HasValue, u => u.CreatedTime <= input.EndTime) .OrderByDescending(x => x.CreatedTime); // 权限过滤 (管理员可看全部,这里简化的商户隔离逻辑) if (merchantId.HasValue) { query = query.Where(x => x.MerchantId == merchantId.Value); } else { // 这里根据实际业务,如果普通用户只能看自己创建的,需要这个过滤 query = query.Where(x => x.CreatedUserId == userId && x.MerchantId == null); } var list = await query.ToPagedListAsync(input); return list.Adapt>(); } /// /// 统一新增/编辑成员 /// /// /// [HttpPost("member/save")] [DisplayName("统一新增/编辑成员")] public async Task SaveUnifiedMember(CreateOrUpdateMemberInput input) { var (userId, merchantId) = await GetCurrentUserAndMerchantId(); // 校验唯一性 (手机号或身份证号,具体根据角色定) // 运动员主要查身份证,领队教练查手机号。这里综合校验。 var queryPhone = _appUserRep.AsQueryable() .Where(x => x.Mobile == input.Phone && x.PersonType == input.PersonType && !x.DeleteMark); var queryIdCard = _appUserRep.AsQueryable() .Where(x => x.IdCardNumber == input.IdCard && x.PersonType == input.PersonType && !x.DeleteMark); if (input.Id.HasValue) { queryPhone = queryPhone.Where(x => x.Id != input.Id.Value); queryIdCard = queryIdCard.Where(x => x.Id != input.Id.Value); } if (merchantId.HasValue) { queryPhone = queryPhone.Where(x => x.MerchantId == merchantId.Value); queryIdCard = queryIdCard.Where(x => x.MerchantId == merchantId.Value); } else { queryPhone = queryPhone.Where(x => x.CreatedUserId == userId && x.MerchantId == null); queryIdCard = queryIdCard.Where(x => x.CreatedUserId == userId && x.MerchantId == null); } // 只有领队和教练强校验手机号 if (input.PersonType == PersonType.Leader || input.PersonType == PersonType.Coach) { if (await queryPhone.AnyAsync()) throw Oops.Bah($"该手机号的成员已存在"); } // 运动员强校验身份证 if (input.PersonType == PersonType.Athlete) { if (await queryIdCard.AnyAsync()) throw Oops.Bah($"该身份证号的成员已存在"); } AppUserEntity entity; if (input.Id.HasValue) { entity = await _appUserRep.GetByIdAsync(input.Id.Value); if (entity == null || entity.DeleteMark) throw Oops.Bah("成员不存在"); await CheckPermission(entity); entity.UpdatedUserId = _authManager.UserId; entity.UpdatedTime = DateTime.Now; } else { entity = new AppUserEntity { Id = YitIdHelper.NextId(), PersonType = input.PersonType, MerchantId = merchantId, CreatedUserId = userId, CreatedTime = DateTime.Now, Status = AvailabilityStatus.Enable, AuditStatus = AuditStatus.Pass }; } entity.Name = input.Name; entity.Gender = input.Gender; entity.IdCardNumber = input.IdCard; entity.IdCardImageUrl = input.IdCardUrl; entity.Mobile = input.Phone; entity.Remark = input.Remark; entity.AuditRemark = ""; // 计算年龄逻辑 if (input.BirthDate.HasValue) { entity.BirthDate = input.BirthDate; var today = DateTime.Today; var age = today.Year - input.BirthDate.Value.Year; if (input.BirthDate.Value.Date > today.AddYears(-age)) age--; entity.Age = age; } else { entity.Age = input.Age; } if (input.PersonType == PersonType.Coach) { entity.SkillLevel = input.SkillLevel; entity.CertificationStatus = input.CertificationStatus; } await _appUserRep.InsertOrUpdateAsync(entity); } #endregion #region 领队/教练管理 (向下兼容) private async Task<(long userId, long? merchantId)> GetCurrentUserAndMerchantId() { var userId = _authManager.UserId; var merchant = await _db.Queryable() .FirstAsync(m => m.CreatedUserId == userId && !m.DeleteMark); return (userId, merchant?.Id); } private async Task CheckPermission(AppUserEntity entity) { var (userId, merchantId) = await GetCurrentUserAndMerchantId(); if (merchantId.HasValue) { if (entity.MerchantId != merchantId.Value) throw Oops.Bah("无权操作该成员"); } else { if (entity.CreatedUserId != userId || entity.MerchantId != null) throw Oops.Bah("无权操作该成员"); } } /// /// 删除成员 /// /// /// [DisplayName("删除成员")] [HttpPost("member/delete")] public async Task DeleteMember([FromBody] long id) { var entity = await _appUserRep.GetByIdAsync(id); if (entity == null || entity.DeleteMark) throw Oops.Bah("成员不存在"); await CheckPermission(entity); entity.DeleteMark = true; entity.UpdatedUserId = _authManager.UserId; entity.UpdatedTime = DateTime.Now; await _appUserRep.UpdateAsync(entity); } /// /// 批量删除成员 /// /// /// [HttpPost("member/batchDelete")] [DisplayName("批量删除成员")] public async Task BatchDeleteMember([FromBody] List ids) { if (ids == null || !ids.Any()) throw Oops.Bah("请选择要删除的记录"); // 逻辑删除 await _appUserRep.UpdateAsync(x => new AppUserEntity { DeleteMark = true }, x => ids.Contains(x.Id)); } /// /// 变更成员状态 /// /// /// [DisplayName("变更成员状态")] [HttpPost("member/status")] public async Task ChangeMemberStatus(ChangeMemberStatusInput input) { var entity = await _appUserRep.GetByIdAsync(input.Id); if (entity == null || entity.DeleteMark) throw Oops.Bah("成员不存在"); await CheckPermission(entity); entity.Status = input.Status; entity.UpdatedUserId = _authManager.UserId; entity.UpdatedTime = DateTime.Now; await _appUserRep.UpdateAsync(entity); } /// /// 识别身份证信息 /// /// /// [HttpPost("member/ocr")] [DisplayName("OCR识别身份证")] public async Task RecognizeIdCard([FromBody] AthleteOcrInput input) { return await _ocrService.RecognizeIdCardAsync(input.ImageUrl); } #endregion }