Files
ZYZ/shared/db/Link.ts
2023-04-12 20:47:35 +08:00

50 lines
2.2 KiB
TypeScript
Raw Permalink 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 BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
@index({ type: 1 })
@index({ serverId: 1 })
export default class Link extends BaseModel {
@prop({ required: true, default: 0 })
type: number; // 1-微信群2-手机绑定 3-公众号关注 4-论坛 5-客服
@prop({ required: true, default: 0 })
serverId: number; // 小区id不限的为0
@prop({ required: true, default: '' })
link: string; // 外链
@prop({ required: true, default: '' })
androidLink: string; // 安卓外链
@prop({ required: true, default: '' })
qrCodeLink: string; // 微信群二维码
public static async findByServerId(serverId: number) {
const result: LinkType[] = await LinkModel.find({ serverId: { $in: [serverId, 0] } }).sort({ serverId: 1 }).lean();
return result;
}
public static async findByType(type: number) {
const result: LinkType = await LinkModel.findOne({ type }).select('-_id -__v').lean();
return result;
}
public static async findByTypes(types: number[]) {
const result: LinkType[] = await LinkModel.find({ type: { $in: types } }).select('-_id -__v').sort({ serverId: 1 }).lean();
return result;
}
public static async updateByServerAndType(serverId: number, type: number, param: LinkUpdateInter, uid: number) {
const result: LinkType = await LinkModel.findOneAndUpdate({ serverId, type }, { $set: {...param, updatedBy: uid}, $setOnInsert: { createdBy: uid } }, { new: true, upsert: true }).lean();
return result;
}
public static async updateLinks(updateArr: { type: number, link?: string, qrCodeLink?: string, androidLink?: string }[], uid: number) {
await LinkModel.bulkWrite(updateArr.map(({type, link, qrCodeLink, androidLink }) => {
return { updateOne: { filter: { type, serverId: 0 }, update: { $set: { link, qrCodeLink, androidLink, updatedBy: uid }, $setOnInsert: { createdBy: uid } }, upsert: true} }
}))
}
}
export const LinkModel = getModelForClass(Link);
export interface LinkType extends Pick<DocumentType<Link>, keyof Link> { };
export type LinkUpdateInter = Partial<LinkType>;