using Furion; using Microsoft.Extensions.DependencyInjection; using SqlSugar; using Yi.Framework.Infrastructure.Ddd.Dtos; using Yi.Framework.Infrastructure.Ddd.Dtos.Abstract; using Yi.Framework.Infrastructure.Ddd.Entities; using Yi.Framework.Infrastructure.Ddd.Repositories; using Yi.Framework.Infrastructure.Ddd.Services.Abstract; namespace Yi.Framework.Infrastructure.Ddd.Services { public abstract class ReadOnlyAppService : ReadOnlyAppService where TEntity : class, IEntity where TEntityDto : IEntityDto { } public abstract class ReadOnlyAppService : ReadOnlyAppService where TEntity : class, IEntity where TEntityDto : IEntityDto { } public abstract class ReadOnlyAppService : ApplicationService, IReadOnlyAppService where TEntity : class, IEntity { /// /// 先暂时用服务定位的方式,之后将更改为属性注入 /// protected IRepository _repository { get => App.GetRequiredService>(); } protected ISugarQueryable _DbQueryable => _repository._DbQueryable; //Mapper protected virtual Task MapToGetOutputDtoAsync(TEntity entity) { return Task.FromResult(_mapper.Map(entity)); } protected virtual Task> MapToGetListOutputDtosAsync(List entities) { var dtos = _mapper.Map>(entities); return Task.FromResult(dtos); } protected virtual Task MapToGetListOutputDtoAsync(TEntity entity) { var dto = _mapper.Map(entity); return Task.FromResult(dto); } /// /// 单查 /// /// /// /// public virtual async Task GetAsync(TKey id) { if (id is null) { throw new ArgumentNullException(nameof(id)); } var entity = await _repository.GetByIdAsync(id); return await MapToGetOutputDtoAsync(entity); } /// /// 多查 /// /// /// public virtual async Task> GetListAsync(TGetListInput input) { var totalCount = -1; var entities = new List(); var entityDtos = new List(); bool isPageList = true; //if (totalCount > 0) //{ //这里还可以追加如果是审计日志,继续拼接条件即可 if (input is IPageTimeResultRequestDto timeInput) { if (timeInput.StartTime is not null) { timeInput.EndTime = timeInput.EndTime ?? DateTime.Now; } } if (input is IPagedAndSortedResultRequestDto sortInput) { entities = await _repository.GetPageListAsync(_ => true, sortInput, sortInput.SortBy, sortInput.SortType); } else { isPageList = false; entities = await _repository.GetListAsync(); } entityDtos = await MapToGetListOutputDtosAsync(entities); //} //如果是分页查询,还需要统计数量 if (isPageList) { totalCount = await _repository.CountAsync(_ => true); } return new PagedResultDto( totalCount, entityDtos ); } } }