using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.Caching; using Win_in.Sfs.Basedata.Application.Contracts; using Win_in.Sfs.Shared.Application; using Win_in.Sfs.Shared.Domain.Shared; namespace Win_in.Sfs.Wms.Job.Domain.Acl.Location { public class LocationAclService : AclServiceBase , ILocationAclService { private readonly ILocationAppService _appService; private readonly IDistributedCache _cache; public LocationAclService( ILocationAppService appService , IDistributedCache cache ) { _appService = appService; _cache = cache; } public virtual async Task GetByCodeAsync(string code) { var dto = await _cache.GetOrAddItemAsync( code, async () => await GetFromAppServiceAsync(code).ConfigureAwait(false), CacheMinutes).ConfigureAwait(false); return dto; } private async Task GetFromAppServiceAsync(string code) { var item = await _appService.GetByCodeAsync(code).ConfigureAwait(false); Check.NotNull(item, "库位代码", $"库位 {code} 不存在"); return item; } public virtual async Task> GetByCodesAsync(IEnumerable codes) { var dtos = new List(); var notInCacheCodes = new List(); foreach (var code in codes) { var dto = await _cache.GetAsync(code).ConfigureAwait(false); if (dto == null) { notInCacheCodes.Add(code); } else { dtos.Add(dto); } } var notInCacheCodeDtos = await _appService.GetByCodesAsync(notInCacheCodes).ConfigureAwait(false); foreach (var notInCacheCodeDto in notInCacheCodeDtos) { await _cache.SetItemAsync(notInCacheCodeDto.Code, notInCacheCodeDto, CacheMinutes).ConfigureAwait(false); } dtos.AddRange(notInCacheCodeDtos); return dtos; } public virtual async Task> GetListByEnablePickAsync(bool enablePick) { return await _appService.GetListByEnablePickAsync(enablePick).ConfigureAwait(false); } public virtual async Task> GetListByTypesAsync(List types) { return await _appService.GetListByTypesAsync(types).ConfigureAwait(false); } public virtual async Task GetFirstByTypeAsync(EnumLocationType locationType) { return await _appService.GetFirstByTypeAsync(locationType).ConfigureAwait(false); } public virtual async Task> GetListByTypesAndErpCodeAsync(List enumLocationTypes, string erpCode) { return await _appService.GetListByTypesAndErpCodeAsync(enumLocationTypes, erpCode).ConfigureAwait(false); } public virtual async Task> GetListByAreasAsync(List locationAreaCodes) { return await _appService.GetListByAreasAsync(locationAreaCodes).ConfigureAwait(false); } public virtual async Task> GetListByGroupsAsync(List locationGroupCodes) { return await _appService.GetListByGroupsAsync(locationGroupCodes).ConfigureAwait(false); } } }