Files
BeWoPlaner/Service/Plugins/AiPromptbausteineService.cs
2026-07-09 17:01:05 +02:00

323 lines
9.1 KiB
C#

using AICore.Facade;
using BeWo.Data;
using BeWo.Data.Access;
using BeWo.Data.Entities;
using BeWo.Data.Utils;
using BeWo.Service.Core;
using BeWo.Service.DCEntityMapper;
using BeWo.Service.ServiceProxy;
using BS.Shared;
using BS.Shared.Core;
using BS.Shared.DataContracts.Feature.AI;
using BS.Shared.Extensions;
using DevExpress.CodeParser.Diagnostics;
using DevExpress.Office.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.Json;
using System.Xml;
namespace BeWo.Service.Plugins
{
public class AiPromptbausteineService
{
public string Tenant => MultitenancyOperationContextExt.Current?.Tenant;
public IEnumerable<AiPromptbausteinFolderDC> GetAiPromptbausteinFolder(AiActionType aiActionType)
{
var own_bib = getOwnFolder(aiActionType);
var public_bib = getPublicFolders(aiActionType);
var ownsoft_bib = getOwnsoftFolder(aiActionType);
var result = new List<AiPromptbausteinFolderDC>() {
own_bib, public_bib
};
if(ownsoft_bib is object)
result.Add(ownsoft_bib);
return result;
}
private AiPromptbausteinFolderDC getOwnFolder(AiActionType aiActionType)
{
var own_bib = PredefinedAiPromptbausteinFolders.CreateOwnBibNode(aiActionType);
DAOFactory.SearchDAO.GetOwnActiveRootTree(aiActionType,
out var own_folders,
out var own_prompts,
out var own_routines);
var own_folders_dcs = MapperFactory.AiPromptbausteinFolder.MapToNewDCs(own_folders);
var own_prompts_dcs = MapperFactory.AiPromptbausteinPrompt.MapToNewDCs(own_prompts);
var own_routines_dcs = MapperFactory.AiPromptbausteinRoutine.MapToNewDCs(own_routines);
own_bib.SubFolders.AddRange(own_folders_dcs);
own_bib.SubPrompts.AddRange(own_prompts_dcs);
own_bib.SubRoutines.AddRange(own_routines_dcs);
initOwnFolder(own_bib);
return own_bib;
}
private void initOwnFolder(AiPromptbausteinFolderDC folder)
{
folder.CanAddFolder = true;
folder.CanAddPrompt = true;
folder.CanAddRoutine = true;
folder.CanEdit = true;
foreach (var prompt in folder.SubPrompts)
{
prompt.ParentFolderOid = folder.Oid;
prompt.CanEdit = true;
}
foreach (var routine in folder.SubRoutines)
{
routine.ParentFolderOid = folder.Oid;
routine.CanEdit = true;
}
foreach (var next_folder in folder.SubFolders)
{
next_folder.ParentFolderOid = folder.Oid;
initOwnFolder(next_folder);
}
}
private AiPromptbausteinFolderDC getPublicFolders(AiActionType aiActionType)
{
var temp = new Dictionary<long, AiPromptbausteinFolderDC>();
var public_bib = PredefinedAiPromptbausteinFolders.CreatePublicBibNode(aiActionType);
// Hol alles
DAOFactory.SearchDAO.GetPublicActiveRootTree(aiActionType,
out var public_folders,
out var public_prompts,
out var public_routines);
var dummy_node = new AiPromptbausteinFolder()
{
SubFolders = public_folders.ToList(),
SubPrompts = public_prompts.ToList(),
SubRoutines = public_routines.ToList(),
ActionType = aiActionType
};
var public_folders2 = new List<AiPromptbausteinFolder>();
var public_prompts2 = new List<AiPromptbausteinPrompt>();
var public_routines2 = new List<AiPromptbausteinRoutine>();
// Finde public Knoten
findPublicItemsDFS(new List<AiPromptbausteinFolder> { dummy_node }, ref public_folders2, ref public_prompts2, ref public_routines2);
var public_folders_dcs = MapperFactory.AiPromptbausteinFolder.MapToNewDCs(public_folders);
var public_prompts_dcs = MapperFactory.AiPromptbausteinPrompt.MapToNewDCs(public_prompts);
var public_routines_dcs = MapperFactory.AiPromptbausteinRoutine.MapToNewDCs(public_routines);
// Erstelle Baum
foreach (var dc in public_folders_dcs)
{
var creator = dc.Creator;
var creator_oid = creator.EmployeeOid;
AiPromptbausteinFolderDC folder;
if (temp.ContainsKey(creator_oid))
{
folder = temp[creator_oid];
}
else
{
folder = PredefinedAiPromptbausteinFolders.CreatePublicSharedBibNode(aiActionType, creator.GetLastNameFirstName());
public_bib.SubFolders.Add(folder);
temp.Add(creator_oid, folder);
}
folder.SubFolders.Add(dc);
}
foreach (var dc in public_prompts_dcs)
{
var creator = dc.Creator;
var creator_oid = creator.EmployeeOid;
AiPromptbausteinFolderDC folder;
if (temp.ContainsKey(creator_oid))
{
folder = temp[creator_oid];
}
else
{
folder = PredefinedAiPromptbausteinFolders.CreatePublicSharedBibNode(aiActionType, creator.GetLastNameFirstName());
public_bib.SubFolders.Add(folder);
temp.Add(creator_oid, folder);
}
folder.SubPrompts.Add(dc);
}
foreach (var dc in public_routines_dcs)
{
var creator = dc.Creator;
var creator_oid = creator.EmployeeOid;
AiPromptbausteinFolderDC folder;
if (temp.ContainsKey(creator_oid))
{
folder = temp[creator_oid];
}
else
{
folder = PredefinedAiPromptbausteinFolders.CreatePublicSharedBibNode(aiActionType, creator.GetLastNameFirstName());
public_bib.SubFolders.Add(folder);
temp.Add(creator_oid, folder);
}
folder.SubRoutines.Add(dc);
}
initPublicFolder(public_bib);
return public_bib;
}
private void initPublicFolder(AiPromptbausteinFolderDC folder)
{
foreach (var prompt in folder.SubPrompts)
{
prompt.ParentFolderOid = folder.Oid;
}
foreach (var routine in folder.SubRoutines)
{
routine.ParentFolderOid = folder.Oid;
}
foreach (var next_folder in folder.SubFolders)
{
next_folder.ParentFolderOid = folder.Oid;
initPublicFolder(next_folder);
}
}
private void findPublicItemsDFS(IEnumerable<AiPromptbausteinFolder> folders,
ref List<AiPromptbausteinFolder> public_folders,
ref List<AiPromptbausteinPrompt> public_prompts,
ref List<AiPromptbausteinRoutine> public_routines)
{
if (public_folders is null)
public_folders = new List<AiPromptbausteinFolder>();
if (public_prompts is null)
public_prompts = new List<AiPromptbausteinPrompt>();
if (public_routines is null)
public_routines = new List<AiPromptbausteinRoutine>();
foreach (var i_folder in folders)
{
if (i_folder.IsPublic)
{
public_folders.Add(i_folder);
}
else
{
foreach (var i_prompt in i_folder.SubPrompts)
{
if (i_prompt.IsPublic)
public_prompts.Add(i_prompt);
}
foreach (var i_routine in i_folder.SubRoutines)
{
if (i_routine.IsPublic)
public_routines.Add(i_routine);
}
findPublicItemsDFS(i_folder.SubFolders,
ref public_folders,
ref public_prompts,
ref public_routines);
}
}
}
private AiPromptbausteinFolderDC getOwnsoftFolder(AiActionType aiActionType)
{
var prompts = syncOwnSoftPrompts(aiActionType);
var prompts_dc = MapperFactory.AiPromptbausteinPrompt.MapToNewDCs(prompts);
var folder = PredefinedAiPromptbausteinFolders.CreateOwnsoftBibNode(aiActionType);
folder.SubPrompts.AddRange(prompts_dc);
return folder;
}
private IEnumerable<AiPromptbausteinPrompt> syncOwnSoftPrompts(AiActionType aiActionType)
{
var comparer = new AiPromptbausteinPromptPromptIdComparer();
var comparer2 = new AiPromptbausteinPromptComparer2();
var current_prompts = DAOFactory.SearchDAO.GetOwnSoftPromptbausteinPrompts(aiActionType);
var current_prompts_actives = current_prompts.Where(x => x.IsActive == ActivationTypeId.Active);
var current_prompts_deactives = current_prompts.Where(x => x.IsActive != ActivationTypeId.Active);
var actives_prompts = getOwnSoftPrompts(aiActionType);
var request_models = MapperFactory.AiPromptbausteinPrompt.MapToNewEntities(actives_prompts);
var toadd = request_models.Except(current_prompts, comparer);
var toremove = current_prompts_actives.Except(request_models, comparer);
var toreadd = current_prompts_deactives.Intersect(request_models, comparer);
foreach (var a in current_prompts)
{
foreach (var b in request_models)
{
if (!comparer.Equals(a, b))
continue;
if(!comparer2.Equals(a, b))
{
var dc = MapperFactory.AiPromptbausteinPrompt.MapToNewDC(b);
MapperFactory.AiPromptbausteinPrompt.MergeWithEntity(dc, a);
DAOFactory.GenericDAO.Update(a);
}
break;
}
}
DAOFactory.GenericDAO.Insert(toadd);
DAOFactory.GenericDAO.Deactivate(toremove);
DAOFactory.GenericDAO.SetActivationType(toreadd, ActivationTypeId.Active);
var prompts = DAOFactory.SearchDAO.GetActiveOwnSoftPromptbausteinPrompts(aiActionType);
return prompts;
}
private List<AiPromptbausteinPromptDC> getOwnSoftPrompts(AiActionType aiActionType)
{
try
{
var url = MergedConfig.GetSetting(WebConfigSetting.AiPromptsUrl);
var client = new AiPromptsApiClient(url);
var response = client.GetAiPrompts(aiActionType);
return response?.ToList() ?? new List<AiPromptbausteinPromptDC>();
}
catch(Exception ex)
{
throw ex;
//var error = ex.ToString();
//Core.Utils.SendErrorMail("subject - test", "body - test");
//MailUtils.SendAiModuleErrorMail("GetOwnSoftPrompts", error, Tenant);
}
return new List<AiPromptbausteinPromptDC>();
}
}
}