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() { // 获取最近一天直流和交流表数据 List daysElectrical = await _emeterHourEnergyService.GetTodayHourlyElectricalData(1); List daysElectricalChange = await _emeterHourEnergyService.GetTodayHourlyElectricalData(2); EmeterEnergyResp resp = new EmeterEnergyResp(); 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>(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.Success(resp, "成功"); } /// /// 直流电能表小时统计 /// /// [HttpGet("GetTodayHourlyEnergy")] public async Task>> GetTodayHourlyElectricalData() { // 直流表数据 return Result>.Success(await _emeterHourEnergyService.GetTodayHourlyElectricalData(1), "成功"); } /// /// 交流电能表小时统计 /// /// [HttpGet("GetTodayHourlyEnergyChange")] public async Task>> GetTodayHourlyElectricalChangeData() { // 交流表数据 return Result>.Success( await _emeterHourEnergyService.GetTodayHourlyElectricalData(2), "成功"); } /// /// 最近一小时电表数据 /// /// [HttpGet("GetTodayMinutelyElectricalData")] public async Task> GetTodayMinutelyElectricalData() { // 获取最近一个小时直流和交流表数据 List minutesElectrical = await _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(1); List minutesElectricalChange = await _emeterMinutesEnergyService.GetTodayMinutelyElectricalData(2); EmeterEnergyResp resp = new EmeterEnergyResp(); var config = new MapperConfiguration(cfg => { cfg.CreateMap().ReverseMap(); }); IMapper mapper = config.CreateMapper(); List EnergyList = mapper.Map>(minutesElectrical); // 类型赋值直流 EnergyList?.ForEach(emeterResp => emeterResp.Type = 0); List EnergyChangeList = mapper.Map>(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.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 _emeterHourEnergyService.GetEnergyMeterRealTime(endTime, 1); List minutesElectricalChange = await _emeterHourEnergyService.GetEnergyMeterRealTime(endTime, 2); EmeterEnergyResp resp = new EmeterEnergyResp { emeterEnergy = TransformEnergyData(minutesElectrical, 0), emeterEnergyChange = TransformEnergyData(minutesElectricalChange, 1) }; return Result.Success(resp, "成功"); } 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(); } } }