ReportWorkMdpSyncService.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.Text.Json;
  2. using Admin.NET.Plugin.AiDOP.DataPlatform.Executors;
  3. namespace Admin.NET.Plugin.AiDOP.Manufacturing;
  4. /// <summary>
  5. /// S6 报工双模式入站:执行器 → mdp_stg_s6_report → mdp_std_s6_report。
  6. /// 源优先 T8 <c>Cj_Bg_Head_Rep</c>(实体 S6_REPORT);API 对偶 S6_REPORT_API。
  7. /// </summary>
  8. public class ReportWorkMdpSyncService : ITransient
  9. {
  10. private const string InboundEntityCode = "S6_REPORT";
  11. private readonly ISqlSugarClient _db;
  12. private readonly MdpSourcePullDispatcher _pullDispatcher;
  13. public ReportWorkMdpSyncService(ISqlSugarClient db, MdpSourcePullDispatcher pullDispatcher)
  14. {
  15. _db = db;
  16. _pullDispatcher = pullDispatcher;
  17. }
  18. public async Task<ReportWorkInboundResult> RunInboundAsync(
  19. long tenantId = 0,
  20. bool fullRefresh = false,
  21. string? entityCode = null,
  22. CancellationToken cancellationToken = default)
  23. {
  24. cancellationToken.ThrowIfCancellationRequested();
  25. await EnsureTablesAsync();
  26. var code = string.IsNullOrWhiteSpace(entityCode) ? InboundEntityCode : entityCode.Trim();
  27. var now = DateTime.Now;
  28. var pullCtx = new MdpPullContext
  29. {
  30. TenantId = tenantId,
  31. FullRefresh = fullRefresh,
  32. TaskCode = "S6_REPORT_INBOUND",
  33. BatchId = $"S6_RPT_IN_{now:yyyyMMddHHmmss}"
  34. };
  35. var pull = await _pullDispatcher.PullByEntityCodeAsync(code, pullCtx, cancellationToken);
  36. var transformBatch = $"{pullCtx.BatchId}_STD";
  37. var stdRows = await TransformStandardAsync(tenantId, transformBatch, now);
  38. return new ReportWorkInboundResult
  39. {
  40. PullBatchId = pullCtx.BatchId,
  41. RowsPulled = pull.RowsWritten,
  42. RowsWrittenStg = pull.RowsWritten,
  43. TransformBatchId = transformBatch,
  44. StandardRows = stdRows
  45. };
  46. }
  47. private async Task<int> TransformStandardAsync(long tenantId, string batchId, DateTime now)
  48. {
  49. // 将 PENDING stg 投影到 std(JSON 字段兼容大小写)
  50. const string sql = """
  51. INSERT INTO mdp_std_s6_report
  52. (tenant_id, factory_id, source_system, work_order_no, report_date, report_qty, ztid,
  53. source_row_id, source_biz_key, sync_batch_id, sync_time)
  54. SELECT
  55. IFNULL(s.tenant_id, @tid),
  56. 1,
  57. IFNULL(NULLIF(s.source_system,''), 'T8'),
  58. IFNULL(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(s.raw_data, '$.noid')), 'null'),
  59. IFNULL(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(s.raw_data, '$.NOID')), 'null'), s.source_biz_key)),
  60. COALESCE(
  61. STR_TO_DATE(NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(s.raw_data, '$.kgdate')), 'null'), ''), '%Y-%m-%d %H:%i:%s'),
  62. STR_TO_DATE(NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(s.raw_data, '$.KGDATE')), 'null'), ''), '%Y-%m-%d %H:%i:%s'),
  63. NULL),
  64. CAST(NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(s.raw_data, '$.sl')), 'null'), '') AS DECIMAL(18,6)),
  65. NULLIF(JSON_UNQUOTE(JSON_EXTRACT(s.raw_data, '$.ztid')), 'null'),
  66. IFNULL(NULLIF(s.source_row_id,''), s.source_biz_key),
  67. s.source_biz_key,
  68. @batch,
  69. @now
  70. FROM mdp_stg_s6_report s
  71. WHERE s.process_status = 'PENDING'
  72. AND s.source_biz_key IS NOT NULL
  73. AND s.source_biz_key <> ''
  74. ON DUPLICATE KEY UPDATE
  75. work_order_no = VALUES(work_order_no),
  76. report_date = VALUES(report_date),
  77. report_qty = VALUES(report_qty),
  78. ztid = VALUES(ztid),
  79. source_row_id = VALUES(source_row_id),
  80. sync_batch_id = VALUES(sync_batch_id),
  81. sync_time = VALUES(sync_time),
  82. update_time = CURRENT_TIMESTAMP
  83. """;
  84. var affected = await _db.Ado.ExecuteCommandAsync(sql,
  85. new SugarParameter("@tid", tenantId),
  86. new SugarParameter("@batch", batchId),
  87. new SugarParameter("@now", now));
  88. await _db.Ado.ExecuteCommandAsync(
  89. "UPDATE mdp_stg_s6_report SET process_status='DONE', update_time=NOW() WHERE process_status='PENDING'");
  90. return affected;
  91. }
  92. private async Task EnsureTablesAsync()
  93. {
  94. await _db.Ado.ExecuteCommandAsync("""
  95. CREATE TABLE IF NOT EXISTS mdp_stg_s6_report (
  96. id bigint NOT NULL AUTO_INCREMENT,
  97. tenant_id bigint NOT NULL DEFAULT 0,
  98. source_system varchar(50) DEFAULT NULL,
  99. source_table varchar(200) DEFAULT NULL,
  100. source_row_id varchar(200) DEFAULT NULL,
  101. source_biz_key varchar(300) DEFAULT NULL,
  102. raw_data json DEFAULT NULL,
  103. sync_batch_id varchar(100) DEFAULT NULL,
  104. create_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  105. sync_time datetime DEFAULT CURRENT_TIMESTAMP,
  106. process_status varchar(20) NOT NULL DEFAULT 'PENDING',
  107. process_message varchar(500) DEFAULT NULL,
  108. update_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  109. PRIMARY KEY (id),
  110. UNIQUE KEY uk_source_key (source_system, source_table, source_biz_key)
  111. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
  112. """);
  113. await _db.Ado.ExecuteCommandAsync("""
  114. CREATE TABLE IF NOT EXISTS mdp_std_s6_report (
  115. id bigint NOT NULL AUTO_INCREMENT,
  116. tenant_id bigint NOT NULL DEFAULT 0,
  117. factory_id bigint DEFAULT 1,
  118. source_system varchar(50) NOT NULL DEFAULT 'T8',
  119. work_order_no varchar(100) NOT NULL,
  120. report_date datetime DEFAULT NULL,
  121. report_qty decimal(18,6) DEFAULT NULL,
  122. ztid varchar(50) DEFAULT NULL,
  123. source_row_id varchar(100) NOT NULL,
  124. source_biz_key varchar(200) NOT NULL,
  125. sync_batch_id varchar(100) NOT NULL,
  126. sync_time datetime NOT NULL,
  127. update_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  128. PRIMARY KEY (id),
  129. UNIQUE KEY uk_mdp_std_s6_report (tenant_id, source_system, source_biz_key)
  130. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
  131. """);
  132. }
  133. }
  134. public class ReportWorkInboundResult
  135. {
  136. public string PullBatchId { get; set; } = string.Empty;
  137. public int RowsPulled { get; set; }
  138. public int RowsWrittenStg { get; set; }
  139. public string TransformBatchId { get; set; } = string.Empty;
  140. public int StandardRows { get; set; }
  141. }