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; using AICore.Context.Core; using DevExpress.DataAccess.Native.Sql; using BS.Shared.Exceptions; using BS.Shared.DataContracts.Feature.AI; namespace BeWo.Service.ServiceUtils { public static class AiContextLoader { public static BaseContextSummary LoadContext(AiActionType actionType, AiViewContextDC view_context, int? sample_count = null) { switch (actionType) { case AiActionType.ServiceRecord: break; case AiActionType.ServiceRecordConversation: return loadServiceRecordConversationContext(view_context.References, sample_count); case AiActionType.ServiceRecordDocumentation: return loadServiceRecordDocumentationContext(view_context.References, view_context.Secondary as string, sample_count); default: break; } throw BeWoNotImplementedException.CreateFromEnum(actionType); } private static BaseContextSummary loadNavigationContext(AiActionType actionType, Dictionary context_reference) { switch (actionType) { //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 BeWoNotImplementedException.CreateFromEnum(actionType); } private static BaseContextSummary loadDetailContext(AiActionType actionType, Dictionary context_reference) { switch (actionType) { //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 BeWoNotImplementedException.CreateFromEnum(actionType); } private static BaseContextSummary loadServiceRecordConversationContext(Dictionary context_reference, int? sample_count = null) { var customer = loadCompactCustomerDC(context_reference); var serviceRecords = loadServiceRecordDCs(context_reference, sample_count); var supportConcept = loadSupportConceptDC(context_reference); var costBearer = supportConcept.CostBearerList.First(x => x.CostBearerOid == context_reference[TableID.CostBearer][0]); return AiContextSummaryFactory.CreateServiceRecordNavigationContextSummary(serviceRecords, customer, costBearer, supportConcept); } private static BaseContextSummary loadServiceRecordDocumentationContext(Dictionary context_reference, string doku_text, int? sample_count = null) { var customer = loadCompactCustomerDC(context_reference); var serviceRecords = loadServiceRecordDCs(context_reference, sample_count); var supportConcept = loadSupportConceptDC(context_reference); var costBearer = supportConcept.CostBearerList.First(x => x.CostBearerOid == context_reference[TableID.CostBearer][0]); return AiContextSummaryFactory.CreateServiceRecordDocumentationFunction(serviceRecords, customer, costBearer, supportConcept, doku_text); } 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 context_reference) => LoadDataContact(MapperFactory.CompactCustomerDC_Customer, context_reference); private static CompactSupportConceptDC loadSupportConceptDC(Dictionary context_reference) => LoadDataContact(MapperFactory.CompactSupportConceptDC_SupportConcept, context_reference); private static IEnumerable loadServiceRecordDCs(Dictionary context_reference, int? sample_count = null) => LoadDataContacts(MapperFactory.ServiceRecordDC_ServiceRecord, context_reference, sample_count); private static TDC LoadDataContact(TMapper mapper, Dictionary context_reference) where TMapper : AbstractIDCEntityMapper 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(oid); var dc = mapper.MapToNewDC(entity); return dc; } private static IEnumerable LoadDataContacts(TMapper mapper, Dictionary context_reference, int? sample_count = null) where TMapper : AbstractIDCEntityMapper where TEntity : BeWoEntityBase, new() where TDC : class, new() { var example = new TEntity(); var tid = example.Tid; var oids = context_reference[tid]; if (sample_count is int count) oids = context_reference[tid].Take(count).ToArray(); var entities = DAOFactory.GenericDAO.LoadByIDs(oids); var dcs = mapper.MapToNewDCs(entities); return dcs; } } }