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

/**
  * 分红记录表
**/
@modelOptions({ schemaOptions: { id: false } })
@index({ shopId: 1 })

export default class DicShopList extends BaseModel {
    @prop({ required: true })
    shopId: number;					// 商店id dic_zyz_shopList内的id

    @prop({ required: true, default: false })
    useJson: boolean;			    // 如果为true，则会随dic_zyz_shop表内商品增加而增加，items只提供顺序折扣等；为false，则只提供items里的商品

    @prop({ required: true, default: [], _id: false, type: () => ShopItem })
    items: ShopItem[];					// 商店id dic_zyz_shopList内的id

    public static async findByShopId(shopId: number) {
        const rec: DicShopListType = await DicShopListModel.findOne({ shopId }).lean();
        return rec;
    } 

    public static async createShop(shopId: number, useJson: boolean, item: ShopItem) {
        const rec: DicShopListType = await DicShopListModel.findOneAndUpdate({ shopId }, { useJson, $push: { items: item } }, { new: true, upsert: true }).lean();
        return rec;
    }

}

export const DicShopListModel = getModelForClass(DicShopList);

export interface DicShopListType extends Pick<DocumentType<DicShopList>, keyof DicShopList>{}
export type DicShopListParam = Partial<DicShopListType>;
