import { getModelForClass, index, modelOptions, prop } from '@typegoose/typegoose' import { dbconn } from 'decorators/dbconn' import { BaseModule } from './Base' import { formatDate, yesterday } from 'zutils/utils/date.util' import logger from 'logger/logger' @dbconn() @index({ from: 1 }, { unique: false }) @index({ hash: 1 }, { unique: true }) @index({ from: 1, dateTag: 1 }, { unique: true }) @index({ from: 1, blockTime: 1 }, { unique: false }) @modelOptions({ schemaOptions: { collection: 'check_in_event', timestamps: true }, }) export class CheckInClass extends BaseModule { @prop({ required: true }) public from!: string @prop() public to: string @prop({ required: true }) public hash: string @prop() public blockNumber: string @prop() public blockHash: string @prop() public blockTime: number @prop() public dateTag: string // 连签天数 @prop({ default: 0 }) public count: number @prop() public value: string @prop() public input: string public static async saveEvent(event: any) { const preDay = formatDate(yesterday()) const preDayEvent = await CheckIn.findOne({ from: event.from, dateTag: preDay }) if (preDayEvent) { event.count = preDayEvent.count + 1 } try { await CheckIn.insertOrUpdate({ from: event.from, dateTag: event.dateTag }, event) } catch (err) { logger.info('save check record error: ', event.dateTag, event.from, err.message || err) } } public toJson() { return { address: this.from, day: this.dateTag, time: this.blockTime, count: this.count, } } } export const CheckIn = getModelForClass(CheckInClass, { existingConnection: CheckInClass['db'], })