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


/**
  * 活动系统 - 成长任务活动积分点兑换奖励
*/
@index({ guildCode: 1, activityId: 1, roundIndex: 1 })

export default class Activity_Guild_Pay_Record extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 区服id

    @prop({ required: true })
    activityId: number;				// 活动Id

    @prop({ required: true })
    roundIndex: number;				// 活动第几轮

    @prop({ required: true })
    roleId: string;			        // 军团code

    @prop({ required: true, type: Number })
    receiveRecord: number[];	    // 领取记录， cellIndex


    //根据活动id查询活动数据
    public static async findData(serverId: number, activityId: number, roundIndex: number, roleId: string) {
      let result: ActivityGuildPayRecordModelType = await ActivityGuildPayRecordModel.findOne({ serverId, roleId, activityId, roundIndex }).lean(true);
      return result;
    }

    public static async receiveItems(serverId: number, activityId: number, roundIndex: number, roleId: string, records: number[]) {
      let result: ActivityGuildPayRecordModelType = await ActivityGuildPayRecordModel.findOneAndUpdate(
        { serverId, roleId, activityId, roundIndex },
        { $push: { receiveRecord: { $each: records } } },
        { upsert: true, new: true }
      ).lean(true);
      return result;
    }
}

export const ActivityGuildPayRecordModel = getModelForClass(Activity_Guild_Pay_Record);

export interface ActivityGuildPayRecordModelType extends Pick<DocumentType<Activity_Guild_Pay_Record>, keyof Activity_Guild_Pay_Record> { }
export type ActivityGuildPayRecordModelTypeParam = Partial<ActivityGuildPayRecordModelType>; // 将所有字段变成可选项