From e4fe2389a4b89b3ea78e9690316f3e12a3e99dd1 Mon Sep 17 00:00:00 2001 From: alireza Date: Wed, 10 Jun 2026 20:17:46 +0330 Subject: [PATCH] feat: add unit filtering and improve unit info handling in charge cycles - Add optional UnitId parameter to CostCyclsList method for filtering cycles by specific unit - Replace UnitIds/UnitNames lists with structured UnitInfoDto containing Id and Name - Clean unit names by removing "GUID#" prefix when present - Update CostCycleDto to use UnitInfoDto list instead of separate Id and Name lists - Fix unit assignment logic in Add and Edit methods to use new DTO structure --- .../Services/Basic/ChargeCycleService.cs | 47 ++++++++++++++----- .../Services/Unit/UnitService.cs | 2 +- .../Controllers/ManageCostController.cs | 5 +- 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/Complex.Application/Services/Basic/ChargeCycleService.cs b/Complex.Application/Services/Basic/ChargeCycleService.cs index 7b74c20..784d8aa 100644 --- a/Complex.Application/Services/Basic/ChargeCycleService.cs +++ b/Complex.Application/Services/Basic/ChargeCycleService.cs @@ -52,7 +52,7 @@ namespace Complex.Application.Services.Basic /// /// public ResultDto> CostCyclsList(Guid ComplexId, DateOnly? StartDate = null, - DateOnly? EndDate = null, int? IncomeCostTitleId = null) + DateOnly? EndDate = null, int? IncomeCostTitleId = null, Guid? UnitId = null) { if (IncomeCostTitleId.HasValue && StartDate.HasValue) StartDate = GetStartOfMonth(StartDate.Value); @@ -70,6 +70,9 @@ namespace Complex.Application.Services.Basic else/*مدیریت شارژ ها فقط*/ ChargeCycleQuery = ChargeCycleQuery.Where(c => c.IncomeCostTitleId == null); + if (UnitId.HasValue) + ChargeCycleQuery = ChargeCycleQuery.Where(c => c.Units == null || c.Units.Any(u => u.Id == UnitId.Value)); + var ChargeCycleList = ChargeCycleQuery.ToList().Select(c => new CostCycleDto { ComplexId = c.ComplexId, @@ -80,8 +83,18 @@ namespace Complex.Application.Services.Basic StartDate = c.StartDate, Id = c.Id, Title = c.Title, - UnitIds = c.Units?.Select(u => u.Id).ToList(), - UnitNames = c.Units?.Select(u => u.Id + "#" + u.UnitName).ToList(), + Units = c.Units?.Select(u => + { + var name = u.UnitName ?? ""; + // Some units store "GUID#Name" format; extract just the name part + var hashIndex = name.IndexOf('#'); + var cleanName = hashIndex >= 0 ? name[(hashIndex + 1)..] : name; + return new UnitInfoDto + { + Id = u.Id, + Name = cleanName + }; + }).ToList(), IncomeCostTitleId = c.IncomeCostTitleId, IncomeCostTitle = c.IncomeCostTitle?.Title }).ToList(); @@ -107,9 +120,12 @@ namespace Complex.Application.Services.Basic cycle = _mapper.Map(costCycleDto); cycle.IncomeCostTitleId = costCycleDto.IncomeCostTitleId; - if (costCycleDto.UnitIds != null) - cycle.Units = _complexDBContext.Units.Where(u => costCycleDto.UnitIds.Contains(u.Id) + if (costCycleDto.Units != null) + { + var unitIds = costCycleDto.Units.Select(u => u.Id).ToList(); + cycle.Units = _complexDBContext.Units.Where(u => unitIds.Contains(u.Id) && u.ComplexId == costCycleDto.ComplexId).ToList(); + } foreach (var u in cycle.Units) { @@ -139,8 +155,11 @@ namespace Complex.Application.Services.Basic costCycleDto.StartDate = GetStartOfMonth(costCycleDto.StartDate); var newCycle = _mapper.Map(costCycleDto); - if (costCycleDto.UnitIds != null) - newCycle.Units = _complexDBContext.Units.Where(u => costCycleDto.UnitIds.Contains(u.Id) && costCycleDto.ComplexId == u.ComplexId).ToList(); + if (costCycleDto.Units != null) + { + var unitIds = costCycleDto.Units.Select(u => u.Id).ToList(); + newCycle.Units = _complexDBContext.Units.Where(u => unitIds.Contains(u.Id) && costCycleDto.ComplexId == u.ComplexId).ToList(); + } //if(newCycle.Units==null) //{ @@ -381,6 +400,12 @@ namespace Complex.Application.Services.Basic public string? CostTitle { get; set; } public DateOnly? PaymentDate { set; get; } } + public class UnitInfoDto + { + public Guid Id { set; get; } + public string Name { set; get; } + } + public class CostCycleDto { public Guid Id { set; get; } @@ -400,13 +425,9 @@ namespace Complex.Application.Services.Basic /// /// اگر برای واحدهای خاصی بود /// + public virtual List? Units { set; get; } /// - /// اگر برای واحدهای خاصی بود - /// - public virtual List? UnitIds { set; get; } - public virtual List? UnitNames { set; get; } - /// - /// null یعنی شارژ + /// null یعنی شارژ /// public int? IncomeCostTitleId { set; get; } public string? IncomeCostTitle { set; get; } diff --git a/Complex.Application/Services/Unit/UnitService.cs b/Complex.Application/Services/Unit/UnitService.cs index 01b4334..288ad16 100644 --- a/Complex.Application/Services/Unit/UnitService.cs +++ b/Complex.Application/Services/Unit/UnitService.cs @@ -149,7 +149,7 @@ namespace Complex.Application.Complex //وقتی واحد تازه ثبت شده پس در مدیریت شارژ قابلیت ثبت اختصاصی برای خودش نداشته پس باید شارژ عمومی برایش تعریف شود // اگر خواست میتواند برایش به صورت اختصاصی هم ثبت کند که اون موقع برایش محاسبه مجدد خواهد شد var costCycleDto = _costService.CostCyclsList(unit.ComplexId, nowDate, nowDate, null).Data - .FirstOrDefault(c => c.UnitIds == null); + .FirstOrDefault(c => c.Units == null); if (costCycleDto != null) { //var costCycle = new CostCycle diff --git a/Complex.EndPoint/Controllers/ManageCostController.cs b/Complex.EndPoint/Controllers/ManageCostController.cs index 2c148b6..e7fd1a5 100644 --- a/Complex.EndPoint/Controllers/ManageCostController.cs +++ b/Complex.EndPoint/Controllers/ManageCostController.cs @@ -41,11 +41,12 @@ namespace Complex.EndPoint.Controllers /// /// [HttpPost] - public ResultDto> CostCyclsList([FromForm] Guid ComplexId, [FromForm] DateTime? StartDate = null, [FromForm] DateTime? EndDate = null) + public ResultDto> CostCyclsList([FromForm] Guid ComplexId, [FromForm] DateTime? StartDate = null, [FromForm] DateTime? EndDate = null, [FromForm] Guid? UnitId = null) { return _costCycleService.CostCyclsList(ComplexId, !StartDate.HasValue ? null : DateOnly.FromDateTime(StartDate.Value), - !EndDate.HasValue ? null : DateOnly.FromDateTime(EndDate.Value)); + !EndDate.HasValue ? null : DateOnly.FromDateTime(EndDate.Value), + UnitId: UnitId); } /// /// بدهی یک واحد