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.

90 lines
3.0 KiB

6 months ago
using Entity.Ammeter;
using Entity.Dto.Req;
6 months ago
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Entity;
6 months ago
using Microsoft.AspNetCore.Mvc.RazorPages;
6 months ago
using Repository.Ammeter;
using SqlSugar;
6 months ago
namespace Service.Ammeter
{
/// <summary>
///
/// </summary>
[Scope("SingleInstance")]
public class EmeterHourEnergyService: BaseServices<EmeterHourEnergy>
{
public EmeterHourEnergyService(EmeterHourEnergyRepository service)
{
this.BaseDal = service;
6 months ago
}
6 months ago
public async Task<Result<PageResult<EmeterHourEnergy>>> Page(PageAmmeterReq queryPageModel)
{
QueryPageModel queryPageModel1 = new QueryPageModel
{
PageNum = queryPageModel.PageNum,
PageSize = queryPageModel.PageSize,
};
if (!string.IsNullOrEmpty(queryPageModel.Code))
6 months ago
return Result<PageResult<EmeterHourEnergy>>.Success(PageResult<EmeterHourEnergy>.ConvertPage(this.BaseDal.QueryIPageByCause(queryPageModel1, u => u.Code == queryPageModel.Code)));
else
6 months ago
return Result<PageResult<EmeterHourEnergy>>.Success(PageResult<EmeterHourEnergy>.ConvertPage(this.BaseDal.QueryIPageByCause(queryPageModel1, null)));
}
/// <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;
}
6 months ago
}
}