LaiTool/src/main/Service/ServiceBasic/bookBackTaskServiceBasic.ts
lq1405 6fa58e4d94 V 3.2.1 2024.11.9
1. (聚合推文)修复原创默认出图方式设置默认值
2. (聚合推文)优化出图显示
3. (聚合推文)添加图片上传功能,包括主图和选图区域
4. (聚合推文)新增图片缓存区,上传缓存图片(主图和选图区的图片),可以直接在当前小说所有的批次中的分镜中调用(下载到主图和选图区)
5. (聚合推文)添加一键修脸开关
6. (聚合推文)原创添加一键锁定
7. (聚合推文)新增小说批次任务显示当前所属小说
8. (聚合推文)小说批次任务状态显示优化
9. (聚合推文)优化后台任务状态显示
10. (聚合推文)原创,反推 一键生图 修复
2024-11-09 16:46:06 +08:00

185 lines
6.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<TaskModal.Task> {
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<number> {
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
}
}
}