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.

138 lines
4.4 KiB

using Entity.Ammeter;
using Entity.Dto.Req;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Entity;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Repository.Ammeter;
using SqlSugar;
namespace Service.Ammeter
{
/// <summary>
///
/// </summary>
[Scope("SingleInstance")]
public class EmeterHourEnergyService: BaseServices<EmeterHourEnergy>
{
public EmeterHourEnergyService(EmeterHourEnergyRepository service)
{
this.BaseDal = service;
}
public async Task<Result<PageResult<EmeterHourEnergy>>> Page(PageAmmeterReq queryPageModel)
{
QueryPageModel queryPageModel1 = new QueryPageModel
{
PageNum = queryPageModel.PageNum,
PageSize = queryPageModel.PageSize,
};
if (!string.IsNullOrEmpty(queryPageModel.Code))
return Result<PageResult<EmeterHourEnergy>>.Success(PageResult<EmeterHourEnergy>.ConvertPage(this.BaseDal.QueryIPageByCause(queryPageModel1, u => u.Code == queryPageModel.Code)));
else
return Result<PageResult<EmeterHourEnergy>>.Success(PageResult<EmeterHourEnergy>.ConvertPage(this.BaseDal.QueryIPageByCause(queryPageModel1, null)));
}
/// <summary>
/// 根据小时统计电量
/// </summary>
/// <returns></returns>
public async Task<List<EmeterHourEnergy>> GetTodayHourlyElectricalData(int type)
{
string sql = $@"
WITH hourly_max_min AS (
SELECT
`code`,
DATE_FORMAT(`time`, '%Y-%m-%d %H:00:00') AS `Hour`,
MAX(`real_time_value`) AS `MaxValue`,
MIN(`real_time_value`) AS `MinValue`,
COUNT(*) AS `record_count`
FROM
`emeter_hour_energy`
WHERE
DATE(`time`) = CURDATE()
AND `type` = {type}
GROUP BY
`code`, DATE_FORMAT(`time`, '%Y-%m-%d %H:00:00')
), previous_hour_max AS (
SELECT
`code`,
`Hour`,
LAG(`MaxValue`) OVER (PARTITION BY `code` ORDER BY `Hour`) AS `PreviousMaxValue`
FROM
hourly_max_min
)
SELECT
h.`code`,
h.`Hour`,
CASE
WHEN h.`record_count` > 1 THEN h.`MaxValue` - h.`MinValue`
ELSE h.`MaxValue` - COALESCE(p.`PreviousMaxValue`, 0)
END AS `Value`
FROM
hourly_max_min h
LEFT JOIN
previous_hour_max p ON h.`code` = p.`code` AND h.`Hour` = p.`Hour`
ORDER BY
h.`code`, h.`Hour`;
";
List<EmeterHourEnergy> emeterEnergies = await this.BaseDal.SqlQueryable(sql);
return emeterEnergies;
}
/// <summary>
/// 获取直流电表实时数据
/// </summary>
/// <returns></returns>
public async Task<List<EmeterHourEnergy>> GetEnergyMeterRealTime(string endTime, int type)
{
// 将传入的时间参数转换为 DateTime 类型
DateTime endDateTime = DateTime.Parse(endTime);
// 根据传入的时间参数计算 1 天前的时间
DateTime startDateTime = endDateTime.AddDays(-1);
// 构建 SQL 查询字符串并包含参数
string sql = @"
WITH latest_data AS (
SELECT
code,
MAX(time) AS latest_upload_time
FROM emeter_hour_energy
WHERE time >= @StartDateTime AND time <= @EndDateTime
AND type = @Type
GROUP BY code
),
max_value_data AS (
SELECT
e.*,
ROW_NUMBER() OVER (PARTITION BY e.code ORDER BY e.real_time_value DESC) AS rn
FROM emeter_hour_energy e
JOIN latest_data ld ON e.code = ld.code AND e.time = ld.latest_upload_time
WHERE e.type = @Type
)
SELECT
*
FROM max_value_data
WHERE rn = 1;
";
// 使用参数化查询防止 SQL 注入
var parameters = new List<SugarParameter>
{
new SugarParameter("@StartDateTime", startDateTime),
new SugarParameter("@EndDateTime", endDateTime),
new SugarParameter("@Type", type)
};
// 调用现有的 SqlQuery 方法
List<EmeterHourEnergy> emeterEnergyChanges = this.BaseDal.SqlQuery(sql, parameters);
return emeterEnergyChanges;
}
}
}