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.

249 lines
9.8 KiB

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<EmeterEnergyResp>> GetLastDayMeterData()
{
// 获取最近一天直流和交流表数据
List<EmeterHourEnergy> daysElectrical = await _emeterHourEnergyService.GetTodayHourlyElectricalData(1);
List<EmeterHourEnergy> daysElectricalChange =
await _emeterHourEnergyService.GetTodayHourlyElectricalData(2);
EmeterEnergyResp resp = new EmeterEnergyResp();
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EmeterResp, EmeterHourEnergy>().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>>(daysElectricalChange);
// 类型赋值交流
EnergyChangeList?.ForEach(emeterResp => emeterResp.Type = 1);
resp.emeterEnergy = EnergyList.OrderBy(i => i.Code).ToList();
resp.emeterEnergyChange = EnergyChangeList.OrderBy(i => i.Code).ToList();
return Result<EmeterEnergyResp>.Success(resp, "成功");
}
/// <summary>
/// 直流电能表小时统计
/// </summary>
/// <returns></returns>
[HttpGet("GetTodayHourlyEnergy")]
public async Task<Result<List<EmeterHourEnergy>>> GetTodayHourlyElectricalData()
{
// 直流表数据
return Result<List<EmeterHourEnergy>>.Success(await _emeterHourEnergyService.GetTodayHourlyElectricalData(1), "成功");
}
/// <summary>
/// 交流电能表小时统计
/// </summary>
/// <returns></returns>
[HttpGet("GetTodayHourlyEnergyChange")]
public async Task<Result<List<EmeterHourEnergy>>> GetTodayHourlyElectricalChangeData()
{
// 交流表数据
return Result<List<EmeterHourEnergy>>.Success(
await _emeterHourEnergyService.GetTodayHourlyElectricalData(2), "成功");
}
/// <summary>
/// 最近一小时电表数据
/// </summary>
/// <returns></returns>
[HttpGet("GetTodayMinutelyElectricalData")]
public async Task<Result<EmeterEnergyResp>> GetTodayMinutelyElectricalData()
{
// 获取最近一个小时直流和交流表数据
List<EmeterMinutesEnergy> minutesElectrical = await _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(1);
List<EmeterMinutesEnergy> minutesElectricalChange =
await _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(2);
EmeterEnergyResp resp = new EmeterEnergyResp();
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EmeterResp, EmeterMinutesEnergy>().ReverseMap();
});
IMapper mapper = config.CreateMapper();
List<EmeterResp> EnergyList = mapper.Map<List<EmeterResp>>(minutesElectrical);
// 类型赋值直流
EnergyList?.ForEach(emeterResp => emeterResp.Type = 0);
List<EmeterResp> EnergyChangeList = mapper.Map<List<EmeterResp>>(minutesElectricalChange);
// 类型赋值交流
EnergyChangeList?.ForEach(emeterResp => emeterResp.Type = 1);
resp.emeterEnergy = EnergyList.OrderBy(i => i.Code).ToList();
resp.emeterEnergyChange = EnergyChangeList.OrderBy(i => i.Code).ToList();
return Result<EmeterEnergyResp>.Success(resp, "成功");
}
/// <summary>
/// 获取直流交流电表实时数据
/// </summary>
/// <returns></returns>
[HttpGet("GetEnergyMeterRealTime")]
public async Task<Result<EmeterEnergyResp>> GetEnergyMeterRealTime(string endTime)
{
// 不传值时查实时
if (!DateTime.TryParse(endTime, out DateTime parsedEndTime))
{
parsedEndTime = DateTime.Now;
}
endTime = parsedEndTime.ToString("yyyy-MM-dd HH:mm:ss");
// 获取实时直流和交流表数据
List<EmeterHourEnergy> minutesElectrical = await _emeterHourEnergyService.GetEnergyMeterRealTime(endTime, 1);
List<EmeterHourEnergy> minutesElectricalChange = await _emeterHourEnergyService.GetEnergyMeterRealTime(endTime, 2);
EmeterEnergyResp resp = new EmeterEnergyResp
{
emeterEnergy = TransformEnergyData(minutesElectrical, 0),
emeterEnergyChange = TransformEnergyData(minutesElectricalChange, 1)
};
return Result<EmeterEnergyResp>.Success(resp, "成功");
}
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();
}
}
}