using Model; using Model.SQLModel; using ORM; using Service.Interface; using SqlSugar; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Service.Implement { public class BaseService : IBaseService where TEntity : BaseEntity, new() { protected readonly SqlSugarRepository _repository; public BaseService(SqlSugarRepository repository) { _repository = repository; } /// /// 查询全部 /// public async Task>> GetAllAsync() { try { var list = await _repository.Entities.ToListAsync(); return Result>.Success(list); } catch (Exception ex) { return Result>.Error("查询所有数据失败", ex); } } /// /// 分页查询 /// public async Task>> GetPagedAsync(int pageIndex, int pageSize, RefAsync 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>.Success(list); } catch (Exception ex) { return Result>.Error("分页查询数据失败", ex); } } /// /// 分页查询,并可以根据日期范围进行过滤 /// public async Task>> GetPagedAsync(int pageIndex, int pageSize, RefAsync total, DateTime? startDate, DateTime? endDate) { try { var list = await _repository.Entities .Where(x=>x.CollectTime>=startDate&&x.CollectTime<=endDate) .OrderBy(d => d.Id) .ToPageListAsync(pageIndex, pageSize, total); total.Value = (int)Math.Ceiling((double)total.Value / pageSize); return Result>.Success(list); } catch (Exception ex) { return Result>.Error("分页查询数据失败", ex); } } /// /// 插入单条数据 /// public async Task> InsertAsync(TEntity entity) { try { var result = await _repository.Context.Insertable(entity).ExecuteCommandAsync(); return Result.Success(result > 0); } catch (Exception ex) { return Result.Error("插入数据失败", ex); } } } }