diff --git a/Service/Ammeter/EmeterEnergyChangeService.cs b/Service/Ammeter/EmeterEnergyChangeService.cs index c794f96..bba64a0 100644 --- a/Service/Ammeter/EmeterEnergyChangeService.cs +++ b/Service/Ammeter/EmeterEnergyChangeService.cs @@ -182,4 +182,27 @@ ORDER BY List emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql); return emeterEnergies; } + + /// + /// 获取交流电表实时数据 + /// + /// + public async Task> GetEnergyMeterRealTime() + { + // 根据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 >= NOW() - INTERVAL 7 DAY + GROUP BY code +) t2 ON t1.code = t2.code AND t1.upload_time = t2.latest_upload_time +WHERE t1.upload_time >= NOW() - INTERVAL 7 DAY; +"; + + List emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql); + return emeterEnergies; + } } \ No newline at end of file diff --git a/Service/Ammeter/EmeterEnergyService.cs b/Service/Ammeter/EmeterEnergyService.cs index 134fbfa..ee33709 100644 --- a/Service/Ammeter/EmeterEnergyService.cs +++ b/Service/Ammeter/EmeterEnergyService.cs @@ -192,6 +192,28 @@ ORDER BY m.`code`, m.`Minute`; "; + List emeterEnergies = await this.BaseDal.SqlQueryable(sql); + return emeterEnergies; + } + /// + /// 获取直流电表实时数据 + /// + /// + public async Task> GetEnergyMeterRealTime() + { + // 根据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 >= NOW() - INTERVAL 7 DAY + GROUP BY code +) t2 ON t1.code = t2.code AND t1.upload_time = t2.latest_upload_time +WHERE t1.upload_time >= NOW() - INTERVAL 7 DAY; +"; + List emeterEnergies = await this.BaseDal.SqlQueryable(sql); return emeterEnergies; } diff --git a/WebStarter/Controllers/Ammeter/AmmeterController.cs b/WebStarter/Controllers/Ammeter/AmmeterController.cs index 9b7ca6a..1591e95 100644 --- a/WebStarter/Controllers/Ammeter/AmmeterController.cs +++ b/WebStarter/Controllers/Ammeter/AmmeterController.cs @@ -153,6 +153,25 @@ namespace WebStarter.Controllers.Ammeter return Result.Success(resp,"成功"); } + /// + /// 获取直流交流电表实时数据 + /// + /// + [HttpGet("GetEnergyMeterRealTime")] + public async Task> GetEnergyMeterRealTime() + { + // 获取实时直流和交流表数据 + List minutesElectrical = await _emeterEnergyService.GetEnergyMeterRealTime(); + List minutesElectricalChange = await _emeterEnergyChangeService.GetEnergyMeterRealTime(); + + EmeterEnergyResp resp = new EmeterEnergyResp(); + + ChargeAssignment(minutesElectrical, minutesElectricalChange, resp); + + + return Result.Success(resp,"成功"); + } + private static void ChargeAssignment(List emeterEnergy, List emeterEnergyChanges, EmeterEnergyResp resp) {