You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

352 lines
13 KiB

5 months ago
using System.Linq.Expressions;
using AutoMapper;
using Entity.Ammeter;
using Entity.Api.Resp;
6 months ago
using HybirdFrameworkCore.Entity;
using Microsoft.AspNetCore.Mvc;
using Service.Ammeter;
using Service.Station;
6 months ago
namespace WebStarter.Controllers.Ammeter
{
/// <summary>
/// 查询电表信息
/// </summary>
6 months ago
[ApiController]
[Route("api/[controller]")]
public class AmmeterController : ControllerBase
6 months ago
{
private EmeterDayEnergyService _emeterDayEnergyService;
private EmeterHourEnergyService _emeterHourEnergyService;
private EmeterMinutesEnergyService _emeterMinutesEnergyService;
private EquipInfoService _equipInfoService;
/// <summary>
/// 直流电
/// </summary>
private EmeterEnergyService _emeterEnergyService;
private EmeterEnergyChangeService _emeterEnergyChangeService;
/// <summary>
/// 查询电表信息
/// </summary>
/// <param name="emeterDayEnergyService"></param>
/// <param name="emeterHourEnergyService"></param>
/// <param name="emeterMinutesEnergyService"></param>
/// <param name="emeterMinutesEnergyChangeService"></param>
/// <param name="equipInfoService"></param>
public AmmeterController(EmeterDayEnergyService emeterDayEnergyService,
EmeterHourEnergyService emeterHourEnergyService,
EmeterMinutesEnergyService emeterMinutesEnergyService,
EquipInfoService equipInfoService,
EmeterEnergyService emeterEnergyService,
EmeterEnergyChangeService emeterEnergyChangeService)
6 months ago
{
_emeterDayEnergyService = emeterDayEnergyService;
_emeterHourEnergyService = emeterHourEnergyService;
_emeterMinutesEnergyService = emeterMinutesEnergyService;
_equipInfoService = equipInfoService;
_emeterEnergyService = emeterEnergyService;
_emeterEnergyChangeService = emeterEnergyChangeService;
6 months ago
}
/// <summary>
/// 七天电表用量
/// </summary>
/// <param name="Code"></param>
/// <returns></returns>
[HttpGet("SevenDaysElectrical")]
public async Task<Result<EmeterEnergyResp>> SevenDaysElectrical()
{
List<EmeterDayEnergy> sevenDaysElectrical = await _emeterDayEnergyService.SevenDaysElectrical(1);
List<EmeterDayEnergy> emeterEnergyChanges = await _emeterDayEnergyService.SevenDaysElectrical(2);
EmeterEnergyResp resp = new EmeterEnergyResp();
ElectricEnergyDay(sevenDaysElectrical, emeterEnergyChanges, resp);
return Result<EmeterEnergyResp>.Success(resp, "成功");
}
/// <summary>
6 months ago
/// 30天累计能耗
/// </summary>
/// <returns></returns>
[HttpGet("GetAllElectricalData")]
6 months ago
public async Task<Result<EmeterEnergyResp>> GetAllElectricalData()
{
List<EmeterDayEnergy> emeterEnergy = await _emeterDayEnergyService.GetAllElectricalData(1);
List<EmeterDayEnergy> emeterEnergyChanges = await _emeterDayEnergyService.GetAllElectricalData(2);
6 months ago
EmeterEnergyResp resp = new EmeterEnergyResp();
ElectricEnergyDay(emeterEnergy, emeterEnergyChanges, resp);
return Result<EmeterEnergyResp>.Success(resp, "成功");
}
6 months ago
/// <summary>
6 months ago
/// 最近一天电表数据
/// </summary>
/// <returns></returns>
6 months ago
[HttpGet("GetLastDayMeterData")]
public async Task<Result<List<EmeterEnergyTimeResp>>> GetLastDayMeterData()
{
5 months ago
DateTime todayZero = DateTime.Today;
5 months ago
List<EmeterHourEnergy> list = await _emeterHourEnergyService.QueryListByClauseAsync(i => DateTime.Parse(i.Time)>=todayZero);
List<EmeterHourEnergy> daysElectrical = list.Where(i => i.Type == 1).ToList();
List<EmeterHourEnergy> daysElectricalChange = list.Where(i => i.Type == 2).ToList();
5 months ago
List<EmeterResp> energyList = new List<EmeterResp>();
foreach (var energy in daysElectrical)
{
5 months ago
EmeterResp resp = new EmeterResp();
resp.Code = energy.Code;
resp.Value = energy.Value;
resp.Hour = energy.Time;
energyList.Add(resp);
}
6 months ago
energyList?.ForEach(emeterResp => emeterResp.Type = 0);
5 months ago
List<EmeterResp> energyChangeList = new List<EmeterResp>();
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<EmeterEnergyTimeResp>();
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>
/// <returns></returns>
[HttpGet("GetTodayMinutelyElectricalData")]
public async Task<Result<List<EmeterEnergyTimeResp>>> GetTodayMinutelyElectricalData()
{
5 months ago
DateTime now = DateTime.Now;
DateTime time = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0);
List<EmeterMinutesEnergy> list = await _emeterMinutesEnergyService.QueryListByClauseAsync(i => DateTime.Parse(i.Time)>=time);
List<EmeterMinutesEnergy> daysElectrical = list.Where(i => i.Type == 1).ToList();
List<EmeterMinutesEnergy> daysElectricalChange = list.Where(i => i.Type == 2).ToList();
5 months ago
List<EmeterResp> energyList = new List<EmeterResp>();
foreach (var energy in daysElectrical)
{
5 months ago
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);
5 months ago
List<EmeterResp> energyChangeList = new List<EmeterResp>();
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);
5 months ago
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();
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>
/// <returns></returns>
[HttpGet("GetEnergyMeterRealTime")]
5 months ago
public async Task<Result<EmeterEnergyResp>> GetEnergyMeterRealTime(string endTime)
{
5 months ago
EmeterEnergyResp resp = new EmeterEnergyResp();
DateTime parsedEndTime;
if (!DateTime.TryParse(endTime, out parsedEndTime))
5 months ago
{
parsedEndTime = DateTime.Now;
5 months ago
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);
5 months ago
}
5 months ago
return Result<EmeterEnergyResp>.Success(resp, "成功");
}
5 months ago
private async Task QueryRealTime(EmeterEnergyResp resp,string time)
{
List<EmeterEnergy> sevenDaysElectrical = await _emeterEnergyService.GetEnergyMeterRealTime(time);
List<EmeterEnergyChange> emeterEnergyChanges = await _emeterEnergyChangeService.GetEnergyMeterRealTime(time);
6 months ago
5 months ago
var config = new MapperConfiguration(cfg =>
{
5 months ago
cfg.CreateMap<EmeterResp, EmeterEnergy>().ReverseMap();
cfg.CreateMap<EmeterResp, EmeterEnergyChange>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
5 months ago
resp.emeterEnergy = MapEnergyData(mapper.Map<List<EmeterResp>>(sevenDaysElectrical), 0);
resp.emeterEnergyChange = MapEnergyData(mapper.Map<List<EmeterResp>>(emeterEnergyChanges), 1);
}
private async Task QueryParameterTime(EmeterEnergyResp resp, string endTime)
{
List<EmeterHourEnergy> minutesElectrical =
await _emeterHourEnergyService.GetEnergyMeterRealTime(endTime, 1);
List<EmeterHourEnergy> minutesElectricalChange =
await _emeterHourEnergyService.GetEnergyMeterRealTime(endTime, 2);
resp.emeterEnergy = TransformEnergyData(minutesElectrical, 0);
resp.emeterEnergyChange = TransformEnergyData(minutesElectricalChange, 1);
}
private List<EmeterResp> MapEnergyData(List<EmeterResp> energyData, int type)
{
energyData?.ForEach(emeterResp => emeterResp.Type = type);
return energyData?.OrderBy(e => e.Code).ToList();
6 months ago
}
5 months ago
private List<EmeterResp> TransformEnergyData(List<EmeterHourEnergy> energyData, int type)
{
5 months ago
return energyData.Select(hourEnergy =>
{
5 months ago
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();
}
5 months ago
private static void ElectricEnergyDay(List<EmeterDayEnergy> daysElectrical, List<EmeterDayEnergy> emeterEnergyChanges, EmeterEnergyResp resp)
6 months ago
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EmeterResp, EmeterDayEnergy>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
List<EmeterResp> EnergyList = mapper.Map<List<EmeterResp>>(daysElectrical);
6 months ago
// 类型赋值直流
EnergyList?.ForEach(emeterResp => emeterResp.Type = 0);
6 months ago
List<EmeterResp> EnergyChangeList = mapper.Map<List<EmeterResp>>(emeterEnergyChanges);
// 类型赋值交流
EnergyChangeList?.ForEach(emeterResp => emeterResp.Type = 1);
resp.emeterEnergy = EnergyList.OrderBy(i => i.Code).ToList();
resp.emeterEnergyChange = EnergyChangeList.OrderBy(i => i.Code).ToList();
}
6 months ago
}
}