use crate::commons::utils::CONFIG as config;
use chrono::{DateTime, Utc};
use serde_json::json;

pub struct GameRoom {
    pub batch_no: String,
    pub item_num: i32,
    pub player_count: i32,
    pub create_date: Option<DateTime<Utc>>,
}

impl GameRoom {
    pub async fn insert(&self) -> bool {
        let Some(_) = &config.db else {
            println!("没有配置数据库, room insert return true;");
            return true;
        };

        let res = crate::db::db_query(
            r#"insert into tb_room(batch_no,item_num,player_count,create_date) values(?,?,?,now());"#,
            vec![
                json!(&self.batch_no),
                json!(&self.item_num),
                json!(&self.player_count),
            ],
        )
        .await;

        match res {
            Ok(_) => true,
            Err(err) => {
                println!("room insert failed: {err}");
                false
            }
        }
    }
}
