添加项目文件。
This commit is contained in:
132
Service/Implement/BaseService.cs
Normal file
132
Service/Implement/BaseService.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
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 BaseService<TEntity> : IBaseService<TEntity> where TEntity : BaseEntity, new()
|
||||
{
|
||||
protected readonly SqlSugarRepository<TEntity> _repository;
|
||||
|
||||
public BaseService(SqlSugarRepository<TEntity> repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询全部
|
||||
/// </summary>
|
||||
public virtual async Task<Result<List<TEntity>>> GetAllAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await _repository.Entities.ToListAsync();
|
||||
return Result<List<TEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<TEntity>>.Error("查询所有数据失败", ex);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据日期查询全部
|
||||
/// </summary>
|
||||
public virtual async Task<Result<List<TEntity>>> GetAllAsyncByDate(DateTime? startDate, DateTime? endDate)
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await _repository.Entities.Where(x => x.CreateTime >= startDate && x.CreateTime <= endDate).ToListAsync();
|
||||
return Result<List<TEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<TEntity>>.Error("查询所有数据失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询
|
||||
/// </summary>
|
||||
public virtual async Task<Result<List<TEntity>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total)
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await _repository.Entities
|
||||
.OrderBy(d => d.Id)
|
||||
.ToPageListAsync(pageIndex, pageSize, total);
|
||||
total.Value = (int)Math.Ceiling((double)total.Value / pageSize);
|
||||
return Result<List<TEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<TEntity>>.Error("分页查询数据失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询,并可以根据日期范围进行过滤
|
||||
/// </summary>
|
||||
public virtual async Task<Result<List<TEntity>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total, DateTime? startDate, DateTime? endDate)
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await _repository.Entities
|
||||
.Where(x=>x.CreateTime>=startDate&&x.CreateTime <= endDate)
|
||||
.OrderBy(d => d.Id)
|
||||
.ToPageListAsync(pageIndex, pageSize, total);
|
||||
total.Value = (int)Math.Ceiling((double)total.Value / pageSize);
|
||||
return Result<List<TEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<TEntity>>.Error("分页查询数据失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 插入单条数据
|
||||
/// </summary>
|
||||
public virtual async Task<Result<bool>> InsertAsync(TEntity entity)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _repository.Context.Insertable(entity).ExecuteCommandAsync();
|
||||
return Result<bool>.Success(result > 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("插入数据失败", ex);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除单条数据(根据 Id)
|
||||
/// </summary>
|
||||
/// <param name="id">主键 Id</param>
|
||||
public virtual async Task<Result<bool>> DeleteAsync(long id)
|
||||
{
|
||||
if (id <= 0)
|
||||
return Result<bool>.Error("主键 Id 无效,无法删除");
|
||||
|
||||
try
|
||||
{
|
||||
// 删除实体
|
||||
var result = await _repository.Context
|
||||
.Deleteable<TEntity>()
|
||||
.Where(x => x.Id == id)
|
||||
.ExecuteCommandAsync();
|
||||
|
||||
return Result<bool>.Success(result > 0);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<bool>.Error("删除数据失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
109
Service/Implement/TestReportService.cs
Normal file
109
Service/Implement/TestReportService.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using Model;
|
||||
using Model.Entity;
|
||||
using Model.Models;
|
||||
using ORM;
|
||||
using Service.Interface;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using static System.Formats.Asn1.AsnWriter;
|
||||
|
||||
namespace Service.Implement
|
||||
{
|
||||
public class TestReportService : BaseService<TestReportEntity>, ITestReportService
|
||||
{
|
||||
public TestReportService(SqlSugarRepository<TestReportEntity> repository) : base(repository)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 TestRoundId 获取符合条件的测试步骤
|
||||
/// </summary>
|
||||
public async Task<Result<List<TestReportEntity>>> GetByTestRoundIdAsync(Guid testRoundId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = await _repository.Entities
|
||||
.Where(x => x.TestRoundId == testRoundId)
|
||||
.ToListAsync();
|
||||
|
||||
return Result<List<TestReportEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<TestReportEntity>>.Error("根据 TestRoundId 查询数据失败", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 TestRoundId 分组,获取唯一记录并按分组统计最大与最小时间,
|
||||
/// 支持按日期范围与台架名称筛选。
|
||||
/// </summary>
|
||||
public async Task<Result<IList<TestReportModel>>> GetFilterList(DateTime? startTime = null, DateTime? endTime = null, string? scope = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 使用 SqlSugar 的 GroupBy 查询,聚合最大/最小时间
|
||||
// 推荐采用这种按组投影的方式,将分组计算交由数据库处理,性能最佳
|
||||
var query = _repository.Context.Queryable<TestReportEntity>();
|
||||
|
||||
// 日期范围筛选
|
||||
if (startTime.HasValue)
|
||||
query = query.Where(x => x.CreateTime >= startTime.Value);
|
||||
if (endTime.HasValue)
|
||||
query = query.Where(x => x.CreateTime <= endTime.Value);
|
||||
|
||||
// 台架名称筛选
|
||||
if (!string.IsNullOrEmpty(scope))
|
||||
query = query.Where(x => x.Scope == scope);
|
||||
|
||||
var list = await query
|
||||
.GroupBy(x => x.TestRoundId)
|
||||
.Select(x => new TestReportModel
|
||||
{
|
||||
TestRoundId = x.TestRoundId,
|
||||
StartTime = SqlFunc.AggregateMin(x.CreateTime),
|
||||
EndTime = SqlFunc.AggregateMax(x.CreateTime),
|
||||
Scope = x.Scope,
|
||||
FileName = x.FileName,
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
return Result<IList<TestReportModel>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<IList<TestReportModel>>.Error("获取分组过滤列表失败", ex);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 根据日期和台架名称筛选获取完整的测试步骤实体列表(无分组,用于导出)
|
||||
/// </summary>
|
||||
public async Task<Result<List<TestReportEntity>>> GetEntitiesByFilter(DateTime? startTime = null, DateTime? endTime = null, string? scope = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _repository.Context.Queryable<TestReportEntity>();
|
||||
|
||||
if (startTime.HasValue)
|
||||
query = query.Where(x => x.CreateTime >= startTime.Value);
|
||||
if (endTime.HasValue)
|
||||
query = query.Where(x => x.CreateTime <= endTime.Value);
|
||||
if (!string.IsNullOrEmpty(scope))
|
||||
query = query.Where(x => x.Scope == scope);
|
||||
|
||||
var list = await query
|
||||
.OrderBy(x => x.CreateTime)
|
||||
.ToListAsync();
|
||||
|
||||
return Result<List<TestReportEntity>>.Success(list);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return Result<List<TestReportEntity>>.Error("获取导出数据失败", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
55
Service/Interface/IBaseService.cs
Normal file
55
Service/Interface/IBaseService.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using Model;
|
||||
using SqlSugar;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Service.Interface
|
||||
{
|
||||
/// <summary>
|
||||
/// 泛型基础服务接口(返回 Result 封装)
|
||||
/// </summary>
|
||||
/// <typeparam name="TEntity">实体类型</typeparam>
|
||||
public interface IBaseService<TEntity> where TEntity : class, new()
|
||||
{
|
||||
/// <summary>
|
||||
/// 查询全部
|
||||
/// </summary>
|
||||
/// <returns>返回包含数据的 Result</returns>
|
||||
Task<Result<List<TEntity>>> GetAllAsync();
|
||||
|
||||
/// <summary>
|
||||
/// 根据日期查询全部
|
||||
/// </summary>
|
||||
/// <param name="startDate">开始日期</param>
|
||||
/// <param name="endDate">结束日期</param>
|
||||
/// <returns>返回包含日期范内的数据的 Result</returns>
|
||||
Task<Result<List<TEntity>>> GetAllAsyncByDate(DateTime? startDate, DateTime? endDate);
|
||||
|
||||
/// <summary>
|
||||
/// 插入单条数据
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns>返回操作是否成功的 Result</returns>
|
||||
Task<Result<bool>> InsertAsync(TEntity entity);
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询
|
||||
/// </summary>
|
||||
/// <param name="pageIndex">页码(从1开始)</param>
|
||||
/// <param name="pageSize">每页数量</param>
|
||||
/// <param name="total">总条数(输出参数)</param>
|
||||
/// <returns>返回包含分页数据的 Result</returns>
|
||||
Task<Result<List<TEntity>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total);
|
||||
|
||||
/// <summary>
|
||||
/// 分页查询,并根据日期范围进行过滤
|
||||
/// </summary>
|
||||
/// <param name="pageIndex">页码(从1开始)</param>
|
||||
/// <param name="pageSize">每页数量</param>
|
||||
/// <param name="total">总条数(输出参数)</param>
|
||||
/// <param name="startDate">开始日期</param>
|
||||
/// <param name="endDate">结束日期</param>
|
||||
/// <returns>返回包含分页数据的 Result</returns>
|
||||
Task<Result<List<TEntity>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total, DateTime? startDate, DateTime? endDate);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
37
Service/Interface/ITestReportService.cs
Normal file
37
Service/Interface/ITestReportService.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using Model;
|
||||
using Model.Entity;
|
||||
using Model.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Service.Interface
|
||||
{
|
||||
public interface ITestReportService:IBaseService<TestReportEntity>
|
||||
{
|
||||
/// <summary>
|
||||
/// 根据TestRoundId获符合的测试步骤
|
||||
/// </summary>
|
||||
/// <param name="TestRoundId">TestRoundId</param>
|
||||
/// <returns>返回符合的TestReportEntity列表</returns>
|
||||
Task<Result<List<TestReportEntity>>> GetByTestRoundIdAsync(Guid TestRoundId);
|
||||
|
||||
/// <summary>
|
||||
/// 根据 TestRoundId 分组,获取唯一记录并按分组统计最大与最小时间
|
||||
/// </summary>
|
||||
/// <param name="startTime">开始日期筛选(可选)</param>
|
||||
/// <param name="endTime">结束日期筛选(可选)</param>
|
||||
/// <param name="scope">台架名称筛选(可选)</param>
|
||||
Task<Result<IList<TestReportModel>>> GetFilterList(DateTime? startTime = null, DateTime? endTime = null, string? scope = null);
|
||||
|
||||
/// <summary>
|
||||
/// 根据日期和台架名称筛选获取完整的测试步骤实体列表(无分组,用于导出)
|
||||
/// </summary>
|
||||
/// <param name="startTime">开始日期筛选(可选)</param>
|
||||
/// <param name="endTime">结束日期筛选(可选)</param>
|
||||
/// <param name="scope">台架名称筛选(可选)</param>
|
||||
Task<Result<List<TestReportEntity>>> GetEntitiesByFilter(DateTime? startTime = null, DateTime? endTime = null, string? scope = null);
|
||||
}
|
||||
}
|
||||
13
Service/Service.csproj
Normal file
13
Service/Service.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ORM\ORM.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user