Files
BeWoPlaner/Service/ServiceUtils/AiContextLoader.cs

154 lines
6.7 KiB
C#
Raw Normal View History

2025-09-12 14:58:41 +02:00
using AICore.Context.Summary;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.ServiceImplementations;
using BS.Shared.DataContracts.Compact;
using BS.Shared.DataContracts;
using BS.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2025-10-22 11:45:15 +02:00
using AICore.Context.Core;
2025-09-12 14:58:41 +02:00
namespace BeWo.Service.ServiceUtils
{
public static class AiContextLoader
{
public static BaseContextSummary LoadContext(AiContextType uicontext, Dictionary<TableID, long[]> context_reference)
{
switch (uicontext)
{
case AiContextType.SupportConceptNavigation:
case AiContextType.CustomerNavigation:
case AiContextType.PersonNavigation:
case AiContextType.EmployeeNavigation:
case AiContextType.OrganisationNavigation:
return loadNavigationContext(uicontext, context_reference);
case AiContextType.CustomerDetail:
case AiContextType.PersonDetail:
case AiContextType.EmployeeDetail:
case AiContextType.OrganisationDetail:
case AiContextType.SupportConceptDetail:
return loadDetailContext(uicontext, context_reference);
case AiContextType.ServiceRecord:
return loadServiceRecordContext(context_reference);
default:
break;
}
throw new NotImplementedException();
}
private static BaseContextSummary loadNavigationContext(AiContextType uicontext, Dictionary<TableID, long[]> context_reference)
{
switch (uicontext)
{
case AiContextType.SupportConceptNavigation:
var list = new CustomerServiceImp().GetAllAuthorizedCompactSupportConcepts();
var filter = list.Where(x => oidsContainsOid(context_reference[TableID.SupportConcept], x.SupportConceptOid));
return AiContextSummaryFactory.CreateSupportConceptNavigationContextSummary(filter);
case AiContextType.CustomerNavigation:
var list2 = new CustomerServiceImp().GetAllAuthorizedCompactCustomers();
var filter2 = list2.Where(x => oidsContainsOid(context_reference[TableID.Customer], x.CustomerOid));
return AiContextSummaryFactory.CreateCustomerNavigationContextSummary(filter2);
case AiContextType.PersonNavigation:
var list3 = new CustomerServiceImp().GetAllAuthorizedCompactPersons();
var filter3 = list3.Where(x => oidsContainsOid(context_reference[TableID.Person], x.PersonOid));
return AiContextSummaryFactory.CreatePersonNavigationContextSummary(filter3);
case AiContextType.EmployeeNavigation:
var list4 = new EmployeeServiceImp().GetAllCompactEmployees();
var filter4 = list4.Where(x => oidsContainsOid(context_reference[TableID.Employee], x.EmployeeOid));
return AiContextSummaryFactory.CreateEmployeeNavigationContextSummary(filter4);
case AiContextType.OrganisationNavigation:
var list5 = new CustomerServiceImp().GetAllOrganisationsCompact();
var filter5 = list5.Where(x => oidsContainsOid(context_reference[TableID.Organisation], x.OrganisationOid));
return AiContextSummaryFactory.CreateOrganisationNavigationContextSummary(filter5);
}
throw new NotImplementedException();
}
private static BaseContextSummary loadDetailContext(AiContextType uicontext, Dictionary<TableID, long[]> context_reference)
{
switch (uicontext)
{
case AiContextType.CustomerDetail:
var oids = context_reference[TableID.Customer];
var dc = new CustomerServiceImp().LoadCustomer(oids[0]);
return AiContextSummaryFactory.CreateCustomerDetailContextSummary(dc);
case AiContextType.PersonDetail:
var oids2 = context_reference[TableID.Person];
var dc2 = new CustomerServiceImp().LoadPerson(oids2[0]);
return AiContextSummaryFactory.CreatePersonDetailContextSummary(dc2);
case AiContextType.EmployeeDetail:
var oids3 = context_reference[TableID.Employee];
var dc3 = new EmployeeServiceImp().LoadEmployee(oids3[0]);
return AiContextSummaryFactory.CreateEmployeeDetailContextSummary(dc3);
case AiContextType.OrganisationDetail:
var oids4 = context_reference[TableID.Organisation];
var dc4 = new CustomerServiceImp().LoadOrganisation(oids4[0]);
return AiContextSummaryFactory.CreateOrganisationDetailContextSummary(dc4);
case AiContextType.SupportConceptDetail:
var oids5 = context_reference[TableID.SupportConcept];
var dc5 = new CustomerServiceImp().LoadSupportConcept(oids5[0]);
return AiContextSummaryFactory.CreateSupportConceptDetailContextSummary(dc5);
}
throw new NotImplementedException();
}
private static BaseContextSummary loadServiceRecordContext(Dictionary<TableID, long[]> context_reference)
{
var customer = loadCompactCustomerDC(context_reference);
var serviceRecords = loadServiceRecordDCs(context_reference);
var supportConcept = loadSupportConceptDC(context_reference);
return AiContextSummaryFactory.CreateServiceRecordNavigationContextSummary(serviceRecords, customer, supportConcept);
}
private static bool oidsContainsOid(long[] oids, long oid)
{
if (oids == null || oids.Length == 0)
return false;
return oids.Contains(oid);
}
private static CompactCustomerDC loadCompactCustomerDC(Dictionary<TableID, long[]> context_reference)
=> LoadDataContact<CompactCustomerDC_Customer, Customer, CompactCustomerDC>(MapperFactory.CompactCustomerDC_Customer, context_reference);
private static CompactSupportConceptDC loadSupportConceptDC(Dictionary<TableID, long[]> context_reference)
=> LoadDataContact<CompactSupportConceptDC_SupportConcept, SupportConcept, CompactSupportConceptDC>(MapperFactory.CompactSupportConceptDC_SupportConcept, context_reference);
private static IEnumerable<ServiceRecordDC> loadServiceRecordDCs(Dictionary<TableID, long[]> context_reference)
=> LoadDataContacts<ServiceRecordDC_ServiceRecord, ServiceRecord, ServiceRecordDC>(MapperFactory.ServiceRecordDC_ServiceRecord, context_reference);
private static TDC LoadDataContact<TMapper, TEntity, TDC>(TMapper mapper, Dictionary<TableID, long[]> context_reference)
where TMapper : AbstractIDCEntityMapper<TEntity, TDC>
where TEntity : BeWoEntityBase, new()
where TDC : class, new()
{
var example = new TEntity();
var tid = example.Tid;
var oid = context_reference[tid][0];
var entity = DAOFactory.GenericDAO.LoadByID<TEntity>(oid);
var dc = mapper.MapToNewDC(entity);
return dc;
}
private static IEnumerable<TDC> LoadDataContacts<TMapper, TEntity, TDC>(TMapper mapper, Dictionary<TableID, long[]> context_reference)
where TMapper : AbstractIDCEntityMapper<TEntity, TDC>
where TEntity : BeWoEntityBase, new()
where TDC : class, new()
{
var example = new TEntity();
var tid = example.Tid;
var oids = context_reference[tid];
var entities = DAOFactory.GenericDAO.LoadByIDs<TEntity>(oids);
var dcs = mapper.MapToNewDCs(entities);
return dcs;
}
}
}