namespace Dpz.Core.Entity.Base.ExpressTreeQuery;

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class QueryFilterAttribute : Attribute
{
    /// <summary>
    ///
    /// </summary>
    /// <param name="condition">请求参数需要达成的条件</param>
    public QueryFilterAttribute(FilterCondition condition)
    {
        Condition = condition;
    }

    /// <summary>
    /// 请求参数需要达成的条件
    /// </summary>
    public FilterCondition Condition { get; }

    /// <summary>
    /// <code>Condition == FilterCondition.EqualValue</code>
    /// 需达成条件的比较值
    /// </summary>
    public object? ConditionValue { get; set; }

    /// <summary>
    /// 比较方式
    /// 默认为 Equal
    /// </summary>
    public Comparison Comparison { get; set; } = Comparison.Equal;

    /// <summary>
    /// 需要比较的字段名称
    /// 默认为 Null
    /// 为 Null 时,获取与该属性相同的属性名称
    /// </summary>
    public string? PropertyName { get; set; }

    /// <summary>
    /// 需要比较的多个字段名称(用于 OR 逻辑)
    /// 默认为 Null
    /// 为 Null 时,使用 PropertyName 处理单个属性
    /// 不为 Null 时,为每个属性名生成比较表达式,并用 OR 连接
    /// 例如:PropertiesNames = ["PathSegments", "ParentPathSegments"]
    /// 生成:x => x.PathSegments == value || x.ParentPathSegments == value
    /// </summary>
    public string[]? PropertiesNames { get; set; }

    /// <summary>
    /// 需要比较字段的值
    /// 默认为 string "10001"
    /// 值为 "10001" 时,会忽略 PropertyValue 值
    /// </summary>
    public object? PropertyValue { get; set; } = GenerateConditionFilter.DefaultPropertyValue;

    /// <summary>
    /// 筛选结束时间 是否 +1 天
    /// </summary>
    public bool EndAddDay { get; set; }

    /// <summary>
    /// 是否筛选一天内的时间
    /// 只支持 <see cref="DateTime"/> 类型
    /// </summary>
    public bool FilterInDay { get; set; }

    /// <summary>
    /// 实体类型
    /// </summary>
    public Type? EntityType { get; set; }

    /// <summary>
    /// 筛选值为 false 时,是否包含 NULL 值
    /// </summary>
    public bool FalseHasNull { get; set; }

    /// <summary>
    /// 比较方式<see cref="Comparison"/> == <see cref="Comparison.SplitOrContains"/> 时,用于分隔字符串的除了空格的其他分隔符
    /// </summary>
    public char[]? SplitOrContainsOtherSeparators { get; set; }
}
⚠⚠    以下内容为AI分析的结果,请根据实际情况进行判断。

这是一个用于表达式树查询的特性类(Attribute),主要用于在实体属性上标记查询过滤条件。让我来详细解释这个代码:

类的基本定义

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class QueryFilterAttribute : Attribute
  • 继承自 Attribute,是一个自定义特性
  • AttributeTargets.Property 表示只能应用在属性上
  • AllowMultiple = true 表示同一个属性可以应用多个该特性

核心属性说明

1. Condition (必需参数)

public FilterCondition Condition { get; }
  • 通过构造函数传入,定义请求参数需要达成的条件
  • 是一个枚举类型,决定了过滤的基本逻辑

2. 比较相关属性

public object? ConditionValue { get; set; }        // 条件比较值
public Comparison Comparison { get; set; } = Comparison.Equal;  // 比较方式,默认相等

3. 属性名称配置

public string? PropertyName { get; set; }          // 单个属性名
public string[]? PropertiesNames { get; set; }     // 多个属性名(OR逻辑)

使用逻辑:

  • PropertiesNames 为空时,使用 PropertyName(如果也为空,则使用当前属性名)
  • PropertiesNames 不为空时,为每个属性生成比较表达式并用 OR 连接

4. 属性值配置

public object? PropertyValue { get; set; } = GenerateConditionFilter.DefaultPropertyValue;
  • 默认值为 "10001"
  • 当值为 "10001" 时会被忽略

5. 时间处理相关

public bool EndAddDay { get; set; }        // 结束时间是否+1天
public bool FilterInDay { get; set; }      // 是否筛选一天内的时间

6. 其他配置

public Type? EntityType { get; set; }                           // 实体类型
public bool FalseHasNull { get; set; }                         // false时是否包含NULL
public char[]? SplitOrContainsOtherSeparators { get; set; }     // 分割字符

使用场景示例

public class UserQuery
{
    // 简单相等比较
    [QueryFilter(FilterCondition.HasValue, Comparison = Comparison.Equal)]
    public string? Name { get; set; }
    
    // 多字段OR查询
    [QueryFilter(FilterCondition.HasValue, 
                PropertiesNames = new[] {"FirstName", "LastName"}, 
                Comparison = Comparison.Contains)]
    public string? SearchText { get; set; }
    
    // 日期范围查询
    [QueryFilter(FilterCondition.HasValue, EndAddDay = true)]
    public DateTime? EndDate { get; set; }
}

总结

这个特性类设计得非常灵活,可以:

  1. 支持多种比较方式和条件
  2. 支持单属性或多属性的OR查询
  3. 特别处理时间类型的查询场景
  4. 提供布尔值和NULL值的特殊处理
  5. 支持字符串分割和包含查询

主要用于动态生成 LINQ 表达式树,实现灵活的数据查询过滤功能。

评论加载中...