监控数据添加
This commit is contained in:
71
Service/Implement/MonitorValueService.cs
Normal file
71
Service/Implement/MonitorValueService.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// 监测数值记录服务实现
|
||||
/// </summary>
|
||||
public class MonitorValueService : BaseService<MonitorValueEntity>, IMonitorValueService
|
||||
{
|
||||
// 继承父类构造函数,注入 SqlSugar 仓储
|
||||
public MonitorValueService(SqlSugarRepository<MonitorValueEntity> repository) : base(repository)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量插入监测数据
|
||||
/// </summary>
|
||||
public async Task<Result<bool>> InsertRangeAsync(List<MonitorValueEntity> entities)
|
||||
{
|
||||
if (entities == null || entities.Count == 0)
|
||||
return Result<bool>.Success(true);
|
||||
|
||||
try
|
||||
{
|
||||
// 使用 SqlSugar 的批量插入,底层会转换为 BulkCopy 或是批量 SQL,速度极快
|
||||
var result = await _repository.Context.Insertable(entities).ExecuteCommandAsync();
|
||||
return Result<bool>.Success(result > 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("批量插入监测数据失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据监测通道名称,分页查询历史记录
|
||||
/// </summary>
|
||||
public async Task<Result<List<MonitorValueEntity>>> GetPagedByNameAsync(string monitorName, int pageIndex, int pageSize, RefAsync<int> 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<List<MonitorValueEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<MonitorValueEntity>>.Error($"根据名称 [{monitorName}] 分页查询数据失败", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
32
Service/Interface/IMonitorValueService.cs
Normal file
32
Service/Interface/IMonitorValueService.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Model;
|
||||
using Model.Entity;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Service.Interface
|
||||
{
|
||||
/// <summary>
|
||||
/// 监测数值记录服务接口
|
||||
/// </summary>
|
||||
public interface IMonitorValueService : IBaseService<MonitorValueEntity>
|
||||
{
|
||||
/// <summary>
|
||||
/// 批量插入监测数据(工控高频采样推荐使用,性能远高于单条循环插入)
|
||||
/// </summary>
|
||||
/// <param name="entities">实体集合</param>
|
||||
/// <returns>返回操作是否成功的 Result</returns>
|
||||
Task<Result<bool>> InsertRangeAsync(List<MonitorValueEntity> entities);
|
||||
|
||||
/// <summary>
|
||||
/// 根据监测通道名称,分页查询历史记录
|
||||
/// </summary>
|
||||
/// <param name="monitorName">通道名称(例如:台架1.读取主轴温度)</param>
|
||||
/// <param name="pageIndex">页码</param>
|
||||
/// <param name="pageSize">每页大小</param>
|
||||
/// <param name="total">总条数输出</param>
|
||||
/// <returns>分页数据结果</returns>
|
||||
Task<Result<List<MonitorValueEntity>>> GetPagedByNameAsync(string monitorName, int pageIndex, int pageSize, RefAsync<int> total);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user