import { DEBUG_PRICE, SDK_37_CONST, STATUS } from "../../consts";
import { RoleModel } from "../../db/Role";
import { UserModel } from "../../db/User";
import { LoginValidateData37 } from "../../domain/sdk";
import { DicRMB } from "../../pubUtils/dictionary/DicRMB";
import { nowSeconds } from "../../pubUtils/timeUtil";
import { resResult } from "../../pubUtils/util";
import { get37Md5SignA, isDebugPay } from "../../pubUtils/sdkUtil";
import { pinus } from "pinus";
import { gameData } from "../../pubUtils/data";

/**
   * 37支付下单
   *
   * @param {number} price 价格订单金额(分)
   * @param {string} message 商品信息
   * @param {string} localOrderID 本地订单号
   *
   */
export async function applyOrder37(localOrderID: string, roleId: string, productInfo: DicRMB) {
    let role = await RoleModel.findByRoleId(roleId);
    let user = await UserModel.findUserByUid(role.userInfo.uid);
    if(!user || !user.channelInfo || !(<LoginValidateData37>user.channelInfo).uid) 
    return {
        code: -1, resData: resResult(STATUS.CHANNEL_INFO_NOT_FOUND)
    }
    let gameCoin = productInfo.gameCoin;
    let ext = pinus.app.get('env');
    let body = {
        game_id: SDK_37_CONST.GAME_ID,
        uid: (<LoginValidateData37>user.channelInfo).uid,
        sid: role.serverId,
        actor_id: role.roleId,
        order_no: localOrderID,
        money: productInfo.price,
        game_coin: gameCoin,
        product_id: productInfo.channelProductID,
        subject: productInfo.message,
        time: nowSeconds(),
        ext
    }
    if(isDebugPay()) {
        body = {...body, money: DEBUG_PRICE}
    }

    let sign = get37Md5SignA(body, SDK_37_CONST.PAY_KEY);


    return { code: 0, data: {
        gameCoin,
        timestamp: nowSeconds(),
        ext,
        sign: sign
    }};
}
