namespace Admin.NET.Plugin.AiDOP.Infrastructure;
///
/// srm_purchase.is_active(varchar 列)↔ 布尔口径的唯一统一转换入口。
/// 背景:该列历史全量存中文“是”,不同下游曾各自解释('是'/'Y'/'1'、CAST AS SIGNED 等),口径分裂。
/// 本类固化:读取兼容多编码 → bool?;写入统一为“是/否”;未知非空值返回 null,绝不静默判停用。
/// DB 与实体类型保持 string?,不改字段类型,不迁移存量数据。
///
public static class AdoS0SrmPurchaseIsActive
{
/// 启用状态的持久化编码(写入 DB,与存量“是”对齐)。
public const string EnabledValue = "是";
/// 停用状态的持久化编码(写入 DB)。
public const string DisabledValue = "否";
/// 启用编码集合(读取判 true / 查询命中,均为小写归一后比较)。
public static readonly string[] EnabledTokens = { "是", "y", "1", "true" };
/// 停用编码集合(读取判 false / 查询命中,均为小写归一后比较)。
public static readonly string[] DisabledTokens = { "否", "n", "0", "false" };
///
/// DB 字符串 → bool?。规则:Trim 后按启用/停用集合判定;null/空白 → null;未知非空值 → null(不默认停用)。
///
public static bool? Parse(string? value)
{
if (value == null) return null;
var trimmed = value.Trim();
if (trimmed.Length == 0) return null;
var lower = trimmed.ToLowerInvariant();
if (Array.IndexOf(EnabledTokens, lower) >= 0) return true;
if (Array.IndexOf(DisabledTokens, lower) >= 0) return false;
return null;
}
///
/// bool? → DB 字符串。true → “是”;false → “否”;null → null(不写值)。
///
public static string? Encode(bool? value)
=> value switch
{
true => EnabledValue,
false => DisabledValue,
_ => null,
};
///
/// 启用判定 SQL 片段(MySQL 方言)。col 为 is_active 列表达式(含表别名,如 sp.is_active)。
/// TRIM+LOWER 归一后 IN 启用集合;NULL/空/未知值天然不命中。
///
public static string EnabledSql(string col)
=> $"TRIM(LOWER({col})) IN ('是','y','1','true')";
/// 停用判定 SQL 片段(MySQL 方言),语义同 取停用集合。
public static string DisabledSql(string col)
=> $"TRIM(LOWER({col})) IN ('否','n','0','false')";
}