✨ feat(诸子列传): 添加功能
This commit is contained in:
59
shared/db/AuthorBook.ts
Normal file
59
shared/db/AuthorBook.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import BaseModel from './BaseModel';
|
||||
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
|
||||
|
||||
/**
|
||||
* 诸子百家
|
||||
**/
|
||||
|
||||
class Author {
|
||||
@prop({ required: true, default: 0 })
|
||||
subId: number; // 条目id
|
||||
|
||||
@prop({ required: true, default: 0 })
|
||||
star: number; // 星级
|
||||
}
|
||||
|
||||
@index({ roleId: 1, bookId: 1 })
|
||||
|
||||
export default class AuthorBook extends BaseModel {
|
||||
@prop({ required: true, default: '' })
|
||||
roleId: string; // 举报人的 roleId
|
||||
|
||||
@prop({ required: true, default: '' })
|
||||
bookId: number; // 列传id
|
||||
|
||||
@prop({ required: true, default: '' })
|
||||
progress: number; // 进度
|
||||
|
||||
@prop({ required: true, default: [], type: Author, _id: false })
|
||||
authors: Author[];
|
||||
|
||||
public static async findByRoleId(roleId: string) {
|
||||
let result: AuthorBookType[] = await AuthorBookModel.find({ roleId }).select('-_id -roleId').lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async findByBookId(roleId: string, bookId: number) {
|
||||
let result: AuthorBookType = await AuthorBookModel.findOne({ roleId, bookId }).select('-_id').lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
// 升星
|
||||
public static async upStar(roleId: string, bookId: number, subId: number, star: number, addProgress: number, initAuthors: number[]) {
|
||||
await AuthorBookModel.findOneAndUpdate({ roleId, bookId }, { $setOnInsert: { progress: 0, authors: initAuthors.map(subId => ({ subId, star: 0 })) } }, { upsert: true });
|
||||
let result: AuthorBookType = await AuthorBookModel.findOneAndUpdate({ roleId, bookId, 'authors.subId': subId, 'authors.star': star }, { $inc: { progress: addProgress, 'authors.$.star': 1 } }, { new: true }).lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
// 重置目录
|
||||
public static async resetSub(roleId: string, bookId: number, subId: number, oldStar: number, incProgress: number) {
|
||||
let result: AuthorBookType = await AuthorBookModel.findOneAndUpdate({ roleId, bookId, 'authors.subId': subId, 'authors.star': oldStar }, { $set: {'authors.$.star': 0}, $inc: { progress: incProgress } }, { new: true }).lean();
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export const AuthorBookModel = getModelForClass(AuthorBook);
|
||||
|
||||
export interface AuthorBookType extends Pick<DocumentType<AuthorBook>, keyof AuthorBook> { }
|
||||
export type AuthorBookParam = Partial<AuthorBookType>;
|
||||
Reference in New Issue
Block a user