LaiTool/src/main/Service/ServiceBasic/bookBackTaskServiceBasic.ts

185 lines
6.9 KiB
TypeScript
Raw Normal View History

import { BookBackTaskStatus, BookBackTaskType, TaskExecuteType } from "../../../define/enum/bookEnum";
import { BookBackTaskListService } from "../../../define/db/service/Book/bookBackTaskListService";
import { Book } from "../../../model/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
}
}
}