diff --git a/src/CncCollector/Core/CollectRecordWriter.cs b/src/CncCollector/Core/CollectRecordWriter.cs
index 98e34b2..ca5ab98 100644
--- a/src/CncCollector/Core/CollectRecordWriter.cs
+++ b/src/CncCollector/Core/CollectRecordWriter.cs
@@ -182,5 +182,112 @@ namespace CncCollector.Core
_log.Error($"记录采集失败状态时出错(address_id={collectAddressId})", ex);
}
}
+
+ ///
+ /// 重放模式批量写入:日志已存在,跳过INSERT,改为UPDATE analysis_summary。
+ /// 其余步骤(写采集记录、更新机床状态、更新地址状态)保持不变。
+ ///
+ public static void WriteBatchReplay(string businessConnStr, string logConnStr,
+ List records, long existingRawLogId, int collectAddressId,
+ DateTime requestTime, string analysisSummary)
+ {
+ var now = DateTime.Now;
+
+ // 1. 更新已有日志的 analysis_summary
+ try
+ {
+ using (var conn = new MySqlConnection(logConnStr))
+ {
+ conn.Open();
+ conn.Execute(
+ "UPDATE log_collect_raw SET analysis_summary = @Summary WHERE id = @Id",
+ new { Id = existingRawLogId, Summary = analysisSummary ?? (string)null });
+ }
+ }
+ catch (Exception ex)
+ {
+ _log.Error($"重放:更新分析日志失败(raw_log_id={existingRawLogId})", ex);
+ }
+
+ if (records == null || records.Count == 0) return;
+
+ // 2-4 步:与 WriteBatch 相同
+ try
+ {
+ using (var conn = new MySqlConnection(businessConnStr))
+ {
+ conn.Open();
+ using (var tran = conn.BeginTransaction())
+ {
+ try
+ {
+ foreach (var r in records)
+ {
+ conn.Execute(@"INSERT INTO cnc_collect_record (machine_id, collect_time, device_time, program_name, part_count,
+ device_status, run_status, operate_mode,
+ power_on_time, run_time, extra_data, created_at)
+ VALUES (@MachineId, @CollectTime, @DeviceTime, @ProgramName, @PartCount,
+ @DeviceStatus, @RunStatus, @OperateMode,
+ @PowerOnTime, @RunTime, @ExtraData, @CreatedAt)",
+ new
+ {
+ r.MachineId,
+ CollectTime = r.CollectTime,
+ r.DeviceTime,
+ ProgramName = r.ProgramName ?? (string)null,
+ r.PartCount,
+ DeviceStatus = r.DeviceStatus ?? (string)null,
+ RunStatus = r.RunStatus ?? (string)null,
+ OperateMode = r.OperateMode ?? (string)null,
+ r.PowerOnTime,
+ r.RunTime,
+ ExtraData = r.ExtraData ?? (string)null,
+ CreatedAt = now
+ }, tran);
+ }
+ tran.Commit();
+ }
+ catch
+ {
+ tran.Rollback();
+ throw;
+ }
+ }
+
+ foreach (var r in records)
+ {
+ try
+ {
+ conn.Execute(@"UPDATE cnc_machine SET last_collect_time = @CollectTime,
+ last_device_status = @DeviceStatus, last_run_status = @RunStatus,
+ last_program_name = @ProgramName, last_part_count = @PartCount,
+ last_operate_mode = @OperateMode
+ WHERE id = @MachineId",
+ new {
+ r.MachineId, r.CollectTime,
+ DeviceStatus = r.DeviceStatus ?? (string)null,
+ RunStatus = r.RunStatus ?? (string)null,
+ ProgramName = r.ProgramName ?? (string)null,
+ r.PartCount,
+ OperateMode = r.OperateMode ?? (string)null
+ });
+ }
+ catch (Exception ex) { _log.Error($"重放:更新机床状态失败(id={r.MachineId})", ex); }
+ }
+
+ try
+ {
+ conn.Execute(@"UPDATE cnc_collect_address SET last_collect_time = @Time, last_collect_status = 'success', fail_count = 0, updated_at = NOW()
+ WHERE id = @Id",
+ new { Time = now, Id = collectAddressId });
+ }
+ catch (Exception ex) { _log.Error($"重放:更新地址状态失败(id={collectAddressId})", ex); }
+ }
+ }
+ catch (Exception ex)
+ {
+ _log.Error($"重放:批量写入失败(raw_log_id={existingRawLogId})", ex);
+ }
+ }
}
}