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

/**
  * 玩家充值订单 
*/
@index({ localOrderID: 1 })

export default class UserOrder extends BaseModel {

    @prop({ required: true })
    serverId: number;		    // 区号
    @prop({ required: true })
    roleId: string;				// 用户id
    @prop({ required: true })
    productID: string;			// 商品
    @prop({ required: true })
    orderID: string;			// 平台订单号
    @prop({ required: true })
    localOrderID: string;	    // 本地订单号
    @prop({ required: true })
    payType: number;	        // 支付类型    PAY_TYPE
    @prop({ required: true })
    price: number;				// 价格
    @prop({ required: true })
    state: number;				// 订单状态 ORDER_STATE 3.支付成功并结算
    @prop({ required: true })
    message: string;	        // 信息
    @prop({ required: true })
    activityId: number;		    // 活动ID
    @prop({ required: true })
    paramStr: string;	        // 订单参数


    //保存平台订单号
    public static async saveOrderID(roleId: string, localOrderID: string, aliOrderID: string) {
        let result: UserOrderModelType = await UserOrderModel.findOneAndUpdate({ roleId, localOrderID },
            { $set: { orderID: aliOrderID } },
            { new: true }).lean(true);
        return result;
    }

    //校验订单
    public static async check(roleId: string, localOrderID: string, message: string = '') {
        let result: UserOrderModelType = await UserOrderModel.findOneAndUpdate({ roleId, localOrderID, state: { $ne: ORDER_STATE.RESULT_SUCCESS } },
            { $set: { state: ORDER_STATE.CHECK_ORDER, message } },
            { new: true }).lean(true);
        return result;
    }

    //订单支付失败
    public static async fail(roleId: string, localOrderID: string, message: string = '') {
        let result: UserOrderModelType = await UserOrderModel.findOneAndUpdate({ roleId, localOrderID, state: { $ne: ORDER_STATE.RESULT_SUCCESS } },
            { $set: { state: ORDER_STATE.RESULT_FAIL, message } },
            { new: true }).lean(true);
        return result;
    }

    //订单支付成功
    public static async success(roleId: string, localOrderID: string, message: string = '') {
        let result: UserOrderModelType = await UserOrderModel.findOneAndUpdate({ roleId, localOrderID, state: { $ne: ORDER_STATE.RESULT_SUCCESS } },
            { $set: { state: ORDER_STATE.RESULT_SUCCESS, message } },
            { new: true }).lean(true);
        return result;
    }

    //查询成功订单详情
    public static async findSuccessOrderByProductID(productID: string, roleId: string, activityId: number) {
        let result: UserOrderModelType[] = await UserOrderModel.find({ productID, roleId, activityId, state: ORDER_STATE.RESULT_SUCCESS }).lean(true);
        return result;
    }

    //查询订单详情
    public static async findOrderByProductID(productID: string, roleId: string, activityId: number) {
        let result: UserOrderModelType[] = await UserOrderModel.find({ productID, roleId, activityId }).lean(true);
        return result;
    }

    //查询订单详情
    public static async findPlayerOrder(productID: string, roleId: string, activityId: number, limit: number) {
        let result: UserOrderModelType[] = await UserOrderModel.find({ productID, roleId, activityId }, {}).limit(limit).sort({ createdAt: -1 }).lean(true);
        return result;
    }

    //查询订单详情
    public static async findOrderByActivityID(activityId: number, roleId: string,) {
        let result: UserOrderModelType[] = await UserOrderModel.find({ activityId, roleId }).lean(true);
        return result;
    }

    //查询订单详情
    public static async findOrder(localOrderID: string) {
        let result: UserOrderModelType = await UserOrderModel.findOne({ localOrderID }).lean(true);
        return result;
    }

    //查询订单详情
    public static async findOrderList(serverId: number, roleId: string, begin: number, count: number) {
        let result: UserOrderModelType[] = await UserOrderModel.find({ serverId, roleId, state: ORDER_STATE.RESULT_SUCCESS }, { price: 1, createdAt: 1, productID: 1 }).skip(begin).limit(count).sort({ createdAt: -1 }).lean(true);
        return result;
    }


    //新增订单
    public static async applyOrder(serverId: number, roleId: string, productID: string, localOrderID: string, orderID: string, price: number, payType: number, activityId: number, paramStr: string, message: string) {
        let result: UserOrderModelType = await UserOrderModel.findOneAndUpdate({ serverId, roleId, productID, localOrderID, orderID, payType, activityId },
            { $set: { price, state: ORDER_STATE.APPLY, message, paramStr: paramStr ? paramStr : '' } },
            { upsert: true, new: true }).lean(true);
        return result;
    }
}

export const UserOrderModel = getModelForClass(UserOrder);

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