import { BookBackTaskStatus, BookBackTaskType, TaskExecuteType } from "../../../define/enum/bookEnum"; import { BookBackTaskListService } from "../../../define/db/service/Book/bookBackTaskListService"; import { Book } from "../../../model/book/book"; import { TaskModal } from "@/model/task"; import { cloneDeep, isEmpty } from "lodash"; export default class BookBackTaskServiceBasic { bookBackTaskListService: BookBackTaskListService constructor() { } private async InitService() { if (!this.bookBackTaskListService) { this.bookBackTaskListService = await BookBackTaskListService.getInstance() } } /** * 添加一个后台任务 * @param bookId 小说ID * @param taskType 后台任务类型 * @param executeType 执行的类型,是不是自动执行 * @param bookTaskId 小说批次任务ID * @param bookTaskDetailId 小说批次任务分镜ID * @param responseMessageName 消息名称 */ async AddBookBackTask(bookId: string, taskType: BookBackTaskType, executeType = TaskExecuteType.AUTO, bookTaskId = null, bookTaskDetailId = null, responseMessageName: string = null ): Promise { await this.InitService(); let res = this.bookBackTaskListService.AddBookBackTask(bookId, taskType, executeType, bookTaskId, bookTaskDetailId, responseMessageName) if (res.code == 0) { throw new Error(res.message) } return res.data as TaskModal.Task } /** * 获取指定状态的任务数量 * @param status * @returns */ async GetAllStatusTaskCount(status: BookBackTaskStatus[]): Promise { await this.InitService(); let count = this.bookBackTaskListService.GetAllStatusTaskCount(status) return count; } /** * 修改后台队列的状态 * @param bookBackTask 要修改的数据 */ async UpdateTaskStatus(bookBackTask: Book.UpdateBookTaskListStatus) { await this.InitService(); this.bookBackTaskListService.UpdateTaskStatus(bookBackTask) } /** * 设置指定消息名称的任务为失败,并设置错误消息 * @param messageName * @param errorMessage */ async SetMessageNameTaskToFail(messageName: string, errorMessage: string) { console.log(messageName, errorMessage) await this.InitService(); let tasks = this.bookBackTaskListService.realm.objects('BookBackTaskList').filtered('messageName == $0 ', messageName) tasks = tasks.filtered('status != $0', BookBackTaskStatus.DONE); tasks = tasks.filtered('status != $0', BookBackTaskStatus.FAIL); let ids = tasks.map((item) => { return item.id }) this.bookBackTaskListService.transaction(() => { for (let i = 0; i < ids.length; i++) { let task = this.bookBackTaskListService.realm.objectForPrimaryKey('BookBackTaskList', ids[i]); task.status = BookBackTaskStatus.FAIL task.errorMessage = errorMessage } }) } /** * 丢弃等待中和重新连接的任务 */ async GiveUpNotStartBackTask() { await this.InitService(); let task = this.bookBackTaskListService.realm.objects('BookBackTaskList').filtered('status == $0 || status == $1', BookBackTaskStatus.WAIT, BookBackTaskStatus.RECONNECT); let ids = task.map((item) => { return item.id }) // 讲所有的人物状态改为放弃,然后errmessage改为 此任务已丢弃 this.bookBackTaskListService.transaction(() => { for (let i = 0; i < ids.length; i++) { const element = this.bookBackTaskListService.realm.objectForPrimaryKey('BookBackTaskList', ids[i]); element.status = BookBackTaskStatus.FAIL element.errorMessage = '任务被丢弃' } }) } /** * 查询指定的条件的后台任务集合 * @param queryTaskCondition */ GetBackTaskCollection(queryTaskCondition: TaskModal.QueryTaskCondition): TaskModal.TaskCollection { let tasks = this.bookBackTaskListService.realm.objects('BookBackTaskList'); if (!isEmpty(queryTaskCondition.bookName)) { let book = this.bookBackTaskListService.realm.objects('Book').filtered('name CONTAINS[c] $0', queryTaskCondition.bookName); let ids = [] as string[] if (book.length > 0) { ids = book.map((item) => { return item.id as string }) } tasks = tasks.filtered('bookId in $0', ids); } if (!isEmpty(queryTaskCondition.bookTaskName)) { let bookTask = this.bookBackTaskListService.realm.objects('BookTask').filtered('name CONTAINS[c] $0', queryTaskCondition.bookTaskName); let ids = [] as string[] if (bookTask.length > 0) { ids = bookTask.map((item) => { return item.id as string }) } tasks = tasks.filtered('bookTaskId in $0', ids); } if (!isEmpty(queryTaskCondition.bookTaskDetailName)) { let bookTaskDetail = this.bookBackTaskListService.realm.objects('BookTaskDetail').filtered('name CONTAINS[c] $0', queryTaskCondition.bookTaskDetailName); let ids = [] as string[] if (bookTaskDetail.length > 0) { ids = bookTaskDetail.map((item) => { return item.id as string }) } tasks = tasks.filtered('bookTaskDetailId in $0', ids); } if (!isEmpty(queryTaskCondition.taskName)) { tasks = tasks.filtered('name CONTAINS[c] $0', queryTaskCondition.taskName); } if (!isEmpty(queryTaskCondition.taskType)) { tasks = tasks.filtered('type == $0', queryTaskCondition.taskType); } if (!isEmpty(queryTaskCondition.taskStatus)) { tasks = tasks.filtered('status == $0', queryTaskCondition.taskStatus); } if (!isEmpty(queryTaskCondition.taskErrorMessage)) { tasks = tasks.filtered('errorMessage CONTAINS[c] $0', queryTaskCondition.taskErrorMessage); } let count = tasks.length tasks = tasks.sorted('createTime', true) let task = tasks.slice((queryTaskCondition.page - 1) * queryTaskCondition.pageSize, queryTaskCondition.page * queryTaskCondition.pageSize) as TaskModal.BackTaskCollection[]; let taskList = Array.from(task).map((item) => { let resObj = { ...item, } as TaskModal.BackTaskCollection return cloneDeep(resObj) }) for (let i = 0; i < taskList.length; i++) { const element = taskList[i]; let book = this.bookBackTaskListService.realm.objectForPrimaryKey('Book', element.bookId) if (book) { element.bookName = book.name as string; } let bookTask = this.bookBackTaskListService.realm.objectForPrimaryKey('BookTask', element.bookTaskId); if (bookTask) { element.bookTaskName = bookTask.name as string; } let bookTaskDetail = this.bookBackTaskListService.realm.objectForPrimaryKey('BookTaskDetail', element.bookTaskDetailId); if (bookTaskDetail) { element.bookTaskDetailName = bookTaskDetail.name as string; } } return { page: queryTaskCondition.page, pageSize: queryTaskCondition.pageSize, count: count, data: taskList } } }