|
@@ -238,61 +238,85 @@ namespace EasyTemplate.Service
|
|
|
// ===== 私有辅助方法 =====
|
|
// ===== 私有辅助方法 =====
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
- /// 处理预警告数据的自动插入或更新逻辑
|
|
|
|
|
|
|
+ /// 处理预警告数据的自动插入或更新逻辑(仿 SQL 触发器逻辑)
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
private async Task ProcessPrewarningAsync(TRecord record)
|
|
private async Task ProcessPrewarningAsync(TRecord record)
|
|
|
{
|
|
{
|
|
|
|
|
+ // 检查是否是有效交易(液体体积 >= 1500)
|
|
|
|
|
+ bool isValidTransaction = record.liquidVl >= 1500;
|
|
|
|
|
+ if (!isValidTransaction)
|
|
|
|
|
+ {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
try
|
|
try
|
|
|
{
|
|
{
|
|
|
// 从 tmEnd 提取日期
|
|
// 从 tmEnd 提取日期
|
|
|
var date = DateOnly.FromDateTime(record.tmEnd);
|
|
var date = DateOnly.FromDateTime(record.tmEnd);
|
|
|
var nozzleId = record.noz;
|
|
var nozzleId = record.noz;
|
|
|
|
|
|
|
|
|
|
+
|
|
|
// 查询是否已存在当天的记录
|
|
// 查询是否已存在当天的记录
|
|
|
var existingPrewarning = await GetPrewarningByDateAndNozzleAsync(date, nozzleId);
|
|
var existingPrewarning = await GetPrewarningByDateAndNozzleAsync(date, nozzleId);
|
|
|
|
|
|
|
|
if (existingPrewarning == null)
|
|
if (existingPrewarning == null)
|
|
|
{
|
|
{
|
|
|
- // 不存在则插入新记录
|
|
|
|
|
|
|
+ // 不存在则插入新记录(对应 trigger 的 insert or ignore)
|
|
|
var newPrewarning = new TPrewarning
|
|
var newPrewarning = new TPrewarning
|
|
|
{
|
|
{
|
|
|
date = date,
|
|
date = date,
|
|
|
nozzle = nozzleId,
|
|
nozzle = nozzleId,
|
|
|
- overproof = record.overproof > 0 ? 1 : 0, // 根据 overproof 设置
|
|
|
|
|
- total = 1, // 首次记录,总数为 1
|
|
|
|
|
- overproofrate = 0, // 初始超标率
|
|
|
|
|
- overproof_alert = 0,
|
|
|
|
|
- overproof_2 = 0,
|
|
|
|
|
- overproofrate_2 = 0,
|
|
|
|
|
- continueoverproof = record.overproof > 0 ? 1 : 0
|
|
|
|
|
|
|
+ total = 1,
|
|
|
|
|
+ overproof = record.overproof,
|
|
|
|
|
+ overproof_alert = record.overproof_alert,
|
|
|
|
|
+ overproofrate = (record.overproof > 0) ? 100 : 0,
|
|
|
|
|
+ overproofrate_alert = (record.overproof_alert > 0) ? 100 : 0,
|
|
|
|
|
+ overproof_2 = record.overproof_2,
|
|
|
|
|
+ overproofrate_2 = (record.overproof_2 > 0) ? 100 : 0,
|
|
|
|
|
+ continueoverproof = record.overproof
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
await CreatePrewarningAsync(newPrewarning);
|
|
await CreatePrewarningAsync(newPrewarning);
|
|
|
- Console.WriteLine($"已为新油枪 {nozzleId} 创建预警告记录,日期:{date}");
|
|
|
|
|
|
|
+ Console.WriteLine($"[预警告] 创建新记录 - 油枪{nozzleId}, 日期:{date}, 液量:{record.liquidVl}, 有效:{isValidTransaction}");
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- // 存在则更新现有记录
|
|
|
|
|
- // 在这里进行自定义修改逻辑
|
|
|
|
|
- if (record.overproof > 0)
|
|
|
|
|
|
|
+ // 存在则更新现有记录(对应 trigger 的 update 逻辑)
|
|
|
|
|
+
|
|
|
|
|
+ existingPrewarning.total += 1;
|
|
|
|
|
+
|
|
|
|
|
+ // 2. overproof: 直接累加
|
|
|
|
|
+ existingPrewarning.overproof += record.overproof;
|
|
|
|
|
+
|
|
|
|
|
+ // 3. overproofrate
|
|
|
|
|
+ if (existingPrewarning.total > 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ existingPrewarning.overproofrate = (100 * existingPrewarning.overproof) / existingPrewarning.total;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 4. overproof_alert: 直接累加
|
|
|
|
|
+ existingPrewarning.overproof_alert += record.overproof_alert;
|
|
|
|
|
+
|
|
|
|
|
+ // 5. overproofrate_alert
|
|
|
|
|
+ if (existingPrewarning.total > 0)
|
|
|
{
|
|
{
|
|
|
- existingPrewarning.overproof++;
|
|
|
|
|
- existingPrewarning.continueoverproof++;
|
|
|
|
|
|
|
+ existingPrewarning.overproofrate_alert = (100 * existingPrewarning.overproof_alert) / existingPrewarning.total;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- existingPrewarning.total++;
|
|
|
|
|
|
|
+ // 6. overproof_2: 直接累加
|
|
|
|
|
+ existingPrewarning.overproof_2 += record.overproof_2;
|
|
|
|
|
|
|
|
- // 计算超标率
|
|
|
|
|
|
|
+ // 7. overproofrate_2
|
|
|
if (existingPrewarning.total > 0)
|
|
if (existingPrewarning.total > 0)
|
|
|
{
|
|
{
|
|
|
- existingPrewarning.overproofrate = (existingPrewarning.overproof * 100) / existingPrewarning.total;
|
|
|
|
|
|
|
+ existingPrewarning.overproofrate_2 = (100 * existingPrewarning.overproof_2) / existingPrewarning.total;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // 可以在这里添加更多的自定义修改逻辑
|
|
|
|
|
- // 例如:根据其他字段更新 overproof_2, overproofrate_2 等
|
|
|
|
|
|
|
+ // 8. continueoverproof: 连续超标次数,累加当前超标值
|
|
|
|
|
+ existingPrewarning.continueoverproof += record.overproof;
|
|
|
|
|
|
|
|
await UpdatePrewarningAsync(existingPrewarning);
|
|
await UpdatePrewarningAsync(existingPrewarning);
|
|
|
- Console.WriteLine($"已更新油枪 {nozzleId} 的预警告记录,日期:{date}");
|
|
|
|
|
|
|
+ Console.WriteLine($"[预警告] 更新记录 - 油枪{nozzleId}, 日期:{date}, 液量:{record.liquidVl}, 有效:{isValidTransaction}, 总数:{existingPrewarning.total}");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|