This commit is contained in:
owenchen committed 2026-05-29 13:02:41 +08:00
1 parent 2e990e687f
commit 4ec2502778
53 files changed
+365 -1854

No files matched your search

+18 -76
View File
@@ -3,7 +3,7 @@ using CloudBuilder.AI.Data;
using CloudBuilder.AI.Entity;
using CloudBuilder.AI.Policy;
using CloudBuilder.AI.SearchCriteria;
using CloudBuilder.AI.Service.Utility;
using CloudBuilder.AI.Utility;
using CloudBuilder.Core.DatabaseAccessor;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.Core.Service;
@@ -29,7 +29,6 @@ namespace CloudBuilder.AI.Service
private IRepository<AiBookEntity> repository;
private ChineseNameExtractor chineseNameExtractor;
public AiBookService(IApplicationService service)
{
this.service = service;
@@ -40,7 +39,7 @@ namespace CloudBuilder.AI.Service
public AiBookData FindByKey(string key)
{
AiBookData data = new AiBookData();
data.AiBook = repository.DetachedEntities.Where(x => x.Guid == key).FirstOrDefault()!;
data.AiBook = repository.DetachedEntities.Where(x => x.BookId == key).FirstOrDefault()!;
if (data.AiBook.FileId > 0)
{
@@ -129,7 +128,7 @@ namespace CloudBuilder.AI.Service
ISecurityGeneratorNumberService securityGeneratorNumberService = service.ServiceProvider.GetService<ISecurityGeneratorNumberService>()!;
data.AiBook.Guid = securityGeneratorNumberService.GetNo(AIPrefixPolicy.AI_BOOK, AIPrefixPolicy.GUID, AIPrefixPolicy.AI_BOOK_PREFIX, YesNoPolicy.NO, AIPrefixPolicy.AI_BOOK_GUID_FORMAT);
data.AiBook.BookId = securityGeneratorNumberService.GetNo(AIPrefixPolicy.AI_BOOK, AIPrefixPolicy.GUID, AIPrefixPolicy.AI_BOOK_PREFIX, YesNoPolicy.NO, AIPrefixPolicy.AI_BOOK_GUID_FORMAT);
repository.InsertNow(data.AiBook);
@@ -144,7 +143,7 @@ namespace CloudBuilder.AI.Service
[UnitOfWork]
public AiBookData Update(AiBookData data)
{
AiBookEntity aiBook = repository.DetachedEntities.Where(x => x.Guid == data.AiBook.Guid).FirstOrDefault();
AiBookEntity aiBook = repository.DetachedEntities.Where(x => x.BookId == data.AiBook.BookId).FirstOrDefault();
if (aiBook == null)
{
throw new ValidatedException("记录不存在.");
@@ -164,7 +163,7 @@ namespace CloudBuilder.AI.Service
[UnitOfWork]
public void Delete(string key)
{
var ent = repository.DetachedEntities.Where(x => x.Guid == key).FirstOrDefault()!;
var ent = repository.DetachedEntities.Where(x => x.BookId == key).FirstOrDefault()!;
if (ent != null) { repository.DeleteNow(ent); }
}
@@ -203,110 +202,53 @@ namespace CloudBuilder.AI.Service
string context = File.ReadAllText(file, Encoding.GetEncoding(encoding));
List<AiChapterEntity> aiChapterEntities = chapterSplitter.SplitNovelIntoChapters(context, pattern);
List<AiChapterDisplayData> aiChapterEntities = chapterSplitter.SplitNovelIntoChapters(context, pattern);
if (aiChapterEntities == null || aiChapterEntities.Count() == 0) return data;
aiChapterEntities.ToLookup(x => x.BookGuid = data.AiBook.Guid);
aiChapterEntities.ToLookup(x => x.Status = ChapterStatusPolicy.PROCESS);
int chapterIndex = 1;
foreach (var chapter in aiChapterEntities)
{
chapter.ChapterId = chapterIndex++;
chapter.BookId = data.AiBook.BookId;
chapter.Status = ChapterStatusPolicy.PROCESS;
}
data.AiChapters = aiChapterEntities.ToArray();
paragraphSplitter.PackParagraphs(data);
IRepository<AiChapterEntity> repositoryAiChapterEntity = repository.GetRepository<IRepository<AiChapterEntity>>();
IRepository<AiParagraphEntity> repositoryAiParagraphEntity = repository.GetRepository<IRepository<AiParagraphEntity>>();
IRepository<AiSentenceEntity> repositoryAiSentenceEntity = repository.GetRepository<IRepository<AiSentenceEntity>>();
AiChapterEntity[] aiChapters = repositoryAiChapterEntity.DetachedEntities.Where(x => x.BookGuid == data.AiBook.Guid).ToArray();
AiChapterEntity[] aiChapters = repositoryAiChapterEntity.DetachedEntities.Where(x => x.BookId == data.AiBook.BookId).ToArray();
if (aiChapters != null && aiChapters.Length > 0)
{
foreach (AiChapterEntity aiChapter in aiChapters)
{
AiParagraphEntity[] aiParagraphs = repositoryAiParagraphEntity.DetachedEntities.Where(x => x.ChapterGuid == aiChapter.Guid).ToArray();
AiSentenceEntity[] aiSentences = repositoryAiSentenceEntity.DetachedEntities.Where(x => x.ChapterId == aiChapter.ChapterId).ToArray();
if (aiParagraphs != null && aiParagraphs.Length > 0)
if (aiSentences != null && aiSentences.Length > 0)
{
foreach (AiParagraphEntity aiParagraph in aiParagraphs)
if (aiSentences != null && aiSentences.Length > 0)
{
AiSentenceEntity[] aiSentences = repositoryAiSentenceEntity.DetachedEntities.Where(x => x.ParagraphGuid == aiParagraph.Guid).ToArray();
if (aiSentences != null && aiSentences.Length > 0)
{
repositoryAiSentenceEntity.DeleteNow(aiSentences);
}
repositoryAiSentenceEntity.DeleteNow(aiSentences);
}
repositoryAiParagraphEntity.DeleteNow(aiParagraphs);
}
}
repositoryAiChapterEntity.DeleteNow(aiChapters);
}
repositoryAiChapterEntity.InsertNow(data.AiChapters);
if (data.AiParagraphs == null || data.AiParagraphs.Count() == 0) return data;
repositoryAiParagraphEntity.InsertNow(data.AiParagraphs);
if (data.AiSentences == null || data.AiSentences.Count() == 0) return data;
repositoryAiSentenceEntity.InsertNow(data.AiSentences);
return FindByKey(key);
}
[PermissionOperate(name: AIPermissionPolicy.AiBook, operate: Operate.Update)]
[UnitOfWork]
public void ExtractNames(string bookGuid)
{
ExtractNamesByChapterGuid(bookGuid, null);
}
[PermissionOperate(name: AIPermissionPolicy.AiBook, operate: Operate.Update)]
[UnitOfWork]
public void ExtractNamesByChapterGuid(string bookGuid, string chapterGuid)
{
if (chineseNameExtractor == null) chineseNameExtractor = new ChineseNameExtractor(service);
IRepository<AiChapterPersonEntity> repositoryAiChapterPersonEntity = repository.GetRepository<IRepository<AiChapterPersonEntity>>();
IRepository<AiParagraphViewEntity> repositoryAiParagraphEntity = repository.GetRepository<IRepository<AiParagraphViewEntity>>();
AiParagraphViewEntity[] paragraphs = repositoryAiParagraphEntity.DetachedEntities.Where(x => x.BookGuid == bookGuid && (string.IsNullOrEmpty(chapterGuid) || x.ChapterGuid == chapterGuid)).ToArray();
if (paragraphs == null || paragraphs.Length == 0) return;
List<string> names = new List<string>();
List<AiChapterPersonEntity> aiChapterPersonEntities = new List<AiChapterPersonEntity>();
AiChapterPersonEntity entity;
int orderIndex = 0;
foreach (var paragraph in paragraphs)
{
names = chineseNameExtractor.ExtractNames(paragraph.Content);
if (names == null || names.Count() == 0) continue;
orderIndex = 1;
foreach (var name in names)
{
entity = new AiChapterPersonEntity();
entity.Guid = Guid.NewGuid().ToString();
entity.ChapterGuid = paragraph.ChapterGuid;
entity.ParagraphGuid = paragraph.Guid;
entity.ParagraphIndex = paragraph.ParagraphIndex;
entity.OrderIndex = orderIndex++;
entity.PersonName = name;
aiChapterPersonEntities.Add(entity);
}
}
if (aiChapterPersonEntities != null && aiChapterPersonEntities.Count() > 0)
repositoryAiChapterPersonEntity.InsertNow(aiChapterPersonEntities);
}
[PermissionOperate(name: AIPermissionPolicy.AiBook, operate: Operate.Find)]
public IActionResult GetTtsStream(string text)