MdpDbSyncNeutralProjection.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Admin.NET.Plugin.AiDOP.Entity.DataPlatform;
  2. namespace Admin.NET.Plugin.AiDOP.DataPlatform;
  3. /// <summary>
  4. /// 方式甲贴源完成后,复用标准 API 已有的通用贴源到中立层投影。
  5. /// 未在 <see cref="InboundNeutralProjectionService"/> 登记的实体为 no-op,
  6. /// 仍由 S1-S7 原有模块转换处理。
  7. /// </summary>
  8. public sealed class MdpDbSyncNeutralProjection : ITransient
  9. {
  10. private readonly ISqlSugarClient _db;
  11. private readonly InboundNeutralProjectionService _projection;
  12. public MdpDbSyncNeutralProjection(
  13. ISqlSugarClient db,
  14. InboundNeutralProjectionService projection)
  15. {
  16. _db = db;
  17. _projection = projection;
  18. }
  19. public async Task ProjectAsync(
  20. IEnumerable<MdpEntity> entities,
  21. long tenantId,
  22. CancellationToken cancellationToken = default)
  23. {
  24. if (tenantId <= 0)
  25. throw new InvalidOperationException("方式甲中立层投影要求 tenantId 大于 0");
  26. var list = entities
  27. .GroupBy(x => x.EntityCode, StringComparer.OrdinalIgnoreCase)
  28. .Select(x => x.OrderByDescending(e => e.TenantId).First())
  29. .ToList();
  30. var sourceIds = list.Select(x => x.SourceId).Distinct().ToList();
  31. var sources = await _db.Queryable<MdpSource>()
  32. .Where(x => sourceIds.Contains(x.Id))
  33. .ToListAsync(cancellationToken);
  34. var sourceById = sources.ToDictionary(x => x.Id);
  35. foreach (var entity in list)
  36. {
  37. cancellationToken.ThrowIfCancellationRequested();
  38. if (!sourceById.TryGetValue(entity.SourceId, out var source))
  39. throw new InvalidOperationException($"实体 {entity.EntityCode} 的数据源不存在");
  40. var neutral = MdpSourceIdentity.NeutralCode(source.SystemCode, source.SourceCode);
  41. var routes = new[] { entity.EntityCode, entity.TransformSourceTable }
  42. .Where(x => !string.IsNullOrWhiteSpace(x))
  43. .Select(x => x!.Trim())
  44. .Distinct(StringComparer.OrdinalIgnoreCase);
  45. foreach (var route in routes)
  46. {
  47. await _projection.ProjectAsync(
  48. route,
  49. tenantId,
  50. neutral,
  51. MdpSourceIdentity.DbSync,
  52. cancellationToken);
  53. }
  54. }
  55. }
  56. }