修复ProductionTracker日期:CURDATE()改为collectTime.Date参数化,支持跨天重放

main
haoliang 1 month ago
parent 88eb8391a9
commit aace303692

@ -62,21 +62,21 @@ namespace CncCollector.Core
conn.Open(); conn.Open();
// 查询当天最新一条记录 // 查询当天最新一条记录
var record = GetCurrentRecord(conn, machineId); var record = GetCurrentRecord(conn, machineId, collectTime.Date);
if (record == null) if (record == null)
{ {
// 当天首次采集 → 创建记录 // 当天首次采集 → 创建记录
InsertRecord(conn, machineId, programName, InsertRecord(conn, machineId, programName,
totalPartCount.Value, totalPartCount.Value); totalPartCount.Value, totalPartCount.Value, collectTime.Date);
logText = "当天首次采集,创建记录"; logText = "当天首次采集,创建记录";
changed = true; changed = true;
} }
else if (record.ProductionDate != DateTime.Today) else if (record.ProductionDate != collectTime.Date)
{ {
// 防御性检查:跨天(理论上不会发生,因为查询条件限定了 CURDATE() // 防御性检查:跨天(理论上不会发生,因为查询条件限定了 CURDATE()
InsertRecord(conn, machineId, programName, InsertRecord(conn, machineId, programName,
totalPartCount.Value, totalPartCount.Value); totalPartCount.Value, totalPartCount.Value, collectTime.Date);
logText = "跨天首次采集,创建记录"; logText = "跨天首次采集,创建记录";
changed = true; changed = true;
} }
@ -93,8 +93,8 @@ namespace CncCollector.Core
var todayTotal = conn.ExecuteScalar<decimal>( var todayTotal = conn.ExecuteScalar<decimal>(
@"SELECT COALESCE(SUM(end_total_count - base_total_count), 0) @"SELECT COALESCE(SUM(end_total_count - base_total_count), 0)
FROM cnc_daily_production FROM cnc_daily_production
WHERE machine_id = @Mid AND production_date = CURDATE()", WHERE machine_id = @Mid AND production_date = @Date",
new { Mid = machineId }); new { Mid = machineId, Date = collectTime.Date });
_log.Info($"机床{machineId}: {logText}(当日累计产量={todayTotal:F5}"); _log.Info($"机床{machineId}: {logText}(当日累计产量={todayTotal:F5}");
return (logText, changed, todayTotal); return (logText, changed, todayTotal);
@ -131,17 +131,17 @@ namespace CncCollector.Core
/// <summary> /// <summary>
/// 查询当天最新一条日产量记录 /// 查询当天最新一条日产量记录
/// </summary> /// </summary>
private DailyProductionRecord GetCurrentRecord(MySqlConnection conn, int machineId) private DailyProductionRecord GetCurrentRecord(MySqlConnection conn, int machineId, DateTime productionDate)
{ {
return conn.QueryFirstOrDefault<DailyProductionRecord>( return conn.QueryFirstOrDefault<DailyProductionRecord>(
@"SELECT id AS Id, machine_id AS MachineId, production_date AS ProductionDate, @"SELECT id AS Id, machine_id AS MachineId, production_date AS ProductionDate,
program_name AS ProgramName, base_total_count AS BaseTotalCount, program_name AS ProgramName, base_total_count AS BaseTotalCount,
end_total_count AS EndTotalCount end_total_count AS EndTotalCount
FROM cnc_daily_production FROM cnc_daily_production
WHERE machine_id = @Mid AND production_date = CURDATE() WHERE machine_id = @Mid AND production_date = @Date
ORDER BY id DESC ORDER BY id DESC
LIMIT 1", LIMIT 1",
new { Mid = machineId }); new { Mid = machineId, Date = productionDate });
} }
/// <summary> /// <summary>
@ -182,7 +182,7 @@ namespace CncCollector.Core
// 合法程序切换 → 创建新记录 // 合法程序切换 → 创建新记录
// 关键:新记录的 base 使用旧记录的 end保证产量连续 // 关键:新记录的 base 使用旧记录的 end保证产量连续
InsertRecord(conn, oldRecord.MachineId, newProgramName, InsertRecord(conn, oldRecord.MachineId, newProgramName,
oldRecord.EndTotalCount, totalPartCount); oldRecord.EndTotalCount, totalPartCount, oldRecord.ProductionDate);
_log.Info($"机床{oldRecord.MachineId}: 程序切换 {oldRecord.ProgramName} → {newProgramName}" + _log.Info($"机床{oldRecord.MachineId}: 程序切换 {oldRecord.ProgramName} → {newProgramName}" +
$"新记录 base={oldRecord.EndTotalCount} end={totalPartCount}"); $"新记录 base={oldRecord.EndTotalCount} end={totalPartCount}");
return ("程序切换,创建新记录", true); return ("程序切换,创建新记录", true);
@ -199,15 +199,16 @@ namespace CncCollector.Core
/// 插入一条新的日产量记录 /// 插入一条新的日产量记录
/// </summary> /// </summary>
private void InsertRecord(MySqlConnection conn, int machineId, private void InsertRecord(MySqlConnection conn, int machineId,
string programName, decimal baseTotalCount, decimal endTotalCount) string programName, decimal baseTotalCount, decimal endTotalCount, DateTime productionDate)
{ {
conn.Execute( conn.Execute(
@"INSERT INTO cnc_daily_production @"INSERT INTO cnc_daily_production
(machine_id, production_date, program_name, base_total_count, end_total_count, created_at, updated_at) (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 new
{ {
MachineId = machineId, MachineId = machineId,
Date = productionDate,
ProgramName = programName, ProgramName = programName,
BaseCount = baseTotalCount, BaseCount = baseTotalCount,
EndCount = endTotalCount EndCount = endTotalCount

@ -41,7 +41,7 @@ namespace CncReplay
conn.Open(); conn.Open();
logs = conn.Query<LogEntry>( logs = conn.Query<LogEntry>(
"SELECT id AS Id, request_time AS RequestTime, raw_json AS RawJson, collect_address_id AS CollectAddressId " + "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) if (logs.Count == 0)

Loading…
Cancel
Save