using Model;
using Model.Entity;
using ORM;
using Service.Interface;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Service.Implement
{
///
/// 监测数值记录服务实现
///
public class MonitorValueService : BaseService, IMonitorValueService
{
// 继承父类构造函数,注入 SqlSugar 仓储
public MonitorValueService(SqlSugarRepository repository) : base(repository)
{
}
///
/// 批量插入监测数据
///
public async Task> InsertRangeAsync(List entities)
{
if (entities == null || entities.Count == 0)
return Result.Success(true);
try
{
// 使用 SqlSugar 的批量插入,底层会转换为 BulkCopy 或是批量 SQL,速度极快
var result = await _repository.Context.Insertable(entities).ExecuteCommandAsync();
return Result.Success(result > 0);
}
catch (Exception ex)
{
return Result.Error("批量插入监测数据失败", ex);
}
}
///
/// 根据监测通道名称,分页查询历史记录
///
public async Task>> GetPagedByNameAsync(string monitorName, int pageIndex, int pageSize, RefAsync total)
{
try
{
var query = _repository.Entities;
// 如果传了名字则按名字过滤
if (!string.IsNullOrEmpty(monitorName))
{
query = query.Where(x => x.MonitorName == monitorName);
}
var list = await query
.OrderByDescending(d => d.CreateTime) // 监测项通常优先看最新的数据
.ToPageListAsync(pageIndex, pageSize, total);
// 计算总页数
total.Value = (int)Math.Ceiling((double)total.Value / pageSize);
return Result>.Success(list);
}
catch (Exception ex)
{
return Result>.Error($"根据名称 [{monitorName}] 分页查询数据失败", ex);
}
}
}
}