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