DFS_Shuo_Chen 1 månad sedan
förälder
incheckning
75ff920bd1

+ 11 - 0
EasyTemplate.Service/GlobalTool.cs

@@ -19,6 +19,17 @@ namespace EasyTemplate.Service
         public const int WarningState_Warning = 2;
 
 
+        public static int vlr_sys_max = 1200;    //3位小数;  气液比上限
+        public static int vlr_sys_min = 1000;    //3位小数;  	气液比下限
+
+        public static int vlr_sys_max_alert = 1500;  //3位小数;  严重气液比上限
+        public static int vlr_sys_min_alert = 600;	//3位小数;  严重气液比下限
+
+        public static int vlr_sys_max_gd_zsh = 1200;    //3位小数;  气液比上限
+        public static int vlr_sys_min_gd_zsh = 1000;    //3位小数;  	气液比下限
+
+
+
         public static Dictionary<int, NozzleState> g_mNozzleState = new Dictionary<int, NozzleState>();
 
         public static ConcurrentQueue<BufferData> g_dataQueue = new ConcurrentQueue<BufferData>();

+ 21 - 3
EasyTemplate.Service/HandleData.cs

@@ -137,8 +137,10 @@ namespace EasyTemplate.Service
                         GlobalTool.g_mNozzleState[record.noz].VLR = ((double)record.VLR / 1000).ToString("F2");
                     }
                     
-                    // 将记录插入到数据库
-                    await InsertRecordToDatabaseAsync(record);
+                   
+
+                        // 将记录插入到数据库
+                        await InsertRecordToDatabaseAsync(record);
                     
                     break;
 
@@ -202,7 +204,23 @@ namespace EasyTemplate.Service
                     callbackflag = record.callbackflag,
                     vccerrorinfo = record.vccerrorinfo ?? string.Empty
                 };
-                
+
+                if(tRecord.vlr > GlobalTool.vlr_sys_max || tRecord.vlr < GlobalTool.vlr_sys_min)
+                {
+                    tRecord.overproof = 1;
+                }
+
+                if (tRecord.vlr > GlobalTool.vlr_sys_max_alert || tRecord.vlr < GlobalTool.vlr_sys_min_alert)
+                {
+                    tRecord.overproof_alert = 1;
+                }
+
+                if (tRecord.vlr > GlobalTool.vlr_sys_max_gd_zsh || tRecord.vlr < GlobalTool.vlr_sys_min_gd_zsh)
+                {
+                    tRecord.overproof_2 = 1;
+                }
+
+
                 // 调用 NozzleService 的插入方法
                 var result = await nozzleService.CreateRecordAsync(tRecord);
                 

+ 45 - 21
EasyTemplate.Service/NozzleService.cs

@@ -238,61 +238,85 @@ namespace EasyTemplate.Service
         // ===== 私有辅助方法 =====
         
         /// <summary>
-        /// 处理预警告数据的自动插入或更新逻辑
+        /// 处理预警告数据的自动插入或更新逻辑(仿 SQL 触发器逻辑)
         /// </summary>
         private async Task ProcessPrewarningAsync(TRecord record)
         {
+            // 检查是否是有效交易(液体体积 >= 1500)
+            bool isValidTransaction = record.liquidVl >= 1500;
+            if (!isValidTransaction)
+            {
+                return;
+            }
+
             try
             {
                 // 从 tmEnd 提取日期
                 var date = DateOnly.FromDateTime(record.tmEnd);
                 var nozzleId = record.noz;
                 
+                
                 // 查询是否已存在当天的记录
                 var existingPrewarning = await GetPrewarningByDateAndNozzleAsync(date, nozzleId);
                 
                 if (existingPrewarning == null)
                 {
-                    // 不存在则插入新记录
+                    // 不存在则插入新记录(对应 trigger 的 insert or ignore)
                     var newPrewarning = new TPrewarning
                     {
                         date = date,
                         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);
-                    Console.WriteLine($"已为新油枪 {nozzleId} 创建预警告记录,日期:{date}");
+                    Console.WriteLine($"[预警告] 创建新记录 - 油枪{nozzleId}, 日期:{date}, 液量:{record.liquidVl}, 有效:{isValidTransaction}");
                 }
                 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)
                     {
-                        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);
-                    Console.WriteLine($"已更新油枪 {nozzleId} 的预警告记录,日期:{date}");
+                    Console.WriteLine($"[预警告] 更新记录 - 油枪{nozzleId}, 日期:{date}, 液量:{record.liquidVl}, 有效:{isValidTransaction}, 总数:{existingPrewarning.total}");
                 }
             }
             catch (Exception ex)

+ 23 - 2
EasyTemplate.Tool/Entity/App/TPrewarning.cs

@@ -16,11 +16,32 @@ namespace EasyTemplate.Tool.Entity.App
 
         [SugarColumn(IsPrimaryKey = true)]
         public int nozzle { get; set; }
-        public int overproof { get; set; }
         public int total { get; set; }
-        public int overproofrate { get; set; }
+
+        /// <summary>
+        /// 超标数量
+        /// </summary>
+        public int overproof { get; set; }
+        /// <summary>
+        /// 严重超标数量
+        /// </summary>
         public int overproof_alert { get; set; }
+        /// <summary>
+        /// 超标率
+        /// </summary>
+        public int overproofrate { get; set; }
+        /// <summary>
+        /// 严重超标率
+        /// </summary>
+        public int overproofrate_alert { get; set; }
+
+        /// <summary>
+        /// 超标数量(GD_ZSH)
+        /// </summary>
         public int overproof_2 { get; set; }
+        /// <summary>
+        /// 超标率(GD_ZSH)
+        /// </summary>
         public int overproofrate_2 { get; set; }
         public int continueoverproof { get; set; }
 

+ 2 - 0
EasyTemplate.Tool/Entity/App/TRecord.cs

@@ -24,6 +24,8 @@ public partial class TRecord: EntityBase
     public DateTime tmBegin { get; set; }
     public DateTime tmEnd { get; set; }
     public int overproof { get; set; }
+    public int overproof_alert { get; set; }
+    public int overproof_2 { get; set; }
     public int uploadflag { get; set; }
     public int uploadflag2 { get; set; }
     public int uploadflag3 { get; set; }