diff --git a/YD_Event.Application/AppUser/AppUserService.cs b/YD_Event.Application/AppUser/AppUserService.cs index 1b75750..313f90e 100644 --- a/YD_Event.Application/AppUser/AppUserService.cs +++ b/YD_Event.Application/AppUser/AppUserService.cs @@ -124,7 +124,24 @@ public class AppUserService : BaseService, ITransient } var list = await query.ToPagedListAsync(input); - return list.Adapt>(); + var result = list.Adapt>(); + + // 设置Age字段:优先使用数据库中的Age,如果没有则根据BirthDate计算 + foreach (var item in result.Rows) + { + if (item.Age.HasValue) + { + // 数据库中已有Age,直接使用 + continue; + } + // 数据库中没有Age,根据BirthDate动态计算 + if (item.BirthDate.HasValue) + { + item.Age = MemberOutput.CalculateAgeFromBirthDate(item.BirthDate); + } + } + + return result; } /// @@ -224,9 +241,12 @@ public class AppUserService : BaseService, ITransient entity.Remark = input.Remark; entity.AuditRemark = ""; - // 仅保存出生日期,年龄在输出时动态计算 + // 保存出生日期 entity.BirthDate = input.BirthDate; + // 保存年龄字段:如果用户填写了年龄,直接保存;如果没有填写但有出生日期,可以在查询时动态计算 + entity.Age = input.Age; + if (input.PersonType == PersonType.Coach) { entity.SkillLevel = input.SkillLevel; diff --git a/YD_Event.Application/AppUser/Dtos/AppUserDtos.cs b/YD_Event.Application/AppUser/Dtos/AppUserDtos.cs index e9c3014..f124d30 100644 --- a/YD_Event.Application/AppUser/Dtos/AppUserDtos.cs +++ b/YD_Event.Application/AppUser/Dtos/AppUserDtos.cs @@ -108,18 +108,21 @@ public class MemberOutput : AppUserOutput public DateTime? BirthDate { get; set; } /// - /// 年龄(动态计算) + /// 年龄 + /// 优先使用数据库存储的Age字段,如果不存在则根据BirthDate动态计算 /// - public int? Age - { - get - { - if (!BirthDate.HasValue) return null; - var today = DateTime.Today; - var age = today.Year - BirthDate.Value.Year; - if (BirthDate.Value.Date > today.AddYears(-age)) age--; - return age; - } + public int? Age { get; set; } + + /// + /// 根据出生日期计算年龄的辅助方法 + /// + public static int? CalculateAgeFromBirthDate(DateTime? birthDate) + { + if (!birthDate.HasValue) return null; + var today = DateTime.Today; + var age = today.Year - birthDate.Value.Year; + if (birthDate.Value.Date > today.AddYears(-age)) age--; + return age; } /// @@ -184,6 +187,11 @@ public class CreateOrUpdateMemberInput /// public DateTime? BirthDate { get; set; } + /// + /// 年龄 + /// + public int? Age { get; set; } + /// /// 联系电话 /// diff --git a/YD_Event.Application/YD_Event.Application.xml b/YD_Event.Application/YD_Event.Application.xml index 552d682..a72a4a8 100644 --- a/YD_Event.Application/YD_Event.Application.xml +++ b/YD_Event.Application/YD_Event.Application.xml @@ -209,7 +209,13 @@ - 年龄(动态计算) + 年龄 + 优先使用数据库存储的Age字段,如果不存在则根据BirthDate动态计算 + + + + + 根据出生日期计算年龄的辅助方法 @@ -267,6 +273,11 @@ 出生日期 (运动员等) + + + 年龄 + + 联系电话 diff --git a/YD_Event.Core/Entities/AppUser.cs b/YD_Event.Core/Entities/AppUser.cs index 4a34292..f654135 100644 --- a/YD_Event.Core/Entities/AppUser.cs +++ b/YD_Event.Core/Entities/AppUser.cs @@ -60,6 +60,12 @@ public class AppUser : Entity, ISoftDelete, ICreatedUserId, ICreatedTime, [SugarColumn(IsNullable = true)] public DateTime? BirthDate { get; set; } + /// + /// 年龄 + /// + [SugarColumn(IsNullable = true)] + public int? Age { get; set; } + /// /// 联系电话 /// diff --git a/YD_Event.Core/YD_Event.Core.xml b/YD_Event.Core/YD_Event.Core.xml index 370aad9..943b627 100644 --- a/YD_Event.Core/YD_Event.Core.xml +++ b/YD_Event.Core/YD_Event.Core.xml @@ -579,6 +579,11 @@ 出生日期 + + + 年龄 + + 联系电话