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


/**
  * 记录
*/

export class Record {
    @prop({ required: true })
    resource: string; //类型&资源id&数量
    @prop({ required: true })
    type: number; //1,存入，2取出
    @prop({ required: true })
    time: Date; //购买时间
}



/**
  * 活动系统 - 大富翁
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_Monopoly_Land extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服Id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    position: number;			    // 位置
    @prop({ required: true, default: 1 })
    level: number;			        // 等级
    @prop({ required: true, default: [] })
    record: Record[];			    // 记录
    @prop({ required: true })
    stopCount: number;			// 停留次数


    //更新等级
    public static async updateLevel(serverId: number, activityId: number, roleId: string, position: number, level: number) {
        let result: ActivityMonopolyLandModelType = await ActivityMonopolyLandModel.findOneAndUpdate({
            serverId, roleId, activityId, position
        },
            { $set: { level } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //更新停留次数
    public static async updateStopCount(serverId: number, activityId: number, roleId: string, position: number) {
        let result: ActivityMonopolyLandModelType = await ActivityMonopolyLandModel.findOneAndUpdate({
            serverId, roleId, activityId, position
        },
            { $inc: { stopCount: 1 } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //添加记录
    public static async addRecord(serverId: number, activityId: number, roleId: string, position: number, type: number, resource: string) {
        let result: ActivityMonopolyLandModelType = await ActivityMonopolyLandModel.findOneAndUpdate({
            serverId, roleId, activityId, position
        },
            { $push: { record: { resource, type, time: new Date() } } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //查询数据
    public static async findData(serverId: number, activityId: number, roleId: string) {
        let result: ActivityMonopolyLandModelType[] = await ActivityMonopolyLandModel.find({ serverId, roleId, activityId }).lean(true);
        return result;
    }

    //查询数据
    public static async findDataByPosition(serverId: number, activityId: number, roleId: string, position: number) {
        let result: ActivityMonopolyLandModelType = await ActivityMonopolyLandModel.findOne({ serverId, roleId, activityId, position }).lean(true);
        return result;
    }

    //删除活动记录
    public static async deleteActivity(serverId: number, activityId: number, roleId: string) {
        await ActivityMonopolyLandModel.deleteMany({ serverId, roleId, activityId });
    }
}

export const ActivityMonopolyLandModel = getModelForClass(Activity_Monopoly_Land);

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