V 2.2.11 优化MJ代理模式

This commit is contained in:
2024-06-27 16:24:41 +08:00
parent bac97bc41c
commit 669e57824d
40 changed files with 3363 additions and 1643 deletions
@@ -26,14 +26,29 @@ export class BookBackTaskListService extends BaseRealmService {
BookBackTaskListService.instance = new BookBackTaskListService()
await super.getInstance()
}
await BookBackTaskListService.instance.open()
return BookBackTaskListService.instance
}
/**
* 获取指定条件的后台任务队列
* bookId 必填
* @param query bookIdbookTaskIdnametypestatus
*/
getBookBackTaskList(query) {
try {
// if()
} catch (error) {
throw error
}
}
/**
* 新增一个小说相关的后台任务队列
* @param bookBackTask 要添加的小说数据
*/
async AddBookBackTaskList(bookBackTask) {
AddBookBackTaskList(bookBackTask) {
try {
// 判断数据是不是存在
if (
+8 -1
View File
@@ -32,6 +32,13 @@ const migration = (oldRealm: Realm, newRealm: Realm) => {
newBookTask[i].isAuto = false // 为新属性设置默认值
}
}
if (oldRealm.schemaVersion < 3) {
const oldBookTask = oldRealm.objects('BookTask')
const newBookTask = newRealm.objects('BookTask')
for (let i = 0; i < oldBookTask.length; i++) {
newBookTask[i].audioPath = null // 为新属性设置默认值
}
}
}
export class BaseRealmService extends BaseService {
@@ -72,7 +79,7 @@ export class BaseRealmService extends BaseService {
BookTaskDetailModel
],
path: this.dbpath,
schemaVersion: 2,
schemaVersion: 3,
migration: migration
}
this.realm = await Realm.open(config)
+48 -11
View File
@@ -11,8 +11,8 @@ import { BookTaskService } from './bookTaskService'
import { BaseRealmService } from './bookBasic.js'
import { isEmpty } from 'lodash'
class BooKService extends BaseRealmService {
static instance: BooKService | null = null
export class BookService extends BaseRealmService {
static instance: BookService | null = null
realm: Realm
private constructor() {
@@ -24,20 +24,59 @@ class BooKService extends BaseRealmService {
* @returns
*/
public static async getInstance() {
if (BooKService.instance === null) {
BooKService.instance = new BooKService()
if (BookService.instance === null) {
BookService.instance = new BookService()
await super.getInstance()
}
return BooKService.instance
await BookService.instance.open()
return BookService.instance
}
/**
* 获取小说信息,没有找到返回null
* @param bookId
*/
GetBookDataById(bookId) {
try {
if (isEmpty(bookId)) {
throw new Error('获取小说信息失败,缺少小说ID')
}
let books = this.realm.objects<BookModel>('Book').filtered('id = $0', bookId)
if (books.length <= 0) {
return successMessage(null, '通过ID获取小说数据成功', 'ReverseBook_GetBookDataById')
} else {
// 对返回的数据进行处理
let resBooks = Array.from(books).map((book) => {
// 这里可以直接操作普通对象
let bookObj = {
...book,
bookFolderPath: path.resolve(
define.project_path,
book.bookFolderPath.replace(/\\/g, '/')
),
oldVideoPath: book.oldVideoPath
? path.resolve(define.project_path, book.oldVideoPath.replace(/\\/g, '/'))
: '',
imageFolder: book.imageFolder
? path.resolve(define.project_path, book.imageFolder.replace(/\\/g, '/'))
: ''
}
return bookObj
})
return successMessage(resBooks[0], '通过ID获取小说数据成功', 'ReverseBook_GetBookDataById')
}
} catch (error) {
throw error
}
}
/**
* 获取小说信息,通过参数查询
* @returns
*/
async GetBookData(bookQuery) {
GetBookData(bookQuery) {
try {
await this.open()
// 获取所有的小说数据,并进行时间降序排序
let books = this.realm.objects<BookModel>('Book')
let book_length = books.length
@@ -106,7 +145,6 @@ class BooKService extends BaseRealmService {
*/
async AddOrModifyBook(book) {
try {
await this.open()
if (book == null) {
throw new Error('小说数据为空,无法修改')
}
@@ -141,6 +179,7 @@ class BooKService extends BaseRealmService {
let bookFolderPath = path.resolve(define.project_path, book.id)
let imageFolder = path.resolve(define.project_path, `${book.id}/tmp`)
let oldVideoPath = path.resolve(define.project_path, `${book.id}/data/${book.id}.mp4`)
let bookTaskImageFolder = path.resolve(imageFolder, 'output_00001')
// 将视频拷贝一个到项目文件下面
if (book.oldVideoPath) {
@@ -150,11 +189,11 @@ class BooKService extends BaseRealmService {
// 创建对应的文件夹
await CheckFolderExistsOrCreate(bookFolderPath)
await CheckFolderExistsOrCreate(imageFolder)
await CheckFolderExistsOrCreate(bookTaskImageFolder) // 创建默认的任务文件夹
// 修改数据
book.oldVideoPath = path.relative(define.project_path, oldVideoPath)
this.realm.write(() => {
this.realm.create('Book', book)
let bookTaskImageFolder = path.resolve(imageFolder, 'output_00001')
// 添加一个任务
let bookTask = {
id: uuidv4(),
@@ -210,5 +249,3 @@ class BooKService extends BaseRealmService {
}
}
}
export default BooKService
@@ -6,6 +6,10 @@ import { BookTaskModel } from '../../model/Book/bookTask.js'
import { BookTaskStatus } from '../../../enum/bookEnum.js'
import { successMessage } from '../../../../main/generalTools.js'
import { BaseRealmService } from './bookBasic'
import { endsWith, isEmpty } from 'lodash'
import { book } from '../../../../preload/book.js'
import { DefaultObject } from 'realm/dist/public-types/schema.js'
import { JoinPath } from '../../../Tools/file.js'
const { v4: uuidv4 } = require('uuid')
let dbPath = path.resolve(define.db_path, 'book.realm')
@@ -30,16 +34,202 @@ export class BookTaskDetailService extends BaseRealmService {
BookTaskDetailService.instance = new BookTaskDetailService()
await super.getInstance()
}
await BookTaskDetailService.instance.open()
return BookTaskDetailService.instance
}
/**
* 添加一条小说人物对应的详细数据
* 更具条件查询执行的小说的分镜信息
* @param condition 查询的条件,idnamebookIdbookTaskId
*/
GetBookTaskData(condition) {
try {
if (condition == null) {
throw new Error('查询小说分镜信息,查询条件不能为空')
}
let query = [] as string[]
if (condition.id) {
query.push(`id = ${condition.id}`)
}
if (condition.bookId) {
query.push(`bookId = ${condition.bookId}`)
}
if (condition.bookTaskId) {
query.push(`bookTaskId = ${condition.bookTaskId}`)
}
if (condition.name) {
query.push(`name = ${condition.name}`)
}
const queryString = query.join(' && ')
let tasksToDelete: Realm.Results<Realm.Object<DefaultObject, never> & DefaultObject>
// 获取指定的数据
if (queryString) {
tasksToDelete = this.realm.objects('BookTaskDetail').filtered(queryString)
} else {
// 返回全部
tasksToDelete = this.realm.objects('BookTaskDetail')
}
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)
})
}
return resObj
})
return successMessage(
resData,
'获取小说的分镜信息成功',
'BookTaskDetailService_GetBookTaskData'
)
} catch (error) {
throw error
}
}
/**
* 通过ID获取指定的小说任务分镜详细数据
* @param bookTaskDetailId
*/
public GetBookTaskDetailDataById(bookTaskDetailId: string) {
try {
if (bookTaskDetailId == null) {
throw new Error('获取小说任务详细信息失败,缺少ID')
}
let bookTaskDetails = this.GetBookTaskData({ id: bookTaskDetailId })
if (bookTaskDetails.data.length <= 0) {
return successMessage(
null,
'未找到对应的小说任务详细信息',
'BookTaskDetailService_GetBookTaskDetailDataById'
)
} else {
return successMessage(
bookTaskDetails.data[0],
'获取小说任务详细信息成功',
'BookTaskDetailService_GetBookTaskDetailDataById'
)
}
} catch (error) {
throw error
}
}
/**
* 添加一条小说任务对应的详细数据
* @param BookTaskDetail
*/
public async AddBookTaskDetail(BookTaskDetail) {
public AddBookTaskDetail(bookTaskDetail) {
try {
// 判断是不是又小说ID
// 判断是不是又小说ID
if (isEmpty(bookTaskDetail.bookId) || isEmpty(bookTaskDetail.bookTaskId)) {
throw new Error(
'新增小说任务详细信息到数据库失败,数据不完整,缺少小说ID或者小说批次任务ID'
)
}
// 开始初始化数据(获取指定的bookId和bookTaskId)中最大的no
let bookTaskDetails = this.realm
.objects<BookTaskModel>('BookTaskDetail')
.filtered(
'bookId = $0 AND bookTaskId = $1',
bookTaskDetail.bookId,
bookTaskDetail.bookTaskId
)
let maxNo = bookTaskDetails.max('no')
bookTaskDetail.no = maxNo ? Number(maxNo) + 1 : 1
let name = bookTaskDetail.no.tosString().padStart(5, '0')
bookTaskDetail.name = name
bookTaskDetail.id = uuidv4()
bookTaskDetail.createTime = new Date()
bookTaskDetail.updateTime = new Date()
// 开始添加
this.transaction(() => {
this.realm.create('BookTaskDetail', bookTaskDetail)
})
//创建成功,返回
return successMessage(
bookTaskDetail,
'新增小说任务详细信息成功',
'BookTaskDetailService_AddBookTaskDetail'
)
} catch (error) {
throw error
}
}
/**
* 更新指定ID的指定数据
* @param bookTaskDetailId
* @param updateData
*/
UpdateBookTaskDetail(bookTaskDetailId: string, updateData) {
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()
})
return successMessage(
null,
'修改小说任务详细信息成功',
'BookTaskDetailService_UpdateBookTaskDetail'
)
} catch (error) {
throw error
}
}
/**
* 删除满足条件的对象吗,必传小说ID和小说任务ID
* @param condition bookIdbookTaskIdnameid
*/
DeleteBookTaskDetail(condition) {
try {
if (isEmpty(condition.id) && isEmpty(condition.bookTaskId) && isEmpty(condition.bookId)) {
throw new Error('删除小说分镜信息失败,没有必要参数')
}
let query = [] as string[]
if (condition.id) {
query.push(`id = ${condition.id}`)
}
if (condition.bookId) {
query.push(`bookId = ${condition.bookId}`)
}
if (condition.bookTaskId) {
query.push(`bookTaskId = ${condition.bookTaskId}`)
}
if (condition.name) {
query.push(`name = ${condition.name}`)
}
if (query.length <= 0) {
throw new Error('删除小说分镜任务失败,没有查询条件')
}
const queryString = query.join(' && ')
let tasksToDelete = this.realm.objects('BookTaskDetail').filtered(queryString)
this.transaction(() => {
this.realm.delete(tasksToDelete)
})
return successMessage(
null,
'删除指定的分镜任务成功',
'BookTaskDetailService_DeleteBookTaskDetail'
)
} catch (error) {
throw error
}
+58 -4
View File
@@ -29,6 +29,7 @@ export class BookTaskService extends BaseRealmService {
BookTaskService.instance = new BookTaskService()
await super.getInstance()
}
await BookTaskService.instance.open()
return BookTaskService.instance
}
@@ -36,9 +37,8 @@ export class BookTaskService extends BaseRealmService {
* 查询满足条件的小说子任务信息
* @param bookTaskCondition 查询条件 idbookIdnamenopage, pageSize
*/
async GetBookTaskData(bookTaskCondition) {
GetBookTaskData(bookTaskCondition) {
try {
await this.open()
// 获取所有的小说数据,并进行时间降序排序
let bookTasks = this.realm.objects<BookTaskModel>('BookTask')
@@ -98,10 +98,64 @@ export class BookTaskService extends BaseRealmService {
}
}
/**
* 通过ID获取小说批次任务的数据
* @param bookTaskId
*/
GetBookTaskDataById(bookTaskId: string) {
try {
if (bookTaskId == null) {
throw new Error('小说任务ID不能为空')
}
let bookTasks = this.GetBookTaskData({ id: bookTaskId })
if (bookTasks.data.bookTasks.length <= 0) {
return successMessage(null, '未找到对应的小说任务', 'BookTaskService_GetBookTaskDataById')
} else {
return successMessage(
bookTasks.data.bookTasks[0],
'查询小说任务成功',
'BookTaskService_GetBookTaskDataById'
)
}
} catch (error) {
throw error
}
}
/**
* 修改小说批次任务的状态
* @param bookTaskId 小说批次任务Id
* @param status 目标状态
*/
UpdateBookTaskStatus(bookTaskId: string, status: BookTaskStatus, errorMsg: string | null = null) {
try {
this.transaction(() => {
// 修改对应小说批次任务的状态
let bookTask = this.realm.objectForPrimaryKey('BookTask', bookTaskId)
if (bookTask == null) {
throw new Error('未找到对应的小说任务')
}
bookTask.status = status
bookTask.updateTime = new Date()
if (errorMsg != null) {
bookTask.errorMsg = errorMsg
}
})
return successMessage(
null,
`修改小说任务状态成功,修改后的状态为${status}`,
'BookTaskService_UpdateBookTaskStatus'
)
} catch (error) {
throw error
}
}
// 添加一条数据
async AddOrModifyBookTask(bookTask) {
AddOrModifyBookTask(bookTask) {
try {
await this.open()
if (bookTask == null) {
throw new Error('添加的小说任务不能为空')
}