using Entity.Ammeter; using Entity.Api.Resp; using HybirdFrameworkCore.Autofac.Attribute; using Repository.Ammeter; using SqlSugar; namespace Service.Ammeter; [Scope("SingleInstance")] public class EmeterEnergyService : BaseServices { public EmeterEnergyService(EmeterEnergyRepository service) { this.BaseDal = service; } /// /// 获取七天用电量 /// /// public async Task> SevenDaysElectrical() { string sql = @" SELECT code, date(upload_time) AS UploadTime, MAX(value) - MIN(value) AS Value FROM emeter_energy WHERE upload_time >= NOW() - INTERVAL 7 DAY GROUP BY code, date(upload_time) ORDER BY code, date(upload_time); "; List emeterEnergies = await this.BaseDal.SqlQueryable(sql); return emeterEnergies; } /// /// 获取全部用电量 /// /// public async Task> GetAllElectricalData() { string sql = @" SELECT code, MAX(value) - MIN(value) AS Value FROM emeter_energy GROUP BY code ORDER BY code; "; List emeterEnergies = await this.BaseDal.SqlQueryable(sql); return emeterEnergies; } /// /// 根据小时统计电量 /// /// public async Task> 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 WHERE DATE(upload_time) = CURDATE() GROUP BY code, DATE_FORMAT(upload_time, '%Y-%m-%d %H:00:00') ORDER BY code, Hour; "; List emeterEnergies = await this.BaseDal.SqlQueryable(sql); return emeterEnergies; } }