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


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

export default class Activity_Task_Point extends BaseModel {
    // @prop({ required: true })
    // serverId: number;				// 服Id
    @prop({ required: true })
    type: number;				    // 任务类型TASK_FUN_TYPE 1.主线，2每日，3.成就

    @prop({ required: true })
    taskType: number;				    // 任务type TASK_TYPE
    @prop({ required: true })
    taskId: number;				    // 任务id
    @prop({ required: true })
    activityId: number;				// 关联的活动id
    @prop({ required: true })
    activityType: number;			// 活动类型
    @prop({ required: true })
    point: number;			        // 增加点数

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

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

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

    public static async createDataIfNotExist(type: number, taskType: number, taskId: number, activityId: number, activityType: number, point: number, uid = 1) {
        let result: ActivityTaskPointModelType = await ActivityTaskPointModel.findOneAndUpdate(
            { type, taskId, activityId }, { $setOnInsert: { point, createdBy: uid }, $set: { taskType, activityType, 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 let ActivityTaskPointModel: ReturnModelType<typeof Activity_Task_Point, {}>;
export function loadActivityTaskPoint(connect: mongoose.Connection) {
    ActivityTaskPointModel = getModelForClass(Activity_Task_Point, {
        existingConnection: connect
    });
}


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