using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Kurs.Platform.BlobStoring; using Kurs.Platform.Entities; using Kurs.Platform.FileManagement; using Kurs.Platform.Intranet; using Microsoft.AspNetCore.Authorization; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Volo.Abp.Domain.Repositories; using Volo.Abp.MultiTenancy; namespace Kurs.Platform.Public; [Authorize] public class IntranetAppService : PlatformAppService, IIntranetAppService { private readonly ICurrentTenant _currentTenant; private readonly BlobManager _blobContainer; private readonly IConfiguration _configuration; private readonly IRepository _eventRepository; private readonly IRepository _employeeRepository; private readonly IRepository _visitorRepository; private readonly IRepository _reservationRepository; private readonly IRepository _trainingRepository; private readonly IRepository _expenseRepository; public IntranetAppService( ICurrentTenant currentTenant, BlobManager blobContainer, IConfiguration configuration, IRepository eventRepository, IRepository employeeRepository, IRepository visitorRepository, IRepository reservationRepository, IRepository trainingRepository, IRepository expenseRepository ) { _currentTenant = currentTenant; _blobContainer = blobContainer; _configuration = configuration; _eventRepository = eventRepository; _employeeRepository = employeeRepository; _visitorRepository = visitorRepository; _reservationRepository = reservationRepository; _trainingRepository = trainingRepository; _expenseRepository = expenseRepository; } public async Task GetIntranetDashboardAsync() { return new IntranetDashboardDto { Events = await GetUpcomingEventsAsync(), Birthdays = await GetBirthdaysAsync(), Visitors = await GetVisitorsAsync(), Reservations = await GetReservationsAsync(), Trainings = await GetTrainingsAsync(), Expenses = await GetExpensesAsync(), Documents = await GetIntranetDocumentsAsync(BlobContainerNames.Intranet) }; } public async Task> GetIntranetDocumentsAsync(string folderPath) { var items = new List(); var cdnBasePath = _configuration["App:CdnPath"]; if (string.IsNullOrEmpty(cdnBasePath)) { Logger.LogWarning("CDN path is not configured"); return items; } var tenantId = _currentTenant.Id?.ToString() ?? "host"; var fullPath = Path.Combine(cdnBasePath, tenantId); if (!string.IsNullOrEmpty(folderPath)) { fullPath = Path.Combine(fullPath, folderPath); } if (!Directory.Exists(fullPath)) { Logger.LogWarning($"Directory not found: {fullPath}"); return items; } var files = Directory.GetFiles(fullPath); foreach (var file in files) { var fileInfo = new FileInfo(file); var relativePath = string.IsNullOrEmpty(folderPath) ? fileInfo.Name : $"{folderPath}/{fileInfo.Name}"; items.Add(new FileItemDto { Id = Guid.NewGuid().ToString(), Name = fileInfo.Name, Type = "file", Size = fileInfo.Length, Extension = fileInfo.Extension, MimeType = GetMimeType(fileInfo.Extension), CreatedAt = fileInfo.CreationTime, ModifiedAt = fileInfo.LastWriteTime, Path = relativePath, ParentId = string.Empty, IsReadOnly = false, ChildCount = 0 }); } return items.OrderBy(x => x.Name).ToList(); } private string GetMimeType(string extension) { return extension.ToLowerInvariant() switch { ".pdf" => "application/pdf", ".doc" => "application/msword", ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", ".xls" => "application/vnd.ms-excel", ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".ppt" => "application/vnd.ms-powerpoint", ".pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation", ".jpg" or ".jpeg" => "image/jpeg", ".png" => "image/png", ".gif" => "image/gif", ".txt" => "text/plain", ".zip" => "application/zip", ".rar" => "application/x-rar-compressed", _ => "application/octet-stream" }; } private async Task GetExpensesAsync() { var queryable = await _expenseRepository.GetQueryableAsync(); var today = DateTime.Today; var oneMonthAgo = today.AddMonths(-1); // Son 1 ay içerisindeki kayıtlar var lastMonthExpenses = queryable .Where(a => a.RequestDate >= oneMonthAgo && a.RequestDate <= today); // Son 1 aydaki toplam talep miktarı var totalRequested = lastMonthExpenses.Sum(a => a.Amount); // Son 1 aydaki onaylanan harcamaların toplamı var totalApproved = lastMonthExpenses .Where(a => a.Status == "approved") .Sum(a => a.Amount); // Son 5 kayıt var last5Expenses = queryable .OrderByDescending(a => a.RequestDate) .Take(5) .ToList(); // Map işlemleri var last5Dtos = ObjectMapper.Map, List>(last5Expenses); // Dönüş DTO'su return new ExpensesDto { TotalRequested = totalRequested, TotalApproved = totalApproved, Last5Expenses = last5Dtos }; } private async Task> GetTrainingsAsync() { var trainings = await _trainingRepository.GetListAsync(a => a.StartDate >= DateTime.Today); return ObjectMapper.Map, List>(trainings); } private async Task> GetReservationsAsync() { var reservations = await _reservationRepository.GetListAsync(a => a.StartDate >= DateTime.Today); return ObjectMapper.Map, List>(reservations); } private async Task> GetVisitorsAsync() { var visitors = await _visitorRepository.GetListAsync(a => a.VisitDate >= DateTime.Today); return ObjectMapper.Map, List>(visitors); } private async Task> GetBirthdaysAsync() { var today = DateTime.Now; var employees = await _employeeRepository .WithDetailsAsync(e => e.EmploymentType, e => e.JobPosition, e => e.Department) .ContinueWith(t => t.Result .Where(e => e.BirthDate.Day == today.Day && e.BirthDate.Month == today.Month) .ToList()); return ObjectMapper.Map, List>(employees); } private async Task> GetUpcomingEventsAsync() { var events = await _eventRepository .WithDetailsAsync(e => e.Category, e => e.Type, e => e.Category, e => e.Photos, e => e.Comments) .ContinueWith(t => t.Result.ToList().Where(e => e.isPublished).OrderByDescending(e => e.CreationTime)); var result = new List(); foreach (var evt in events) { var employee = await _employeeRepository .WithDetailsAsync(e => e.JobPosition) .ContinueWith(t => t.Result.FirstOrDefault(e => e.Id == evt.EmployeeId)); if (employee != null) { var calendarEvent = new EventDto { Id = evt.Id.ToString(), Name = evt.Name, Description = evt.Description, TypeName = evt.Type?.Name, CategoryName = evt.Category?.Name, Date = evt.Date, Place = evt.Place, Organizer = new EventOrganizerDto { Id = employee.Id, Name = employee.FullName, Position = employee.JobPosition.Name, Avatar = employee.Avatar }, Participants = evt.ParticipantsCount, Photos = [], Comments = [], Likes = evt.Likes, IsPublished = evt.isPublished }; // Comment'lerin author bilgilerini doldur if (evt.Comments != null && evt.Comments.Any()) { foreach (var comment in evt.Comments) { var commentAuthor = await _employeeRepository .WithDetailsAsync(e => e.JobPosition) .ContinueWith(t => t.Result.FirstOrDefault(e => e.Id == comment.EmployeeId)); if (commentAuthor != null) { calendarEvent.Comments.Add(new EventCommentDto { Id = comment.Id.ToString(), Author = new EventOrganizerDto { Id = commentAuthor.Id, Name = commentAuthor.FullName, Position = commentAuthor.JobPosition.Name, Avatar = commentAuthor.Avatar }, Content = comment.Content, CreationTime = comment.CreationTime, Likes = comment.Likes }); } } } result.Add(calendarEvent); } } return result; } }