LaiTool/src/define/db/service/Book/bookTaskDetailService.ts

318 lines
11 KiB
TypeScript
Raw Normal View History

2024-06-24 13:11:19 +08:00
import Realm from 'realm'
import path from 'path'
import { define } from '../../../define.js'
import { BookTaskModel } from '../../model/Book/bookTask.js'
2024-08-03 12:46:12 +08:00
import { successMessage } from '../../../../main/Public/generalTools'
2024-06-24 13:11:19 +08:00
import { BaseRealmService } from './bookBasic'
2024-08-12 16:26:08 +08:00
import { cloneDeep, isEmpty } from 'lodash'
2024-08-03 12:46:12 +08:00
import { JoinPath } from '../../../Tools/file'
import { BookTaskDetailModel, ReversePrompt } from '../../model/Book/bookTaskDetail.js'
2024-06-24 13:11:19 +08:00
const { v4: uuidv4 } = require('uuid')
2024-08-03 12:46:12 +08:00
import { Book } from "../../../../model/book"
import { GeneralResponse } from '../../../../model/generalResponse.js'
2024-06-24 13:11:19 +08:00
let dbPath = path.resolve(define.db_path, 'book.realm')
// 版本迁移
2024-08-03 12:46:12 +08:00
const migration = (oldRealm: Realm, newRealm: Realm) => { }
2024-06-24 13:11:19 +08:00
export class BookTaskDetailService extends BaseRealmService {
static instance: BookTaskDetailService | null = null
realm: Realm
private constructor() {
super()
}
/**
*
* @returns
*/
public static async getInstance() {
if (BookTaskDetailService.instance === null) {
BookTaskDetailService.instance = new BookTaskDetailService()
await super.getInstance()
}
2024-06-27 16:24:41 +08:00
await BookTaskDetailService.instance.open()
2024-06-24 13:11:19 +08:00
return BookTaskDetailService.instance
}
/**
2024-06-27 16:24:41 +08:00
*
* @param condition idnamebookIdbookTaskId
*/
2024-08-03 12:46:12 +08:00
GetBookTaskData(condition: Book.QueryBookTaskDetailCondition) {
2024-06-27 16:24:41 +08:00
try {
if (condition == null) {
throw new Error('查询小说分镜信息,查询条件不能为空')
}
2024-07-13 15:44:13 +08:00
let tasksToDelete = this.realm.objects<BookTaskDetailModel>('BookTaskDetail')
2024-06-27 16:24:41 +08:00
if (condition.id) {
2024-07-13 15:44:13 +08:00
tasksToDelete = tasksToDelete.filtered('id==$0', condition.id)
2024-06-27 16:24:41 +08:00
}
if (condition.bookId) {
2024-07-13 15:44:13 +08:00
tasksToDelete = tasksToDelete.filtered('bookId==$0', condition.bookId)
2024-06-27 16:24:41 +08:00
}
if (condition.bookTaskId) {
2024-07-13 15:44:13 +08:00
tasksToDelete = tasksToDelete.filtered('bookTaskId==$0', condition.bookTaskId)
2024-06-27 16:24:41 +08:00
}
if (condition.name) {
2024-07-13 15:44:13 +08:00
tasksToDelete = tasksToDelete.filtered('name==$0', condition.name)
2024-06-27 16:24:41 +08:00
}
2024-07-13 15:44:13 +08:00
2024-08-03 12:46:12 +08:00
2024-06-27 16:24:41 +08:00
let resData = Array.from(tasksToDelete).map((item) => {
let resObj = {
...item,
videoPath: JoinPath(define.project_path, item.videoPath),
audioPath: JoinPath(define.project_path, item.audioPath),
oldImage: JoinPath(define.project_path, item.oldImage),
outImagePath: JoinPath(define.project_path, item.outImagePath),
subImagePath: (item.subImagePath as string[])?.map((subImage) => {
return JoinPath(define.project_path, subImage)
2024-08-03 12:46:12 +08:00
}),
characterTags: item.characterTags ? item.characterTags.map((tag) => tag) : null,
2024-08-18 16:22:19 +08:00
subValue: isEmpty(item.subValue) ? null : JSON.parse(item.subValue),
2024-08-03 12:46:12 +08:00
reversePrompt: item.reversePrompt.map((reversePrompt) => {
return {
...reversePrompt
}
}),
mjMessage: item.mjMessage ? item.mjMessage.toJSON() : null,
2024-06-27 16:24:41 +08:00
}
2024-08-03 12:46:12 +08:00
return cloneDeep(resObj)
2024-06-27 16:24:41 +08:00
})
return successMessage(
resData,
'获取小说的分镜信息成功',
'BookTaskDetailService_GetBookTaskData'
)
} catch (error) {
throw error
}
}
/**
* ID获取指定的小说任务分镜详细数据
* @param bookTaskDetailId
*/
2024-08-03 12:46:12 +08:00
public GetBookTaskDetailDataById(bookTaskDetailId: string): Book.SelectBookTaskDetail {
2024-06-27 16:24:41 +08:00
try {
if (bookTaskDetailId == null) {
throw new Error('获取小说任务详细信息失败缺少ID')
}
let bookTaskDetails = this.GetBookTaskData({ id: bookTaskDetailId })
if (bookTaskDetails.data.length <= 0) {
2024-08-03 12:46:12 +08:00
return null;
2024-06-27 16:24:41 +08:00
} else {
2024-08-03 12:46:12 +08:00
return bookTaskDetails.data[0]
2024-06-27 16:24:41 +08:00
}
} catch (error) {
throw error
}
}
/**
*
2024-06-24 13:11:19 +08:00
* @param BookTaskDetail
*/
2024-06-27 16:24:41 +08:00
public AddBookTaskDetail(bookTaskDetail) {
2024-06-24 13:11:19 +08:00
try {
2024-06-27 16:24:41 +08:00
// 判断是不是又小说ID
if (isEmpty(bookTaskDetail.bookId) || isEmpty(bookTaskDetail.bookTaskId)) {
throw new Error(
'新增小说任务详细信息到数据库失败数据不完整缺少小说ID或者小说批次任务ID'
)
}
// 开始初始化数据获取指定的bookId和bookTaskId中最大的no
let bookTaskDetails = this.realm
.objects<BookTaskModel>('BookTaskDetail')
.filtered(
2024-07-13 15:44:13 +08:00
'bookId == $0 AND bookTaskId == $1',
2024-06-27 16:24:41 +08:00
bookTaskDetail.bookId,
bookTaskDetail.bookTaskId
)
let maxNo = bookTaskDetails.max('no')
bookTaskDetail.no = maxNo ? Number(maxNo) + 1 : 1
2024-07-13 15:44:13 +08:00
let name = bookTaskDetail.no.toString().padStart(5, '0')
2024-06-27 16:24:41 +08:00
bookTaskDetail.name = name
bookTaskDetail.id = uuidv4()
2024-08-03 12:46:12 +08:00
bookTaskDetail.imageLock = false
2024-06-27 16:24:41 +08:00
bookTaskDetail.createTime = new Date()
bookTaskDetail.updateTime = new Date()
2024-07-13 15:44:13 +08:00
bookTaskDetail.adetailer = false // 先写死false
2024-06-27 16:24:41 +08:00
// 开始添加
this.transaction(() => {
this.realm.create('BookTaskDetail', bookTaskDetail)
})
//创建成功,返回
return successMessage(
bookTaskDetail,
'新增小说任务详细信息成功',
'BookTaskDetailService_AddBookTaskDetail'
)
} catch (error) {
throw error
}
}
/**
* ID的指定数据
* @param bookTaskDetailId
* @param updateData
*/
2024-08-18 16:22:19 +08:00
UpdateBookTaskDetail(bookTaskDetailId: string, updateData: Book.SelectBookTaskDetail): Book.SelectBookTaskDetail {
2024-06-27 16:24:41 +08:00
try {
this.transaction(() => {
let bookTaskDetail = this.realm.objectForPrimaryKey('BookTaskDetail', bookTaskDetailId)
if (bookTaskDetail == null) {
throw new Error('未找到对应的小说任务详细信息')
}
// 开始修改
for (let key in updateData) {
bookTaskDetail[key] = updateData[key]
}
bookTaskDetail.updateTime = new Date()
})
2024-08-18 16:22:19 +08:00
let res = this.GetBookTaskDetailDataById(bookTaskDetailId)
return res;
2024-06-27 16:24:41 +08:00
} catch (error) {
throw error
}
}
2024-08-18 16:22:19 +08:00
/**
* ID的反推提示词数据
* @param bookTaskDetailId
* @param mjMessage
*/
2024-08-03 12:46:12 +08:00
UpdateBookTaskDetailMjMessage(bookTaskDetailId: string, mjMessage: Book.MJMessage): void {
try {
2024-08-18 16:22:19 +08:00
console.log('UpdateBookTaskDetailMjMessage', bookTaskDetailId, mjMessage)
2024-08-03 12:46:12 +08:00
this.transaction(() => {
let mjMessageRes = this.realm.objectForPrimaryKey('MJMessage', bookTaskDetailId)
let bookTaskDetail = this.realm.objectForPrimaryKey('BookTaskDetail', bookTaskDetailId)
if (bookTaskDetail.mjMessage == null) {
// 新增
mjMessage.id = bookTaskDetailId
bookTaskDetail.mjMessage = mjMessage
} else {
for (const key in mjMessage) {
mjMessageRes[key] = mjMessage[key]
}
}
})
} catch (error) {
throw error
}
}
/**
* ID的反推提示词数据
* @param bookTaskDetailId ID
* @param reversePromptId ID
* @param updateData
*/
UpdateBookTaskDetailReversePrompt(bookTaskDetailId: string, reversePromptId: string, reversePrompt: Book.ReversePrompt): GeneralResponse.SuccessItem {
try {
this.transaction(() => {
let bookTaskDetails = this.realm.objects<ReversePrompt>("ReversePrompt");
bookTaskDetails = bookTaskDetails.filtered("id = $0 && bookTaskDetailId = $1", reversePromptId, bookTaskDetailId);
if (bookTaskDetails.length <= 0) {
throw new Error("未找到执行的翻译数据,无法写回")
}
let bookTaskDetail = bookTaskDetails[0];
// 直接写入
for (const key in reversePrompt) {
bookTaskDetail[key] = reversePrompt[key]
}
})
return successMessage(null, `${reversePromptId} 更新完成`, "BookTaskDetailService_UpdateBookTaskDetailReversePrompt")
} catch (error) {
throw error
}
}
2024-06-27 16:24:41 +08:00
/**
* ID和小说任务ID
* @param condition bookIdbookTaskIdnameid
*/
DeleteBookTaskDetail(condition) {
try {
if (isEmpty(condition.id) && isEmpty(condition.bookTaskId) && isEmpty(condition.bookId)) {
throw new Error('删除小说分镜信息失败,没有必要参数')
}
2024-07-13 15:44:13 +08:00
let tasksToDelete = this.realm.objects<BookTaskDetailModel>('BookTaskDetail')
2024-06-27 16:24:41 +08:00
if (condition.id) {
2024-07-13 15:44:13 +08:00
tasksToDelete = tasksToDelete.filtered('id==$0', condition.id)
2024-06-27 16:24:41 +08:00
}
if (condition.bookId) {
2024-07-13 15:44:13 +08:00
tasksToDelete = tasksToDelete.filtered('bookId==$0', condition.bookId)
2024-06-27 16:24:41 +08:00
}
if (condition.bookTaskId) {
2024-07-13 15:44:13 +08:00
tasksToDelete = tasksToDelete.filtered('bookTaskId==$0', condition.bookTaskId)
2024-06-27 16:24:41 +08:00
}
if (condition.name) {
2024-07-13 15:44:13 +08:00
tasksToDelete = tasksToDelete.filtered('name==$0', condition.name)
2024-06-27 16:24:41 +08:00
}
this.transaction(() => {
this.realm.delete(tasksToDelete)
})
return successMessage(
null,
'删除指定的分镜任务成功',
'BookTaskDetailService_DeleteBookTaskDetail'
)
2024-06-24 13:11:19 +08:00
} catch (error) {
throw error
}
}
2024-08-03 12:46:12 +08:00
/**
* ID的小说任务详细数据中的所有的反推提示词数据
* @param bookTaskDetailId ID
*/
DeleteBookTaskDetailReversePromptById(bookTaskDetailId: string): void {
this.transaction(() => {
2024-08-18 16:22:19 +08:00
let bookTaskDetails = this.realm.objects<BookTaskDetailModel>('BookTaskDetail').filtered('id = $0', bookTaskDetailId);
if (bookTaskDetails.length <= 0) {
throw new Error('删除小说任务详细信息的反推提示词失败,未找到对应的分镜信息')
}
let bookTaskDetail = bookTaskDetails[0];
bookTaskDetail.gptPrompt = undefined;
// 删除所有的反推提示词
if (bookTaskDetail.reversePrompt) {
bookTaskDetail.reversePrompt.forEach(item => {
this.realm.delete(item)
})
}
2024-08-03 12:46:12 +08:00
})
}
2024-08-21 11:47:05 +08:00
/**
* MJ中生成图片的所有的信息 outImagePath subImagePath mjMessage (mjMessage是另一张表)
* @param bookTaskDetailId
*/
DeleteBoookTaskDetailGenerateImage(bookTaskDetailId: string): void {
this.transaction(() => {
let bookTaskDetail = this.realm.objectForPrimaryKey<BookTaskDetailModel>('BookTaskDetail', bookTaskDetailId)
if (bookTaskDetail == null) {
throw new Error("没有找到要删除的分镜信息")
}
if (bookTaskDetail.mjMessage) {
this.realm.delete(bookTaskDetail.mjMessage)
}
bookTaskDetail.mjMessage = undefined
bookTaskDetail.outImagePath = undefined;
bookTaskDetail.subImagePath = []
})
}
2024-06-24 13:11:19 +08:00
}