diff --git a/Entity/Api/Resp/EmeterEnergyTimeResp.cs b/Entity/Api/Resp/EmeterEnergyTimeResp.cs new file mode 100644 index 0000000..1eb44e1 --- /dev/null +++ b/Entity/Api/Resp/EmeterEnergyTimeResp.cs @@ -0,0 +1,8 @@ +namespace Entity.Api.Resp; + +public class EmeterEnergyTimeResp +{ + public string? time { get; set; } + public List emeterEnergy { get; set; } + public List emeterEnergyChange { get; set; } +} \ No newline at end of file diff --git a/Service/MyJob/EmeterHourJob.cs b/Service/MyJob/EmeterHourJob.cs index 34acbc9..756ae69 100644 --- a/Service/MyJob/EmeterHourJob.cs +++ b/Service/MyJob/EmeterHourJob.cs @@ -66,7 +66,7 @@ public class EmeterHourJob : AbstractCronJob EmeterHourEnergy emeterMinutesEnergy = new EmeterHourEnergy(); emeterMinutesEnergy.Code = keyValuePair.Key; emeterMinutesEnergy.Time = dateTime; - emeterMinutesEnergy.Type = 1; + emeterMinutesEnergy.Type = 2; var value = keyValuePair.Value; var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList(); emeterMinutesEnergy.RealTimeValue = orderByDescending[0].Value; diff --git a/Service/MyJob/EmeterMinuteJob.cs b/Service/MyJob/EmeterMinuteJob.cs index 2794fcf..214cd54 100644 --- a/Service/MyJob/EmeterMinuteJob.cs +++ b/Service/MyJob/EmeterMinuteJob.cs @@ -71,7 +71,7 @@ public class EmeterMinuteJob : AbstractCronJob EmeterMinutesEnergy emeterMinutesEnergy = new EmeterMinutesEnergy(); emeterMinutesEnergy.Code = keyValuePair.Key; emeterMinutesEnergy.Time = dateTime; - emeterMinutesEnergy.Type = 1; + emeterMinutesEnergy.Type = 2; var value = keyValuePair.Value; var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList(); emeterMinutesEnergy.RealTimeValue = orderByDescending[0].Value; diff --git a/WebStarter/Controllers/Ammeter/AmmeterController.cs b/WebStarter/Controllers/Ammeter/AmmeterController.cs index e47b2b8..63f79bf 100644 --- a/WebStarter/Controllers/Ammeter/AmmeterController.cs +++ b/WebStarter/Controllers/Ammeter/AmmeterController.cs @@ -95,33 +95,66 @@ namespace WebStarter.Controllers.Ammeter /// /// [HttpGet("GetLastDayMeterData")] - public async Task> GetLastDayMeterData() + public async Task>> GetLastDayMeterData() { // 获取最近一天直流和交流表数据 - List daysElectrical = await _emeterHourEnergyService.GetTodayHourlyElectricalData(1); - List daysElectricalChange = - await _emeterHourEnergyService.GetTodayHourlyElectricalData(2); + var daysElectricalTask = _emeterHourEnergyService.GetTodayHourlyElectricalData(1); + var daysElectricalChangeTask = _emeterHourEnergyService.GetTodayHourlyElectricalData(2); + + await Task.WhenAll(daysElectricalTask, daysElectricalChangeTask); + + var daysElectrical = daysElectricalTask.Result; + var daysElectricalChange = daysElectricalChangeTask.Result; - EmeterEnergyResp resp = new EmeterEnergyResp(); - var config = new MapperConfiguration(cfg => { cfg.CreateMap().ReverseMap(); }); - IMapper mapper = config.CreateMapper(); + var mapper = config.CreateMapper(); - List EnergyList = mapper.Map>(daysElectrical); - // 类型赋值直流 - EnergyList?.ForEach(emeterResp => emeterResp.Type = 0); + var energyList = mapper.Map>(daysElectrical); + energyList?.ForEach(emeterResp => emeterResp.Type = 0); - List EnergyChangeList = mapper.Map>(daysElectricalChange); - // 类型赋值交流 - EnergyChangeList?.ForEach(emeterResp => emeterResp.Type = 1); + var energyChangeList = mapper.Map>(daysElectricalChange); + energyChangeList?.ForEach(emeterResp => emeterResp.Type = 1); - resp.emeterEnergy = EnergyList.OrderBy(i => i.Code).ToList(); - resp.emeterEnergyChange = EnergyChangeList.OrderBy(i => i.Code).ToList(); - - return Result.Success(resp, "成功"); + var respList = new List(); + + var groupedEnergyList = energyList + .Where(e => !string.IsNullOrEmpty(e.Hour)) + .GroupBy(e => e.Hour) + .Select(g => new EmeterEnergyTimeResp + { + time = g.Key, + emeterEnergy = g.ToList() + }) + .ToList(); + respList.AddRange(groupedEnergyList); + + var groupedEnergyChangeList = energyChangeList + .Where(e => !string.IsNullOrEmpty(e.Hour)) + .GroupBy(e => e.Hour) + .Select(g => new EmeterEnergyTimeResp + { + time = g.Key, + emeterEnergyChange = g.ToList() + }) + .ToList(); + + foreach (var changeItem in groupedEnergyChangeList) + { + var existingItem = respList.FirstOrDefault(r => r.time == changeItem.time); + if (existingItem != null) + { + existingItem.emeterEnergyChange = changeItem.emeterEnergyChange; + } + else + { + respList.Add(changeItem); + } + } + + return Result>.Success(respList, "成功"); } /// @@ -153,34 +186,68 @@ namespace WebStarter.Controllers.Ammeter /// /// [HttpGet("GetTodayMinutelyElectricalData")] - public async Task> GetTodayMinutelyElectricalData() + public async Task>> GetTodayMinutelyElectricalData() { // 获取最近一个小时直流和交流表数据 - List minutesElectrical = await _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(1); - List minutesElectricalChange = - await _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(2); + var minutesElectricalTask = _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(1); + var minutesElectricalChangeTask = _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(2); - EmeterEnergyResp resp = new EmeterEnergyResp(); + await Task.WhenAll(minutesElectricalTask, minutesElectricalChangeTask); + + var minutesElectrical = minutesElectricalTask.Result; + var minutesElectricalChange = minutesElectricalChangeTask.Result; var config = new MapperConfiguration(cfg => { cfg.CreateMap().ReverseMap(); }); - IMapper mapper = config.CreateMapper(); + var mapper = config.CreateMapper(); - List EnergyList = mapper.Map>(minutesElectrical); + var energyList = mapper.Map>(minutesElectrical); // 类型赋值直流 - EnergyList?.ForEach(emeterResp => emeterResp.Type = 0); + energyList?.ForEach(emeterResp => emeterResp.Type = 0); - List EnergyChangeList = mapper.Map>(minutesElectricalChange); + var energyChangeList = mapper.Map>(minutesElectricalChange); // 类型赋值交流 - EnergyChangeList?.ForEach(emeterResp => emeterResp.Type = 1); + energyChangeList?.ForEach(emeterResp => emeterResp.Type = 1); - resp.emeterEnergy = EnergyList.OrderBy(i => i.Code).ToList(); - resp.emeterEnergyChange = EnergyChangeList.OrderBy(i => i.Code).ToList(); + var respList = new List(); + var groupedEnergyList = energyList + .Where(e => !string.IsNullOrEmpty(e.Minute)) + .GroupBy(e => e.Minute) + .Select(g => new EmeterEnergyTimeResp + { + time = g.Key, + emeterEnergy = g.ToList() + }) + .ToList(); + respList.AddRange(groupedEnergyList); + + var groupedEnergyChangeList = energyChangeList + .Where(e => !string.IsNullOrEmpty(e.Minute)) + .GroupBy(e => e.Minute) + .Select(g => new EmeterEnergyTimeResp + { + time = g.Key, + emeterEnergyChange = g.ToList() + }) + .ToList(); - return Result.Success(resp, "成功"); + foreach (var changeItem in groupedEnergyChangeList) + { + var existingItem = respList.FirstOrDefault(r => r.time == changeItem.time); + if (existingItem != null) + { + existingItem.emeterEnergyChange = changeItem.emeterEnergyChange; + } + else + { + respList.Add(changeItem); + } + } + + return Result>.Success(respList, "成功"); } ///