Files
BeWoPlaner/Service/ServiceUtils/AiSystemBuilder.cs
2025-04-14 12:16:57 +02:00

248 lines
7.1 KiB
C#

using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Service.DCEntityMapper;
using BS.Shared;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using DevExpress.PivotGrid.OLAP.Mdx;
using DevExpress.XtraRichEdit.Commands;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace BeWo.Service.ServiceUtils
{
public static class AiSystemBuilder
{
public static string CreateSystemInstructions(int uicontext, Dictionary<TableID, long[]> bewoobjects)
{
var system_prompt = getSystemPrompt(2);
Dictionary<string, object> data = null;
switch (uicontext)
{
case 0:
break;
case 1:
data = createCustomerNavigationContext(bewoobjects);
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
case 11:
break;
case 12:
break;
case 13:
break;
case 14:
break;
case 15:
break;
case 16:
break;
case 17:
break;
case 18:
break;
// Service Record
case 19:
data = createServiceRecordContext(bewoobjects);
break;
default:
throw new NotImplementedException("UI Context unknown");
}
var text = JsonConvert.SerializeObject(data);
var result = system_prompt + text;
return result;
}
private static Dictionary<string, object> createCustomerNavigationContext(Dictionary<TableID, long[]> bewoobjects)
{
if (bewoobjects is null || !bewoobjects.Any())
return null;
var customers = new List<Dictionary<string,object>>();
if (bewoobjects.TryGetValue(TableID.Customer, out long[] cvalues) && (cvalues?.Length ?? 0) > 0)
{
var customers_e = DAOFactory.GenericDAO.GetByIDs<Customer>(cvalues);
var customers_dc = MapperFactory.CompactCustomerDC_Customer.MapToNewDCs(customers_e);
customers = customers_dc.Select(getCustomer).ToList();
}
var dict = new Dictionary<string, object>
{
{ "Aktuelle Ansicht", "Klienten Navigation" },
{ "Klienten", customers }
};
return dict;
}
private static Dictionary<string, object> createServiceRecordContext(Dictionary<TableID, long[]> bewoobjects)
{
if (bewoobjects is null || !bewoobjects.Any())
return null;
var selected = new Dictionary<string, object>();
if (bewoobjects.TryGetValue(TableID.Customer, out long[] cvalues) && (cvalues?.Length ?? 0) > 0)
{
var customer = DAOFactory.GenericDAO.GetByID<Customer>(cvalues[0]);
var customer_dc = MapperFactory.CompactCustomerDC_Customer.MapToNewDC(customer);
selected.Add("CustomerName", customer_dc.LastNameFirstName);
}
if (bewoobjects.TryGetValue(TableID.CostBearer, out long[] cbvalues) && (cbvalues?.Length ?? 0) > 0)
{
var costbearer = DAOFactory.GenericDAO.GetByID<Organisation>(cbvalues[0]);
var costbearer_dc = MapperFactory.CompactOrganisationDC_Organisation.MapToNewDC(costbearer);
selected.Add("CostBearerName", costbearer_dc.Name);
}
if (bewoobjects.TryGetValue(TableID.SupportConcept, out long[] svalues) && (svalues?.Length ?? 0) > 0)
{
var supportconcept = DAOFactory.GenericDAO.GetByID<SupportConcept>(svalues[0]);
var supportconcept_dc = MapperFactory.CompactSupportConceptDC_SupportConcept.MapToNewDC(supportconcept);
selected.Add("SupportConceptDuration", $"{supportconcept_dc.StartDate?.Date} - {supportconcept_dc.EndDate?.Date}");
}
var records = new List<Dictionary<string, object>>();
if (bewoobjects.TryGetValue(TableID.ServiceRecord, out long[] values) && (values?.Length ?? 0) > 0)
{
var service_records = DAOFactory.GenericDAO.GetByIDs<ServiceRecord>(values);
var service_records_dc = MapperFactory.ServiceRecordDC_ServiceRecord.MapToNewDCs(service_records);
foreach (var service_record in service_records_dc)
{
records.Add(getServiceRecord(service_record));
}
}
var dict = new Dictionary<string, object>
{
{ "Aktuelle Ansicht", "Zeiterfassung" },
{ "Selected", selected },
{ "Zeiterfassungen", records }
};
return dict;
}
private static Dictionary<string, object> getServiceRecord(ServiceRecordDC service_record)
{
var record_dict = new Dictionary<string, object>
{
{ "StartDate", service_record.Start },
{ "EndDate", service_record.End },
{ "Duration (Std)", service_record.DurationInStunden },
{ "ServiceCategory", service_record.ServiceDescription.Category.Name },
{ "ServiceDescription", service_record.ServiceDescription.Name },
{ "Documentation", service_record.Notice
+ "\n" + service_record.Notice2
+ "\n" + service_record.Notice3
+ "\n" + service_record.Notice4
+ "\n" + service_record.Notice5},
{ "Distance (Meter)", service_record.DistanceInMeter ?? 0 },
{ "InsertedOn", service_record.InsertedOn },
{ "Betrag", service_record.Betrag },
{ "InsertUser", service_record.InsUser },
{ "Employee", service_record.Employee }
};
return record_dict;
}
private static Dictionary<string, object> getCustomer(CompactCustomerDC customer)
{
var record_dict = new Dictionary<string, object>
{
{"Address", customer.AddressString},
{"Alias", customer.CustomerAlias},
{"CustomerOid", customer.CustomerOid},
{"DateOfBirth", customer.DateOfBirthString},
{"EqutyContribution", customer.EquityContribution},
{"Fullname", customer.FullName},
{"InvoiceAddress", customer.InvoiceAddressString}
};
return record_dict;
}
private static Dictionary<string, object> getEmployee(CompactEmployeeDC compactEmployee)
{
return null;
}
private static string getSystemPrompt(int i)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = $"BeWo.Service.AI.system_prompt{i}.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
return result;
}
}
//private static Dictionary<TableID, List<long>> getDictionary(List<Tuple<TableID, long>> tuples)
//{
// var seperate = new Dictionary<TableID, List<long>>();
// foreach (var bewoobject in tuples)
// {
// var tid = bewoobject.Item1;
// var oid = bewoobject.Item2;
// if (seperate.TryGetValue(tid, out var list))
// {
// list.Add(oid);
// }
// else
// {
// seperate.Add(tid, new List<long>() { oid });
// }
// }
// return seperate;
//}
//private static bool tryGetSingleValue(Dictionary<TableID, List<long>> sorted, TableID tid, out long oid)
//{
// oid = 0;
// if (!tryGetMultipleValues(sorted, tid, out List<long> oids) || oids is null || oids.Count == 0)
// return false;
// oid = oids[0];
// return true;
//}
//private static bool tryGetMultipleValues(Dictionary<TableID, List<long>> sorted, TableID tid, out List<long> oids)
//{
// return sorted.TryGetValue(tid, out oids);
//}
}
}