| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- using EasyTemplate.Tool;
- using EasyTemplate.Tool.Entity;
- using EasyTemplate.Tool.Entity.App;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace EasyTemplate.Service
- {
- /// <summary>
- /// 预警告统计服务
- /// </summary>
- public class WarningService
- {
- private readonly SqlSugarRepository<TPrewarning> _prewarning;
- private readonly SqlSugarRepository<TRecord> _record;
- public WarningService(SqlSugarRepository<TPrewarning> prewarning, SqlSugarRepository<TRecord> record)
- {
- _prewarning = prewarning;
- _record = record;
- }
- /// <summary>
- /// 获取 35 天内所有油枪的统计数据
- /// </summary>
- public async Task<List<WarningStatistics>> Get35DaysStatisticsAsync()
- {
- var endDate = DateOnly.FromDateTime(DateTime.Today);
- var startDate = endDate.AddDays(-34); // 包含今天共 35 天
- return await CalculateStatisticsAsync(startDate, endDate);
- }
- /// <summary>
- /// 按日期范围获取统计数据
- /// </summary>
- public async Task<List<WarningStatistics>> GetStatisticsByDateRangeAsync(DateOnly startDate, DateOnly endDate)
- {
- return await CalculateStatisticsAsync(startDate, endDate);
- }
- /// <summary>
- /// 按油枪和日期范围筛选统计数据
- /// </summary>
- public async Task<List<WarningStatistics>> GetStatisticsByFilterAsync(WarningStatisticsQuery query)
- {
- var endDate = query.EndDate ?? DateOnly.FromDateTime(DateTime.Today);
- var startDate = query.StartDate ?? endDate.AddDays(-34);
- var allStats = await CalculateStatisticsAsync(startDate, endDate);
- // 按油枪筛选
- if (query.NozzleId.HasValue)
- {
- allStats = allStats.Where(s => s.nozzle == query.NozzleId.Value).ToList();
- }
- return allStats;
- }
- /// <summary>
- /// 更新指定日期的统计数据(用于新交易插入后)
- /// </summary>
- public async Task UpdateDailyStatisticsAsync(DateOnly date, int nozzleId)
- {
- try
- {
- Console.WriteLine($"[预警告统计] 开始更新油枪{nozzleId},日期:{date} 的统计数据");
- // 重新计算该油枪在该日期的统计
- var stats = await CalculateSingleDayStatisticsAsync(date, nozzleId);
-
- if (stats != null)
- {
- Console.WriteLine($"[预警告统计] 更新成功 - 总笔数:{stats.daily_total}, 超标:{stats.daily_overproof}, 超标率:{stats.daily_overproofrate}%");
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"[预警告统计] 更新失败:{ex.Message}");
- }
- }
- /// <summary>
- /// 计算单个日期的统计数据
- /// </summary>
- private async Task<WarningStatistics> CalculateSingleDayStatisticsAsync(DateOnly date, int nozzleId)
- {
- // 获取当天的预警告记录
- var prewarning = await _prewarning.AsQueryable()
- .Where(p => p.date == date && p.nozzle == nozzleId)
- .FirstAsync();
- if (prewarning == null)
- {
- return null;
- }
- return new WarningStatistics
- {
- nozzle = nozzleId,
- statistics_date = date,
- daily_total = prewarning.total,
- daily_overproof = prewarning.overproof,
- daily_overproofrate = prewarning.overproofrate,
- daily_overproof_alert = prewarning.overproof_alert,
- daily_overproofrate_alert = prewarning.overproofrate_alert,
- daily_overproof_2 = prewarning.overproof_2,
- daily_overproofrate_2 = prewarning.overproofrate_2,
- is_accumulated = prewarning.total >= 5,
- accumulated_days = 1
- };
- }
- /// <summary>
- /// 核心统计计算逻辑
- /// </summary>
- private async Task<List<WarningStatistics>> CalculateStatisticsAsync(DateOnly startDate, DateOnly endDate)
- {
- Console.WriteLine($"[预警告统计] 开始计算统计,范围:{startDate} 至 {endDate}");
- var results = new List<WarningStatistics>();
- // 获取该日期范围内的所有预警告记录
- var prewarnings = await _prewarning.AsQueryable()
- .Where(p => p.date >= startDate && p.date <= endDate)
- .ToListAsync();
- // 按油枪分组
- var groupedByNozzle = prewarnings.GroupBy(p => p.nozzle);
- foreach (var nozzleGroup in groupedByNozzle)
- {
- var nozzleId = nozzleGroup.Key;
- var sortedData = nozzleGroup.OrderBy(p => p.date).ToList();
- // 为每一天计算统计数据
- for (int i = 0; i < sortedData.Count; i++)
- {
- var currentPrewarning = sortedData[i];
- var currentDate = currentPrewarning.date;
- var stats = new WarningStatistics
- {
- nozzle = nozzleId,
- statistics_date = currentDate,
- daily_total = currentPrewarning.total,
- daily_overproof = currentPrewarning.overproof,
- daily_overproofrate = currentPrewarning.overproofrate,
- daily_overproof_alert = currentPrewarning.overproof_alert,
- daily_overproofrate_alert = currentPrewarning.overproofrate_alert,
- daily_overproof_2 = currentPrewarning.overproof_2,
- daily_overproofrate_2 = currentPrewarning.overproofrate_2
- };
- // 计算 35 天累计统计
- CalculateAccumulatedStatistics(sortedData, i, stats);
- results.Add(stats);
- }
- }
- Console.WriteLine($"[预警告统计] 计算完成,共{results.Count}条记录");
- return results;
- }
- /// <summary>
- /// 计算累计统计数据(核心业务逻辑 - 按 C++ update_warning 实现)
- /// 规则:从最早日期正向迭代到当前日期,累加统计并在满足条件时清零
- /// </summary>
- private void CalculateAccumulatedStatistics(List<TPrewarning> sortedData, int currentIndex, WarningStatistics stats)
- {
- // 累加器初始化(对应 C++ acc_over, acc_over_alert, acc_total, acc_over_2)
- int acc_over = 0; // 累计超标记录
- int acc_over_alert = 0; // 累计严重超标记录
- int acc_total = 0; // 累计总数
- int acc_over_2 = 0; // 累计二级超标记录(广东中石化)
-
- // 连续天数计数器
- int continue_days = 0;
- int continue_days_2 = 0;
- int continue_days_alert = 0;
-
- // 最后计算的超标率
- int last_rate = 0;
- int last_rate_2 = 0;
- int last_rate_alert = 0;
- // 获取区域规则配置
- bool isBeijingMode = VRRules.RefReachCount; // 北京模式:ref_reach_count
- int refCount = VRRules.RefCount; // 标准模式阈值(通常 5)
- int refPrewarningIndex = VRRules.RefPrewarningIndex; // 超标率阈值
- int refPrewarningIndexAlert = VRRules.RefPrewarningIndexAlert; // 严重超标率阈值
- bool isZhejiangSpecial = VRRules.BCountAllDay; // 浙江中石化特殊逻辑
- // 正向迭代:从第 0 天到当前天(对应 C++ mapDayWarning.begin() 到 end)
- for (int i = 0; i <= currentIndex && i < sortedData.Count; i++)
- {
- var day = sortedData[i];
-
- // 累加数据
- acc_over += day.overproof;
- acc_over_alert += day.overproof_alert;
- acc_total += day.total;
- acc_over_2 += day.overproof_2;
-
- // 计算累计超标率(避免除以 0)
- last_rate = acc_total == 0 ? 0 : (acc_over * 100) / acc_total;
- last_rate_2 = acc_total == 0 ? 0 : (acc_over_2 * 100) / acc_total;
- last_rate_alert = acc_total == 0 ? 0 : (acc_over_alert * 100) / acc_total;
-
- // 更新警告对象中的累计值(用于显示)
- // 注意:这里不直接设置 stats,因为可能还需要清零
-
- if (isBeijingMode)
- {
- // === 北京模式(ref_reach_count)===
- // 规则:超标率>=阈值 且 总笔数>=5 时,连续天数 +1
-
- // 普通超标判断
- if (last_rate >= refPrewarningIndex)
- {
- if (acc_total >= 5)
- {
- continue_days++;
- }
- }
- else
- {
- if (acc_total >= 5)
- {
- continue_days = 0;
- }
- }
-
- // 严重超标判断
- if (last_rate_alert >= refPrewarningIndexAlert)
- {
- if (acc_total >= 5)
- {
- continue_days_alert++;
- }
- }
- else
- {
- if (acc_total >= 5)
- {
- continue_days_alert = 0;
- }
- }
-
- // 已经算入一天,清零累加器
- acc_over = 0;
- acc_over_alert = 0;
- acc_total = 0;
- acc_over_2 = 0;
- }
- else
- {
- // === 标准模式 ===
- // 规则:总笔数>=ref_count(通常 5) 时才检查超标率
-
- if (acc_total >= refCount)
- {
- // 普通超标判断
- if (last_rate >= refPrewarningIndex)
- {
- continue_days++;
- }
- else
- {
- continue_days = 0;
- }
-
- // 严重超标判断
- if (last_rate_alert >= refPrewarningIndexAlert)
- {
- continue_days_alert++;
- }
- else
- {
- continue_days_alert = 0;
- }
-
- // 二级超标判断(广东中石化)
- if (last_rate_2 >= refPrewarningIndex) // || day.continueoverproof == 1
- {
- continue_days_2++;
- }
- else
- {
- continue_days_2 = 0;
- }
-
- // 已经算入一天,清零累加器
- acc_over = 0;
- acc_over_alert = 0;
- acc_total = 0;
- acc_over_2 = 0;
- }
- else
- {
- // 该分支是 ref_count 大于 0(即需要进行累计)的情况下,
- // 而 acc_total 小于 ref_count 时进入
-
- if (isZhejiangSpecial)
- {
- // === 浙江中石化特殊逻辑 ===
- // 不足 5 笔时延续之前的预警状态
- if (continue_days > 0)
- {
- continue_days++;
- }
-
- if (continue_days_alert > 0)
- {
- continue_days_alert++;
- }
- }
- }
- }
- }
-
- // 设置最终统计结果
- stats.continue_days = continue_days;
- stats.continue_days_2 = continue_days_2;
- stats.continue_days_alert = continue_days_alert;
- stats.last_overproofrate = last_rate;
- stats.last_overproofrate_2 = last_rate_2;
- stats.last_overproofrate_alert = last_rate_alert;
-
- // 同时保留原有的累计字段(用于兼容)
- stats.total_count = acc_total;
- stats.total_overproof = acc_over;
- stats.total_overproof_alert = acc_over_alert;
- stats.total_overproof_2 = acc_over_2;
- stats.total_overproofrate = last_rate;
- stats.total_overproofrate_2 = last_rate_2;
- stats.total_overproofrate_alert = last_rate_alert;
- }
- /// <summary>
- /// 获取所有有数据的油枪 ID 列表
- /// </summary>
- public async Task<List<int>> GetAllNozzleIdsAsync()
- {
- var nozzles = await _prewarning.AsQueryable()
- .Select(p => p.nozzle)
- .Distinct()
- .ToListAsync();
- return nozzles;
- }
- /// <summary>
- /// 获取统计日期范围(最早到最新)
- /// </summary>
- public async Task<(DateOnly minDate, DateOnly maxDate)> GetDateRangeAsync()
- {
- var dates = await _prewarning.AsQueryable()
- .Select(p => p.date)
- .ToListAsync();
-
- if (dates == null || dates.Count == 0)
- {
- return (new DateOnly(), new DateOnly());
- }
-
- var minDate = dates.Min();
- var maxDate = dates.Max();
- return (minDate, maxDate);
- }
- }
- }
|