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

export class GroupShopRecord {

  @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;             // 达成时的价格

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

/**
  * 活动系统 - 团购总记录
*/
@index({ activityId: 1, itemId: 1 })

export default class Activity_Group_Shop_Rec extends BaseModel {

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

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

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

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

  @prop({ required: true, type: GroupShopRecord, _id: false })
  records: GroupShopRecord[];   // 记录


  public static async findByActivity(activityId: number) {
    let result: ActivityGroupShopRecType[] = await ActivityGroupShopRecModel.find({ activityId }).lean();
    return result;
  }

  public static async incBuyCnt(activityId: number, itemId: number, id: number, inc: number) {
    let result: ActivityGroupShopRecType = await ActivityGroupShopRecModel.findOneAndUpdate(
      { activityId, itemId }, { $inc: { sum: inc }, $set: { id } }, { new: true, upsert: true }
    ).lean();
    return result;
  }

  public static async addRecord(activityId: number, itemId: number, record: GroupShopRecord) {
    let result: ActivityGroupShopRecType = await ActivityGroupShopRecModel.findOneAndUpdate(
      { activityId, itemId }, { $push: { records: record } }, { new: true, upsert: true }
    ).lean();
    return result;
  }

  public static async setSum(activityId: number, itemId: number, sum: number) {
    await ActivityGroupShopRecModel.findOneAndUpdate({ activityId, itemId }, { $setOnInsert: { sum: 0 } }, { new: true, upsert: true }).lean();
    let result: ActivityGroupShopRecType = await ActivityGroupShopRecModel.findOneAndUpdate(
      { activityId, itemId, sum: { $lt: sum } }, { $set: { sum } }, { new: true }
    ).lean();
    return result;
  }

  public static async debugSetSum(activityId: number, itemId: number, sum: number) {
    if(itemId == 0) {
      await ActivityGroupShopRecModel.updateMany({ activityId }, { $set: { sum } }, { new: true, upsert: true });
      let result: ActivityGroupShopRecType[] = await ActivityGroupShopRecModel.find({ activityId }).lean();
      return result;
    } else {
      await ActivityGroupShopRecModel.updateMany({ activityId, itemId }, { $set: { sum } }, { new: true, upsert: true });
      let result: ActivityGroupShopRecType[] = await ActivityGroupShopRecModel.find({ activityId, itemId }).lean();
      return result;
    }
  }
}

export const ActivityGroupShopRecModel = getModelForClass(Activity_Group_Shop_Rec);

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