87 lines
3.6 KiB
TypeScript
87 lines
3.6 KiB
TypeScript
import BaseModel from './BaseModel';
|
|
import { index, getModelForClass, prop, DocumentType, Ref } from '@typegoose/typegoose';
|
|
import Role, { RoleType } from './Role';
|
|
import Guild, { GuildType } from './Guild';
|
|
import { GUILD_AUTH, USER_GUILD_STATUS, GUILD_JOB, GUILD_APPLY_STATUS, GUILD_APPLY_TYPE, GUILD_PER_PAGE, GUILD_STATUS } from '../consts';
|
|
import { genCode } from '../pubUtils/util';
|
|
|
|
@index({ roleId: 1 })
|
|
export default class UserGuildApply extends BaseModel {
|
|
@prop({ required: true })
|
|
applyCode: string; // 唯一code
|
|
|
|
@prop({ required: true })
|
|
roleId: string; // 玩家id
|
|
|
|
@prop({ required: true })
|
|
role: Ref<Role>;
|
|
|
|
@prop({ required: true })
|
|
guildCode: string; // 公会唯一code
|
|
|
|
@prop({ required: true })
|
|
guild: Ref<Guild>;
|
|
|
|
@prop({ required: true, enum: GUILD_APPLY_TYPE, select: false})
|
|
type: number;
|
|
|
|
public static async createUserGuildApply(role: RoleType, guild: GuildType, type: number) {
|
|
const applyCode = genCode(10);
|
|
const doc = new UserGuildApplyModel();
|
|
const update = Object.assign(doc.toJSON(), { applyCode, roleId: role.roleId, role: role._id, guildCode: guild.code, guild: guild._id, type });
|
|
delete update._id;
|
|
const result: UserGuildApplyType = await UserGuildApplyModel.findOneAndUpdate({ applyCode }, update, { upsert: true, new: true }).lean();
|
|
|
|
return result;
|
|
}
|
|
|
|
public static async deleteApply(roleId: string) {
|
|
const result = await UserGuildApplyModel.deleteMany({ roleId, type: GUILD_APPLY_TYPE.APPLY });
|
|
return result;
|
|
}
|
|
|
|
public static async deleteApplyByGuild(guildCode: string) {
|
|
const result = await UserGuildApplyModel.deleteMany({ guildCode, type: GUILD_APPLY_TYPE.APPLY });
|
|
return result;
|
|
}
|
|
|
|
public static async deleteApplyByApplyCode(applyCodeList: string[]) {
|
|
const result = await UserGuildApplyModel.deleteMany({ applyCode: { $in: applyCodeList}, type: GUILD_APPLY_TYPE.APPLY });
|
|
return result;
|
|
}
|
|
|
|
public static async findByCode(applyCode: string) {
|
|
const userGuildApply: UserGuildApplyType = await UserGuildApplyModel.findOne({ applyCode }).lean();
|
|
return userGuildApply;
|
|
}
|
|
|
|
public static async findApplyByRole(roleId: string) {
|
|
const userGuildApply: UserGuildApplyType[] = await UserGuildApplyModel.find({ roleId, type: GUILD_APPLY_TYPE.APPLY }).lean();
|
|
return userGuildApply;
|
|
}
|
|
|
|
public static async getListByApplyCode(applyCodeList: string[]) {
|
|
const userGuildApply: UserGuildApplyType[] = await UserGuildApplyModel.find({ applyCode: { $in: applyCodeList}, type: GUILD_APPLY_TYPE.APPLY }).lean();
|
|
return userGuildApply;
|
|
}
|
|
|
|
public static async getListByGuild(code: string, lastApplyCode: string) {
|
|
let condition = { guildCode: code, type: GUILD_APPLY_TYPE.APPLY };
|
|
if(lastApplyCode) {
|
|
const lastApply = await this.findByCode(lastApplyCode);
|
|
condition["createdAt"] = { $lt: lastApply.createdAt };
|
|
}
|
|
const list: UserGuildApplyType[] = await UserGuildApplyModel.find(condition)
|
|
.sort({ createdAt: -1 })
|
|
.select('applyCode role')
|
|
.populate('role', 'roleId roleName ce headHid sHid lv title job loginTime', 'Role')
|
|
.limit(GUILD_PER_PAGE).lean();
|
|
return list
|
|
}
|
|
}
|
|
|
|
export const UserGuildApplyModel = getModelForClass(UserGuildApply);
|
|
|
|
export interface UserGuildApplyType extends Pick<DocumentType<UserGuildApply>, keyof UserGuildApply> { };
|
|
export type UserGuildApplyUpdateParam = Partial<UserGuildApplyType>; // 将所有字段变成可选项
|