2025-05-28 17:36:49 +08:00

64 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

namespace JiShe.CollectBus.IoTDB.Options
{
/// <summary>
/// 查询条件
/// </summary>
public class QueryCondition
{
/// <summary>
/// 字段
/// </summary>
public string Field { get; set; }
/// <summary>
/// 操作符,>=<
/// </summary>
public string Operator { get; set; }
/// <summary>
/// 是否数值,如果是数值,则进行数值比较,否则进行字符串比较
/// </summary>
public bool IsNumber { get; set; } = false;
private object _rawValue;
/// <summary>
/// 值
/// </summary>
public object Value
{
get => ApplyValueConversion(_rawValue);
set => _rawValue = value;
}
/// <summary>
/// 值转换
/// </summary>
/// <param name="rawValue"></param>
/// <returns></returns>
private object ApplyValueConversion(object rawValue)
{
string declaredTypeName = rawValue.GetType().Name;
Func<object, object> converter = GetQueryConditionValue(declaredTypeName);
return converter(rawValue);
}
/// <summary>
/// 查询条件值转换委托
/// </summary>
/// <param name="declaredTypeName"></param>
/// <returns></returns>
private Func<object, object> GetQueryConditionValue(string declaredTypeName)
{
return declaredTypeName?.ToUpper() switch
{
"DATETIME" => v => v != null ? ((DateTime)v).ToUniversalTime().Ticks : null,
"STRING" => v => v != null ? $"'{v}'" : "''",
_ => v => v
};
}
}
}