import { query } from "../sql/query"; // 确保路径是正确的

class OrderModel {
  //获取订单
  async getOrder(order_id: any) {
    return await query(`SELECT * FROM game_order WHERE order_id = ?`, [
      order_id,
    ]);
  }

  async getOrderByToken(token: any) {
    return await query(`SELECT * FROM game_order WHERE token = ?`, [token]);
  }

  //添加订单
  async createOrder(
    orderId: any,
    uid: any,
    level: any,
    amount: any,
    role_id: any,
    role_name: any,
    product_id: any,
    server_id: any,
    channel_id: any,
    create_time: string,
    platform: string
  ) {
    return await query(
      `INSERT INTO game_order(order_id, uid, level,amount,role_id,role_name,product_id,server_id,channel_id,create_time,platform) VALUES('${orderId}', '${uid}', '${level}', '${amount}', '${role_id}', '${role_name}','${product_id}','${server_id}',  '${channel_id}','${create_time}','${platform}')`,
      []
    );
  }

  //更新订单状态
  async updateOrderStats(
    orderId: any,
    status: any,
    out_trade_no: string,
    update_time: string,
    uid: any
  ) {
    let isFirst = 0;

    const order_info = await query(
      `SELECT * FROM game_order WHERE uid = ? and status = 2 limit 1`,
      [uid]
    );
    if (!order_info[0]) {
      isFirst = 1;
    }

    const sql =
      "UPDATE game_order SET status = ?, update_time = ?, out_trade_no = ?, is_first = ? WHERE order_id = ?";
    return await query(sql, [
      status,
      update_time,
      out_trade_no,
      isFirst,
      orderId,
    ]);
  }

  //更新订单token
  async updateOrderToken(orderId: any, token: string, platform: string) {
    const sql =
      "UPDATE game_order SET token = ?, platform = ? WHERE order_id = ?";
    return await query(sql, [token, platform, orderId]);
  }
}

module.exports = new OrderModel();
