using AutoMapper;
using Entity.Ammeter;
using Entity.Api.Resp;
using Entity.Dto.Req;
using HybirdFrameworkCore.Entity;
using Microsoft.AspNetCore.Mvc;
using Service.Ammeter;
using Service.Station;
using SqlSugar;
namespace WebStarter.Controllers.Ammeter
{
///
/// 查询电表信息
///
[ApiController]
[Route("api/[controller]")]
public class AmmeterController : ControllerBase
{
private EmeterDayEnergyService _emeterDayEnergyService;
private EmeterHourEnergyService _emeterHourEnergyService;
private EmeterMinutesEnergyService _emeterMinutesEnergyService;
private EmeterMinutesEnergyChangeService _emeterMinutesEnergyChangeService;
private EquipInfoService _equipInfoService;
///
/// 直流电
///
private EmeterEnergyService _emeterEnergyService;
private EmeterEnergyChangeService _emeterEnergyChangeService;
///
/// 查询电表信息
///
///
///
///
///
///
public AmmeterController(EmeterDayEnergyService emeterDayEnergyService, EmeterHourEnergyService emeterHourEnergyService,
EmeterMinutesEnergyService emeterMinutesEnergyService, EmeterMinutesEnergyChangeService emeterMinutesEnergyChangeService,
EquipInfoService equipInfoService,
EmeterEnergyService emeterEnergyService,
EmeterEnergyChangeService emeterEnergyChangeService)
{
_emeterDayEnergyService = emeterDayEnergyService;
_emeterHourEnergyService = emeterHourEnergyService;
_emeterMinutesEnergyService = emeterMinutesEnergyService;
_emeterMinutesEnergyChangeService = emeterMinutesEnergyChangeService;
_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,"成功");
}
#region 统计分析/电能表累计值信息
///
/// 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()
{
// 获取实时直流和交流表数据
List minutesElectrical = await _emeterEnergyService.GetEnergyMeterRealTime();
List minutesElectricalChange = await _emeterEnergyChangeService.GetEnergyMeterRealTime();
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;
resp.emeterEnergyChange = EnergyChangeList;
}
///
/// 统计分析/电能表累计值信息
///
[HttpGet("TEgEmeterTotalEnergyValue/{Code}")]
public async Task>> GetNewEmeterMinutesEnergy(string Code)
{
var query = @"
SELECT t1.*
FROM
(
SELECT code, MAX(time) AS latest_time
FROM emeter_minutes_energy
WHERE code = @Code
GROUP BY code
) AS latest_records
JOIN emeter_minutes_energy t1
ON latest_records.code = t1.code
AND latest_records.latest_time = t1.time;";
var parameters = new List
{
new SugarParameter("@Code", Code)
};
var result = await Task.Run(() => _emeterMinutesEnergyService.SqlQuery(query, parameters));
return Result>.Success(result);
}
///
/// 删除电能表累计值信息
///
[HttpGet("delete/TEgEmeterTotalEnergyValue/{id}")]
public Result DeleteOneEmeterMinutesEnergy(long id)
{
return Result.Success(_emeterMinutesEnergyService.DeleteById(id));
}
///
/// 删除电能表累计值信息
///
[HttpPost("TEgEmeterTotalEnergyValue/BatchDelete")]
public Result DeleteMoreEmeterMinutesEnergy(int[] ids)
{
return Result.Success(_emeterMinutesEnergyService.DeleteByIds(ids));
}
#endregion
#region 统计分析/电能表变化值信息
///
/// 统计分析/电能表变化值信息
///
[HttpPost("TEgEmeterRealEnergyValue")]
public async Task>> GetEmeterMinutesEnergyChange([FromBody] PageAmmeterReq queryPageModel)
{
return Result>.Success(await _emeterMinutesEnergyChangeService.Page(queryPageModel));
}
///
/// 删除电能表变化值信息
///
[HttpGet("TEgEmeterRealEnergyValue/{id}")]
public Result DeleteEmeterMinutesEnergyChange(long id)
{
return Result.Success(_emeterMinutesEnergyChangeService.DeleteById(id));
}
///
/// 批量删除电能表变化值信息
///
[HttpPost("TEgEmeterRealEnergyValue/BatchDelete")]
public Result DeleteEmeterMinutesEnergyChange(int[] ids)
{
return Result.Success(_emeterMinutesEnergyChangeService.DeleteByIds(ids));
}
#endregion
#region 统计分析/电能表小时能耗值
///
/// 统计分析/电能表小时能耗值
///
[HttpPost("TEgEmeterHourEnergyValue")]
public async Task>> GetEmeterHourEnergy([FromBody] PageAmmeterReq queryPageModel)
{
return await _emeterHourEnergyService.Page(queryPageModel);
}
///
/// 删除电能表小时能耗值
///
[HttpGet("TEgEmeterHourEnergyValue/{id}")]
public Result DeleteEmeterHourEnergy(long id)
{
return Result.Success(_emeterHourEnergyService.DeleteById(id));
}
///
/// 批量删除电能表小时能耗值
///
[HttpPost("TEgEmeterHourEnergyValue/BatchDelete")]
public Result DeleteListEmeterHourEnergy(int[] ids)
{
return Result.Success(_emeterHourEnergyService.DeleteByIds(ids));
}
#endregion
#region 统计分析/电能表每天能耗值
///
/// 统计分析/电能表每天能耗值
///
[HttpPost("TEgEmeterDayEnergyValue")]
public async Task>> GetEmeterDayEnergy([FromBody] PageAmmeterReq queryPageModel)
{
return Result>.Success(await _emeterDayEnergyService.Page(queryPageModel));
}
///
/// 删除电能表每天能耗值
///
[HttpGet("TEgEmeterDayEnergyValue/{id}")]
public Result DeleteEmeterDayEnergy(long id)
{
return Result.Success(_emeterDayEnergyService.DeleteById(id));
}
///
/// 批量删除电能表每天能耗值
///
[HttpPost("TEgEmeterDayEnergyValue/BatchDelete")]
public Result DeleteListEmeterDayEnergy(int[] ids)
{
return Result.Success(_emeterDayEnergyService.DeleteByIds(ids));
}
#endregion
#region 统计分析/换电站分时用电统计信息
//TODO::
//小时计算总能耗值,小时计算充电能耗值,小时计算换电能耗值,小时计算其他能耗值,开始时间,结束时间
#endregion
#region 统计分析/换电站每天用电统计信息
//TODO::
//每天计算总能耗值,每天计算充电能耗值,每天计算换电能耗值,每天计算其他能耗值,开始时间,结束时间
#endregion
#region 电能监控/交流电能监控
///
/// 电能监控/交流电能监控
///
[HttpGet("ElecMeterParam/GetElecMeterParamValue")]
public Result> GetElecMeterParamValue()
{
var EquipInfo = _equipInfoService.QueryListByClause(i => i.TypeCode == 0);
List strings = new List();
foreach (var kvp in EquipInfo)
{
strings.Add(kvp.Code);
}
//TODO::过滤,确保编码相同只出现一次
return Result>.Success(_emeterMinutesEnergyService.QueryListByClause(i => strings.Contains(i.Code)).ToList());
}
#endregion
#region 电能监控/直流电能监控
///
/// 电能监控/直流电能监控
///
[HttpGet("ManualSwap/GetDcCurrentInfo")]
public Result> GetDcCurrentInfo()
{
var EquipInfo = _equipInfoService.QueryListByClause(i => i.TypeCode == 1);
List strings = new List();
foreach (var kvp in EquipInfo)
{
strings.Add(kvp.Code);
}
//TODO::过滤,确保编码相同只出现一次
return Result>.Success(_emeterMinutesEnergyService.QueryListByClause(i => strings.Contains(i.Code)).ToList());
}
#endregion
}
}