using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Sozsoft.Platform.Entities; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; namespace Sozsoft.Platform.AiBots; [Authorize] public class AiBotAppService : CrudAppService< AiBot, AiBotDto, Guid, PagedAndSortedResultRequestDto> { public AiBotAppService(IRepository repo) : base(repo) { } public override async Task> GetListAsync(PagedAndSortedResultRequestDto input) { await CheckGetListPolicyAsync(); if (CurrentTenant.IsAvailable && !string.IsNullOrWhiteSpace(CurrentTenant.Name)) { var tenantName = CurrentTenant.Name; List tenantEntities; using (CurrentTenant.Change(null)) { var hostQuery = await CreateFilteredQueryAsync(input); tenantEntities = await AsyncExecuter.ToListAsync(hostQuery); } var filteredQuery = tenantEntities .Where(aiBot => IsAvailableForTenant(aiBot.Tenants, tenantName)) .AsQueryable(); var tenantTotalCount = filteredQuery.LongCount(); filteredQuery = ApplySorting(filteredQuery, input); filteredQuery = ApplyPaging(filteredQuery, input); var tenantDtos = await MapToGetListOutputDtosAsync(filteredQuery.ToList()); return new PagedResultDto(tenantTotalCount, tenantDtos); } var query = await CreateFilteredQueryAsync(input); var totalCount = await AsyncExecuter.CountAsync(query); query = ApplySorting(query, input); query = ApplyPaging(query, input); var entities = await AsyncExecuter.ToListAsync(query); var dtos = await MapToGetListOutputDtosAsync(entities); return new PagedResultDto(totalCount, dtos); } private static bool IsAvailableForTenant(string tenants, string tenantName) { if (string.IsNullOrWhiteSpace(tenants)) { return true; } return tenants .Split('|', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) .Any(name => string.Equals(name, tenantName, StringComparison.OrdinalIgnoreCase)); } }