From 1e18fe1067d58a65b16b3465b0e0b39c6febac82 Mon Sep 17 00:00:00 2001 From: MahanCh Date: Wed, 7 May 2025 22:39:09 +0330 Subject: [PATCH] changes --- .../DomainService/IRollCallDomainService.cs | 111 ++++++++++-------- .../RollCallApplication.cs | 6 + .../Pages/Company/AndroidApk/Index.cshtml.cs | 25 ++-- 3 files changed, 83 insertions(+), 59 deletions(-) diff --git a/Company.Domain/RollCallAgg/DomainService/IRollCallDomainService.cs b/Company.Domain/RollCallAgg/DomainService/IRollCallDomainService.cs index 100af86d..6905fce8 100644 --- a/Company.Domain/RollCallAgg/DomainService/IRollCallDomainService.cs +++ b/Company.Domain/RollCallAgg/DomainService/IRollCallDomainService.cs @@ -15,6 +15,7 @@ using System.Threading.Tasks; using _0_Framework.Application; using OfficeOpenXml; using OfficeOpenXml.Drawing.Chart; +using System.Collections; namespace Company.Domain.RollCallAgg.DomainService; @@ -25,7 +26,7 @@ public interface IRollCallDomainService long workshopId); TimeOnly GetEmployeeOffSetForRegularSettings(long employeeId, long workshopId); - DateTime GetEmployeeShiftDateByRollCallStartDate(long workshopId, long employeeId, DateTime rollCallStartDate,DateTime rollCallEndDate); + DateTime GetEmployeeShiftDateByRollCallStartDate(long workshopId, long employeeId, DateTime rollCallStartDate, DateTime rollCallEndDate); void CalculateTimeDifferences(RollCall rollCall); @@ -173,16 +174,16 @@ public class RollCallDomainService : IRollCallDomainService } public DateTime GetEmployeeShiftDateByRollCallStartDate(long workshopId, long employeeId, - DateTime rollCallStartDate,DateTime rollCallEndDate) + DateTime rollCallStartDate, DateTime rollCallEndDate) { var shiftDetails = GetEmployeeShiftDetails(employeeId, workshopId); - + var offset = GetEmployeeOffSetForRegularSettings(employeeId, workshopId); return shiftDetails.shiftType switch { WorkshopShiftStatus.Regular => CalculateRegularShiftDate(rollCallStartDate, offset), - WorkshopShiftStatus.Rotating => FindRotatingShift(rollCallStartDate,rollCallEndDate,shiftDetails.rotatingShifts).start.Date, + WorkshopShiftStatus.Rotating => FindRotatingShift(rollCallStartDate, rollCallEndDate, shiftDetails.rotatingShifts).start.Date, WorkshopShiftStatus.Irregular => rollCallStartDate.Date, _ => throw new ArgumentOutOfRangeException() }; @@ -256,7 +257,7 @@ public class RollCallDomainService : IRollCallDomainService var lateEntryRollCall = rollCallsInShift.OrderBy(x => x.StartDate).FirstOrDefault(x => x.StartDate > employeeShift.start); - + var previousShift = employeeShifts.OrderByDescending(x => x.start) .FirstOrDefault(x => x.end < employeeShift.start); @@ -299,7 +300,7 @@ public class RollCallDomainService : IRollCallDomainService var lateExitRollCall = rollCallsInShift.OrderBy(x => x.EndDate).FirstOrDefault(x => x.EndDate > employeeShift.end); - + // تعجیل در خروج - زود رفتن var nextShift = employeeShifts.OrderBy(x => x.start) @@ -374,7 +375,7 @@ public class RollCallDomainService : IRollCallDomainService var lateEntryRollCallRotating = rollCallsInRotatingShift.OrderBy(x => x.StartDate).FirstOrDefault(x => x.StartDate > shift.start); - + if (earlyEntryRollCallRotating != null) { @@ -405,7 +406,7 @@ public class RollCallDomainService : IRollCallDomainService var lateExitRollCallRotating = rollCallsInRotatingShift.OrderBy(x => x.EndDate).FirstOrDefault(x => x.EndDate > shift.end); - + if (earlyExitRollCallRotating != null && (rollCallsInRotatingShift.Any(x => x.StartDate < rotatingShiftEnd && x.StartDate > earlyExitRollCallRotating.EndDate) == false)) @@ -453,10 +454,10 @@ public class RollCallDomainService : IRollCallDomainService DateTime startEntryWithDate = startDate.Add(startRollCall.TimeOfDay); DateTime endEntryWithDate = endDate.Add(endRollCall.TimeOfDay); - DateTime oneHourBeforeStart = startEntryWithDate.AddHours(-1); - DateTime oneHourAfterStart = startEntryWithDate.AddHours(1); - DateTime oneHourBeforeEnd = endEntryWithDate.AddHours(-1); - DateTime oneHourAfterEnd = endEntryWithDate.AddHours(1); + DateTime twoHourBeforeStart = startEntryWithDate.AddHours(-2); + DateTime twoHourAfterStart = startEntryWithDate.AddHours(2); + DateTime twoHourBeforeEnd = endEntryWithDate.AddHours(-2); + DateTime twoHourAfterEnd = endEntryWithDate.AddHours(2); var shiftDateTimes = rotatingShifts.SelectMany(shift => @@ -477,56 +478,68 @@ public class RollCallDomainService : IRollCallDomainService #region مقایسه شروع حضور غیاب با شیفت - var startFilteredTimes = shiftDateTimes.Where(shift => - (oneHourBeforeStart <= shift.Start && oneHourAfterStart >= shift.Start) || - (oneHourBeforeStart <= shift.End && oneHourAfterStart >= shift.End)).ToList(); + //var startFilteredTimes = shiftDateTimes.Where(shift => + // (twoHourBeforeStart <= shift.Start && twoHourAfterStart >= shift.Start) || + // (twoHourBeforeStart <= shift.End && twoHourAfterStart >= shift.End)).ToList(); - if (startFilteredTimes.Count == 0) - { - startFilteredTimes = shiftDateTimes; - } - else if (startFilteredTimes.Count == 1) - { - var startChosenShift = startFilteredTimes.First(); + //if (startFilteredTimes.Count == 0) + //{ + // startFilteredTimes = shiftDateTimes; + //} + //else if (startFilteredTimes.Count == 1) + //{ + // var startChosenShift = startFilteredTimes.First(); - if (startChosenShift.End < startChosenShift.Start) - startChosenShift.End = startChosenShift.End.AddDays(1); + // if (startChosenShift.End < startChosenShift.Start) + // startChosenShift.End = startChosenShift.End.AddDays(1); - return startChosenShift; - } + // return startChosenShift; + //} - #endregion + //#endregion - #region مقایسه پایان حضورغیاب با شیفت - - var endFilteredTimes = shiftDateTimes.Where(shift => - (oneHourBeforeEnd <= shift.Start && oneHourAfterEnd >= shift.Start) || - (oneHourBeforeEnd <= shift.End && oneHourAfterEnd >= shift.End)).ToList(); - if (endFilteredTimes.Count == 0) - { - endFilteredTimes = startFilteredTimes; - } - else if (endFilteredTimes.Count == 1) - { - var endChosenShift = endFilteredTimes.First(); - return endChosenShift; - } + //#region مقایسه پایان حضورغیاب با شیفت + //var endFilteredTimes = shiftDateTimes.Where(shift => + // (twoHourBeforeEnd <= shift.Start && twoHourAfterEnd >= shift.Start) || + // (twoHourBeforeEnd <= shift.End && twoHourAfterEnd >= shift.End)).ToList(); + //if (endFilteredTimes.Count == 0) + //{ + // endFilteredTimes = startFilteredTimes; + //} + //else if (endFilteredTimes.Count == 1) + //{ + // var endChosenShift = endFilteredTimes.First(); + // return endChosenShift; + //} #endregion #region اشتراک حضور غیاب و شیفت - var overlapShifts = endFilteredTimes.Select(shift => new - { - Shift = shift, - Overlap = new TimeSpan(Math.Max(0, - Math.Min(shift.End.Ticks, oneHourAfterEnd.Ticks) - - Math.Max(shift.Start.Ticks, oneHourBeforeStart.Ticks))) - }); + var overlapShifts = shiftDateTimes + .Select(shift => new + { + Shift = shift, + Overlap = new TimeSpan(Math.Max(0, + Math.Min(shift.End.Ticks, endRollCall.Ticks) - + Math.Max(shift.Start.Ticks, startRollCall.Ticks))), + // زمان حضور فرد در شیفت (مجموع Overlap با شیفت) + TotalTimeInShift = new TimeSpan(Math.Max(0, + Math.Min(shift.End.Ticks, endRollCall.Ticks) - + Math.Max(shift.Start.Ticks, startRollCall.Ticks))), + StartDistance = Math.Abs((shift.Start - startRollCall).Ticks), + EndDistance = Math.Abs((shift.End - endRollCall).Ticks), + TotalDistance = Math.Abs((shift.Start - startRollCall).Ticks) + Math.Abs((shift.End - endRollCall).Ticks) + }) + .OrderByDescending(s => s.TotalTimeInShift) // 1. بیشترین زمان حضور فرد + .ThenByDescending(s => s.Overlap) // 2. بیشترین Overlap + .ThenBy(s=>s.TotalDistance) + .ThenBy(s => s.StartDistance) + .ThenBy(x=>x.EndDistance); // 3. اگر برابر بود، Start نزدیک‌تر - var overlapChosenShift = overlapShifts.MaxBy(s => s.Overlap); + var overlapChosenShift = overlapShifts.First(); var end = overlapChosenShift.Shift.End; if (overlapChosenShift.Shift.End < overlapChosenShift.Shift.Start) end = overlapChosenShift.Shift.End.AddDays(1); diff --git a/CompanyManagment.Application/RollCallApplication.cs b/CompanyManagment.Application/RollCallApplication.cs index 9b84101c..3206e47d 100644 --- a/CompanyManagment.Application/RollCallApplication.cs +++ b/CompanyManagment.Application/RollCallApplication.cs @@ -504,6 +504,12 @@ public class RollCallApplication : IRollCallApplication _rollCallRepository.AddRange(rollCallsAsEntityModels); _rollCallRepository.SaveChanges(); foreach (var rollCallsAsEntityModel in rollCallsAsEntityModels) + { + rollCallsAsEntityModel.ClearTimeDiff(); + rollCallsAsEntityModel.SetShiftDate(_rollCallDomainService); + } + _rollCallRepository.SaveChanges(); + foreach (var rollCallsAsEntityModel in rollCallsAsEntityModels) { rollCallsAsEntityModel.Edit(rollCallsAsEntityModel.StartDate.Value, rollCallsAsEntityModel.EndDate.Value, _rollCallDomainService); } diff --git a/ServiceHost/Areas/AdminNew/Pages/Company/AndroidApk/Index.cshtml.cs b/ServiceHost/Areas/AdminNew/Pages/Company/AndroidApk/Index.cshtml.cs index 289d337f..dbeced5e 100644 --- a/ServiceHost/Areas/AdminNew/Pages/Company/AndroidApk/Index.cshtml.cs +++ b/ServiceHost/Areas/AdminNew/Pages/Company/AndroidApk/Index.cshtml.cs @@ -59,9 +59,9 @@ namespace ServiceHost.Areas.AdminNew.Pages.Company.AndroidApk public IActionResult OnPostShiftDateNew() { - var startRollCall = new DateTime(2025, 3, 21); - var rollCalls = _context.RollCalls.Where(x => x.ShiftDate >= startRollCall && x.EndDate != null).ToList(); - var r1 = rollCalls.Take(10000).ToList(); + var startRollCall = new DateTime(2025, 4, 21); + var rollCalls = _context.RollCalls.Where(x => x.ShiftDate >= startRollCall && x.EndDate != null && x.WorkshopId == 344).ToList(); + var r1 = rollCalls.ToList(); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("endStep 1 ============"); @@ -82,9 +82,9 @@ namespace ServiceHost.Areas.AdminNew.Pages.Company.AndroidApk //Console.WriteLine("endStep 1 ============"); //SetRollCall(r1); - await RefactorEmployeeDocumentItem(); - await ChangeIsConfirmed(); - await RemoveEmployeeClientTemps(); + await RefactorEmployeeDocumentItem(); + await ChangeIsConfirmed(); + await RemoveEmployeeClientTemps(); ViewData["message"] = "تومام دو"; return Page(); @@ -96,7 +96,7 @@ namespace ServiceHost.Areas.AdminNew.Pages.Company.AndroidApk .Include(x => x.EmployeeDocumentItemCollection) .Where(x => x.IsConfirmed); - var employeeClientTemps = await _context.EmployeeClientTemps.Where(x=>employeeDocuments.Any(a=>a.WorkshopId == x.WorkshopId && a.EmployeeId == x.EmployeeId)).ToListAsync(); + var employeeClientTemps = await _context.EmployeeClientTemps.Where(x => employeeDocuments.Any(a => a.WorkshopId == x.WorkshopId && a.EmployeeId == x.EmployeeId)).ToListAsync(); foreach (var employeeClientTemp in employeeClientTemps) { @@ -108,7 +108,7 @@ namespace ServiceHost.Areas.AdminNew.Pages.Company.AndroidApk private async System.Threading.Tasks.Task ChangeIsConfirmed() { - var employeeDocuments = await _context.EmployeeDocuments.Include(x=>x.EmployeeDocumentItemCollection).ToListAsync(); + var employeeDocuments = await _context.EmployeeDocuments.Include(x => x.EmployeeDocumentItemCollection).ToListAsync(); foreach (var employeeDocument in employeeDocuments) { employeeDocument.UpdateRequiredItemsSubmittedByClient(); @@ -294,14 +294,19 @@ namespace ServiceHost.Areas.AdminNew.Pages.Company.AndroidApk var endedRollCalls2 = r1.Where(x => x.EndDate != null).ToList(); var countSetTDRollCall = endedRollCalls2.Count; var stepSetTDRollCal = 1; + foreach (var rollCall in endedRollCalls2) + { + rollCall.ClearTimeDiff(); + } + _context.SaveChanges(); foreach (var endedRollCall in endedRollCalls2) { - endedRollCall.SetShiftDate(_rollCallDomainService); + endedRollCall.Edit(endedRollCall.StartDate.Value, endedRollCall.EndDate.Value, _rollCallDomainService); Console.WriteLine($"{stepSetTDRollCal} - {countSetTDRollCall} ended Set Time Differences{endedRollCall.id}"); stepSetTDRollCal += 1; } - _context.SaveChanges(); + }