|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询电表信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
public class AmmeterController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
_emeterDayEnergyService = emeterDayEnergyService;
|
|
|
|
|
_emeterHourEnergyService = emeterHourEnergyService;
|
|
|
|
|
_emeterMinutesEnergyService = emeterMinutesEnergyService;
|
|
|
|
|
_equipInfoService = equipInfoService;
|
|
|
|
|
_emeterEnergyService = emeterEnergyService;
|
|
|
|
|
_emeterEnergyChangeService = emeterEnergyChangeService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
/// 30天累计能耗
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("GetAllElectricalData")]
|
|
|
|
|
public async Task<Result<EmeterEnergyResp>> GetAllElectricalData()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
List<EmeterDayEnergy> emeterEnergy = await _emeterDayEnergyService.GetAllElectricalData(1);
|
|
|
|
|
List<EmeterDayEnergy> emeterEnergyChanges = await _emeterDayEnergyService.GetAllElectricalData(2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EmeterEnergyResp resp = new EmeterEnergyResp();
|
|
|
|
|
ElectricEnergyDay(emeterEnergy, emeterEnergyChanges, resp);
|
|
|
|
|
return Result<EmeterEnergyResp>.Success(resp, "成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 最近一天电表数据
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("GetLastDayMeterData")]
|
|
|
|
|
public async Task<Result<List<EmeterEnergyTimeResp>>> GetLastDayMeterData()
|
|
|
|
|
{
|
|
|
|
|
DateTime todayZero = DateTime.Today;
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
List<EmeterResp> energyList = new List<EmeterResp>();
|
|
|
|
|
|
|
|
|
|
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<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()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
List<EmeterResp> energyList = new List<EmeterResp>();
|
|
|
|
|
|
|
|
|
|
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<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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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")]
|
|
|
|
|
public async Task<Result<EmeterEnergyResp>> 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<EmeterEnergyResp>.Success(resp, "成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task QueryRealTime(EmeterEnergyResp resp,string time)
|
|
|
|
|
{
|
|
|
|
|
List<EmeterEnergy> sevenDaysElectrical = await _emeterEnergyService.GetEnergyMeterRealTime(time);
|
|
|
|
|
List<EmeterEnergyChange> emeterEnergyChanges = await _emeterEnergyChangeService.GetEnergyMeterRealTime(time);
|
|
|
|
|
|
|
|
|
|
var config = new MapperConfiguration(cfg =>
|
|
|
|
|
{
|
|
|
|
|
cfg.CreateMap<EmeterResp, EmeterEnergy>().ReverseMap();
|
|
|
|
|
cfg.CreateMap<EmeterResp, EmeterEnergyChange>().ReverseMap();
|
|
|
|
|
});
|
|
|
|
|
IMapper mapper = config.CreateMapper();
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<EmeterResp> TransformEnergyData(List<EmeterHourEnergy> 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<EmeterDayEnergy> daysElectrical, List<EmeterDayEnergy> emeterEnergyChanges, EmeterEnergyResp resp)
|
|
|
|
|
{
|
|
|
|
|
var config = new MapperConfiguration(cfg =>
|
|
|
|
|
{
|
|
|
|
|
cfg.CreateMap<EmeterResp, EmeterDayEnergy>().ReverseMap();
|
|
|
|
|
});
|
|
|
|
|
IMapper mapper = config.CreateMapper();
|
|
|
|
|
|
|
|
|
|
List<EmeterResp> EnergyList = mapper.Map<List<EmeterResp>>(daysElectrical);
|
|
|
|
|
// 类型赋值直流
|
|
|
|
|
EnergyList?.ForEach(emeterResp => emeterResp.Type = 0);
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|