From cdb88744cfd1b19ac42b3d2f3d94d39bb7c0ad71 Mon Sep 17 00:00:00 2001 From: haoliang <821644@qq.com> Date: Fri, 15 May 2026 22:08:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20WriteBatchReplay=20?= =?UTF-8?q?=E9=9D=99=E6=80=81=E6=96=B9=E6=B3=95=EF=BC=9A=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=87=8D=E6=94=BE=E6=A8=A1=E5=BC=8F=E6=89=B9=E9=87=8F=E5=86=99?= =?UTF-8?q?=E5=85=A5=E9=87=87=E9=9B=86=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CncCollector/Core/CollectRecordWriter.cs | 107 +++++++++++++++++++ 1 file changed, 107 insertions(+) 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); + } + } } }