using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace OASystem.Domain.Entities { /// /// 实体类完成状态验证扩展方法 /// 提供便捷的方法来检查实体对象是否所有字段都已填写完成 /// public static class EntityExtensions { /// /// 检查实体对象的所有字段是否都已填写完成 /// 包括所有属性(字符串、值类型、引用类型) /// /// 要检查的实体对象 /// /// true: 所有字段都已正确填写 /// false: 存在未填写或为默认值的字段 /// /// /// /// var entity = new YourEntity { Name = "John", Age = 25 }; /// if (entity.IsCompleted()) /// { /// Console.WriteLine("所有字段已填写完成"); /// } /// /// public static bool IsCompleted(this object obj) { // 获取实体类型的所有属性 var properties = obj.GetType().GetProperties(); // 使用LINQ的All方法检查所有属性是否都已填写 // 如果所有属性都满足IsPropertyFilled条件,返回true;否则返回false return properties.All(property => IsPropertyFilled(property, obj)); } /// /// 检查实体对象的必填字段是否都已填写完成 /// 只检查标记了[RequiredField]特性的属性 /// /// 要检查的实体对象 /// /// true: 所有必填字段都已正确填写 /// false: 存在未填写的必填字段 /// /// /// /// var entity = new YourEntity { Name = "John" }; // Age标记为[RequiredField] /// if (!entity.IsRequiredCompleted()) /// { /// Console.WriteLine("请填写所有必填字段"); /// } /// /// public static bool IsRequiredCompleted(this object obj) { // 获取实体类型的所有属性 var properties = obj.GetType().GetProperties(); // 筛选出标记了[RequiredField]特性的属性(必填字段) var requiredProperties = properties.Where(p => p.GetCustomAttribute() != null); // 检查所有必填字段是否都已填写 return requiredProperties.All(property => IsPropertyFilled(property, obj)); } /// /// 检查单个属性是否已填写 - 使用模式匹配的安全版本 /// 根据属性类型采用不同的验证规则 /// /// 要检查的属性信息 /// 包含该属性的对象实例 /// /// true: 属性已正确填写 /// false: 属性未填写或为默认值 /// /// /// 验证规则: /// 1. 字符串类型:不能为null、空字符串或空白字符串 /// 2. 值类型:不能等于该类型的默认值(如int不能为0,DateTime不能为MinValue等) /// 3. 布尔类型:总是返回true(因为bool总是有true或false值) /// 4. 引用类型:不能为null /// private static bool IsPropertyFilled(PropertyInfo property, object obj) { // 获取属性的值 var value = property.GetValue(obj); // 如果值为null,直接返回false if (value == null) return false; return property.PropertyType switch { // 字符串类型 Type t when t == typeof(string) => !string.IsNullOrWhiteSpace((string)value), // 值类型使用模式匹配安全转换 Type t when t == typeof(int) => value is int intValue && intValue != 0, Type t when t == typeof(long) => value is long longValue && longValue != 0, Type t when t == typeof(decimal) => value is decimal decimalValue && decimalValue != 0, Type t when t == typeof(double) => value is double doubleValue && doubleValue != 0, Type t when t == typeof(float) => value is float floatValue && floatValue != 0, Type t when t == typeof(DateTime) => value is DateTime dateValue && dateValue != DateTime.MinValue, Type t when t == typeof(bool) => value is bool, // bool只要不是null就认为已填写 // 可空类型处理 Type t when IsNullableType(t) => !IsDefaultNullableValue(value), // 其他值类型 Type t when t.IsValueType => !value.Equals(Activator.CreateInstance(t)), // 引用类型(已经检查过null) _ => true }; } /// /// 检查类型是否为可空类型(Nullable) /// /// 要检查的类型 /// 是否为可空类型 private static bool IsNullableType(Type type) { return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); } /// /// 检查可空类型的值是否为默认值(null或默认值) /// /// 可空类型的值 /// 是否为默认值 private static bool IsDefaultNullableValue(object value) { var underlyingType = Nullable.GetUnderlyingType(value.GetType()); if (underlyingType == null) return true; var defaultValue = Activator.CreateInstance(underlyingType); return value.Equals(defaultValue); } /// /// 快速检查实体是否包含数据(至少有一个字段已填写) /// 用于判断实体是否为空或未初始化 /// /// 要检查的实体对象 /// /// true: 至少有一个字段已填写 /// false: 所有字段都为默认值或空 /// /// /// /// var entity = new YourEntity(); /// if (!entity.HasData()) /// { /// Console.WriteLine("实体没有任何数据"); /// } /// /// public static bool HasData(this object obj) { // 获取所有属性 var properties = obj.GetType().GetProperties(); // 使用LINQ的Any方法检查是否存在至少一个已填写的属性 return properties.Any(property => IsPropertyFilled(property, obj)); } } /// /// 必填字段标记特性 /// 用于标记实体类中必须填写的属性 /// [AttributeUsage(AttributeTargets.Property)] public class RequiredFieldAttribute : Attribute { /// /// 验证失败时显示的错误消息 /// public string ErrorMessage { get; } /// /// 初始化必填字段特性 /// /// 验证失败时显示的错误消息 public RequiredFieldAttribute(string errorMessage = "该字段为必填项") { ErrorMessage = errorMessage; } } }