2024-08-18 16:22:19 +08:00
|
|
|
|
import { DEFINE_STRING } from "../../../define/define_string";
|
|
|
|
|
|
import { GeneralResponse } from "../../../model/generalResponse";
|
|
|
|
|
|
import { BookService } from "../../../define/db/service/Book/bookService";
|
|
|
|
|
|
import { Book } from "../../../model/book";
|
|
|
|
|
|
import { BookTaskService } from "../../../define/db/service/Book/bookTaskService";
|
|
|
|
|
|
import { TaskScheduler } from "../taskScheduler";
|
|
|
|
|
|
import { LoggerStatus, OtherData } from "../../../define/enum/softwareEnum";
|
|
|
|
|
|
import { BookTaskDetailService } from "../../../define/db/service/Book/bookTaskDetailService";
|
|
|
|
|
|
import { BookBackTaskListService } from "../../../define/db/service/Book/bookBackTaskListService";
|
|
|
|
|
|
import { BookBackTaskType, BookTaskStatus, TaskExecuteType } from "../../../define/enum/bookEnum";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 该类中封装了小说的基础服务,主要是检查每次引入对应的服务类
|
|
|
|
|
|
* 这边进行一个统一的调用,方便后续的维护
|
|
|
|
|
|
*/
|
|
|
|
|
|
export class BookServiceBasic {
|
|
|
|
|
|
bookService: BookService
|
|
|
|
|
|
bookTaskService: BookTaskService;
|
|
|
|
|
|
taskScheduler: TaskScheduler
|
|
|
|
|
|
bookTaskDetailService: BookTaskDetailService
|
|
|
|
|
|
bookBackTaskListService: BookBackTaskListService
|
|
|
|
|
|
constructor() {
|
|
|
|
|
|
this.taskScheduler = new TaskScheduler()
|
|
|
|
|
|
}
|
|
|
|
|
|
async InitService() {
|
|
|
|
|
|
if (!this.bookService) {
|
|
|
|
|
|
this.bookService = await BookService.getInstance()
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!this.bookTaskService) {
|
|
|
|
|
|
this.bookTaskService = await BookTaskService.getInstance()
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!this.bookTaskDetailService) {
|
|
|
|
|
|
this.bookTaskDetailService = await BookTaskDetailService.getInstance()
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!this.bookBackTaskListService) {
|
|
|
|
|
|
this.bookBackTaskListService = await BookBackTaskListService.getInstance()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 主动返回前端的消息
|
|
|
|
|
|
sendReturnMessage(data: GeneralResponse.MessageResponse, message_name = DEFINE_STRING.BOOK.GET_COPYWRITING_RETURN) {
|
|
|
|
|
|
global.newWindow[0].win.webContents.send(message_name, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-04 19:49:20 +08:00
|
|
|
|
|
2024-09-12 14:13:09 +08:00
|
|
|
|
|
2024-09-04 19:49:20 +08:00
|
|
|
|
//#region 事务操作
|
|
|
|
|
|
transaction(callback: (realm: any) => void) {
|
|
|
|
|
|
this.bookService.transaction(() => {
|
|
|
|
|
|
callback(this.bookService.realm)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
|
|
|
2024-08-18 16:22:19 +08:00
|
|
|
|
//#region 小说相关的基础服务
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 通过小说ID获取小说数据
|
|
|
|
|
|
* @param bookId 小说ID
|
2024-09-12 14:13:09 +08:00
|
|
|
|
* @returns
|
2024-08-18 16:22:19 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async GetBookDataById(bookId: string): Promise<Book.SelectBook> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
let book = this.bookService.GetBookDataById(bookId);
|
|
|
|
|
|
if (book == null) {
|
|
|
|
|
|
let msg = '未找到对应的小说数据,请检查'
|
|
|
|
|
|
throw new Error(msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
return book
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更新小说指定ID的数据
|
|
|
|
|
|
* @param bookId 小说ID
|
|
|
|
|
|
* @param data 小说要更新的数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
async UpdateBookData(bookId: string, data: Book.SelectBook): Promise<Book.SelectBook> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
let res = this.bookService.UpdateBookData(bookId, data)
|
|
|
|
|
|
return res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 删除指定的小说数据
|
|
|
|
|
|
* @param bookId 需要删除的小说ID
|
|
|
|
|
|
*/
|
|
|
|
|
|
async DeleteBookData(bookId: string): Promise<void> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
this.bookService.DeleteBookData(bookId)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
|
|
//#region 小说批次任务相关的基础服务
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 通过小说ID获取小说批次任务数据
|
|
|
|
|
|
* @param bookTaskId 小说批次任务ID
|
2024-09-12 14:13:09 +08:00
|
|
|
|
* @returns
|
2024-08-18 16:22:19 +08:00
|
|
|
|
*/
|
2024-08-21 11:47:05 +08:00
|
|
|
|
async GetBookTaskDataById(bookTaskId: string): Promise<Book.SelectBookTask> {
|
2024-08-18 16:22:19 +08:00
|
|
|
|
await this.InitService();
|
|
|
|
|
|
let bookTask = this.bookTaskService.GetBookTaskDataById(bookTaskId);
|
|
|
|
|
|
if (bookTask == null) {
|
|
|
|
|
|
let msg = '未找到对应的小说批次任务数据,请检查';
|
|
|
|
|
|
throw new Error(msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
return bookTask
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 通过查询条件获取小说批次任务数据
|
|
|
|
|
|
* @param bookTaskCondition 小说批次的查询条件
|
|
|
|
|
|
*/
|
|
|
|
|
|
async GetBookTaskData(bookTaskCondition: Book.QueryBookTaskCondition): Promise<{ bookTasks: Book.SelectBookTask[], total: number }> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
let bookTasks = this.bookTaskService.GetBookTaskData(bookTaskCondition)
|
|
|
|
|
|
if (bookTasks.data.bookTasks.length <= 0 || bookTasks.data.total <= 0) {
|
|
|
|
|
|
throw new Error("未找到对应的小说批次任务数据,请检查")
|
|
|
|
|
|
}
|
|
|
|
|
|
return bookTasks.data
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-04 19:49:20 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取最大的小说批次任务的编号
|
|
|
|
|
|
* @param bookId 小说ID
|
|
|
|
|
|
*/
|
|
|
|
|
|
async GetMaxBookTaskNo(bookId: string): Promise<number> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
let maxNo = this.bookTaskService.realm
|
|
|
|
|
|
.objects('BookTask')
|
|
|
|
|
|
.filtered('bookId = $0', bookId)
|
|
|
|
|
|
.max('no')
|
|
|
|
|
|
let no = maxNo == null ? 1 : Number(maxNo) + 1
|
|
|
|
|
|
return no
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-18 16:22:19 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 更新小说批次任务的数据
|
|
|
|
|
|
* @param bookTaskId 小说批次任务ID
|
2024-09-12 14:13:09 +08:00
|
|
|
|
* @param data
|
2024-08-18 16:22:19 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async UpdetedBookTaskData(bookTaskId: string, data: Book.SelectBookTask): Promise<void> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
this.bookTaskService.UpdetedBookTaskData(bookTaskId, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-04 19:49:20 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 重置小说批次任务的数据
|
|
|
|
|
|
* @param bookTaskId 指定的重置的小说批次任务的ID
|
|
|
|
|
|
*/
|
|
|
|
|
|
async ResetBookTask(bookTaskId: string): Promise<void> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
this.bookTaskService.ResetBookTask(bookTaskId)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-18 16:22:19 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 删除指定的小说批次任务的数据
|
|
|
|
|
|
* @param bookTaskId 需要删除的指定的小说批次任务ID
|
|
|
|
|
|
*/
|
|
|
|
|
|
async DeleteBookTaskData(bookTaskId: string): Promise<void> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
this.bookTaskService.DeleteBookTask(bookTaskId)
|
|
|
|
|
|
}
|
|
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
|
|
//#region 小说批次任务对应的分镜的相关的基础服务
|
|
|
|
|
|
|
2024-09-12 14:13:09 +08:00
|
|
|
|
async AddBookTaskDetail(bookTaskDetail: Book.SelectBookTaskDetail): Promise<void> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
this.bookTaskDetailService.AddBookTaskDetail(bookTaskDetail)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-18 16:22:19 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取指定的小说批次任务分镜数据,通过分镜ID
|
|
|
|
|
|
* @param bookTaskDetailId 小说批次任务分镜ID
|
2024-09-12 14:13:09 +08:00
|
|
|
|
* @returns
|
2024-08-18 16:22:19 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async GetBookTaskDetailDataById(bookTaskDetailId: string): Promise<Book.SelectBookTaskDetail> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
let bookTaskDetail = this.bookTaskDetailService.GetBookTaskDetailDataById(bookTaskDetailId)
|
|
|
|
|
|
if (bookTaskDetail == null) {
|
|
|
|
|
|
let msg = "未找到对应的小说批次任务分镜数据,请检查"
|
|
|
|
|
|
global.logger.error('BookServiceBasic_GetBookTaskDetailDataById', msg);
|
|
|
|
|
|
throw new Error("未找到对应的小说批次任务分镜数据,请检查")
|
|
|
|
|
|
}
|
|
|
|
|
|
return bookTaskDetail
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取指定的小说批次任务分镜数据,通过查询条件
|
2024-09-12 14:13:09 +08:00
|
|
|
|
* @param condition
|
|
|
|
|
|
* @param returnEmpty 是否返回空数据,默认 false 不返回,如果返回空数据,会抛出异常
|
2024-08-18 16:22:19 +08:00
|
|
|
|
*/
|
2024-09-12 14:13:09 +08:00
|
|
|
|
async GetBookTaskDetailData(condition: Book.QueryBookTaskDetailCondition, returnEmpty: boolean = false): Promise<Book.SelectBookTaskDetail[]> {
|
2024-08-18 16:22:19 +08:00
|
|
|
|
await this.InitService();
|
|
|
|
|
|
let bookTaskDetails = this.bookTaskDetailService.GetBookTaskData(condition)
|
2024-09-12 14:13:09 +08:00
|
|
|
|
if (!returnEmpty && bookTaskDetails.data.length <= 0) {
|
2024-08-18 16:22:19 +08:00
|
|
|
|
let msg = "未找到对应的小说批次任务分镜数据,请检查";
|
|
|
|
|
|
throw new Error(msg)
|
|
|
|
|
|
}
|
|
|
|
|
|
return bookTaskDetails.data
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 修改小说的分镜详细数据,通过ID
|
|
|
|
|
|
* @param bookTaskDetailId 小说分镜的ID
|
|
|
|
|
|
* @param data 要修改的数据,是个对象,会修改全部
|
|
|
|
|
|
*/
|
|
|
|
|
|
async UpdateBookTaskDetail(bookTaskDetailId: string, data: Book.SelectBookTaskDetail): Promise<Book.SelectBookTaskDetail> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
let res = this.bookTaskDetailService.UpdateBookTaskDetail(bookTaskDetailId, data)
|
|
|
|
|
|
return res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更行小说批次的状态
|
|
|
|
|
|
* @param bookTaskId 小说批次的ID
|
|
|
|
|
|
* @param status 修改后的状态
|
|
|
|
|
|
* @param errorMsg 错误消息
|
|
|
|
|
|
*/
|
|
|
|
|
|
async UpdateBookTaskStatus(bookTaskId: string, status: BookTaskStatus, errorMsg: string | null = null): Promise<void> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
this.bookTaskService.UpdateBookTaskStatus(bookTaskId, status, errorMsg);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 删除指定的小说分镜的反推提示词
|
2024-09-12 14:13:09 +08:00
|
|
|
|
* @param bookTaskDetail
|
2024-08-18 16:22:19 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async DeleteBookTaskDetailReversePromptById(bookTaskDetailId: string): Promise<void> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
this.bookTaskDetailService.DeleteBookTaskDetailReversePromptById(bookTaskDetailId)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-21 11:47:05 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 删除指定的小说分镜生成的图片
|
2024-09-12 14:13:09 +08:00
|
|
|
|
* @param bookTaskDetailId
|
2024-08-21 11:47:05 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async DeleteBoookTaskDetailGenerateImage(bookTaskDetailId: string): Promise<void> {
|
|
|
|
|
|
await this.InitService()
|
|
|
|
|
|
this.bookTaskDetailService.DeleteBoookTaskDetailGenerateImage(bookTaskDetailId);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-12 14:13:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 修改小说分镜数据的反推提示词
|
|
|
|
|
|
* @param bookTaskDetailId 小说分镜的ID
|
|
|
|
|
|
* @param reversePromptId 反推提示词的ID
|
|
|
|
|
|
* @param data 反推提示词数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
async UpdateBookTaskDetailReversePrompt(bookTaskDetailId: string, reversePromptId: string, data: Book.ReversePrompt): Promise<void> {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
this.bookTaskDetailService.UpdateBookTaskDetailReversePrompt(bookTaskDetailId, reversePromptId, data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更行小说分镜的MJ消息,就是出图信息
|
|
|
|
|
|
* @param bookTaskDetailId 小说分镜的数据信息
|
|
|
|
|
|
* @param mjMessage 要保存到分镜信息
|
|
|
|
|
|
*/
|
|
|
|
|
|
async UpdateBookTaskDetailMjMessage(bookTaskDetailId: string, mjMessage: Book.MJMessage) {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
this.bookTaskDetailService.UpdateBookTaskDetailMjMessage(bookTaskDetailId, mjMessage)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-18 16:22:19 +08:00
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
|
|
//#region 小说后台任务相关操作
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加一个后台任务
|
|
|
|
|
|
* @param bookId 小说ID
|
|
|
|
|
|
* @param taskType 后台任务类型
|
|
|
|
|
|
* @param executeType 执行的类型,是不是自动执行
|
|
|
|
|
|
* @param bookTaskId 小说批次任务ID
|
|
|
|
|
|
* @param bookTaskDetailId 小说批次任务分镜ID
|
|
|
|
|
|
*/
|
|
|
|
|
|
async AddBookBackTask(bookId: string,
|
|
|
|
|
|
taskType: BookBackTaskType,
|
|
|
|
|
|
executeType = TaskExecuteType.AUTO,
|
|
|
|
|
|
bookTaskId = null,
|
2024-09-12 14:13:09 +08:00
|
|
|
|
bookTaskDetailId = null,
|
|
|
|
|
|
responseMessageName: string = null
|
|
|
|
|
|
): Promise<TaskModal.Task> {
|
2024-08-18 16:22:19 +08:00
|
|
|
|
|
|
|
|
|
|
await this.InitService();
|
2024-09-12 14:13:09 +08:00
|
|
|
|
let res = this.bookBackTaskListService.AddBookBackTask(bookId, taskType, executeType, bookTaskId, bookTaskDetailId, responseMessageName)
|
2024-08-18 16:22:19 +08:00
|
|
|
|
if (res.code == 0) {
|
|
|
|
|
|
throw new Error(res.message)
|
|
|
|
|
|
}
|
|
|
|
|
|
return res.data as TaskModal.Task
|
|
|
|
|
|
}
|
|
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 通过小说ID和小说批次任务ID获取小说和小说批次任务数据
|
|
|
|
|
|
* @param bookId 小说ID
|
|
|
|
|
|
* @param bookTaskName 小说批次的名字,或者是小说批次任务的ID
|
2024-09-12 14:13:09 +08:00
|
|
|
|
* @returns
|
2024-08-18 16:22:19 +08:00
|
|
|
|
*/
|
|
|
|
|
|
async GetBookAndTask(bookId: string, bookTaskName: string) {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
let book = this.bookService.GetBookDataById(bookId)
|
|
|
|
|
|
if (book == null) {
|
|
|
|
|
|
throw new Error("查找小说数据失败");
|
|
|
|
|
|
}
|
|
|
|
|
|
// 获取小说对应的批次任务数据,默认初始化为第一个
|
|
|
|
|
|
let condition = {
|
|
|
|
|
|
bookId: bookId
|
|
|
|
|
|
} as Book.QueryBookBackTaskCondition
|
|
|
|
|
|
if (bookTaskName == "output_00001") {
|
|
|
|
|
|
condition["name"] = bookTaskName
|
|
|
|
|
|
} else {
|
|
|
|
|
|
condition["id"] = bookTaskName
|
|
|
|
|
|
}
|
|
|
|
|
|
let bookTaskRes = this.bookTaskService.GetBookTaskData(condition)
|
|
|
|
|
|
if (bookTaskRes.data.bookTasks.length <= 0 || bookTaskRes.data.total <= 0) {
|
|
|
|
|
|
let msg = "没有找到对应的小说批次任务数据"
|
|
|
|
|
|
this.taskScheduler.AddLogToDB(bookId, book.type, msg, OtherData.DEFAULT, LoggerStatus.FAIL)
|
|
|
|
|
|
throw new Error(msg)
|
|
|
|
|
|
}
|
|
|
|
|
|
return { book: book as Book.SelectBook, bookTask: bookTaskRes.data.bookTasks[0] as Book.SelectBookTask }
|
|
|
|
|
|
}
|
2024-09-12 14:13:09 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 修改后台队列的状态
|
|
|
|
|
|
* @param bookBackTask 要修改的数据
|
|
|
|
|
|
*/
|
|
|
|
|
|
async UpdateTaskStatus(bookBackTask: Book.UpdateBookTaskListStatus) {
|
|
|
|
|
|
await this.InitService();
|
|
|
|
|
|
this.bookBackTaskListService.UpdateTaskStatus(bookBackTask)
|
|
|
|
|
|
}
|
2024-08-18 16:22:19 +08:00
|
|
|
|
}
|