电表查询优化

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();
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;

@ -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;

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

Loading…
Cancel
Save