// 仅记录农场农田的记录，因为一块田可以反复被不同人种植、收获，不像矿场林场就是固定次数

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

@index({ leagueCode: 1, configId: 1, farmId: 1, fieldId: 1 })
export default class GVGLeagueFarmRec extends BaseModel {

    @prop({ required: true })
    leagueCode: string;          // 联军唯一code

    @prop({ required: true })
    configId: number;

    @prop({ required: true })
    farmId: number;              // 农庄id

    @prop({ required: true })
    type: number;              // 农庄类型

    @prop({ required: true })
    fieldId: number;              // 农庄id

    @prop({ required: true })
    startRoleId: string;          // 种田玩家/开始游戏玩家

    @prop({ required: true })
    startTime: Date;           // 种值时间/开始时间

    @prop({ required: false })
    addType: number;             // 种子加成

    @prop({ required: false })
    seedType: number;            // 实际种的种子类型

    @prop({ required: false })
    itemId: number;            // 实际种的种子类型

    @prop({ required: false })
    endRoleId: string;          // 收获玩家/结束游戏玩家

    @prop({ required: false })
    endTime: Date;         // 收获时间/结束时间

    @prop({ required: false })
    output: number;              // 产量

    @prop({ required: false })
    outputStr: string;             // 产量计算公式

    public static async insertRecs(type: GVG_RESOURCE_TYPE,params: GVGLeagueFarmType[]) {
        let insertParams = params.map(field => {
            delete field._id;
            return { ...field, startRoleId: field.lockRoleId, startTime: new Date(), type }
        })
        await GVGLeagueFarmRecModel.insertMany(insertParams);
    }

    public static async updateRecs(params: GVGLeagueFarmType[], roleId: string) {
        if(params.length == 0) return;
        let { configId, leagueCode, farmId } = params[0];
        let fieldIds = params.map(cur => cur.fieldId);
        await GVGLeagueFarmRecModel.updateMany({ configId, leagueCode, farmId, fieldId: { $in: fieldIds } }, { $set: { endRoleId: roleId, endTime: new Date() } });
    }
}

export const GVGLeagueFarmRecModel = getModelForClass(GVGLeagueFarmRec);

export interface GVGLeagueFarmRecType extends Pick<DocumentType<GVGLeagueFarmRec>, keyof GVGLeagueFarmRec> {
    id: number;
};
export type GVGLeagueFarmRecUpdate = Partial<GVGLeagueFarmRecType>; // 将所有字段变成可选项
