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


/**
  * 活动系统 - 弹出商店
*/
@index({ roleId: 1 })

export default class Activity_Pop_Up_Shop extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    beginTime: Date;				// 开始时间
    @prop({ required: true })
    endTime: Date;				    // 结束时间
    @prop({ required: true })
    id: number;				        // 任务id
    @prop({ required: true })
    type: number;				    // 任务类型
    @prop({ required: true })
    totalCount: number;				// 累计达成次数
    @prop({ required: true })
    data: string;					// 数据信息
    @prop({ required: true })
    buyCount: number;		    // 购买次数
    @prop({ required: true })
    isPush: boolean;		    // 是否推送过

    //购买记录
    public static async addRecord(serverId: number, activityId: number, roleId: string, id: number, type: number, buyCount: number, beginTime: Date) {
        let result: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOneAndUpdate({
            serverId, roleId, activityId, id, type,
            beginTime: beginTime
        },
            { $inc: { buyCount: buyCount } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //查询现在正在进行的活动数据
    public static async findAllOpenData(serverId: number, activityId: number, roleId: string) {
        let nowDate = new Date();
        let result: ActivityPopUpShopModelType[] = await ActivityPopUpShopModel.find({
            serverId, roleId, activityId,
            beginTime: { $lt: nowDate }, endTime: { $gt: nowDate },
            isBuy: { $ne: true },
        }).lean(true);
        return result;
    }

    //根据活动taskId查询现在正在进行的活动数据
    public static async findDataByTaskId(serverId: number, activityId: number, roleId: string, id: number, type: number) {
        let result: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOne({
            serverId, roleId, activityId, id, type,
        }).lean(true);
        return result;
    }

    //根据活动开始时间查询现在正在开启的商店数据
    public static async findDataByBeginTime(serverId: number, activityId: number, roleId: string, id: number, type: number, beginTime: Date) {
        console.log('beginTime', serverId, roleId, activityId, id, type, beginTime)
        let result: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOne({
            serverId, roleId, activityId, id, type, beginTime: beginTime
        }).lean(true);
        return result;
    }


    //根据活动统计完成任务次数
    public static async setTaskCount(serverId: number, activityId: number, roleId: string, id: number, type: number, count: number) {
        let result: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOneAndUpdate({ serverId, roleId, activityId, id, type },
            { $set: { totalCount: count } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //根据活动统计完成任务次数
    public static async addTaskCount(serverId: number, activityId: number, roleId: string, id: number, type: number, addCount: number) {
        let result: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOneAndUpdate({ serverId, roleId, activityId, id, type },
            { $inc: { totalCount: addCount } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //根据活动记录统计数据
    public static async addTaskRecord(serverId: number, activityId: number, roleId: string, id: number, type: number, data: string,) {
        let result: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOneAndUpdate({ serverId, roleId, activityId, id, type },
            { $set: { data: data } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //标记push消息
    public static async pushMessage(serverId: number, activityId: number, roleId: string, id: number, type: number, beginTime: Date, endTime: Date) {
        let result: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOneAndUpdate({ serverId, roleId, activityId, id, type },
            { $set: { isPush: true, beginTime, endTime } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //添加数据并标记push消息
    public static async addTaskPushMessage(serverId: number, activityId: number, roleId: string, id: number, type: number, totalCount: number, beginTime: Date, endTime: Date) {
        let result: ActivityPopUpShopModelType = await ActivityPopUpShopModel.findOneAndUpdate({ serverId, roleId, activityId, id, type, totalCount, beginTime, endTime },
            { $set: { isPush: true } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //添加数据并标记push消息
    public static async addTaskArrayPushMessage(serverId: number, activityId: number, roleIds: string[], id: number, type: number, totalCount: number, beginTime: Date, endTime: Date) {
        let activityData = [];
        for (let roleId of roleIds) {
            activityData.push(
                { serverId, roleId, activityId, id, type, totalCount, beginTime, endTime, isPush: true, buyCount: 0, data: JSON.stringify({ totalCount }) }
            )
        }
        await ActivityPopUpShopModel.insertMany(activityData);
    }

    //删除活动领取记录
    public static async deleteActivity(_serverId: number, activityId: number, roleId: string, id: number) {
        let nowDate = new Date();
        await ActivityPopUpShopModel.deleteMany({
            roleId, activityId, id,
            beginTime: { $lt: nowDate }, endTime: { $gt: nowDate }
        });
    }
}

export const ActivityPopUpShopModel = getModelForClass(Activity_Pop_Up_Shop);

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