refactor: optimize institution contract list retrieval and improve workshop group loading

This commit is contained in:
2025-12-25 14:33:00 +03:30
parent ad4b0be033
commit 649242fc76

View File

@@ -1098,19 +1098,27 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
public async Task<PagedResult<GetInstitutionContractListItemsViewModel>> GetList(
InstitutionContractListSearchModel searchModel)
{
var query = _context.InstitutionContractSet
.Include(x => x.WorkshopGroup)
.ThenInclude(x => x.InitialWorkshops)
.Include(x => x.WorkshopGroup)
.ThenInclude(x => x.CurrentWorkshops)
.Include(x => x.ContactInfoList).AsNoTracking();
var now = DateTime.Today;
var nowFa = now.ToFarsi();
var endFa = nowFa.FindeEndOfMonth();
var endThisMontGr = endFa.ToGeorgianDateTime();
var joinedQuery = query.Join(_context.PersonalContractingParties
var contractsWithExtension = await _context.InstitutionContractSet
.Where(x => x.IsActiveString == "true")
.Select(x => new { x.ContractingPartyId, x.ExtensionNo })
.ToListAsync();
var contractsWithExtensionLookup = contractsWithExtension
.GroupBy(x => x.ContractingPartyId)
.ToDictionary(
g => g.Key,
g => g.Max(x => x.ExtensionNo)
);
var joinedQuery = _context.InstitutionContractSet
.AsNoTracking()
.Join(_context.PersonalContractingParties
.AsNoTracking()
.Include(x => x.Employers)
.ThenInclude(x => x.WorkshopEmployers)
.ThenInclude(x => x.Workshop),
@@ -1138,7 +1146,8 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
: x.contract.ContractAmount == 0
? (int)InstitutionContractListStatus.Free
: !x.contractingParty.Employers
.SelectMany(e => e.WorkshopEmployers.Select(we => we.Workshop)).Any()
.SelectMany(e => e.WorkshopEmployers
.Select(we => we.Workshop)).Any()
? (int)InstitutionContractListStatus.WithoutWorkshop
: (int)InstitutionContractListStatus.Active
});
@@ -1301,6 +1310,15 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
var list = await orderedQuery.ApplyPagination(searchModel.PageIndex, searchModel.PageSize).ToListAsync();
var contractingPartyIds = list.Select(x => x.contractingParty.id).ToList();
var contractIds = list.Select(x => x.contract.id).ToList();
// بارگذاری WorkshopGroups فقط برای قراردادهای صفحه فعلی
var workshopGroups = await _context.InstitutionContractWorkshopGroups
.AsNoTracking()
.Include(x=>x.InitialWorkshops )
.Include(x => x.CurrentWorkshops)
.Where(x => contractIds.Contains(x.InstitutionContractId))
.ToDictionaryAsync(x => x.InstitutionContractId, x => x);
var financialStatements = _context.FinancialStatments.Include(x => x.FinancialTransactionList)
.Where(x => contractingPartyIds.Contains(x.ContractingPartyId)).ToList();
@@ -1324,13 +1342,19 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
var archiveCode = minArchiveCode == 10000000 ? 0 : minArchiveCode;
var status = Enum.Parse<InstitutionContractListStatus>(x.StatusPriority.ToString());
List<InstitutionContractWorkshopBase> currentStateWorkshops = x.contract.WorkshopGroup?.CurrentWorkshops
// دریافت WorkshopGroup از dictionary بارگذاری شده
workshopGroups.TryGetValue(x.contract.id, out var workshopGroup);
List<InstitutionContractWorkshopBase> currentStateWorkshops = workshopGroup?.CurrentWorkshops
.Cast<InstitutionContractWorkshopBase>().ToList();
var statement = financialStatements.FirstOrDefault(f => f.ContractingPartyId == x.contractingParty.id);
currentStateWorkshops?.AddRange(
x.contract.WorkshopGroup?.InitialWorkshops.Where(w => !w.WorkshopCreated) ?? []);
if (currentStateWorkshops != null && workshopGroup != null)
{
currentStateWorkshops.AddRange(workshopGroup.InitialWorkshops.Where(w => !w.WorkshopCreated));
}
var workshopDetails = currentStateWorkshops?.Select(w =>
{
@@ -1380,7 +1404,7 @@ public class InstitutionContractRepository : RepositoryBase<long, InstitutionCon
IsExpired = x.contract.ContractEndGr <= endThisMontGr,
ContractingPartyId = x.contractingParty.id,
Workshops = workshopDetails,
IsInPersonContract = x.contract.WorkshopGroup?.CurrentWorkshops
IsInPersonContract = workshopGroup?.CurrentWorkshops
.Any(y => y.Services.ContractInPerson) ?? true,
IsOldContract = x.contract.SigningType == InstitutionContractSigningType.Legacy
};