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


/**
  * 活动系统 - 团购总记录
*/
@index({ activityId: 1, uid: 1 })

export default class Activity_Bind_Phone_Reward extends BaseModel {

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

  @prop({ required: true })
  uid: number;			            // 玩家账号id

  @prop({ required: true })
  status: number;			            // 状态 0-未绑定 1-已绑定 2-已领取

  @prop({ required: true })
  serverId: number;			            // 领取时的服务器id

  @prop({ required: true })
  roleId: string;			            // 领取时的账号

  @prop({ required: true })
  roleName: string;			            // 领取时的账号名

  public static async findByUid(activityId: number, uid: number) {
    let result: ActivityBindPhoneRewardType = await ActivityBindPhoneRewardModel.findOne({ activityId, uid }).lean();
    return result;
  }

  public static async addRecord(activityId: number, uid: number, status: number, param: ActivityBindPhoneRewardUpdate) {
    let result: ActivityBindPhoneRewardType = await ActivityBindPhoneRewardModel.findOneAndUpdate({ activityId, uid }, { $set: { status, ...param} }, { new: true, upsert: true }).lean();
    return result;
  }
}

export const ActivityBindPhoneRewardModel = getModelForClass(Activity_Bind_Phone_Reward);

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