using System.Linq.Expressions; using Abp.Dependency; using Abp.Domain.Repositories; using Abp.Runtime.Caching; using VberZero.BaseSystem; namespace VberZero.DomainService.States; public class SysStatesManager : ISysStatesManager, ISingletonDependency { private ICacheManager CacheManager { get; set; } private IRepository Repository { get; set; } public SysStatesManager( ICacheManager cacheManage, IRepository repository) { CacheManager = cacheManage; Repository = repository; } #region GetSelectList #region CodeFilter public async Task GetSelectStrAsync(QueryStateDisplayValue input, params string[] codeFilter) { return await GetSelectStrAsync(input.CodeKey, codeFilter); } public async Task GetSelectStrAsync(string codeKey, params string[] codeFilter) { var options = ""; var list = await GetStateListAsync(codeKey, codeFilter); foreach (var l in list) { options += $"\r\n"; } return options; } #endregion CodeFilter #region CodeFilterReversal public async Task GetSelectStrReversalAsync(QueryStateDisplayValue input, params string[] codeFilter) { return await GetSelectStrReversalAsync(input.CodeKey, codeFilter); } public async Task GetSelectStrReversalAsync(string codeKey, params string[] codeFilter) { var options = ""; var list = await GetStateListReversalAsync(codeKey, codeFilter); foreach (var l in list) { options += $"\r\n"; } return options; } #endregion CodeFilterReversal #region Exp public async Task GetSelectStrAsync(QueryStateDisplayValue input, Expression> exp = null) { return await GetSelectStrAsync(input.CodeKey, exp); } public async Task GetSelectStrAsync(string codeKey, Expression> exp = null) { var options = ""; var list = await GetStateListAsync(codeKey, exp); foreach (var l in list) { options += $"\r\n"; } return options; } #endregion Exp #region StateDisplayDto public async Task> GetStateListAsync(QueryStateDisplayValue input, Expression> exp = null) { return await GetStateListAsync(input.CodeKey, exp); } public async Task> GetStateListAsync(string codeKey, Expression> exp = null) { var list = (await Repository.GetAllListAsync(a => a.CodeKey == codeKey)) .AsQueryable(); if (exp != null) { list = list.Where(exp); } var dtoList = list.Select(a => new StateDisplayDto() { CodeValue = a.CodeValue, DisplayValue = a.DisplayValue }).ToList(); return dtoList; } public async Task> GetStateListAsync(string codeKey, params string[] codeFilter) { var list = await Repository.GetAllListAsync(a => a.CodeKey == codeKey); if (codeFilter.Any()) { foreach (var code in codeFilter) { list = list.Where(a => a.CodeValue == code).ToList(); } } var dtos = list.Select(a => new StateDisplayDto() { CodeValue = a.CodeValue, DisplayValue = a.DisplayValue }).ToList(); return dtos; } public async Task> GetStateListReversalAsync(string codeKey, params string[] codeFilter) { var list = await Repository.GetAllListAsync(a => a.CodeKey == codeKey); if (codeFilter.Any()) { foreach (var code in codeFilter) { list = list.Where(a => a.CodeValue != code).ToList(); } } var dtos = list.Select(a => new StateDisplayDto() { CodeValue = a.CodeValue, DisplayValue = a.DisplayValue }).ToList(); return dtos; } #endregion StateDisplayDto #endregion GetSelectList #region GetDisplayValue public async Task GetDisplayValueAsync(QueryStateDisplayValue input) { return await GetDisplayValueAsync(input.CodeKey, input.CodeValue); } public async Task GetDisplayValueAsync(string codeKey, string codeValue) { return await CacheManager.GetCache(VzConsts.CacheSysState).GetAsync( codeKey + "." + codeValue, () => DisplayValueAsync(codeKey, codeValue)); } private async Task DisplayValueAsync(string codeKey, string codeValue) { var state = await Repository.FirstOrDefaultAsync(a => a.CodeKey == codeKey && a.CodeValue == codeValue); return state?.DisplayValue; } public string GetDisplayValue(QueryStateDisplayValue input) { return GetDisplayValue(input.CodeKey, input.CodeValue); } public string GetDisplayValue(string codeKey, string codeValue) { return CacheManager.GetCache(VzConsts.CacheSysState).Get( codeKey + "." + codeValue, () => DisplayValue(codeKey, codeValue)); } private string DisplayValue(string codeKey, string codeValue) { var state = Repository.FirstOrDefault(a => a.CodeKey == codeKey && a.CodeValue == codeValue); return state?.DisplayValue; } #endregion GetDisplayValue }