From aace303692c7aa032944c8066ec9463bc2b3cb70 Mon Sep 17 00:00:00 2001 From: haoliang <821644@qq.com> Date: Sat, 16 May 2026 00:02:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DProductionTracker=E6=97=A5?= =?UTF-8?q?=E6=9C=9F=EF=BC=9ACURDATE()=E6=94=B9=E4=B8=BAcollectTime.Date?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=8C=96=EF=BC=8C=E6=94=AF=E6=8C=81=E8=B7=A8?= =?UTF-8?q?=E5=A4=A9=E9=87=8D=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CncCollector/Core/ProductionTracker.cs | 25 +++++++++++----------- src/CncReplay/Program.cs | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/CncCollector/Core/ProductionTracker.cs b/src/CncCollector/Core/ProductionTracker.cs index 80dc6ec..8a70ed1 100644 --- a/src/CncCollector/Core/ProductionTracker.cs +++ b/src/CncCollector/Core/ProductionTracker.cs @@ -62,21 +62,21 @@ namespace CncCollector.Core conn.Open(); // 查询当天最新一条记录 - var record = GetCurrentRecord(conn, machineId); + var record = GetCurrentRecord(conn, machineId, collectTime.Date); if (record == null) { // 当天首次采集 → 创建记录 InsertRecord(conn, machineId, programName, - totalPartCount.Value, totalPartCount.Value); + totalPartCount.Value, totalPartCount.Value, collectTime.Date); logText = "当天首次采集,创建记录"; changed = true; } - else if (record.ProductionDate != DateTime.Today) + else if (record.ProductionDate != collectTime.Date) { // 防御性检查:跨天(理论上不会发生,因为查询条件限定了 CURDATE()) InsertRecord(conn, machineId, programName, - totalPartCount.Value, totalPartCount.Value); + totalPartCount.Value, totalPartCount.Value, collectTime.Date); logText = "跨天首次采集,创建记录"; changed = true; } @@ -93,8 +93,8 @@ namespace CncCollector.Core var todayTotal = conn.ExecuteScalar( @"SELECT COALESCE(SUM(end_total_count - base_total_count), 0) FROM cnc_daily_production - WHERE machine_id = @Mid AND production_date = CURDATE()", - new { Mid = machineId }); + WHERE machine_id = @Mid AND production_date = @Date", + new { Mid = machineId, Date = collectTime.Date }); _log.Info($"机床{machineId}: {logText}(当日累计产量={todayTotal:F5})"); return (logText, changed, todayTotal); @@ -131,17 +131,17 @@ namespace CncCollector.Core /// /// 查询当天最新一条日产量记录 /// - private DailyProductionRecord GetCurrentRecord(MySqlConnection conn, int machineId) + private DailyProductionRecord GetCurrentRecord(MySqlConnection conn, int machineId, DateTime productionDate) { return conn.QueryFirstOrDefault( @"SELECT id AS Id, machine_id AS MachineId, production_date AS ProductionDate, program_name AS ProgramName, base_total_count AS BaseTotalCount, end_total_count AS EndTotalCount FROM cnc_daily_production - WHERE machine_id = @Mid AND production_date = CURDATE() + WHERE machine_id = @Mid AND production_date = @Date ORDER BY id DESC LIMIT 1", - new { Mid = machineId }); + new { Mid = machineId, Date = productionDate }); } /// @@ -182,7 +182,7 @@ namespace CncCollector.Core // 合法程序切换 → 创建新记录 // 关键:新记录的 base 使用旧记录的 end,保证产量连续 InsertRecord(conn, oldRecord.MachineId, newProgramName, - oldRecord.EndTotalCount, totalPartCount); + oldRecord.EndTotalCount, totalPartCount, oldRecord.ProductionDate); _log.Info($"机床{oldRecord.MachineId}: 程序切换 {oldRecord.ProgramName} → {newProgramName}," + $"新记录 base={oldRecord.EndTotalCount} end={totalPartCount}"); return ("程序切换,创建新记录", true); @@ -199,15 +199,16 @@ namespace CncCollector.Core /// 插入一条新的日产量记录 /// private void InsertRecord(MySqlConnection conn, int machineId, - string programName, decimal baseTotalCount, decimal endTotalCount) + string programName, decimal baseTotalCount, decimal endTotalCount, DateTime productionDate) { conn.Execute( @"INSERT INTO cnc_daily_production (machine_id, production_date, program_name, base_total_count, end_total_count, created_at, updated_at) - VALUES (@MachineId, CURDATE(), @ProgramName, @BaseCount, @EndCount, NOW(), NOW())", + VALUES (@MachineId, @Date, @ProgramName, @BaseCount, @EndCount, NOW(), NOW())", new { MachineId = machineId, + Date = productionDate, ProgramName = programName, BaseCount = baseTotalCount, EndCount = endTotalCount diff --git a/src/CncReplay/Program.cs b/src/CncReplay/Program.cs index 14bc670..9bbdfbc 100644 --- a/src/CncReplay/Program.cs +++ b/src/CncReplay/Program.cs @@ -41,7 +41,7 @@ namespace CncReplay conn.Open(); logs = conn.Query( "SELECT id AS Id, request_time AS RequestTime, raw_json AS RawJson, collect_address_id AS CollectAddressId " + - "FROM log_collect_raw WHERE is_success = 1 ORDER BY request_time ASC").AsList(); + "FROM log_collect_raw WHERE is_success = 1 AND DATE(request_time) IN ('2026-05-13','2026-05-14','2026-05-15') ORDER BY request_time ASC").AsList(); } if (logs.Count == 0)