71 lines
1.8 KiB
TypeScript
71 lines
1.8 KiB
TypeScript
import { getModelForClass, index, modelOptions, prop, ReturnModelType } from '@typegoose/typegoose'
|
|
import { BaseModule } from '../Base'
|
|
import { dbconn } from '../../decorators/dbconn'
|
|
|
|
@dbconn()
|
|
@index({ accountId: 1 }, { unique: true })
|
|
@modelOptions({ schemaOptions: { collection: 'game_user', timestamps: true } })
|
|
class GameUserClass extends BaseModule {
|
|
@prop()
|
|
public accountId: string
|
|
@prop()
|
|
public nickname: string
|
|
@prop()
|
|
public avatar: string
|
|
|
|
/**
|
|
* 手机号
|
|
* @type {string}
|
|
*/
|
|
@prop()
|
|
public mobile: string
|
|
|
|
@prop({ default: 0 })
|
|
public sex?: string
|
|
@prop({ default: 'CN' })
|
|
public country?: string
|
|
@prop()
|
|
public province?: string
|
|
@prop()
|
|
public city?: string
|
|
@prop({ default: false })
|
|
public locked: boolean
|
|
@prop()
|
|
public lockedTime?: Date
|
|
/**
|
|
* 该帐号关联的所有店铺
|
|
* @type {string[]}
|
|
*/
|
|
@prop({ type: () => [String] })
|
|
public shops: string[]
|
|
/**
|
|
* 当前关联店铺
|
|
* @type {string}
|
|
*/
|
|
@prop()
|
|
public shop: string
|
|
|
|
public static async getByAccountID(this: ReturnModelType<typeof GameUserClass>, accountId: string) {
|
|
let records = await this.find({ accountId }).limit(1)
|
|
return records.length > 0 ? records[0] : null
|
|
}
|
|
|
|
public static async userMapByAccountIDS(this: ReturnModelType<typeof GameUserClass>, accountIds: string[]) {
|
|
const records = await this.find({ accountId: { $in: accountIds } })
|
|
let map: Map<string, any> = new Map()
|
|
for (let record of records) {
|
|
map.set(record.accountId, record)
|
|
}
|
|
return map
|
|
}
|
|
|
|
public updateFromReq(data: any) {
|
|
super.updateFromReq(data)
|
|
if (data.shop) {
|
|
this.shops.pushOnce(data.shop)
|
|
}
|
|
}
|
|
}
|
|
|
|
export const GameUser = getModelForClass(GameUserClass, { existingConnection: GameUserClass.db })
|