import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { nowSeconds } from '../pubUtils/timeUtil';
import { GroupShopDiscount } from '../domain/activityField/groupShopField';

/**
  * 活动系统 - 团购玩家记录
*/
@index({ roleId: 1, activityId: 1, itemId: 1 })
@index({ activityId: 1, itemId: 1, 'records.price': 1 })

export class GroupShopBuyRecord {

  @prop({ required: true })
  time: number;             // 购买时间

  @prop({ required: true })
  sum: number;             // 达成时的总额

  @prop({ required: true })
  discountId: number;          // 达成时的折扣

  @prop({ required: true })
  discount: number;             // 达成时的折扣

  @prop({ required: true })
  price: number;             // 达成时的价格

  @prop({ required: true })
  buyCnt: number;			           // 购买次数

  constructor(curDiscount: GroupShopDiscount, buyCnt: number) {
    this.time = nowSeconds();
    if(curDiscount) {
      this.sum = curDiscount.sum;
      this.discountId = curDiscount.id;
      this.discount = curDiscount.discount;
      this.price = curDiscount.price;
    }
    this.buyCnt = buyCnt;
  }
}

export default class Activity_Group_Shop_User_Rec extends BaseModel {

  @prop({ required: true })
  activityId: number;				    // 活动Id

  @prop({ required: true })
  roleId: string;				        // 玩家Id

  @prop({ required: true })
  itemId: number;			            // 唯一id

  @prop({ required: true })
  id: number;			            // 物品id

  @prop({ required: true })
  buyCnt: number;			           // 购买次数

  @prop({ required: true, type: GroupShopBuyRecord, _id: false })
  records: GroupShopBuyRecord[];			           // 购买次数

  public static async findByActivityAndRoleId(activityId: number, roleId: string) {
    let result: ActivityGroupShopUserRecType[] = await ActivityGroupShopUserRecModel.find({ roleId, activityId }).lean();
    return result;
  }

  public static async incBuyCnt(activityId: number, roleId: string, itemId: number, id: number, inc: number, record: GroupShopBuyRecord) {
    let result: ActivityGroupShopUserRecType = await ActivityGroupShopUserRecModel.findOneAndUpdate(
      { roleId, activityId, itemId }, { $inc: { buyCnt: inc }, $setOnInsert: { id }, $push: { records: record } }, { new: true, upsert: true }
      ).lean();
    return result;
  }

  public static async clearCnt(activityId: number, roleId: string) {
    await ActivityGroupShopUserRecModel.deleteMany({ roleId, activityId });
  }

  public static async findByPrice(activityId: number, itemId: number, price: number) {
    let result: ActivityGroupShopUserRecType[] = await ActivityGroupShopUserRecModel.find({ activityId, itemId, 'records.price': { $gt: price } }).lean();
    return result;
  }
}

export const ActivityGroupShopUserRecModel = getModelForClass(Activity_Group_Shop_User_Rec);

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