import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { nowSeconds, getTimeFun } from '../pubUtils/timeUtil';

@index({ guildCode: 1, dntTime: -1 })

export default class WishPoolReport extends BaseModel {
    @prop({ required: true })
    guildCode: string;					// 军团code

    @prop({ required: true })
    dntTime: number;                   //捐献时间

    @prop({ required: true })
    wishRoleId: string;                    //许愿者

    @prop({ required: true })
    wishRoleName: string;                  //许愿者name

    @prop({ required: true })
    goodId: number;                    //捐献物品

    @prop({ required: true })
    count: number;                    //捐献物品数量

    @prop({ required: true })
    dntRoleId: string;                 //捐赠人

    @prop({ required: true })
    dntRoleName: string;              //捐赠人name

    public static async addReport(guildCode: string, wishRoleId: string, wishRoleName: string, dntRoleId: string, dntRoleName: string, goodId: number, count: number) {
        const doc = new WishPoolReportModel();
        const report = Object.assign(doc.toJSON(), { guildCode, wishRoleId, wishRoleName, dntRoleId, dntRoleName, goodId, dntTime: nowSeconds(), count });
        await WishPoolReportModel.create(report);
        return report;
    }
    //获得day天之前的许愿池战报
    public static async getReportsByTime(guildCode: string, day: number) {
        let time = <number>getTimeFun().getBeforeDayWithHour(day);
        const reports = await WishPoolReportModel.find({ guildCode,  dntTime: {$gte: time}}).sort({ dntTime : -1 });
        return reports;
    }
}

export const WishPoolReportModel = getModelForClass(WishPoolReport);

export interface WishPoolReportType extends Pick<DocumentType<WishPoolReport>, keyof WishPoolReport>{}