using System.Linq.Expressions;
using AutoMapper;
using Entity.Ammeter;
using Entity.Api.Resp;
using HybirdFrameworkCore.Entity;
using Microsoft.AspNetCore.Mvc;
using Service.Ammeter;
using Service.Station;
namespace WebStarter.Controllers.Ammeter
{
///
/// 查询电表信息
///
[ApiController]
[Route("api/[controller]")]
public class AmmeterController : ControllerBase
{
private EmeterDayEnergyService _emeterDayEnergyService;
private EmeterHourEnergyService _emeterHourEnergyService;
private EmeterMinutesEnergyService _emeterMinutesEnergyService;
private EquipInfoService _equipInfoService;
///
/// 直流电
///
private EmeterEnergyService _emeterEnergyService;
private EmeterEnergyChangeService _emeterEnergyChangeService;
///
/// 查询电表信息
///
///
///
///
///
///
public AmmeterController(EmeterDayEnergyService emeterDayEnergyService,
EmeterHourEnergyService emeterHourEnergyService,
EmeterMinutesEnergyService emeterMinutesEnergyService,
EquipInfoService equipInfoService,
EmeterEnergyService emeterEnergyService,
EmeterEnergyChangeService emeterEnergyChangeService)
{
_emeterDayEnergyService = emeterDayEnergyService;
_emeterHourEnergyService = emeterHourEnergyService;
_emeterMinutesEnergyService = emeterMinutesEnergyService;
_equipInfoService = equipInfoService;
_emeterEnergyService = emeterEnergyService;
_emeterEnergyChangeService = emeterEnergyChangeService;
}
///
/// 七天电表用量
///
///
///
[HttpGet("SevenDaysElectrical")]
public async Task> SevenDaysElectrical()
{
List sevenDaysElectrical = await _emeterDayEnergyService.SevenDaysElectrical(1);
List emeterEnergyChanges = await _emeterDayEnergyService.SevenDaysElectrical(2);
EmeterEnergyResp resp = new EmeterEnergyResp();
ElectricEnergyDay(sevenDaysElectrical, emeterEnergyChanges, resp);
return Result.Success(resp, "成功");
}
///
/// 30天累计能耗
///
///
[HttpGet("GetAllElectricalData")]
public async Task> GetAllElectricalData()
{
List emeterEnergy = await _emeterDayEnergyService.GetAllElectricalData(1);
List emeterEnergyChanges = await _emeterDayEnergyService.GetAllElectricalData(2);
EmeterEnergyResp resp = new EmeterEnergyResp();
ElectricEnergyDay(emeterEnergy, emeterEnergyChanges, resp);
return Result.Success(resp, "成功");
}
///
/// 最近一天电表数据
///
///
[HttpGet("GetLastDayMeterData")]
public async Task>> GetLastDayMeterData()
{
DateTime todayZero = DateTime.Today;
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();
List energyList = new List();
foreach (var energy in daysElectrical)
{
EmeterResp resp = new EmeterResp();
resp.Code = energy.Code;
resp.Value = energy.Value;
resp.Hour = energy.Time;
energyList.Add(resp);
}
energyList?.ForEach(emeterResp => emeterResp.Type = 0);
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();
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, "成功");
}
///
/// 最近一小时电表数据
///
///
[HttpGet("GetTodayMinutelyElectricalData")]
public async Task>> GetTodayMinutelyElectricalData()
{
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();
List energyList = new List();
foreach (var energy in daysElectrical)
{
EmeterResp resp = new EmeterResp();
resp.Code = energy.Code;
resp.Value = energy.Value;
resp.Minute = energy.Time;
energyList.Add(resp);
}
energyList?.ForEach(emeterResp => emeterResp.Type = 0);
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
.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();
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, "成功");
}
///
/// 获取直流交流电表实时数据
///
///
[HttpGet("GetEnergyMeterRealTime")]
public async Task> GetEnergyMeterRealTime(string endTime)
{
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);
}
return Result.Success(resp, "成功");
}
private async Task QueryRealTime(EmeterEnergyResp resp,string time)
{
List sevenDaysElectrical = await _emeterEnergyService.GetEnergyMeterRealTime(time);
List emeterEnergyChanges = await _emeterEnergyChangeService.GetEnergyMeterRealTime(time);
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap().ReverseMap();
cfg.CreateMap().ReverseMap();
});
IMapper mapper = config.CreateMapper();
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 =>
{
DateTime? uploadTime = DateTime.TryParse(hourEnergy.Time, out DateTime parsedTime)
? parsedTime
: (DateTime?)null;
return new EmeterResp
{
Code = hourEnergy.Code,
Value = hourEnergy.RealTimeValue,
UploadTime = uploadTime,
Type = type
};
}).OrderBy(e => e.Code).ToList();
}
private static void ElectricEnergyDay(List daysElectrical, List emeterEnergyChanges, EmeterEnergyResp resp)
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap().ReverseMap();
});
IMapper mapper = config.CreateMapper();
List EnergyList = mapper.Map>(daysElectrical);
// 类型赋值直流
EnergyList?.ForEach(emeterResp => emeterResp.Type = 0);
List EnergyChangeList = mapper.Map>(emeterEnergyChanges);
// 类型赋值交流
EnergyChangeList?.ForEach(emeterResp => emeterResp.Type = 1);
resp.emeterEnergy = EnergyList.OrderBy(i => i.Code).ToList();
resp.emeterEnergyChange = EnergyChangeList.OrderBy(i => i.Code).ToList();
}
}
}