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.

74 lines
2.1 KiB

using Entity.Ammeter;
using HybirdFrameworkCore.Autofac.Attribute;
using Repository.Ammeter;
namespace Service.Ammeter;
[Scope("SingleInstance")]
public class EmeterEnergyChangeService : BaseServices<EmeterEnergyChange>
{
private readonly EmeterEnergyChangeRepository _emeterEnergyChangeRep;
public EmeterEnergyChangeService(EmeterEnergyChangeRepository emeterEnergyChangeRep)
{
_emeterEnergyChangeRep = emeterEnergyChangeRep;
}
public async Task<List<EmeterEnergyChange>> SevenDaysElectrical()
{
string sql = @"
SELECT
code,
date(upload_time) AS UploadTime,
MAX(value) - MIN(value) AS Value
FROM
emeter_energy_change
WHERE
upload_time >= NOW() - INTERVAL 7 DAY
GROUP BY
code, date(upload_time)
ORDER BY
code, date(upload_time);
";
List<EmeterEnergyChange> emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql);
return emeterEnergies;
}
public async Task<List<EmeterEnergyChange>> GetAllElectricalData()
{
string sql = @"
SELECT
code,
MAX(value) - MIN(value) AS Value
FROM
emeter_energy_change
GROUP BY
code
ORDER BY
code;
";
List<EmeterEnergyChange> emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql);
return emeterEnergies;
}
public async Task<List<EmeterEnergyChange>> GetTodayHourlyElectricalData()
{
string sql = @"
SELECT
code,
DATE_FORMAT(upload_time, '%Y-%m-%d %H:00:00') AS Hour,
MAX(value) - MIN(value) AS Value
FROM
emeter_energy_change
WHERE
DATE(upload_time) = CURDATE()
GROUP BY
code, DATE_FORMAT(upload_time, '%Y-%m-%d %H:00:00')
ORDER BY
code, Hour;
";
List<EmeterEnergyChange> emeterEnergies = await _emeterEnergyChangeRep.SqlQueryable(sql);
return emeterEnergies;
}
}