You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

117 lines
4.2 KiB

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<TEntity, TEntityDto, TRequestInput, TCreateInput, TImportInput>
: SfsBaseDataAppServiceBase<TEntity, TEntityDto, TRequestInput, TCreateInput, TImportInput>
, ISfsGetByCodeAppService<TEntityDto>
where TEntityDto : class, IHasCode, IEntityDto<Guid>, new()
where TEntity : class, IEntity<Guid>, IHasCode, ISetId
where TRequestInput : SfsRequestInputBase
where TImportInput : class, IImportInput, new()
{
protected SfsBaseDataWithCodeAppServiceBase(ISfsRepositoryBase<TEntity> repository, IDistributedCache<TEntityDto> cache) : base(repository, cache)
{
}
/// <summary>
/// 根据编号获取实体DTO
/// </summary>
/// <param name="code">编号</param>
/// <returns></returns>
[HttpGet("by-code/{code}")]
public virtual async Task<TEntityDto> 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<List<TEntityDto>> GetByCodesAsync(IEnumerable<string> codes)
{
var dtoList = new List<TEntityDto>();
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<List<TEntityDto>> GetByCodesWithPostAsync(IEnumerable<string> codes)
{
var dtoList = new List<TEntityDto>();
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<TEntityDto> 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<TEntity, TEntityDto>(entity);
}
public override async Task<PagedResultDto<TEntityDto>> 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<Func<TEntity, bool>> BuildSearchExpression(string keyWord)
{
Expression<Func<TEntity, bool>> expression = p => p.Code.Contains(keyWord);
return expression;
}
}