KeeperResolveService.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using SqlSugar;
  2. namespace Admin.NET.Plugin.AiDOP.DataPlatform.Wms;
  3. /// <summary>
  4. /// 领料保管员解析。移植基准:旧 DOP ResourceExamineAppService.GenerateNbr
  5. /// (按 EmpWorkDutyMaster.ItemNum1..ItemNum2 字符串区间匹配 Employee;非存储过程 Bonded 分支)。
  6. /// </summary>
  7. public sealed class KeeperResolveService : ITransient
  8. {
  9. private readonly MdpSourceScopeFactory _scopeFactory;
  10. public KeeperResolveService(MdpSourceScopeFactory scopeFactory)
  11. {
  12. _scopeFactory = scopeFactory;
  13. }
  14. public sealed class KeeperHit
  15. {
  16. public string ItemNum { get; set; } = "";
  17. public int DutyRecID { get; set; }
  18. public string Employee { get; set; } = "";
  19. public string? Location { get; set; }
  20. }
  21. /// <summary>
  22. /// 按物料号匹配职责;返回命中列表。未命中不抛错(旧 C# 仅跳过收集 User1)。
  23. /// 若业务要求「未设置保管员则中断建单」,由调用方检查空命中。
  24. /// </summary>
  25. public async Task<IReadOnlyList<KeeperHit>> ResolveByItemsAsync(
  26. string domain, IEnumerable<string> itemNums, CancellationToken ct = default)
  27. {
  28. var items = itemNums.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.OrdinalIgnoreCase).ToList();
  29. if (items.Count == 0) return Array.Empty<KeeperHit>();
  30. var db = await _scopeFactory.GetScopeAsync("DOPDEMORQ_SQLSERVER", ct);
  31. var duties = await db.Ado.SqlQueryAsync<DutyRow>(
  32. @"SELECT RecID, Employee, ItemNum1, ItemNum2, Location
  33. FROM EmpWorkDutyMaster
  34. WHERE Domain=@d",
  35. new SugarParameter("@d", domain));
  36. var hits = new List<KeeperHit>();
  37. foreach (var item in items)
  38. {
  39. // 与旧 C# 一致:0 >= Compare(ItemNum1, item) && 0 >= Compare(item, ItemNum2)
  40. var ewdm = duties.Find(s =>
  41. string.Compare(s.ItemNum1, item, StringComparison.OrdinalIgnoreCase) <= 0
  42. && string.Compare(item, s.ItemNum2, StringComparison.OrdinalIgnoreCase) <= 0);
  43. if (ewdm == null || string.IsNullOrEmpty(ewdm.Employee))
  44. continue;
  45. hits.Add(new KeeperHit
  46. {
  47. ItemNum = item,
  48. DutyRecID = ewdm.RecID,
  49. Employee = ewdm.Employee!,
  50. Location = ewdm.Location
  51. });
  52. }
  53. return hits;
  54. }
  55. /// <summary>拼 NbrMaster.User1(逗号)与 User2(JSON 数组字符串),与旧 DOP 一致。</summary>
  56. public static (string user1, string user2) FormatKeeperUsers(IEnumerable<KeeperHit> hits)
  57. {
  58. var emps = hits.Select(x => x.Employee).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().ToList();
  59. if (emps.Count == 0) return ("", "[]");
  60. var user1 = string.Join(",", emps);
  61. var user2 = System.Text.Json.JsonSerializer.Serialize(emps);
  62. return (user1, user2);
  63. }
  64. private sealed class DutyRow
  65. {
  66. public int RecID { get; set; }
  67. public string? Employee { get; set; }
  68. public string? ItemNum1 { get; set; }
  69. public string? ItemNum2 { get; set; }
  70. public string? Location { get; set; }
  71. }
  72. }