302 lines
12 KiB
C#
302 lines
12 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()
|
|
{
|
|
var today = DateTime.Today;
|
|
var fileAlertsVM = new List<FileAlertViewModel>();
|
|
|
|
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();
|
|
|
|
var files = await _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)
|
|
).ToListAsync();
|
|
|
|
var fileWithState = files.Select(x =>
|
|
{
|
|
if (x.id == 74)
|
|
{
|
|
|
|
}
|
|
var state = GetFileState(x);
|
|
return new
|
|
{
|
|
File = x,
|
|
State = state,
|
|
StateDate = GetFileStateDate(x, state)
|
|
};
|
|
}).Where(x => x.StateDate > today).ToList();
|
|
|
|
|
|
foreach (var file in fileWithState)
|
|
{
|
|
if (file.File.FileAlertsList.Count == 0)
|
|
{
|
|
var dueDate = file.StateDate + TimeSpan.FromDays(fileStates.Where(x => x.State == file.State).FirstOrDefault().Deadline);
|
|
var workingDaysDifference = Tools.GetWorkingDaysDifference(today, dueDate);
|
|
|
|
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
|
|
};
|
|
if (workingDaysDifference < 0)
|
|
fileAlert.IsExpired = true;
|
|
|
|
fileAlertsVM.Add(fileAlert);
|
|
}
|
|
}
|
|
|
|
else if (file.File.FileAlertsList.Count == 1)
|
|
{
|
|
var dueDate = file.StateDate + TimeSpan.FromDays(fileStates.Where(x => x.State == file.State).FirstOrDefault().Deadline);
|
|
var workingDaysDifference = Tools.GetWorkingDaysDifference(today, dueDate);
|
|
|
|
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
|
|
};
|
|
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.Where(x => x.State == file.State).FirstOrDefault().Deadline) + TimeSpan.FromDays(totalAdditionalDeadline);
|
|
var workingDaysDifference = Tools.GetWorkingDaysDifference(today, dueDate);
|
|
|
|
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
|
|
};
|
|
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
|
|
|
|
} |