382 lines
16 KiB
C#
382 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using _0_Framework.Application;
|
|
using _0_Framework_b.InfraStructure;
|
|
using Company.Domain.File1;
|
|
using Company.Domain.FileAlert;
|
|
using CompanyManagment.App.Contracts.File1;
|
|
using CompanyManagment.App.Contracts.FileAlert;
|
|
using CompanyManagment.App.Contracts.FileState;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
namespace CompanyManagment.EFCore.Repository;
|
|
|
|
public class FileAlertRepository : RepositoryBase<long, FileAlert>, IFileAlertRepository
|
|
{
|
|
private readonly CompanyContext _context;
|
|
public FileAlertRepository(CompanyContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public FileAlertViewModel GetDetails(long id)
|
|
{
|
|
return _context.FileAlerts.Select(x => new FileAlertViewModel
|
|
{
|
|
Id = x.id,
|
|
File_Id = x.File_Id,
|
|
FileState_Id = x.FileState_Id,
|
|
AdditionalDeadline = x.AdditionalDeadline,
|
|
}).FirstOrDefault(x => x.Id == id);
|
|
}
|
|
|
|
public void Remove(long id)
|
|
{
|
|
var fileAlert = _context.FileAlerts.Where(x => x.id == id).FirstOrDefault();
|
|
|
|
Remove(fileAlert);
|
|
}
|
|
|
|
public List<EditFileAlert> Search(FileAlertSearchModel searchModel)
|
|
{
|
|
var query = _context.FileAlerts.Select(x => new EditFileAlert
|
|
{
|
|
Id = x.id,
|
|
File_Id = x.File_Id,
|
|
FileState_Id = x.FileState_Id,
|
|
AdditionalDeadline = x.AdditionalDeadline
|
|
});
|
|
|
|
//TODO if
|
|
if (searchModel.FileState_Id != 0)
|
|
{
|
|
query = query.Where(x => x.FileState_Id == searchModel.FileState_Id);
|
|
}
|
|
|
|
if (searchModel.File_Id != 0)
|
|
{
|
|
query = query.Where(x => x.File_Id == searchModel.File_Id);
|
|
}
|
|
|
|
return query.OrderByDescending(x => x.Id).ToList();
|
|
}
|
|
|
|
#region Mahan
|
|
public async Task<List<FileAlertViewModel>> GetFileAlerts(FileAlertSearchModel searchModel)
|
|
{
|
|
var today = DateTime.Today;
|
|
var fileAlertsVM = new List<FileAlertViewModel>();
|
|
|
|
|
|
|
|
var filesQuery = _context.Files.Where(x => x.Status == FileEnums.ACTIVE)
|
|
.Include(x => x.BoardsList).ThenInclude(x => x.ProceedingSessionsList)
|
|
.Include(x => x.PetitionsList)
|
|
.Where(file =>
|
|
file.FileClass == null
|
|
|| file.HasMandate != 2
|
|
|| (file.BoardsList.Any(a => a.BoardType_Id == 1 && a.DisputeResolutionPetitionDate == new DateTime()))
|
|
|| (file.BoardsList.Any(a =>
|
|
a.BoardType_Id == 1 && a.DisputeResolutionPetitionDate != new DateTime() &&
|
|
a.ProceedingSessionsList.Count == 0))
|
|
|| (file.BoardsList.Any(a => a.BoardType_Id == 1 && a.ProceedingSessionsList.Count != 0) &&
|
|
(file.PetitionsList.Any(x => x.BoardType_Id == 1) == false))
|
|
|| (file.PetitionsList.Any(x => x.BoardType_Id == 1) && file.BoardsList.Any(a =>
|
|
a.BoardType_Id == 2 && a.DisputeResolutionPetitionDate == new DateTime()))
|
|
|| (file.BoardsList.Any(a =>
|
|
a.BoardType_Id == 2 && a.DisputeResolutionPetitionDate != new DateTime() &&
|
|
a.ProceedingSessionsList.Count == 0))
|
|
|| (file.PetitionsList.Any(x => x.BoardType_Id == 2) == false)
|
|
);
|
|
if (!string.IsNullOrWhiteSpace(searchModel.ArchiveNo))
|
|
{
|
|
var archiveNo = Convert.ToInt64(searchModel.ArchiveNo);
|
|
filesQuery = filesQuery.Where(x => x.ArchiveNo == archiveNo);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(searchModel.FileClass))
|
|
{
|
|
filesQuery = filesQuery.Where(x => x.FileClass == searchModel.FileClass);
|
|
}
|
|
|
|
if (searchModel.UserId > 0)
|
|
{
|
|
filesQuery = filesQuery.Where(x =>
|
|
(x.Client == 1 && x.Reqester == searchModel.UserId) ||
|
|
(x.Client == 2 && x.Summoned == searchModel.UserId));
|
|
}
|
|
|
|
var files = await filesQuery.ToListAsync();
|
|
|
|
var fileWithState = files.Select(x =>
|
|
{
|
|
var state = GetFileState(x);
|
|
return new
|
|
{
|
|
File = x,
|
|
State = state,
|
|
StateDate = GetFileStateDate(x, state)
|
|
};
|
|
}).ToList();
|
|
|
|
if (searchModel.FileState_Id>0)
|
|
{
|
|
fileWithState =fileWithState.Where(x => x.State == searchModel.FileState_Id).ToList();
|
|
}
|
|
|
|
fileWithState = fileWithState.Where(x => x.StateDate < today).ToList();
|
|
|
|
var requesterIds = fileWithState.Select(x => x.File.Reqester);
|
|
var summonedIds = fileWithState.Select(x => x.File.Summoned);
|
|
|
|
var requesterEmployees = await _context.Employees.Where(x => requesterIds.Contains(x.id)).ToListAsync();
|
|
var summonedEmployers = await _context.Employers.Where(x => summonedIds.Contains(x.id)).ToListAsync();
|
|
|
|
var fileStates = await _context.FileStates.Include(x => x.FileTiming).Select(x => new
|
|
{
|
|
Id = x.id,
|
|
x.FileTiming_Id,
|
|
x.State,
|
|
x.Title,
|
|
x.FileTiming.Deadline
|
|
}).ToListAsync();
|
|
foreach (var file in fileWithState)
|
|
{
|
|
var clientFullName = file.File.Client == 1
|
|
? requesterEmployees.FirstOrDefault(x => x.id == file.File.Reqester)?.FullName
|
|
: summonedEmployers.FirstOrDefault(x => x.id == file.File.Summoned)?.FullName ?? "-";
|
|
|
|
var oppositePersonFullName = file.File.Client == 2
|
|
? requesterEmployees.FirstOrDefault(x => x.id == file.File.Reqester)?.FullName
|
|
: summonedEmployers.FirstOrDefault(x => x.id == file.File.Summoned)?.FullName ?? "-";
|
|
|
|
if (file.File.FileAlertsList == null || file.File.FileAlertsList.Count == 0)
|
|
{
|
|
var dueDate = file.StateDate + TimeSpan.FromDays(fileStates.FirstOrDefault(x => x.State == file.State).Deadline);
|
|
var workingDaysDifference = GetDaysWorkingDays(today, dueDate.Value);
|
|
|
|
if (workingDaysDifference <= 1)
|
|
{
|
|
var fileAlertEntity = new FileAlert(file.File.id, file.State, 0);
|
|
Create(fileAlertEntity);
|
|
SaveChanges();
|
|
var fileState = fileStates.FirstOrDefault(x => x.Id == file.State);
|
|
|
|
var fileAlert = new FileAlertViewModel()
|
|
{
|
|
AdditionalDeadline = fileAlertEntity.AdditionalDeadline,
|
|
FileState = new FileStateViewModel()
|
|
{
|
|
State = fileState?.State ?? 0,
|
|
Id = fileState.Id,
|
|
FileTiming_Id = fileState.FileTiming_Id,
|
|
Title = fileState.Title
|
|
},
|
|
Id = fileAlertEntity.id,
|
|
FileState_Id = fileAlertEntity.FileState_Id,
|
|
File_Id = fileAlertEntity.File_Id,
|
|
IsExpired = false,
|
|
File = new CreateFile()
|
|
{
|
|
FileClass = file.File.FileClass,
|
|
ArchiveNo = file.File.ArchiveNo,
|
|
ClientFullName = clientFullName,
|
|
OppositePersonFullName = oppositePersonFullName,
|
|
}
|
|
};
|
|
if (workingDaysDifference < 0)
|
|
fileAlert.IsExpired = true;
|
|
|
|
fileAlertsVM.Add(fileAlert);
|
|
}
|
|
}
|
|
|
|
else if (file.File.FileAlertsList.Count == 1)
|
|
{
|
|
var dueDate = file.StateDate + TimeSpan.FromDays(fileStates.FirstOrDefault(x => x.State == file.State).Deadline);
|
|
var workingDaysDifference = GetDaysWorkingDays(today, dueDate.Value);
|
|
|
|
if (workingDaysDifference <= 1)
|
|
{
|
|
var fileAlertEntity = file.File.FileAlertsList.First();
|
|
var fileState = fileStates.FirstOrDefault(x => x.Id == file.State);
|
|
|
|
var fileAlert = new FileAlertViewModel()
|
|
{
|
|
AdditionalDeadline = fileAlertEntity.AdditionalDeadline,
|
|
FileState = new FileStateViewModel()
|
|
{
|
|
State = fileState?.State ?? 0,
|
|
Id = fileState.Id,
|
|
FileTiming_Id = fileState.FileTiming_Id,
|
|
Title = fileState.Title
|
|
},
|
|
Id = fileAlertEntity.id,
|
|
FileState_Id = fileAlertEntity.FileState_Id,
|
|
File_Id = fileAlertEntity.File_Id,
|
|
IsExpired = false,
|
|
File = new CreateFile()
|
|
{
|
|
FileClass = file.File.FileClass,
|
|
ArchiveNo = file.File.ArchiveNo,
|
|
ClientFullName = clientFullName,
|
|
OppositePersonFullName = oppositePersonFullName,
|
|
}
|
|
};
|
|
if (workingDaysDifference < 0)
|
|
fileAlert.IsExpired = true;
|
|
|
|
fileAlertsVM.Add(fileAlert);
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
var totalAdditionalDeadline = file.File.FileAlertsList.Sum(x => x.AdditionalDeadline);
|
|
var dueDate = file.StateDate + TimeSpan.FromDays(fileStates.FirstOrDefault(x => x.State == file.State).Deadline) + TimeSpan.FromDays(totalAdditionalDeadline);
|
|
var workingDaysDifference = GetDaysWorkingDays(today, dueDate.Value); ;
|
|
|
|
if (workingDaysDifference <= 1)
|
|
{
|
|
var fileAlertEntity = file.File.FileAlertsList.Last();
|
|
|
|
var fileState = fileStates.FirstOrDefault(x => x.Id == file.State);
|
|
|
|
var fileAlert = new FileAlertViewModel()
|
|
{
|
|
AdditionalDeadline = fileAlertEntity.AdditionalDeadline,
|
|
FileState = new FileStateViewModel()
|
|
{
|
|
State = fileState?.State ?? 0,
|
|
Id = fileState.Id,
|
|
FileTiming_Id = fileState.FileTiming_Id,
|
|
Title = fileState.Title
|
|
},
|
|
Id = fileAlertEntity.id,
|
|
FileState_Id = fileAlertEntity.FileState_Id,
|
|
File_Id = fileAlertEntity.File_Id,
|
|
IsExpired = false,
|
|
File = new CreateFile()
|
|
{
|
|
FileClass = file.File.FileClass,
|
|
ArchiveNo = file.File.ArchiveNo,
|
|
ClientFullName = clientFullName,
|
|
OppositePersonFullName = oppositePersonFullName,
|
|
}
|
|
};
|
|
if (workingDaysDifference < 0)
|
|
fileAlert.IsExpired = true;
|
|
|
|
fileAlertsVM.Add(fileAlert);
|
|
}
|
|
}
|
|
}
|
|
|
|
return fileAlertsVM;
|
|
}
|
|
public int GetFileState(File1 file)
|
|
{
|
|
//if (file.FileClass == null || (file.FileClass != null && file.DiagnosisBoard.DisputeResolutionPetitionDate == null))
|
|
if (file.FileClass == null)
|
|
return FileStateEnums.FILE_CLASS_NOT_REGISTERED;
|
|
|
|
//if (file.HasMandate != 2 || (file.HasMandate == 2 && file.DiagnosisBoard.DisputeResolutionPetitionDate == null))
|
|
if (file.HasMandate != 2)
|
|
return FileStateEnums.MANDATE_NOT_REGISTERED;
|
|
|
|
if ((file.BoardsList.Any(a => a.BoardType_Id == 1 && a.DisputeResolutionPetitionDate == new DateTime())))
|
|
return FileStateEnums.NO_PETITION_DATE_ISSUED;
|
|
|
|
if ((file.BoardsList.Any(a =>
|
|
a.BoardType_Id == 1 && a.DisputeResolutionPetitionDate != new DateTime() &&
|
|
a.ProceedingSessionsList.Count == 1)))
|
|
return FileStateEnums.NO_DIAGNOSIS_INVITATION_ISSUED;
|
|
|
|
if ((file.BoardsList.Any(a => a.BoardType_Id == 1 && a.ProceedingSessionsList.Count > 1) &&
|
|
(file.PetitionsList.Any(x => x.BoardType_Id == 1) == false)))
|
|
return FileStateEnums.NO_DIAGNOSIS_PETITION_ISSUED;
|
|
|
|
if ((file.PetitionsList.Any(x => x.BoardType_Id == 1) && file.BoardsList.Any(a =>
|
|
a.BoardType_Id == 2 && a.DisputeResolutionPetitionDate == new DateTime()) || file.BoardsList.Any(a =>
|
|
a.BoardType_Id == 2) == false))
|
|
return FileStateEnums.PROTEST_NOT_REGISTERED;
|
|
|
|
if ((file.BoardsList.Any(a =>
|
|
a.BoardType_Id == 2 && a.DisputeResolutionPetitionDate != new DateTime() &&
|
|
a.ProceedingSessionsList.Count == 1)))
|
|
return FileStateEnums.NO_DISPUTE_INVITATION_ISSUED;
|
|
|
|
if ((file.PetitionsList.Any(x => x.BoardType_Id == 2) == false))
|
|
return FileStateEnums.NO_DISPUTE_PETITION_ISSUED;
|
|
|
|
return 0;
|
|
}
|
|
public DateTime? GetFileStateDate(File1 file, int state)
|
|
{
|
|
var diagnosisBoard = file.BoardsList.FirstOrDefault(x => x.BoardType_Id == 1);
|
|
var diagnosisPetition = file.PetitionsList.FirstOrDefault(x => x.BoardType_Id == 1);
|
|
var disputeResolutionBoard = file.BoardsList.FirstOrDefault(x => x.BoardType_Id == 2);
|
|
switch (state)
|
|
{
|
|
case FileStateEnums.FILE_CLASS_NOT_REGISTERED:
|
|
return file.ClientVisitDate;
|
|
|
|
case FileStateEnums.MANDATE_NOT_REGISTERED:
|
|
return file.ClientVisitDate;
|
|
|
|
case FileStateEnums.NO_PETITION_DATE_ISSUED:
|
|
return file.ClientVisitDate;
|
|
|
|
case FileStateEnums.NO_DIAGNOSIS_INVITATION_ISSUED:
|
|
return diagnosisBoard?.DisputeResolutionPetitionDate;
|
|
|
|
case FileStateEnums.NO_DIAGNOSIS_PETITION_ISSUED:
|
|
var lastDiagnosisPs = file.BoardsList.First(x => x.BoardType_Id == 1).ProceedingSessionsList.LastOrDefault();
|
|
|
|
return lastDiagnosisPs.Date;
|
|
|
|
case FileStateEnums.PROTEST_NOT_REGISTERED:
|
|
return diagnosisPetition?
|
|
.NotificationPetitionDate;
|
|
|
|
case FileStateEnums.NO_DISPUTE_INVITATION_ISSUED:
|
|
return disputeResolutionBoard?.DisputeResolutionPetitionDate;
|
|
|
|
case FileStateEnums.NO_DISPUTE_PETITION_ISSUED:
|
|
var lastDisputeResolutionPs = file.BoardsList.First(x => x.BoardType_Id == 2).ProceedingSessionsList.LastOrDefault();
|
|
return lastDisputeResolutionPs?.Date;
|
|
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
public int GetDaysWorkingDays(DateTime startDate, DateTime endDate)
|
|
{
|
|
int sign = startDate <= endDate ? 1 : -1;
|
|
|
|
DateTime from = startDate <= endDate ? startDate : endDate;
|
|
DateTime to = startDate <= endDate ? endDate : startDate;
|
|
|
|
int totalDays = (to - from).Days + 1;
|
|
int fullWeeks = totalDays / 7;
|
|
int remainingDays = totalDays % 7;
|
|
|
|
int fridays = fullWeeks;
|
|
|
|
for (int i = 0; i < remainingDays; i++)
|
|
{
|
|
var currentDay = from.AddDays(i);
|
|
if (currentDay.DayOfWeek == DayOfWeek.Friday)
|
|
fridays++;
|
|
}
|
|
|
|
var holidays = _context.HolidayItems.Count(x => x.Holidaydate >= from && x.Holidaydate <= to);
|
|
|
|
return (totalDays - fridays - holidays) * sign;
|
|
}
|
|
} |