using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Volo.Abp.Application.Dtos; using Volo.Abp.Caching; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; using Win_in.Sfs.Shared; using Win_in.Sfs.Shared.Application; using Win_in.Sfs.Shared.Application.Contracts; using Win_in.Sfs.Shared.Domain; namespace Win_in.Sfs.Basedata.Application; [ApiExplorerSettings(GroupName = "BaseData")] public abstract class SfsBaseDataWithCodeAppServiceBase : SfsBaseDataAppServiceBase , ISfsGetByCodeAppService where TEntityDto : class, IHasCode, IEntityDto, new() where TEntity : class, IEntity, IHasCode, ISetId where TRequestInput : SfsRequestInputBase where TImportInput : class, IImportInput, new() { protected SfsBaseDataWithCodeAppServiceBase(ISfsRepositoryBase repository, IDistributedCache cache) : base(repository, cache) { } /// /// 根据编号获取实体DTO /// /// 编号 /// [HttpGet("by-code/{code}")] public virtual async Task GetByCodeAsync(string code) { var dto = await Cache.GetOrAddItemAsync( $"{typeof(TEntityDto).Name}:{code}".ToString(), async () => await GetFromRepositoryAsync(code).ConfigureAwait(false), SfsCacheConst.SeveralMinutes).ConfigureAwait(false); return dto; } [AllowAnonymous] [HttpGet("by-codes")] public virtual async Task> GetByCodesAsync(IEnumerable codes) { var dtoList = new List(); foreach (var code in codes) { var dto = await Cache.GetOrAddItemAsync( $"{typeof(TEntityDto).Name}:{code}".ToString(), async () => await GetFromRepositoryAsync(code).ConfigureAwait(false), SfsCacheConst.SeveralMinutes).ConfigureAwait(false); if (dto != null) { dtoList.Add(dto); } } return dtoList; } [AllowAnonymous] [HttpPost("by-codes-with-post")] public virtual async Task> GetByCodesWithPostAsync(IEnumerable codes) { var dtoList = new List(); foreach (var code in codes) { var dto = await Cache.GetOrAddItemAsync( $"{typeof(TEntityDto).Name}:{code}".ToString(), async () => await GetFromRepositoryAsync(code).ConfigureAwait(false), SfsCacheConst.SeveralMinutes).ConfigureAwait(false); if (dto != null) { dtoList.Add(dto); } } return dtoList; } protected virtual async Task GetFromRepositoryAsync(string code) { // var entity = await _repository.FindAsync(p => p.Code == code); var displayName = typeof(TEntityDto).GetNameOfDisplay(); var entity = await _repository.FirstOrDefaultAsync(p => p.Code == code).ConfigureAwait(false); // if (entity == null) // { // throw new UserFriendlyException($"未找到代码为 {code} 的 {displayName} 记录。"); // } return ObjectMapper.Map(entity); } public override async Task> GetPagedListByKeyWordAsync(string keyWord, int skipCount, int maxResultCount, string sorting, bool includeDetails = false, CancellationToken cancellationToken = default) { var expression = BuildSearchExpression(keyWord); return await GetPagedListAsync(expression, skipCount, maxResultCount, sorting, includeDetails, cancellationToken).ConfigureAwait(false); } protected override Expression> BuildSearchExpression(string keyWord) { Expression> expression = p => p.Code.Contains(keyWord); return expression; } }