import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, modelOptions } from '@typegoose/typegoose';
@index({ roleId: 1, id: 1 })
@index({ seqId: 1 })
@modelOptions({ schemaOptions: { id: false } })
export default class Skin extends BaseModel {
    @prop({ required: true, default: '' })
    roleId: string;					// 角色 id
    @prop({ required: true, default: '' })
    roleName: string;				// 角色名称

    @prop({ required: true, default: '' })
    id: number;				         // 皮肤id
    @prop({ required: true, default: '', select: false})
    skinName: string;			     // 皮肤名称
    @prop({ required: true, default: 0 })
    hid: number;

    public static async findbyRole(roleId: string) {
        const rec: SkinType[] = await SkinModel.find({ roleId }, {_id: 0}).select('id skinName hid').lean();
        return rec;
    }

    public static async findbyRoleAndHid(roleId: string, hid: number) {
        const rec: SkinType[] = await SkinModel.find({ roleId, hid }).select('id').lean();
        return rec;
    }

    public static async increaseSkin(roleId: string, id: number, info: { roleId: string, roleName: string, id: number, skinName: string, hid: number }, lean = true) {
        const doc = new SkinModel();
        const setOnInsert = Object.assign(doc.toJSON(), info);
        const items: SkinType = await SkinModel.findOneAndUpdate({ roleId, id }, { $setOnInsert: setOnInsert }, { new: true, upsert: true }).lean(lean);
        return items;
    }
}

export const SkinModel = getModelForClass(Skin);

export interface SkinType extends Pick<DocumentType<Skin>, keyof Skin> {
    id: number;
};
