添加项目文件。

This commit is contained in:
czj
2026-06-05 10:57:09 +08:00
parent f29671b374
commit d960cb5912
166 changed files with 15996 additions and 0 deletions

View 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);
}
}
}
}

View 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
View 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>