MdpExcelValidation.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Globalization;
  2. using System.Security.Cryptography;
  3. using System.Text.Json;
  4. using Admin.NET.Plugin.AiDOP.Entity.DataPlatform;
  5. namespace Admin.NET.Plugin.AiDOP.DataPlatform.FileImport;
  6. public static class MdpExcelValidation
  7. {
  8. public static Dictionary<string, object?> MapRow(
  9. IReadOnlyDictionary<string, object?> raw,
  10. IReadOnlyList<MdpFieldMapping> mappings)
  11. {
  12. var mapped = new Dictionary<string, object?>(StringComparer.OrdinalIgnoreCase);
  13. foreach (var map in mappings.OrderBy(x => x.SortOrder))
  14. {
  15. if (map.TargetField.Equals("tenant_id", StringComparison.OrdinalIgnoreCase))
  16. continue;
  17. raw.TryGetValue(map.SourceField, out var val);
  18. if ((val == null || val is DBNull || string.IsNullOrWhiteSpace(Convert.ToString(val)))
  19. && !string.IsNullOrWhiteSpace(map.DefaultValue))
  20. val = map.DefaultValue;
  21. if (map.IsRequired == 1 && (val == null || string.IsNullOrWhiteSpace(Convert.ToString(val))))
  22. throw new InvalidOperationException($"必填列 {map.SourceField} 为空");
  23. var text = Convert.ToString(val, CultureInfo.InvariantCulture);
  24. MdpFileImportSecurity.EnsureCell(text);
  25. mapped[map.TargetField] = val;
  26. }
  27. MdpFileImportSecurity.StripClientTenant(mapped);
  28. return mapped;
  29. }
  30. public static string BusinessKey(string? expr, IDictionary<string, object?> mapped)
  31. {
  32. if (string.IsNullOrWhiteSpace(expr))
  33. throw new InvalidOperationException("实体未配置业务键");
  34. var parts = expr.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
  35. var vals = new List<string>();
  36. foreach (var p in parts)
  37. {
  38. if (!mapped.TryGetValue(p, out var v) || v == null || string.IsNullOrWhiteSpace(Convert.ToString(v)))
  39. throw new InvalidOperationException($"业务键 {p} 为空");
  40. vals.Add(Convert.ToString(v, CultureInfo.InvariantCulture)!.Trim());
  41. }
  42. return string.Join("|", vals);
  43. }
  44. public static string RowHash(IDictionary<string, object?> mapped)
  45. {
  46. var json = JsonSerializer.Serialize(mapped.OrderBy(x => x.Key, StringComparer.OrdinalIgnoreCase)
  47. .ToDictionary(x => x.Key, x => x.Value?.ToString()));
  48. var bytes = SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(json));
  49. return Convert.ToHexString(bytes).ToLowerInvariant();
  50. }
  51. public static bool IsPullStage(string? stageType, string? stepCode)
  52. {
  53. var t = (stageType ?? stepCode ?? string.Empty).Trim().ToUpperInvariant();
  54. return t is "PULL" or "EXTRACT" or "INBOUND" or "SYNC_STAGING";
  55. }
  56. }