电表查询优化

master
tq 5 months ago
parent 34a8995f15
commit 719f79f06b

@ -0,0 +1,8 @@
namespace Entity.Api.Resp;
public class EmeterEnergyTimeResp
{
public string? time { get; set; }
public List<EmeterResp> emeterEnergy { get; set; }
public List<EmeterResp> emeterEnergyChange { get; set; }
}

@ -66,7 +66,7 @@ public class EmeterHourJob : AbstractCronJob
EmeterHourEnergy emeterMinutesEnergy = new EmeterHourEnergy(); EmeterHourEnergy emeterMinutesEnergy = new EmeterHourEnergy();
emeterMinutesEnergy.Code = keyValuePair.Key; emeterMinutesEnergy.Code = keyValuePair.Key;
emeterMinutesEnergy.Time = dateTime; emeterMinutesEnergy.Time = dateTime;
emeterMinutesEnergy.Type = 1; emeterMinutesEnergy.Type = 2;
var value = keyValuePair.Value; var value = keyValuePair.Value;
var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList(); var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList();
emeterMinutesEnergy.RealTimeValue = orderByDescending[0].Value; emeterMinutesEnergy.RealTimeValue = orderByDescending[0].Value;

@ -71,7 +71,7 @@ public class EmeterMinuteJob : AbstractCronJob
EmeterMinutesEnergy emeterMinutesEnergy = new EmeterMinutesEnergy(); EmeterMinutesEnergy emeterMinutesEnergy = new EmeterMinutesEnergy();
emeterMinutesEnergy.Code = keyValuePair.Key; emeterMinutesEnergy.Code = keyValuePair.Key;
emeterMinutesEnergy.Time = dateTime; emeterMinutesEnergy.Time = dateTime;
emeterMinutesEnergy.Type = 1; emeterMinutesEnergy.Type = 2;
var value = keyValuePair.Value; var value = keyValuePair.Value;
var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList(); var orderByDescending = value.OrderByDescending(i => i.UploadTime).ToList();
emeterMinutesEnergy.RealTimeValue = orderByDescending[0].Value; emeterMinutesEnergy.RealTimeValue = orderByDescending[0].Value;

@ -95,33 +95,66 @@ namespace WebStarter.Controllers.Ammeter
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[HttpGet("GetLastDayMeterData")] [HttpGet("GetLastDayMeterData")]
public async Task<Result<EmeterEnergyResp>> GetLastDayMeterData() public async Task<Result<List<EmeterEnergyTimeResp>>> GetLastDayMeterData()
{ {
// 获取最近一天直流和交流表数据 // 获取最近一天直流和交流表数据
List<EmeterHourEnergy> daysElectrical = await _emeterHourEnergyService.GetTodayHourlyElectricalData(1); var daysElectricalTask = _emeterHourEnergyService.GetTodayHourlyElectricalData(1);
List<EmeterHourEnergy> daysElectricalChange = var daysElectricalChangeTask = _emeterHourEnergyService.GetTodayHourlyElectricalData(2);
await _emeterHourEnergyService.GetTodayHourlyElectricalData(2);
EmeterEnergyResp resp = new EmeterEnergyResp(); await Task.WhenAll(daysElectricalTask, daysElectricalChangeTask);
var daysElectrical = daysElectricalTask.Result;
var daysElectricalChange = daysElectricalChangeTask.Result;
var config = new MapperConfiguration(cfg => var config = new MapperConfiguration(cfg =>
{ {
cfg.CreateMap<EmeterResp, EmeterHourEnergy>().ReverseMap(); cfg.CreateMap<EmeterResp, EmeterHourEnergy>().ReverseMap();
}); });
IMapper mapper = config.CreateMapper(); var mapper = config.CreateMapper();
List<EmeterResp> EnergyList = mapper.Map<List<EmeterResp>>(daysElectrical); var energyList = mapper.Map<List<EmeterResp>>(daysElectrical);
// 类型赋值直流 energyList?.ForEach(emeterResp => emeterResp.Type = 0);
EnergyList?.ForEach(emeterResp => emeterResp.Type = 0);
List<EmeterResp> EnergyChangeList = mapper.Map<List<EmeterResp>>(daysElectricalChange); var energyChangeList = mapper.Map<List<EmeterResp>>(daysElectricalChange);
// 类型赋值交流 energyChangeList?.ForEach(emeterResp => emeterResp.Type = 1);
EnergyChangeList?.ForEach(emeterResp => emeterResp.Type = 1);
resp.emeterEnergy = EnergyList.OrderBy(i => i.Code).ToList(); var respList = new List<EmeterEnergyTimeResp>();
resp.emeterEnergyChange = EnergyChangeList.OrderBy(i => i.Code).ToList();
return Result<EmeterEnergyResp>.Success(resp, "成功"); 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<List<EmeterEnergyTimeResp>>.Success(respList, "成功");
} }
/// <summary> /// <summary>
@ -153,34 +186,68 @@ namespace WebStarter.Controllers.Ammeter
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[HttpGet("GetTodayMinutelyElectricalData")] [HttpGet("GetTodayMinutelyElectricalData")]
public async Task<Result<EmeterEnergyResp>> GetTodayMinutelyElectricalData() public async Task<Result<List<EmeterEnergyTimeResp>>> GetTodayMinutelyElectricalData()
{ {
// 获取最近一个小时直流和交流表数据 // 获取最近一个小时直流和交流表数据
List<EmeterMinutesEnergy> minutesElectrical = await _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(1); var minutesElectricalTask = _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(1);
List<EmeterMinutesEnergy> minutesElectricalChange = var minutesElectricalChangeTask = _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(2);
await _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 => var config = new MapperConfiguration(cfg =>
{ {
cfg.CreateMap<EmeterResp, EmeterMinutesEnergy>().ReverseMap(); cfg.CreateMap<EmeterResp, EmeterMinutesEnergy>().ReverseMap();
}); });
IMapper mapper = config.CreateMapper(); var mapper = config.CreateMapper();
List<EmeterResp> EnergyList = mapper.Map<List<EmeterResp>>(minutesElectrical); var energyList = mapper.Map<List<EmeterResp>>(minutesElectrical);
// 类型赋值直流 // 类型赋值直流
EnergyList?.ForEach(emeterResp => emeterResp.Type = 0); energyList?.ForEach(emeterResp => emeterResp.Type = 0);
List<EmeterResp> EnergyChangeList = mapper.Map<List<EmeterResp>>(minutesElectricalChange); var energyChangeList = mapper.Map<List<EmeterResp>>(minutesElectricalChange);
// 类型赋值交流 // 类型赋值交流
EnergyChangeList?.ForEach(emeterResp => emeterResp.Type = 1); energyChangeList?.ForEach(emeterResp => emeterResp.Type = 1);
resp.emeterEnergy = EnergyList.OrderBy(i => i.Code).ToList(); var respList = new List<EmeterEnergyTimeResp>();
resp.emeterEnergyChange = EnergyChangeList.OrderBy(i => i.Code).ToList();
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<EmeterEnergyResp>.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<List<EmeterEnergyTimeResp>>.Success(respList, "成功");
} }
/// <summary> /// <summary>

Loading…
Cancel
Save