69 lines
2.6 KiB
TypeScript
69 lines
2.6 KiB
TypeScript
import BaseModel from './BaseModel';
|
|
import { index, getModelForClass, prop, DocumentType } 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, select = '-_id') {
|
|
let result: AuthorBookType[] = await AuthorBookModel.find({ roleId }).select(select).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;
|
|
}
|
|
|
|
public static async findProgressByRoleId(roleId:string, select = '-_id') {
|
|
let progressCount = 0;
|
|
let result: AuthorBookType[] = await AuthorBookModel.find({ roleId }).select(select).lean();
|
|
for(let {progress} of result){
|
|
progressCount+=progress;
|
|
}
|
|
return progressCount;
|
|
}
|
|
|
|
}
|
|
|
|
export const AuthorBookModel = getModelForClass(AuthorBook);
|
|
|
|
export interface AuthorBookType extends Pick<DocumentType<AuthorBook>, keyof AuthorBook> { }
|
|
export type AuthorBookParam = Partial<AuthorBookType>;
|