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

class ServerModel {
  //获取
  async getServerList(tag: any = 1, status: number = 0, sort: string = "desc") {
    if (status > 0) {
      return await query(
        `SELECT id,status,name,ip,port,tips,white_list,sid FROM game_server WHERE tag = ? and status =  ? order by id ${sort}`,
        [tag, status]
      );
    } else {
      return await query(
        `SELECT id,status,name,ip,port,tips,white_list,sid FROM game_server WHERE tag = ? order by id ${sort}`,
        [tag]
      );
    }
  }
  async getServerListById(tag: any, id: any) {
    return await query(`SELECT *  FROM game_enter_server WHERE  id = ? `, [id]);
  }
  async getAllServerList(tag: any = 1, ip: string) {
    const rows = await query(`SELECT * FROM game_server WHERE tag = ? and is_show = 1` , [tag]);
    const resultArray = (rows as any[]).map((row) => {
      let status = row.status;
      if ((status == 0 || status == 3) && row.white_list) {
        const list = row.white_list.split(",");
        if (list.length > 0) {
          if (list.includes(ip)) {
            status = 1;
          }
        }
      }

      let server = "";
      if (row.wss) {
        server = row.wss;
      } else {
        server = `ws://${row.ip}:${row.port}`;
      }

      return {
        sid: row.sid,
        id: row.id,
        name: row.name,
        server: server,
        wss: row.wss,
        status: status,
        tips: row.tips,
        ip: row.ip,
        port: row.port,
        db_name: row.db_name,
        tag:row.tag,
      };
    });
    return resultArray;
  }

  async checkEnterServerByUid(uid: any, server_id: any, tag: any) {
    return await query(
      `SELECT * FROM game_enter_server where uid  = ? and tag = ? and server_id = ?`,
      [uid, tag, server_id]
    );
  }

  async getEnterServerListByUid(uid: any, tag: any) {
    return await query(
      `SELECT ges.id, ges.uid, ges.server_id, ges.create_time as last_login_time , gs.ip, gs.port,gs.status,gs.name,
           gs.tips,gs.wss,gs.db_name,gs.is_show FROM game_enter_server as ges left join game_server as gs on ges.server_id = gs.sid and
            ges.tag = gs.tag  WHERE ges.uid = ? and ges.tag = ? AND gs.is_show=1 order by ges.create_time desc `,
      [uid, tag]
    );
  }

  //记录进区
  async enterServer(uid: any, server_id: any, create_time: string, tag: any) {
    return await query(
      `INSERT INTO game_enter_server(uid,server_id,first_server_id,create_time,tag) VALUES('${uid}','${server_id}', '${server_id}', '${create_time}','${tag}')`,
      []
    );
  }

  //更新进区
  async updateEnterServer(id: any, create_time: string) {
    const sql =
      "UPDATE game_enter_server SET create_time = ? WHERE id = ?";
    return await query(sql, [create_time, id]);
  }

  //更新状态
  async updateServerStatus(id: any, status: any) {
    const sql = "UPDATE game_server SET status = ? WHERE id in (?)";
    return await query(sql, [status, id]);
  }
}

module.exports = new ServerModel();
