AdoS0SrmPurchaseIsActive.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. namespace Admin.NET.Plugin.AiDOP.Infrastructure;
  2. /// <summary>
  3. /// srm_purchase.is_active(varchar 列)↔ 布尔口径的唯一统一转换入口。
  4. /// 背景:该列历史全量存中文“是”,不同下游曾各自解释('是'/'Y'/'1'、CAST AS SIGNED 等),口径分裂。
  5. /// 本类固化:读取兼容多编码 → bool?;写入统一为“是/否”;未知非空值返回 null,绝不静默判停用。
  6. /// DB 与实体类型保持 string?,不改字段类型,不迁移存量数据。
  7. /// </summary>
  8. public static class AdoS0SrmPurchaseIsActive
  9. {
  10. /// <summary>启用状态的持久化编码(写入 DB,与存量“是”对齐)。</summary>
  11. public const string EnabledValue = "是";
  12. /// <summary>停用状态的持久化编码(写入 DB)。</summary>
  13. public const string DisabledValue = "否";
  14. /// <summary>启用编码集合(读取判 true / 查询命中,均为小写归一后比较)。</summary>
  15. public static readonly string[] EnabledTokens = { "是", "y", "1", "true" };
  16. /// <summary>停用编码集合(读取判 false / 查询命中,均为小写归一后比较)。</summary>
  17. public static readonly string[] DisabledTokens = { "否", "n", "0", "false" };
  18. /// <summary>
  19. /// DB 字符串 → bool?。规则:Trim 后按启用/停用集合判定;null/空白 → null;未知非空值 → null(不默认停用)。
  20. /// </summary>
  21. public static bool? Parse(string? value)
  22. {
  23. if (value == null) return null;
  24. var trimmed = value.Trim();
  25. if (trimmed.Length == 0) return null;
  26. var lower = trimmed.ToLowerInvariant();
  27. if (Array.IndexOf(EnabledTokens, lower) >= 0) return true;
  28. if (Array.IndexOf(DisabledTokens, lower) >= 0) return false;
  29. return null;
  30. }
  31. /// <summary>
  32. /// bool? → DB 字符串。true → “是”;false → “否”;null → null(不写值)。
  33. /// </summary>
  34. public static string? Encode(bool? value)
  35. => value switch
  36. {
  37. true => EnabledValue,
  38. false => DisabledValue,
  39. _ => null,
  40. };
  41. /// <summary>
  42. /// 启用判定 SQL 片段(MySQL 方言)。col 为 is_active 列表达式(含表别名,如 sp.is_active)。
  43. /// TRIM+LOWER 归一后 IN 启用集合;NULL/空/未知值天然不命中。
  44. /// </summary>
  45. public static string EnabledSql(string col)
  46. => $"TRIM(LOWER({col})) IN ('是','y','1','true')";
  47. /// <summary>停用判定 SQL 片段(MySQL 方言),语义同 <see cref="EnabledSql"/> 取停用集合。</summary>
  48. public static string DisabledSql(string col)
  49. => $"TRIM(LOWER({col})) IN ('否','n','0','false')";
  50. }