添加项目文件。
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
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