using System.Globalization; using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Connectors; namespace Admin.NET.Plugin.AiDOP.DataPlatform.Relational; public sealed record RelationalFieldRule(string Field, string Kind, string? TimeZone = null, string? Encoding = null, int? Precision = null, int? Scale = null) { public object Apply(object source) => Kind switch { "text" when source is byte[] bytes && Encoding is not null => RelationalFieldValues.Decode(bytes, Encoding), "utc" when source is DateTimeOffset instant => instant.ToUniversalTime(), "utc" when source is DateTime time && time.Kind == DateTimeKind.Utc => new DateTimeOffset(time), "utc" when source is DateTime local && TimeZone is not null && TimeZoneInfo.TryFindSystemTimeZoneById(TimeZone, out var zone) => RelationalFieldValues.ToUtc(local, zone), "date" when source is DateOnly date => date, "date" when source is DateTime dateTime => DateOnly.FromDateTime(dateTime), "decimal" when Precision.HasValue && Scale.HasValue => RelationalFieldValues.DecimalValue( Convert.ToString(source, CultureInfo.InvariantCulture) ?? "", Precision.Value, Scale.Value), _ => throw new ConnectorContractException(ConnectorError.InvalidBatch), }; public bool Valid => Kind switch { "text" => Encoding is "utf-8" or "utf8" or "utf8mb4" or "al32utf8" or "gbk" or "gb18030" or "big5", "utc" => TimeZone is null || TimeZoneInfo.TryFindSystemTimeZoneById(TimeZone, out _), "date" => true, "decimal" => Precision is >= 1 and <= 28 && Scale is >= 0 && Scale <= Precision, _ => false, }; }