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

/**
  * 累计充值活动 
*/
@index({ channelId: 1 })
@index({ id: 1 })

export default class HistoryOrder extends BaseModel {

    @prop({ required: true })
    serverId: number;		    // 区号

    @prop({ required: true })
    channelId: string;		    // 37账号id

    @prop({ required: true })
    roleId: string;		        // 当时的玩家id

    @prop({ required: true })
    roleName: string;		    // 当时的玩家名

    @prop({ required: true })
    totalPay: number;		    // 总支付金额

    @prop({ required: true })
    isReceived: boolean;        // 是否领取

    @prop({ required: true })
    receiveRoleId: string;              // 领取的玩家id

    public static async findByChannelId(channelId: string) {
        let result: HistoryOrderModelType[] = await HistoryOrderModel.find({ channelId }).lean();
        return result;
    }

    public static async receive(id: string, roleId: string) {
      await HistoryOrderModel.findByIdAndUpdate(id, { $set: { isReceived: true, receiveRoleId: roleId } }, { new: true }).lean();
  }
}

export const HistoryOrderModel = getModelForClass(HistoryOrder);

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