From e86b8520dda3f0fca73542854720da29a2c2f713 Mon Sep 17 00:00:00 2001 From: tq <1916474859@qq,com> Date: Thu, 4 Jul 2024 14:33:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service/Ammeter/EmeterDayEnergyService.cs | 24 +- Service/Ammeter/EmeterEnergyChangeService.cs | 201 +---------------- Service/Ammeter/EmeterEnergyService.cs | 213 +----------------- Service/Ammeter/EmeterHourEnergyService.cs | 48 ---- Service/Ammeter/EmeterMinutesEnergyService.cs | 49 ---- Service/MyJob/CountDayOrderJob.cs | 14 +- Service/MyJob/EmeterDayJob.cs | 60 ++--- Service/MyJob/EmeterHourJob.cs | 7 +- Service/MyJob/EmeterMinuteJob.cs | 7 +- .../Controllers/Ammeter/AmmeterController.cs | 176 +++++++++------ 10 files changed, 182 insertions(+), 617 deletions(-) diff --git a/Service/Ammeter/EmeterDayEnergyService.cs b/Service/Ammeter/EmeterDayEnergyService.cs index adca2cb..3c4b30c 100644 --- a/Service/Ammeter/EmeterDayEnergyService.cs +++ b/Service/Ammeter/EmeterDayEnergyService.cs @@ -100,18 +100,18 @@ ORDER BY public async Task> GetAllElectricalData(int type) { string sql = $@" - SELECT - code, - MAX(real_time_value) - MIN(real_time_value) AS Value - FROM - emeter_day_energy - WHERE - time >= DATE_SUB(NOW(), INTERVAL 1 MONTH) - AND type = {type} - GROUP BY - code - ORDER BY - code; + SELECT + code, + MAX(real_time_value) - MIN(real_time_value) AS Value + FROM + emeter_day_energy + WHERE + time >= DATE_FORMAT(NOW(), '%Y-%m-01') + AND type = {type} + GROUP BY + code + ORDER BY + code; "; List emeterEnergies = await this.BaseDal.SqlQueryable(sql); diff --git a/Service/Ammeter/EmeterEnergyChangeService.cs b/Service/Ammeter/EmeterEnergyChangeService.cs index eaef956..707873b 100644 --- a/Service/Ammeter/EmeterEnergyChangeService.cs +++ b/Service/Ammeter/EmeterEnergyChangeService.cs @@ -14,175 +14,6 @@ public class EmeterEnergyChangeService : BaseServices { _emeterEnergyChangeRep = emeterEnergyChangeRep; } - - public async Task> SevenDaysElectrical() - { - // 根据code分组,取最近7天数据,每天最大值-最小值=当日用电量,如果当天目前只有一条数据,取当天最大值-前一天最大值=当天耗电量 - string sql = @" - WITH daily_max_min AS ( - SELECT - `code`, - DATE(`upload_time`) AS `UploadTime`, - MAX(`value`) AS `MaxValue`, - MIN(`value`) AS `MinValue`, - COUNT(*) AS `Count` - FROM - `emeter_energy_change` - WHERE - `upload_time` >= NOW() - INTERVAL 8 DAY - GROUP BY - `code`, DATE(`upload_time`) -), previous_day_max AS ( - SELECT - `code`, - `UploadTime`, - `MaxValue`, - `MinValue`, - `Count`, - LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `UploadTime`) AS `PreviousMaxValue` - FROM - daily_max_min -), daily_consumption AS ( - SELECT - d.`code`, - d.`UploadTime`, - CASE - WHEN d.`Count` > 1 THEN d.`MaxValue` - d.`MinValue` - WHEN d.`Count` = 1 AND d.`PreviousMaxValue` IS NOT NULL THEN d.`MaxValue` - d.`PreviousMaxValue` - ELSE 0 - END AS `DailyValue` - FROM - previous_day_max d - WHERE - d.`UploadTime` >= NOW() - INTERVAL 7 DAY -) -SELECT - `UploadTime`, - SUM(`DailyValue`) AS `Value` -FROM - daily_consumption -GROUP BY - `UploadTime` -ORDER BY - `UploadTime`; - - - - "; - - List emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql); - return emeterEnergies; - } - - public async Task> GetAllElectricalData() - { - string sql = @" - SELECT - code, - MAX(value) - MIN(value) AS Value - FROM - emeter_energy_change - WHERE - upload_time >= DATE_SUB(NOW(), INTERVAL 1 MONTH) - GROUP BY - code - ORDER BY - code; - - "; - List emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql); - return emeterEnergies; - } - - public async Task> GetTodayHourlyElectricalData() - { - string sql = @" - WITH hourly_max_min AS ( - SELECT - `code`, - DATE_FORMAT(`upload_time`, '%Y-%m-%d %H:00:00') AS `Hour`, - MAX(`value`) AS `MaxValue`, - MIN(`value`) AS `MinValue`, - COUNT(*) AS `record_count` - FROM - `emeter_energy_change` - WHERE - DATE(`upload_time`) = CURDATE() - GROUP BY - `code`, DATE_FORMAT(`upload_time`, '%Y-%m-%d %H:00:00') - ), previous_hour_max AS ( - SELECT - `code`, - `Hour`, - LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `Hour`) AS `PreviousMaxValue` - FROM - `hourly_max_min` - ) - SELECT - h.`code`, - h.`Hour`, - CASE - WHEN h.`record_count` > 1 THEN h.`MaxValue` - h.`MinValue` - ELSE h.`MaxValue` - COALESCE(p.`PreviousMaxValue`, 0) - END AS `Value` - FROM - `hourly_max_min` h - LEFT JOIN - `previous_hour_max` p ON h.`code` = p.`code` AND h.`Hour` = p.`Hour` - ORDER BY - h.`code`, h.`Hour`; - "; - List emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql); - return emeterEnergies; - } - - /// - /// 根据分钟统计电量 - /// - /// - public async Task> GetTodayMinutelyElectricalData() - { - string sql = @" - WITH minute_max_min AS ( - SELECT - `code`, - DATE_FORMAT(`upload_time`, '%Y-%m-%d %H:%i:00') AS `Minute`, - MAX(`value`) AS `MaxValue`, - MIN(`value`) AS `MinValue`, - COUNT(*) AS `record_count` - FROM - `emeter_energy_change` - WHERE - `upload_time` >= NOW() - INTERVAL 1 HOUR - GROUP BY - `code`, DATE_FORMAT(`upload_time`, '%Y-%m-%d %H:%i:00') - ), previous_minute_max AS ( - SELECT - `code`, - `Minute`, - LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `Minute`) AS `PreviousMaxValue` - FROM - `minute_max_min` - ) - SELECT - m.`code`, - m.`Minute`, - CASE - WHEN m.`record_count` > 1 THEN m.`MaxValue` - m.`MinValue` - WHEN p.`PreviousMaxValue` IS NULL THEN 0 - ELSE m.`MaxValue` - p.`PreviousMaxValue` - END AS `Value` - FROM - `minute_max_min` m - LEFT JOIN - `previous_minute_max` p ON m.`code` = p.`code` AND m.`Minute` = p.`Minute` - ORDER BY - m.`code`, m.`Minute`; -"; - - List emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql); - return emeterEnergies; - } /// /// 获取交流电表实时数据 @@ -193,30 +24,14 @@ ORDER BY // 将传入的时间参数转换为DateTime类型 DateTime endDateTime = DateTime.Parse(endTime); - // 根据传入的时间参数计算7天前的时间 - DateTime startDateTime = endDateTime.AddDays(-7); + // 根据传入的时间参数计算1分钟的时间 + DateTime startDateTime = endDateTime.AddMinutes(-1); - // 根据code分组,取最新时间的一条数据,数据范围7天 - string sql = @" - SELECT t1.* - FROM emeter_energy_change t1 - JOIN ( - SELECT code, MAX(upload_time) as latest_upload_time - FROM emeter_energy_change - WHERE upload_time >= @StartDateTime AND upload_time <= @EndDateTime - GROUP BY code - ) t2 ON t1.code = t2.code AND t1.upload_time = t2.latest_upload_time - WHERE t1.upload_time >= @StartDateTime AND t1.upload_time <= @EndDateTime; - "; - - // 使用参数化查询防止SQL注入 - var parameters = new List - { - new SugarParameter("@StartDateTime", startDateTime), - new SugarParameter("@EndDateTime", endDateTime) - }; - - List emeterEnergyChanges = _emeterEnergyChangeRep.SqlQuery(sql, parameters); - return emeterEnergyChanges; + List list = await _emeterEnergyChangeRep.QueryListByClauseAsync(i => i.UploadTime>=startDateTime); + + return list + .GroupBy(e => e.Code) + .Select(g => g.OrderByDescending(e => e.UploadTime).First()) + .ToList(); } } \ No newline at end of file diff --git a/Service/Ammeter/EmeterEnergyService.cs b/Service/Ammeter/EmeterEnergyService.cs index 0236ad4..3e04024 100644 --- a/Service/Ammeter/EmeterEnergyService.cs +++ b/Service/Ammeter/EmeterEnergyService.cs @@ -14,188 +14,7 @@ public class EmeterEnergyService : BaseServices { this.BaseDal = service; } - - /// - /// 获取七天用电量 - /// - /// - public async Task> SevenDaysElectrical() - { - // 根据code分组,取最近7天数据,每天最大值-最小值=当日用电量,如果当天目前只有一条数据,取当天最大值-前一天最大值=当天耗电量 - string sql = @" - WITH daily_max_min AS ( - SELECT - `code`, - DATE(`upload_time`) AS `UploadTime`, - MAX(`value`) AS `MaxValue`, - MIN(`value`) AS `MinValue`, - COUNT(*) AS `Count` - FROM - `emeter_energy` - WHERE - `upload_time` >= NOW() - INTERVAL 8 DAY - GROUP BY - `code`, DATE(`upload_time`) -), previous_day_max AS ( - SELECT - `code`, - `UploadTime`, - `MaxValue`, - `MinValue`, - `Count`, - LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `UploadTime`) AS `PreviousMaxValue` - FROM - daily_max_min -), daily_consumption AS ( - SELECT - d.`code`, - d.`UploadTime`, - CASE - WHEN d.`Count` > 1 THEN d.`MaxValue` - d.`MinValue` - WHEN d.`Count` = 1 AND d.`PreviousMaxValue` IS NOT NULL THEN d.`MaxValue` - d.`PreviousMaxValue` - ELSE 0 - END AS `DailyValue` - FROM - previous_day_max d - WHERE - d.`UploadTime` >= NOW() - INTERVAL 7 DAY -) -SELECT - `UploadTime`, - SUM(`DailyValue`) AS `Value` -FROM - daily_consumption -GROUP BY - `UploadTime` -ORDER BY - `UploadTime`; - - - - "; - List emeterEnergies = await this.BaseDal.SqlQueryable(sql); - return emeterEnergies; - } - - /// - /// 获取全部用电量 - /// - /// - public async Task> GetAllElectricalData() - { - string sql = @" - SELECT - code, - MAX(value) - MIN(value) AS Value - FROM - emeter_energy - WHERE - upload_time >= DATE_SUB(NOW(), INTERVAL 1 MONTH) - GROUP BY - code - ORDER BY - code; - - "; - - List emeterEnergies = await this.BaseDal.SqlQueryable(sql); - return emeterEnergies; - } - - /// - /// 根据小时统计电量 - /// - /// - public async Task> GetTodayHourlyElectricalData() - { - string sql = @" - WITH hourly_max_min AS ( - SELECT - `code`, - DATE_FORMAT(`upload_time`, '%Y-%m-%d %H:00:00') AS `Hour`, - MAX(`value`) AS `MaxValue`, - MIN(`value`) AS `MinValue`, - COUNT(*) AS `record_count` - FROM - `emeter_energy` - WHERE - DATE(`upload_time`) = CURDATE() - GROUP BY - `code`, DATE_FORMAT(`upload_time`, '%Y-%m-%d %H:00:00') - ), previous_hour_max AS ( - SELECT - `code`, - `Hour`, - LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `Hour`) AS `PreviousMaxValue` - FROM - `hourly_max_min` - ) - SELECT - h.`code`, - h.`Hour`, - CASE - WHEN h.`record_count` > 1 THEN h.`MaxValue` - h.`MinValue` - ELSE h.`MaxValue` - COALESCE(p.`PreviousMaxValue`, 0) - END AS `Value` - FROM - `hourly_max_min` h - LEFT JOIN - `previous_hour_max` p ON h.`code` = p.`code` AND h.`Hour` = p.`Hour` - ORDER BY - h.`code`, h.`Hour`; - "; - - List emeterEnergies = await this.BaseDal.SqlQueryable(sql); - return emeterEnergies; - } - - /// - /// 根据分钟统计电量 - /// - /// - public async Task> GetTodayMinutelyElectricalData() - { - string sql = @" - WITH minute_max_min AS ( - SELECT - `code`, - DATE_FORMAT(`upload_time`, '%Y-%m-%d %H:%i:00') AS `Minute`, - MAX(`value`) AS `MaxValue`, - MIN(`value`) AS `MinValue`, - COUNT(*) AS `record_count` - FROM - `emeter_energy` - WHERE - `upload_time` >= NOW() - INTERVAL 1 HOUR - GROUP BY - `code`, DATE_FORMAT(`upload_time`, '%Y-%m-%d %H:%i:00') - ), previous_minute_max AS ( - SELECT - `code`, - `Minute`, - LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `Minute`) AS `PreviousMaxValue` - FROM - `minute_max_min` - ) - SELECT - m.`code`, - m.`Minute`, - CASE - WHEN m.`record_count` > 1 THEN m.`MaxValue` - m.`MinValue` - WHEN p.`PreviousMaxValue` IS NULL THEN 0 - ELSE m.`MaxValue` - p.`PreviousMaxValue` - END AS `Value` - FROM - `minute_max_min` m - LEFT JOIN - `previous_minute_max` p ON m.`code` = p.`code` AND m.`Minute` = p.`Minute` - ORDER BY - m.`code`, m.`Minute`; -"; - - List emeterEnergies = await this.BaseDal.SqlQueryable(sql); - return emeterEnergies; - } + /// /// 获取直流电表实时数据 /// @@ -205,30 +24,14 @@ ORDER BY // 将传入的时间参数转换为DateTime类型 DateTime endDateTime = DateTime.Parse(endTime); - // 根据传入的时间参数计算7天前的时间 + // 根据传入的时间参数计算1分钟的时间 DateTime startDateTime = endDateTime.AddMinutes(-1); - // 根据code分组,取最新时间的一条数据,数据范围7天 - string sql = @" - SELECT t1.* - FROM emeter_energy t1 - JOIN ( - SELECT code, MAX(upload_time) as latest_upload_time - FROM emeter_energy - WHERE upload_time >= @StartDateTime AND upload_time <= @EndDateTime - GROUP BY code - ) t2 ON t1.code = t2.code AND t1.upload_time = t2.latest_upload_time - WHERE t1.upload_time >= @StartDateTime AND t1.upload_time <= @EndDateTime; - "; - - // 使用参数化查询防止SQL注入 - var parameters = new List - { - new SugarParameter("@StartDateTime", startDateTime), - new SugarParameter("@EndDateTime", endDateTime) - }; - - List emeterEnergies = this.BaseDal.SqlQuery(sql, parameters); - return emeterEnergies; + List list = await this.BaseDal.QueryListByClauseAsync(i => i.UploadTime>=startDateTime); + + return list + .GroupBy(e => e.Code) + .Select(g => g.OrderByDescending(e => e.UploadTime).First()) + .ToList(); } } \ No newline at end of file diff --git a/Service/Ammeter/EmeterHourEnergyService.cs b/Service/Ammeter/EmeterHourEnergyService.cs index 95054ce..86dd2a3 100644 --- a/Service/Ammeter/EmeterHourEnergyService.cs +++ b/Service/Ammeter/EmeterHourEnergyService.cs @@ -32,54 +32,6 @@ namespace Service.Ammeter return Result>.Success(PageResult.ConvertPage(this.BaseDal.QueryIPageByCause(queryPageModel1, null))); } - /// - /// 根据小时统计电量 - /// - /// - public async Task> GetTodayHourlyElectricalData(int type) - { - string sql = $@" - WITH hourly_max_min AS ( - SELECT - `code`, - DATE_FORMAT(`time`, '%Y-%m-%d %H:00:00') AS `Hour`, - MAX(`real_time_value`) AS `MaxValue`, - MIN(`real_time_value`) AS `MinValue`, - COUNT(*) AS `record_count` - FROM - `emeter_hour_energy` - WHERE - DATE(`time`) = CURDATE() - AND `type` = {type} - GROUP BY - `code`, DATE_FORMAT(`time`, '%Y-%m-%d %H:00:00') - ), previous_hour_max AS ( - SELECT - `code`, - `Hour`, - LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `Hour`) AS `PreviousMaxValue` - FROM - hourly_max_min - ) - SELECT - h.`code`, - h.`Hour`, - CASE - WHEN h.`record_count` > 1 THEN h.`MaxValue` - h.`MinValue` - ELSE h.`MaxValue` - COALESCE(p.`PreviousMaxValue`, 0) - END AS `Value` - FROM - hourly_max_min h - LEFT JOIN - previous_hour_max p ON h.`code` = p.`code` AND h.`Hour` = p.`Hour` - ORDER BY - h.`code`, h.`Hour`; - "; - - List emeterEnergies = await this.BaseDal.SqlQueryable(sql); - return emeterEnergies; - } - /// /// 获取直流电表实时数据 diff --git a/Service/Ammeter/EmeterMinutesEnergyService.cs b/Service/Ammeter/EmeterMinutesEnergyService.cs index 0cbd296..007ff33 100644 --- a/Service/Ammeter/EmeterMinutesEnergyService.cs +++ b/Service/Ammeter/EmeterMinutesEnergyService.cs @@ -32,54 +32,5 @@ namespace Service.Ammeter return PageResult.ConvertPage(_emeterMinutesEnergyRepository1.QueryIPageByCause(queryPageModel1, null)); } - - /// - /// 根据分钟统计电量 - /// - /// - public async Task> GetTodayMinutelyElectricalData(int type) - { - string sql = $@" - WITH minute_max_min AS ( - SELECT - `code`, - DATE_FORMAT(`time`, '%Y-%m-%d %H:%i:00') AS `Minute`, - MAX(`real_time_value`) AS `MaxValue`, - MIN(`real_time_value`) AS `MinValue`, - COUNT(*) AS `record_count` - FROM - `emeter_minutes_energy` - WHERE - `time` >= NOW() - INTERVAL 1 HOUR - AND `type` = {type} - GROUP BY - `code`, DATE_FORMAT(`time`, '%Y-%m-%d %H:%i:00') - ), previous_minute_max AS ( - SELECT - `code`, - `Minute`, - LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `Minute`) AS `PreviousMaxValue` - FROM - minute_max_min - ) - SELECT - m.`code`, - m.`Minute`, - CASE - WHEN m.`record_count` > 1 THEN m.`MaxValue` - m.`MinValue` - WHEN p.`PreviousMaxValue` IS NULL THEN 0 - ELSE m.`MaxValue` - p.`PreviousMaxValue` - END AS `Value` - FROM - minute_max_min m - LEFT JOIN - previous_minute_max p ON m.`code` = p.`code` AND m.`Minute` = p.`Minute` - ORDER BY - m.`code`, m.`Minute`; -"; - - List emeterEnergies = await this.BaseDal.SqlQueryable(sql); - return emeterEnergies; - } } } diff --git a/Service/MyJob/CountDayOrderJob.cs b/Service/MyJob/CountDayOrderJob.cs index 784399e..2905a29 100644 --- a/Service/MyJob/CountDayOrderJob.cs +++ b/Service/MyJob/CountDayOrderJob.cs @@ -87,7 +87,19 @@ public class CountDayOrderJob : AbstractCronJob dayCount.ToltalSwapAllTime = Math.Round(totalMinutes, 2); dayCount.State = StaticStationInfo.StationStatus; - _ExStationDayRunResultService.Insert(dayCount); + + + var dateTime = DateTime.Now; + var formattedDateTime = dateTime.ToString("yyyy-MM-dd HH:mm"); + + // 查询当前分钟的数据 + var existingData = _ExStationDayRunResultService.QueryListByClause(i => + i.CreatedTime.HasValue && formattedDateTime.Equals(i.CreatedTime.Value.ToString("yyyy-MM-dd HH:mm"))); + + if (!existingData.Any()) + { + _ExStationDayRunResultService.Insert(dayCount); + } return Task.CompletedTask; } diff --git a/Service/MyJob/EmeterDayJob.cs b/Service/MyJob/EmeterDayJob.cs index 275891f..03338f6 100644 --- a/Service/MyJob/EmeterDayJob.cs +++ b/Service/MyJob/EmeterDayJob.cs @@ -32,43 +32,31 @@ public class EmeterDayJob: AbstractCronJob List emeterEnergiesChange = _EmeterEnergyChangeRepository.QueryListByClause(i => dateTime.Equals(i.UploadTime.ToString("yyyy-MM-dd")), "upload_time desc"); + List db = new List(); if (emeterEnergies.Count > 0) { Dictionary> dictionary = emeterEnergies.GroupBy(i => i.Code).ToDictionary(i => i.Key, i => i.ToList()); - List list = new List(); + foreach (var keyValuePair in dictionary) { EmeterDayEnergy emitterDayEnergy = new EmeterDayEnergy(); emitterDayEnergy.Code = keyValuePair.Key; emitterDayEnergy.Time = dateTime; emitterDayEnergy.Type = 1; - - EmeterEnergy? maxValue = keyValuePair.Value.MaxBy(x => x.Value); - - EmeterEnergy? minValue = keyValuePair.Value.MinBy(x => x.Value); - - if (maxValue != null) - { - emitterDayEnergy.RealTimeValue = maxValue.Value; - } - - if (maxValue != null && minValue != null) - { - emitterDayEnergy.Value = maxValue.Value - minValue.Value; - } - - list.Add(emitterDayEnergy); + var value = keyValuePair.Value; + var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList(); + emitterDayEnergy.RealTimeValue = orderByDescending[0].Value; + emitterDayEnergy.Value = orderByDescending[0].Value - + orderByDescending[^1].Value; + db.Add(emitterDayEnergy); } - - _EmeterDayEnergyRepository.Insert(list); } if (emeterEnergiesChange.Count > 0) { Dictionary> dictionaryChange = emeterEnergiesChange.GroupBy(i => i.Code).ToDictionary(i => i.Key, i => i.ToList()); - List list = new List(); foreach (var keyValuePair in dictionaryChange) { @@ -76,25 +64,23 @@ public class EmeterDayJob: AbstractCronJob emitterDayEnergy.Code = keyValuePair.Key; emitterDayEnergy.Time = dateTime; emitterDayEnergy.Type = 2; - - EmeterEnergyChange? maxValue = keyValuePair.Value.MaxBy(x => x.Value); - - EmeterEnergyChange? minValue = keyValuePair.Value.MinBy(x => x.Value); - - if (maxValue != null) - { - emitterDayEnergy.RealTimeValue = maxValue.Value; - } - - if (maxValue != null && minValue != null) - { - emitterDayEnergy.Value = maxValue.Value - minValue.Value; - } - - list.Add(emitterDayEnergy); + var value = keyValuePair.Value; + var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList(); + emitterDayEnergy.RealTimeValue = orderByDescending[0].Value; + emitterDayEnergy.Value = orderByDescending[0].Value - + orderByDescending[^1].Value; + db.Add(emitterDayEnergy); } + } - _EmeterDayEnergyRepository.Insert(list); + if (db.Count > 0) + { + var existingData = _EmeterDayEnergyRepository.QueryListByClause(i => + dateTime.Equals(i.Time)); + if (!existingData.Any()) + { + _EmeterDayEnergyRepository.Insert(db); + } } return Task.CompletedTask; diff --git a/Service/MyJob/EmeterHourJob.cs b/Service/MyJob/EmeterHourJob.cs index 756ae69..2cc57dd 100644 --- a/Service/MyJob/EmeterHourJob.cs +++ b/Service/MyJob/EmeterHourJob.cs @@ -78,7 +78,12 @@ public class EmeterHourJob : AbstractCronJob if (db.Count > 0) { - _EmeterHourEnergy.Insert(db); + var existingData = _EmeterHourEnergy.QueryListByClause(i => + dateTime.Equals(i.Time)); + if (!existingData.Any()) + { + _EmeterHourEnergy.Insert(db); + } } return Task.CompletedTask; diff --git a/Service/MyJob/EmeterMinuteJob.cs b/Service/MyJob/EmeterMinuteJob.cs index 214cd54..7bd5692 100644 --- a/Service/MyJob/EmeterMinuteJob.cs +++ b/Service/MyJob/EmeterMinuteJob.cs @@ -83,7 +83,12 @@ public class EmeterMinuteJob : AbstractCronJob if (db.Count > 0) { - EmeterMinutesEnergyRepository.Insert(db); + var existingData = EmeterMinutesEnergyRepository.QueryListByClause(i => + dateTime.Equals(i.Time)); + if (!existingData.Any()) + { + EmeterMinutesEnergyRepository.Insert(db); + } } diff --git a/WebStarter/Controllers/Ammeter/AmmeterController.cs b/WebStarter/Controllers/Ammeter/AmmeterController.cs index 63f79bf..9c72066 100644 --- a/WebStarter/Controllers/Ammeter/AmmeterController.cs +++ b/WebStarter/Controllers/Ammeter/AmmeterController.cs @@ -1,4 +1,5 @@ -using AutoMapper; +using System.Linq.Expressions; +using AutoMapper; using Entity.Ammeter; using Entity.Api.Resp; using HybirdFrameworkCore.Entity; @@ -97,25 +98,37 @@ namespace WebStarter.Controllers.Ammeter [HttpGet("GetLastDayMeterData")] public async Task>> GetLastDayMeterData() { - // 获取最近一天直流和交流表数据 - var daysElectricalTask = _emeterHourEnergyService.GetTodayHourlyElectricalData(1); - var daysElectricalChangeTask = _emeterHourEnergyService.GetTodayHourlyElectricalData(2); + DateTime todayZero = DateTime.Today; - await Task.WhenAll(daysElectricalTask, daysElectricalChangeTask); - - var daysElectrical = daysElectricalTask.Result; - var daysElectricalChange = daysElectricalChangeTask.Result; + List list = await _emeterHourEnergyService.QueryListByClauseAsync(i => DateTime.Parse(i.Time)>=todayZero); + + List daysElectrical = list.Where(i => i.Type == 1).ToList(); + + List daysElectricalChange = list.Where(i => i.Type == 2).ToList(); - var config = new MapperConfiguration(cfg => + List energyList = new List(); + + foreach (var energy in daysElectrical) { - cfg.CreateMap().ReverseMap(); - }); - var mapper = config.CreateMapper(); + EmeterResp resp = new EmeterResp(); + resp.Code = energy.Code; + resp.Value = energy.Value; + resp.Hour = energy.Time; + energyList.Add(resp); + } - var energyList = mapper.Map>(daysElectrical); energyList?.ForEach(emeterResp => emeterResp.Type = 0); - - var energyChangeList = mapper.Map>(daysElectricalChange); + + List energyChangeList = new List(); + + foreach (var energy in daysElectricalChange) + { + EmeterResp resp = new EmeterResp(); + resp.Code = energy.Code; + resp.Value = energy.Value; + resp.Hour = energy.Time; + energyChangeList.Add(resp); + } energyChangeList?.ForEach(emeterResp => emeterResp.Type = 1); var respList = new List(); @@ -157,30 +170,6 @@ namespace WebStarter.Controllers.Ammeter return Result>.Success(respList, "成功"); } - /// - /// 直流电能表小时统计 - /// - /// - [HttpGet("GetTodayHourlyEnergy")] - public async Task>> GetTodayHourlyElectricalData() - { - // 直流表数据 - return Result>.Success(await _emeterHourEnergyService.GetTodayHourlyElectricalData(1), "成功"); - } - - - /// - /// 交流电能表小时统计 - /// - /// - [HttpGet("GetTodayHourlyEnergyChange")] - public async Task>> GetTodayHourlyElectricalChangeData() - { - // 交流表数据 - return Result>.Success( - await _emeterHourEnergyService.GetTodayHourlyElectricalData(2), "成功"); - } - /// /// 最近一小时电表数据 /// @@ -188,29 +177,42 @@ namespace WebStarter.Controllers.Ammeter [HttpGet("GetTodayMinutelyElectricalData")] public async Task>> GetTodayMinutelyElectricalData() { - // 获取最近一个小时直流和交流表数据 - var minutesElectricalTask = _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(1); - var minutesElectricalChangeTask = _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(2); - - await Task.WhenAll(minutesElectricalTask, minutesElectricalChangeTask); - - var minutesElectrical = minutesElectricalTask.Result; - var minutesElectricalChange = minutesElectricalChangeTask.Result; + + DateTime now = DateTime.Now; + DateTime time = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0); + + List list = await _emeterMinutesEnergyService.QueryListByClauseAsync(i => DateTime.Parse(i.Time)>=time); + + List daysElectrical = list.Where(i => i.Type == 1).ToList(); + + List daysElectricalChange = list.Where(i => i.Type == 2).ToList(); - var config = new MapperConfiguration(cfg => + List energyList = new List(); + + foreach (var energy in daysElectrical) { - cfg.CreateMap().ReverseMap(); - }); - var mapper = config.CreateMapper(); + EmeterResp resp = new EmeterResp(); + resp.Code = energy.Code; + resp.Value = energy.Value; + resp.Minute = energy.Time; + energyList.Add(resp); + } - var energyList = mapper.Map>(minutesElectrical); - // 类型赋值直流 energyList?.ForEach(emeterResp => emeterResp.Type = 0); - - var energyChangeList = mapper.Map>(minutesElectricalChange); - // 类型赋值交流 + + List energyChangeList = new List(); + + foreach (var energy in daysElectricalChange) + { + EmeterResp resp = new EmeterResp(); + resp.Code = energy.Code; + resp.Value = energy.Value; + resp.Minute = energy.Time; + energyChangeList.Add(resp); + } energyChangeList?.ForEach(emeterResp => emeterResp.Type = 1); - + + var respList = new List(); var groupedEnergyList = energyList @@ -257,31 +259,64 @@ namespace WebStarter.Controllers.Ammeter [HttpGet("GetEnergyMeterRealTime")] public async Task> GetEnergyMeterRealTime(string endTime) { - // 不传值时查实时 - if (!DateTime.TryParse(endTime, out DateTime parsedEndTime)) + EmeterEnergyResp resp = new EmeterEnergyResp(); + DateTime parsedEndTime; + + if (!DateTime.TryParse(endTime, out parsedEndTime)) { parsedEndTime = DateTime.Now; + endTime = parsedEndTime.ToString("yyyy-MM-dd HH:mm:ss"); + await QueryRealTime(resp,endTime); + } + else + { + endTime = parsedEndTime.ToString("yyyy-MM-dd HH:mm:ss"); + await QueryParameterTime(resp, endTime); } - endTime = parsedEndTime.ToString("yyyy-MM-dd HH:mm:ss"); + return Result.Success(resp, "成功"); + } - // 获取实时直流和交流表数据 - List minutesElectrical = await _emeterHourEnergyService.GetEnergyMeterRealTime(endTime, 1); - List minutesElectricalChange = await _emeterHourEnergyService.GetEnergyMeterRealTime(endTime, 2); + private async Task QueryRealTime(EmeterEnergyResp resp,string time) + { + List sevenDaysElectrical = await _emeterEnergyService.GetEnergyMeterRealTime(time); + List emeterEnergyChanges = await _emeterEnergyChangeService.GetEnergyMeterRealTime(time); - EmeterEnergyResp resp = new EmeterEnergyResp + var config = new MapperConfiguration(cfg => { - emeterEnergy = TransformEnergyData(minutesElectrical, 0), - emeterEnergyChange = TransformEnergyData(minutesElectricalChange, 1) - }; + cfg.CreateMap().ReverseMap(); + cfg.CreateMap().ReverseMap(); + }); + IMapper mapper = config.CreateMapper(); - return Result.Success(resp, "成功"); + resp.emeterEnergy = MapEnergyData(mapper.Map>(sevenDaysElectrical), 0); + resp.emeterEnergyChange = MapEnergyData(mapper.Map>(emeterEnergyChanges), 1); + } + + private async Task QueryParameterTime(EmeterEnergyResp resp, string endTime) + { + List minutesElectrical = + await _emeterHourEnergyService.GetEnergyMeterRealTime(endTime, 1); + List minutesElectricalChange = + await _emeterHourEnergyService.GetEnergyMeterRealTime(endTime, 2); + + resp.emeterEnergy = TransformEnergyData(minutesElectrical, 0); + resp.emeterEnergyChange = TransformEnergyData(minutesElectricalChange, 1); + } + + private List MapEnergyData(List energyData, int type) + { + energyData?.ForEach(emeterResp => emeterResp.Type = type); + return energyData?.OrderBy(e => e.Code).ToList(); } + private List TransformEnergyData(List energyData, int type) { - return energyData.Select(hourEnergy => + return energyData.Select(hourEnergy => { - DateTime? uploadTime = DateTime.TryParse(hourEnergy.Time, out DateTime parsedTime) ? parsedTime : (DateTime?)null; + DateTime? uploadTime = DateTime.TryParse(hourEnergy.Time, out DateTime parsedTime) + ? parsedTime + : (DateTime?)null; return new EmeterResp { @@ -292,6 +327,7 @@ namespace WebStarter.Controllers.Ammeter }; }).OrderBy(e => e.Code).ToList(); } + private static void ElectricEnergyDay(List daysElectrical, List emeterEnergyChanges, EmeterEnergyResp resp) {