EmployeePositionMapService.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. namespace Admin.NET.Plugin.AiDOP.DataPlatform;
  2. /// <summary>
  3. /// 把源岗位原文写入 <c>mdp_employee_position_map</c>。MANUAL 行不覆盖。
  4. /// </summary>
  5. public sealed class EmployeePositionMapService : ITransient
  6. {
  7. private readonly ISqlSugarClient _db;
  8. public EmployeePositionMapService(ISqlSugarClient db) => _db = db;
  9. public async Task SyncNativeAsync(long tenantId, CancellationToken cancellationToken = default)
  10. {
  11. var rows = await _db.Ado.SqlQueryAsync<RawPosition>(
  12. """
  13. SELECT DISTINCT IFNULL(Domain,'') AS Domain,
  14. COALESCE(NULLIF(JobTitle,''), NULLIF(WorkCtr,'')) AS Raw
  15. FROM EmployeeMaster
  16. WHERE tenant_id=@tenant
  17. AND COALESCE(NULLIF(JobTitle,''), NULLIF(WorkCtr,'')) IS NOT NULL
  18. """,
  19. new { tenant = tenantId });
  20. await UpsertAsync(tenantId, MdpSourceIdentity.Native, "JobTitle", rows, cancellationToken);
  21. }
  22. public async Task SyncT8Async(long tenantId, CancellationToken cancellationToken = default)
  23. {
  24. var rows = await _db.Ado.SqlQueryAsync<RawPosition>(
  25. """
  26. SELECT DISTINCT IFNULL(ztid,'') AS Domain, IFNULL(gw,'') AS Raw
  27. FROM mdp_std_t8_sys_pelist
  28. WHERE tenant_id=@tenant AND gw IS NOT NULL AND gw<>''
  29. """,
  30. new { tenant = tenantId });
  31. await UpsertAsync(tenantId, "T8", "gw", rows, cancellationToken);
  32. }
  33. public async Task UpsertAsync(long tenantId, string sourceSystem, string field, IEnumerable<RawPosition> rows, CancellationToken cancellationToken = default)
  34. {
  35. foreach (var row in rows)
  36. {
  37. cancellationToken.ThrowIfCancellationRequested();
  38. var code = EmployeePositionRules.Classify(row.Raw);
  39. await _db.Ado.ExecuteCommandAsync(
  40. """
  41. INSERT INTO mdp_employee_position_map
  42. (tenant_id, source_system, domain, src_position_raw, src_position_field, position_code, role_source)
  43. VALUES (@tenant, @src, @domain, @raw, @field, @code, 'AUTO_KEYWORD')
  44. ON DUPLICATE KEY UPDATE
  45. position_code = IF(role_source='MANUAL', position_code, VALUES(position_code)),
  46. src_position_field = IF(role_source='MANUAL', src_position_field, VALUES(src_position_field))
  47. """,
  48. new { tenant = tenantId, src = sourceSystem, domain = row.Domain ?? "", raw = row.Raw, field, code });
  49. if (code == EmployeePositionRules.Unknown)
  50. {
  51. await _db.Ado.ExecuteCommandAsync(
  52. """
  53. INSERT INTO mdp_source_gate_log
  54. (tenant_id, std_object, source_system, gate_reason, row_count, sample_keys)
  55. VALUES (@tenant, 'EMPLOYEE', @src, 'POSITION_UNMAPPED', 1, @raw)
  56. """,
  57. new { tenant = tenantId, src = sourceSystem, raw = row.Raw });
  58. }
  59. }
  60. }
  61. public sealed class RawPosition
  62. {
  63. public string Domain { get; set; } = "";
  64. public string Raw { get; set; } = "";
  65. }
  66. }