diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Commands/ChangeStatusSection/ChangeStatusSectionCommandHandler.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Commands/ChangeStatusSection/ChangeStatusSectionCommandHandler.cs index 9e6803f5..d5d0cd62 100644 --- a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Commands/ChangeStatusSection/ChangeStatusSectionCommandHandler.cs +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Commands/ChangeStatusSection/ChangeStatusSectionCommandHandler.cs @@ -52,7 +52,10 @@ public class ChangeStatusSectionCommandHandler : IBaseCommandHandlerx.UserId is > 0).ToList(); + // حذف ProjectSections که در validSkills نیستند + var validSkillIds = skillItems.Select(x => x.SkillId).ToList(); + var sectionsToRemove = project.ProjectSections + .Where(s => !validSkillIds.Contains(s.SkillId)) + .ToList(); + + foreach (var section in sectionsToRemove) + { + project.RemoveProjectSection(section.SkillId); + } + // تخصیص در سطح پروژه foreach (var item in skillItems) { @@ -102,6 +113,16 @@ public class SetTimeProjectCommandHandler : IBaseCommandHandler !validSkillIds.Contains(s.SkillId)) + .ToList(); + + foreach (var section in phaseSectionsToRemove) + { + phase.RemovePhaseSection(section.SkillId); + } + // برای phase هم باید section‌ها را به‌روزرسانی کنیم foreach (var item in skillItems ) { @@ -122,6 +143,16 @@ public class SetTimeProjectCommandHandler : IBaseCommandHandler !validSkillIds.Contains(s.SkillId)) + .ToList(); + + foreach (var section in taskSectionsToRemove) + { + task.RemoveSection(section.Id); + } + foreach (var item in skillItems) { var section = task.Sections.FirstOrDefault(s => s.SkillId == item.SkillId); @@ -177,6 +208,18 @@ public class SetTimeProjectCommandHandler : IBaseCommandHandlerx.UserId is > 0).ToList(); + + // حذف PhaseSections که در validSkills نیستند + var validSkillIds = skillItems.Select(x => x.SkillId).ToList(); + var sectionsToRemove = phase.PhaseSections + .Where(s => !validSkillIds.Contains(s.SkillId)) + .ToList(); + + foreach (var section in sectionsToRemove) + { + phase.RemovePhaseSection(section.SkillId); + } + // به‌روزرسانی یا اضافه کردن PhaseSection foreach (var item in skillItems) { @@ -200,6 +243,16 @@ public class SetTimeProjectCommandHandler : IBaseCommandHandler !validSkillIds.Contains(s.SkillId)) + .ToList(); + + foreach (var section in taskSectionsToRemove) + { + task.RemoveSection(section.Id); + } + foreach (var item in skillItems) { var section = task.Sections.FirstOrDefault(s => s.SkillId == item.SkillId); @@ -246,7 +299,16 @@ public class SetTimeProjectCommandHandler : IBaseCommandHandlerx.UserId is > 0).ToList(); - task.ClearTaskSections(); + // حذف سکشن‌هایی که در validSkills نیستند + var validSkillIds = validSkills.Select(x => x.SkillId).ToList(); + var sectionsToRemove = task.Sections + .Where(s => !validSkillIds.Contains(s.SkillId)) + .ToList(); + + foreach (var sectionToRemove in sectionsToRemove) + { + task.RemoveSection(sectionToRemove.Id); + } foreach (var skillItem in validSkills) { diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/GetProjectSearchQuery.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/GetProjectSearchQuery.cs new file mode 100644 index 00000000..8e9e292d --- /dev/null +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/GetProjectSearchQuery.cs @@ -0,0 +1,11 @@ +using GozareshgirProgramManager.Application._Common.Interfaces; + +namespace GozareshgirProgramManager.Application.Modules.Projects.Queries.GetProjectHierarchySearch; + +/// +/// درخواست جستجو در سراسر سلسله‌مراتب پروژه (پروژه، فاز، تسک). +/// نتایج با اطلاعات مسیر سلسله‌مراتب برای پشتیبانی از ناوبری درخت در رابط کاربری بازگردانده می‌شود. +/// +public record GetProjectSearchQuery( + string SearchQuery) : IBaseQuery; + \ No newline at end of file diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/GetProjectSearchQueryHandler.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/GetProjectSearchQueryHandler.cs new file mode 100644 index 00000000..e9cb3c25 --- /dev/null +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/GetProjectSearchQueryHandler.cs @@ -0,0 +1,132 @@ +using GozareshgirProgramManager.Application._Common.Interfaces; +using GozareshgirProgramManager.Application._Common.Models; +using GozareshgirProgramManager.Domain.ProjectAgg.Enums; +using Microsoft.EntityFrameworkCore; + +namespace GozareshgirProgramManager.Application.Modules.Projects.Queries.GetProjectHierarchySearch; + +/// +/// Handler برای درخواست جستجوی سراسری در سلسله‌مراتب پروژه. +/// این handler در تمام سطح‌های پروژه، فاز و تسک جستجو می‌کند و از تمام فیلدهای متنی (نام، توضیحات) استفاده می‌کند. +/// همچنین در زیرمجموعه‌های هر سطح (ProjectSections، PhaseSections، TaskSections) جستجو می‌کند. +/// +public class GetProjectSearchQueryHandler : IBaseQueryHandler +{ + private readonly IProgramManagerDbContext _context; + private const int MaxResults = 50; + + public GetProjectSearchQueryHandler(IProgramManagerDbContext context) + { + _context = context; + } + + public async Task> Handle( + GetProjectSearchQuery request, + CancellationToken cancellationToken) + { + var searchQuery = request.SearchQuery.ToLower(); + var results = new List(); + + // جستجو در پروژه‌ها و ProjectSections + var projects = await SearchProjects(searchQuery, cancellationToken); + results.AddRange(projects); + + // جستجو در فازها و PhaseSections + var phases = await SearchPhases(searchQuery, cancellationToken); + results.AddRange(phases); + + // جستجو در تسک‌ها و TaskSections + var tasks = await SearchTasks(searchQuery, cancellationToken); + results.AddRange(tasks); + + // مرتب‌سازی نتایج: ابتدا بر اساس سطح سلسله‌مراتب (پروژه → فاز → تسک)، سپس بر اساس نام + var sortedResults = results + .OrderBy(r => r.Level) + .ThenBy(r => r.Title) + .Take(MaxResults) + .ToList(); + + var response = new GetProjectSearchResponse(sortedResults); + return OperationResult.Success(response); + } + + /// + /// جستجو در جدول پروژه‌ها (نام، توضیحات) و ProjectSections (نام مهارت، توضیحات اولیه) + /// + private async Task> SearchProjects( + string searchQuery, + CancellationToken cancellationToken) + { + var projects = await _context.Projects + .Where(p => + p.Name.ToLower().Contains(searchQuery) || + (p.Description != null && p.Description.ToLower().Contains(searchQuery))) + .Select(p => new ProjectHierarchySearchResultDto + { + Id = p.Id, + Title = p.Name, + Level = ProjectHierarchyLevel.Project, + ProjectId = null, + PhaseId = null + }) + .ToListAsync(cancellationToken); + + return projects; + } + + /// + /// جستجو در جدول فازهای پروژه (نام، توضیحات) و PhaseSections + /// + private async Task> SearchPhases( + string searchQuery, + CancellationToken cancellationToken) + { + var phases = await _context.ProjectPhases + .Where(ph => + ph.Name.ToLower().Contains(searchQuery) || + (ph.Description != null && ph.Description.ToLower().Contains(searchQuery))) + .Select(ph => new ProjectHierarchySearchResultDto + { + Id = ph.Id, + Title = ph.Name, + Level = ProjectHierarchyLevel.Phase, + ProjectId = ph.ProjectId, + PhaseId = null + }) + .ToListAsync(cancellationToken); + + return phases; + } + + /// + /// جستجو در جدول تسک‌های پروژه (نام، توضیحات) و TaskSections (نام مهارت، توضیح اولیه، اطلاعات اضافی) + /// + private async Task> SearchTasks( + string searchQuery, + CancellationToken cancellationToken) + { + var tasks = await _context.ProjectTasks + .Include(t => t.Sections) + .Include(t => t.Phase) + .Where(t => + t.Name.ToLower().Contains(searchQuery) || + (t.Description != null && t.Description.ToLower().Contains(searchQuery)) || + t.Sections.Any(s => + (s.InitialDescription != null && s.InitialDescription.ToLower().Contains(searchQuery)) || + s.AdditionalTimes.Any(at => at.Reason != null && at.Reason.ToLower().Contains(searchQuery)))) + .Select(t => new ProjectHierarchySearchResultDto + { + Id = t.Id, + Title = t.Name, + Level = ProjectHierarchyLevel.Task, + ProjectId = t.Phase.ProjectId, + PhaseId = t.PhaseId + }) + .ToListAsync(cancellationToken); + + return tasks; + } + +} + + diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/GetProjectSearchQueryValidator.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/GetProjectSearchQueryValidator.cs new file mode 100644 index 00000000..70c25241 --- /dev/null +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/GetProjectSearchQueryValidator.cs @@ -0,0 +1,18 @@ +using FluentValidation; + +namespace GozareshgirProgramManager.Application.Modules.Projects.Queries.GetProjectHierarchySearch; + +/// +/// اعتبارسنج برای درخواست جستجوی سراسری +/// +public class GetProjectSearchQueryValidator : AbstractValidator +{ + public GetProjectSearchQueryValidator() + { + RuleFor(x => x.SearchQuery) + .NotEmpty().WithMessage("متن جستجو نمی‌تواند خالی باشد.") + .MinimumLength(2).WithMessage("متن جستجو باید حداقل 2 حرف باشد.") + .MaximumLength(500).WithMessage("متن جستجو نمی‌تواند بیش از 500 حرف باشد."); + } +} + diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/GetProjectSearchResponse.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/GetProjectSearchResponse.cs new file mode 100644 index 00000000..288206f7 --- /dev/null +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/GetProjectSearchResponse.cs @@ -0,0 +1,8 @@ +namespace GozareshgirProgramManager.Application.Modules.Projects.Queries.GetProjectHierarchySearch; + +/// +/// پوسته‌ی پاسخ برای نتایج جستجوی سراسری +/// +public record GetProjectSearchResponse( + List Results); + diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/ProjectHierarchySearchResultDto.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/ProjectHierarchySearchResultDto.cs new file mode 100644 index 00000000..036d3a8b --- /dev/null +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/GetProjectHierarchySearch/ProjectHierarchySearchResultDto.cs @@ -0,0 +1,36 @@ +using GozareshgirProgramManager.Domain.ProjectAgg.Enums; + +namespace GozareshgirProgramManager.Application.Modules.Projects.Queries.GetProjectHierarchySearch; + +/// +/// DTO برای نتایج جستجوی سراسری در سلسله‌مراتب پروژه. +/// حاوی اطلاعات کافی برای بازسازی مسیر سلسله‌مراتب و بسط درخت در رابط کاربری است. +/// +public record ProjectHierarchySearchResultDto +{ + /// + /// شناسه آیتم (پروژه، فاز یا تسک) + /// + public Guid Id { get; init; } + + /// + /// نام/عنوان آیتم + /// + public string Title { get; init; } = string.Empty; + + /// + /// سطح سلسله‌مراتب این آیتم + /// + public ProjectHierarchyLevel Level { get; init; } + + /// + /// شناسه پروژه - همیشه برای فاز و تسک پر شده است، برای پروژه با شناسه خود پر می‌شود + /// + public Guid? ProjectId { get; init; } + + /// + /// شناسه فاز - فقط برای تسک پر شده است، برای پروژه و فاز خالی است + /// + public Guid? PhaseId { get; init; } +} + diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/ProjectSetTimeDetails/ProjectSetTimeDetailsQueryHandler.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/ProjectSetTimeDetails/ProjectSetTimeDetailsQueryHandler.cs index c3f48bfa..aee31370 100644 --- a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/ProjectSetTimeDetails/ProjectSetTimeDetailsQueryHandler.cs +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/ProjectSetTimeDetails/ProjectSetTimeDetailsQueryHandler.cs @@ -75,14 +75,14 @@ public class ProjectSetTimeDetailsQueryHandler CreationDate = x.CreationDate.ToFarsi() }).OrderBy(x => x.CreationDate).ToList() ?? [], InitCreationTime = section?.CreationDate.ToFarsi() ?? "", - SkillName = skill?.Name ?? "", + SkillName = skill.Name ?? "", UserFullName = user?.FullName ?? "", SectionId = section?.Id ?? Guid.Empty, InitialDescription = section?.InitialDescription ?? "", InitialHours = (int)(section?.InitialEstimatedHours.TotalHours ?? 0), InitialMinutes = section?.InitialEstimatedHours.Minutes ?? 0, UserId = section?.OriginalAssignedUserId ?? 0, - SkillId = task.Id, + SkillId = skill.Id, }; }).OrderBy(x => x.SkillId).ToList(), task.Id, diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/ProjectSetTimeDetails/ProjectSetTimeDetailsQueryValidator.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/ProjectSetTimeDetails/ProjectSetTimeDetailsQueryValidator.cs index a91c0756..bd2c0dbe 100644 --- a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/ProjectSetTimeDetails/ProjectSetTimeDetailsQueryValidator.cs +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/Projects/Queries/ProjectSetTimeDetails/ProjectSetTimeDetailsQueryValidator.cs @@ -1,4 +1,4 @@ -using FluentValidation; +using FluentValidation; using GozareshgirProgramManager.Domain.ProjectAgg.Enums; namespace GozareshgirProgramManager.Application.Modules.Projects.Queries.ProjectSetTimeDetails; diff --git a/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Entities/Project.cs b/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Entities/Project.cs index dc2bfa15..8e8e2b31 100644 --- a/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Entities/Project.cs +++ b/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Entities/Project.cs @@ -74,6 +74,15 @@ public class Project : ProjectHierarchyNode } } + public void RemoveProjectSection(Guid skillId) + { + var section = _projectSections.FirstOrDefault(s => s.SkillId == skillId); + if (section != null) + { + _projectSections.Remove(section); + } + } + public void ClearProjectSections() { _projectSections.Clear(); diff --git a/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Entities/ProjectPhase.cs b/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Entities/ProjectPhase.cs index 3534c50f..60b98df9 100644 --- a/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Entities/ProjectPhase.cs +++ b/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Entities/ProjectPhase.cs @@ -87,6 +87,15 @@ public class ProjectPhase : ProjectHierarchyNode } } + public void RemovePhaseSection(Guid skillId) + { + var section = _phaseSections.FirstOrDefault(s => s.SkillId == skillId); + if (section != null) + { + _phaseSections.Remove(section); + } + } + public void ClearPhaseSections() { _phaseSections.Clear(); diff --git a/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Repositories/ITaskSectionRepository.cs b/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Repositories/ITaskSectionRepository.cs index b4a4c5dd..487991a8 100644 --- a/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Repositories/ITaskSectionRepository.cs +++ b/ProgramManager/src/Domain/GozareshgirProgramManager.Domain/ProjectAgg/Repositories/ITaskSectionRepository.cs @@ -13,4 +13,5 @@ public interface ITaskSectionRepository: IRepository Task> GetAssignedToUserAsync(long userId); Task> GetActiveSectionsIncludeAllAsync(CancellationToken cancellationToken); + Task HasUserAnyInProgressSectionAsync(long userId, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Persistence/Repositories/ProjectPhaseRepository.cs b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Persistence/Repositories/ProjectPhaseRepository.cs index 81306c82..f00d4512 100644 --- a/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Persistence/Repositories/ProjectPhaseRepository.cs +++ b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Persistence/Repositories/ProjectPhaseRepository.cs @@ -19,6 +19,7 @@ public class ProjectPhaseRepository : RepositoryBase, IProje public Task GetWithTasksAsync(Guid phaseId) { return _context.ProjectPhases + .Include(x=>x.PhaseSections) .Include(p => p.Tasks) .ThenInclude(t => t.Sections) .ThenInclude(s => s.Skill) diff --git a/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Persistence/Repositories/TaskSectionRepository.cs b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Persistence/Repositories/TaskSectionRepository.cs index 89fb05fd..1b29e154 100644 --- a/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Persistence/Repositories/TaskSectionRepository.cs +++ b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Persistence/Repositories/TaskSectionRepository.cs @@ -45,4 +45,11 @@ public class TaskSectionRepository:RepositoryBase,ITaskSection .Include(x => x.AdditionalTimes) .ToListAsync(cancellationToken); } + + public async Task HasUserAnyInProgressSectionAsync(long userId, CancellationToken cancellationToken = default) + { + return await _context.TaskSections + .AnyAsync(x => x.CurrentAssignedUserId == userId && x.Status == TaskSectionStatus.InProgress, + cancellationToken); + } } \ No newline at end of file diff --git a/ServiceHost/Areas/Admin/Controllers/ProgramManager/ProjectController.cs b/ServiceHost/Areas/Admin/Controllers/ProgramManager/ProjectController.cs index a9f0fce8..f11f5851 100644 --- a/ServiceHost/Areas/Admin/Controllers/ProgramManager/ProjectController.cs +++ b/ServiceHost/Areas/Admin/Controllers/ProgramManager/ProjectController.cs @@ -1,4 +1,4 @@ -using System.Runtime.InteropServices; +using System.Runtime.InteropServices; using GozareshgirProgramManager.Application._Common.Models; using GozareshgirProgramManager.Application.Modules.Projects.Commands.AssignProject; using GozareshgirProgramManager.Application.Modules.Projects.Commands.AutoStopOverTimeTaskSections; @@ -17,6 +17,7 @@ using GozareshgirProgramManager.Application.Modules.Projects.Queries.ProjectBoar using GozareshgirProgramManager.Application.Modules.Projects.Queries.ProjectDeployBoardDetail; using GozareshgirProgramManager.Application.Modules.Projects.Queries.ProjectDeployBoardList; using GozareshgirProgramManager.Application.Modules.Projects.Queries.ProjectSetTimeDetails; +using GozareshgirProgramManager.Application.Modules.Projects.Queries.GetProjectHierarchySearch; using MediatR; using Microsoft.AspNetCore.Mvc; using ServiceHost.BaseControllers; @@ -40,6 +41,15 @@ public class ProjectController : ProgramManagerBaseController return res; } + [HttpGet("search")] + public async Task>> Search( + [FromQuery] string query) + { + var searchQuery = new GetProjectSearchQuery(query); + var res = await _mediator.Send(searchQuery); + return res; + } + [HttpPost] public async Task> Create([FromBody] CreateProjectCommand command) {