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


/**
  * 游戏中的任务完成后，增加的积分点数关联到活动数据
*/
@index({ taskId: 1 })

export default class Activity_Task_Point extends BaseModel {
    // @prop({ required: true })
    // serverId: number;				// 服Id
    @prop({ required: true })
    taskType: number;				    // 任务类型TASK_FUN_TYPE 1.主线，2每日，3.成就
    @prop({ required: true })
    taskId: number;				    // 任务id
    @prop({ required: true })
    activityId: number;				// 关联的活动id
    @prop({ required: true })
    point: number;			        // 增加点数

    //更新活动数据
    public static async updateData(taskType: number, taskId: number, activityId: number, point: number, uid = 1) {
        let result: ActivityTaskPointModelType = await ActivityTaskPointModel.findOneAndUpdate(
            { taskType, taskId, activityId }, { $set: { point, updatedBy: uid }, $setOnInsert: { createdBy: uid } }, { upsert: true, new: true }
        ).lean(true);
        return result;
    }

    //查询数据
    public static async findData(taskType: number, taskId: number) {
        let result: ActivityTaskPointModelType[] = await ActivityTaskPointModel.find(
            { taskType, taskId }).lean(true);
        return result;
    }

    //删除数据
    public static async deleteData(taskType: number, taskId: number, activityId: number) {
        await ActivityTaskPointModel.deleteOne({ taskType, taskId, activityId });
    }

    public static async createDataIfNotExist(taskType: number, taskId: number, activityId: number, point: number, uid = 1) {
        let result: ActivityTaskPointModelType = await ActivityTaskPointModel.findOneAndUpdate(
            { taskType, taskId, activityId }, { $setOnInsert: { point, createdBy: uid, updatedBy: uid } }, { upsert: true, new: true }
        ).lean(true);
        return result;
    }

    private static getSearchObj(form: { taskType?: number, taskId?: number, activityId?: number }) {
        let searchObj = {};
        if (form['taskType']) searchObj['taskType'] = form.taskType;
        if (form['taskId']) searchObj['taskId'] = form.taskId;
        if (form['activityId']) searchObj['activityId'] = form.activityId;
        return searchObj
    }

    public static async findByCondition(page: number, pageSize: number, sortField: string = 'updatedAt', sortOrder: string = 'descend', form: { taskType?: number, taskId?: number, activityId?: number } = {}) {

        let searchObj = this.getSearchObj(form);
        let sort = {};
        if (sortField && sortOrder) {
            if (sortOrder == 'ascend') {
                sort[sortField] = 1;
            } else if (sortOrder == 'descend') {
                sort[sortField] = -1;
            }
        }
        const result: ActivityTaskPointModelType[] = await ActivityTaskPointModel.find(searchObj).limit(pageSize).skip((page - 1) * pageSize).sort(sort).lean({ getters: true, virtuals: true });
        return result;

    }

    public static async countByCondition(form: { taskType?: number, taskId?: number, activityId?: number } = {}) {

        let searchObj = this.getSearchObj(form);
        const result = await ActivityTaskPointModel.count(searchObj);
        return result;
    }

}

export const ActivityTaskPointModel = getModelForClass(Activity_Task_Point);

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