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 _emeterEnergyService.SevenDaysElectrical();
List emeterEnergyChanges = await _emeterEnergyChangeService.SevenDaysElectrical();
EmeterEnergyResp resp = new EmeterEnergyResp();
ChargeAssignment(sevenDaysElectrical, emeterEnergyChanges, resp);
return Result.Success(resp, "成功");
}
///
/// 30天累计能耗
///
///
[HttpGet("GetAllElectricalData")]
public async Task> GetAllElectricalData()
{
List emeterEnergy = await _emeterEnergyService.GetAllElectricalData();
List emeterEnergyChanges = await _emeterEnergyChangeService.GetAllElectricalData();
EmeterEnergyResp resp = new EmeterEnergyResp();
ChargeAssignment(emeterEnergy, emeterEnergyChanges, resp);
return Result.Success(resp, "成功");
}
///
/// 最近一天电表数据
///
///
[HttpGet("GetLastDayMeterData")]
public async Task> GetLastDayMeterData()
{
// 获取最近一天直流和交流表数据
List daysElectrical = await _emeterEnergyService.GetTodayHourlyElectricalData();
List daysElectricalChange =
await _emeterEnergyChangeService.GetTodayHourlyElectricalData();
EmeterEnergyResp resp = new EmeterEnergyResp();
ChargeAssignment(daysElectrical, daysElectricalChange, resp);
return Result.Success(resp, "成功");
}
///
/// 直流电能表小时统计
///
///
[HttpGet("GetTodayHourlyEnergy")]
public async Task>> GetTodayHourlyElectricalData()
{
// 直流表数据
return Result>.Success(await _emeterEnergyService.GetTodayHourlyElectricalData(), "成功");
}
///
/// 交流电能表小时统计
///
///
[HttpGet("GetTodayHourlyEnergyChange")]
public async Task>> GetTodayHourlyElectricalChangeData()
{
// 交流表数据
return Result>.Success(
await _emeterEnergyChangeService.GetTodayHourlyElectricalData(), "成功");
}
///
/// 最近一小时电表数据
///
///
[HttpGet("GetTodayMinutelyElectricalData")]
public async Task> GetTodayMinutelyElectricalData()
{
// 获取最近一个小时直流和交流表数据
List minutesElectrical = await _emeterEnergyService.GetTodayMinutelyElectricalData();
List minutesElectricalChange =
await _emeterEnergyChangeService.GetTodayMinutelyElectricalData();
EmeterEnergyResp resp = new EmeterEnergyResp();
ChargeAssignment(minutesElectrical, minutesElectricalChange, resp);
return Result.Success(resp, "成功");
}
///
/// 获取直流交流电表实时数据
///
///
[HttpGet("GetEnergyMeterRealTime")]
public async Task> GetEnergyMeterRealTime(string endTime)
{
// 不传值时查实时
if (!DateTime.TryParse(endTime, out DateTime parsedEndTime))
{
parsedEndTime = DateTime.Now;
}
endTime = parsedEndTime.ToString("yyyy-MM-dd HH:mm:ss");
// 获取实时直流和交流表数据
List minutesElectrical = await _emeterEnergyService.GetEnergyMeterRealTime(endTime);
List minutesElectricalChange =
await _emeterEnergyChangeService.GetEnergyMeterRealTime(endTime);
EmeterEnergyResp resp = new EmeterEnergyResp();
ChargeAssignment(minutesElectrical, minutesElectricalChange, resp);
return Result.Success(resp, "成功");
}
private static void ChargeAssignment(List emeterEnergy,
List emeterEnergyChanges, EmeterEnergyResp resp)
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap().ReverseMap();
cfg.CreateMap().ReverseMap();
});
IMapper mapper = config.CreateMapper();
List EnergyList = mapper.Map>(emeterEnergy);
// 类型赋值直流
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();
}
}
}