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

227 lines
7.2 KiB
TypeScript
Raw Normal View History

2024-06-24 13:11:19 +08:00
import Realm from 'realm'
import path from 'path'
import { BaseService } from '../baseService.js'
import { define } from '../../../define.js'
import { BookTaskModel } from '../../model/Book/bookTask.js'
import { BookTaskStatus } from '../../../enum/bookEnum.js'
import { successMessage } from '../../../../main/generalTools.js'
import { BaseRealmService } from './bookBasic'
2024-06-27 16:24:41 +08:00
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'
2024-07-13 15:44:13 +08:00
import { BookTaskDetailModel } from '../../model/Book/bookTaskDetail.js'
2024-06-24 13:11:19 +08:00
const { v4: uuidv4 } = require('uuid')
let dbPath = path.resolve(define.db_path, 'book.realm')
// 版本迁移
const migration = (oldRealm: Realm, newRealm: Realm) => {}
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
*/
GetBookTaskData(condition) {
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-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)
})
}
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
}
}
/**
*
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()
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
*/
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('删除小说分镜信息失败,没有必要参数')
}
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
}
}
}