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

/**
  * 角色的个推标签数据 
*/
@index({ roleId: 1 })

export default class Getui_Tags extends BaseModel {
    @prop({ required: true })
    roleId: string;				// 角色id
    @prop({ required: true })
    cid: string;			    // 个推标识
    @prop({ required: true })
    tags: string[];					// 标签


    //查询用户信息
    public static async findRole(roleId: string) {
        let result: GetuiTagsModelType = await GetuiTagsModel.findOne(
            { roleId }).lean(true);
        return result;
    }

    //查询用户信息
    public static async findRoles(roleIds: string[]) {
        let result: GetuiTagsModelType[] = await GetuiTagsModel.find(
            { roleId: { $in: roleIds } }).lean(true);
        return result;
    }

    //更新个推标签
    public static async updateTags(roleId: string, tags: string[]) {
        let result: GetuiTagsModelType = await GetuiTagsModel.findOneAndUpdate(
            { roleId }, { $set: { tags: tags } }, { new: true }).lean(true);
        return result;
    }

    //添加/更新个推信息
    public static async updateCID(roleId: string, cid: string) {
        let result: GetuiTagsModelType = await GetuiTagsModel.findOneAndUpdate(
            { roleId }, { $set: { cid: cid } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //删除角色个推信息
    public static async delectRole(roleId: string) {
        await GetuiTagsModel.deleteOne({ roleId });
    }
}

export const GetuiTagsModel = getModelForClass(Getui_Tags);

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