From d5ae73d81755b722c42c37632fb9f4ba869e5515 Mon Sep 17 00:00:00 2001 From: liyp Date: Sun, 21 Apr 2024 12:22:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=9B=B4=E6=96=B0message=5Fs?= =?UTF-8?q?eq?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + main.go | 13 +++-- tools/update_message_seq.go | 105 ++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100644 tools/update_message_seq.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c4b789 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +tools/data.db +test.json \ No newline at end of file diff --git a/main.go b/main.go index 43e85c5..6e649f7 100644 --- a/main.go +++ b/main.go @@ -43,10 +43,12 @@ func insertMessage(db *sql.DB, data map[string]interface{}) error { sender_card = sender_nickname } sender_role := sender["role"].(string) - fmt.Println(post_type, message_time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role) + message_seq := data["message_seq"].(float64) + + fmt.Println(post_type, message_time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role, message_seq) // Insert data into database - _, err = db.Exec("INSERT INTO messages ( post_type, message_type, time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", - post_type, message_type, message_time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role) + _, err = db.Exec("INSERT INTO messages ( post_type, message_type, time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role, message_seq) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + post_type, message_type, message_time, group_id, message_id, raw_message, sender_user_id, sender_nickname, sender_card, sender_role, message_seq) fmt.Println("Data inserted successfully!") if err != nil { log.Fatal(err) @@ -88,12 +90,13 @@ func handlePost(w http.ResponseWriter, r *http.Request) { message_type TEXT, time DATETIME, group_id INTEGER, - message_id INTEGER, + message_id INTEGER UNIQUE NOT NULL, raw_message TEXT, sender_user_id INTEGER, sender_nickname TEXT, sender_card TEXT, - sender_role TEXT + sender_role TEXT, + message_seq INTEGER )`) if err != nil { log.Fatal(err) diff --git a/tools/update_message_seq.go b/tools/update_message_seq.go new file mode 100644 index 0000000..e93cb3e --- /dev/null +++ b/tools/update_message_seq.go @@ -0,0 +1,105 @@ +package main + +import ( + "database/sql" + "encoding/json" + "fmt" + "io" + "log" + "net/http" + + _ "github.com/mattn/go-sqlite3" +) + +func main() { + // Open SQLite database + db, err := sql.Open("sqlite3", "./data.db") + if err != nil { + log.Fatal(err) + } + defer db.Close() + + // Create messages table if not exists + _, err = db.Exec(` + CREATE TABLE IF NOT EXISTS messages ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + message_id INTEGER UNIQUE NOT NULL, + message_seq INTEGER + ) + `) + if err != nil { + log.Fatal(err) + } + + // Fetch messageIDs from database + rows, err := db.Query("SELECT message_id FROM messages") + if err != nil { + log.Fatal(err) + } + defer rows.Close() + + for rows.Next() { + var messageID int + err := rows.Scan(&messageID) + if err != nil { + log.Fatal(err) + } + + // Fetch message_seq from API + messageSeq, err := fetchMessageSeq(messageID) + if err != nil { + log.Println(err) + continue + } + + // Insert or update message_seq in database + err = insertOrUpdateMessageSeq(db, messageID, messageSeq) + if err != nil { + log.Println(err) + continue + } + + fmt.Printf("MessageSeq for message_id %d is %d\n", messageID, messageSeq) + } + + if err := rows.Err(); err != nil { + log.Fatal(err) + } +} + +func fetchMessageSeq(messageID int) (int, error) { + url := fmt.Sprintf("http://47.92.132.159:5700/get_msg?message_id=%d", messageID) + + resp, err := http.Get(url) + if err != nil { + return 0, err + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return 0, err + } + + var messageResp map[string]interface{} + err = json.Unmarshal(body, &messageResp) + if err != nil { + return 0, err + } + + return messageResp["message_seq"].(int), nil +} + +func insertOrUpdateMessageSeq(db *sql.DB, messageID, messageSeq int) error { + // Try to insert messageSeq + // _, err := db.Exec("INSERT INTO messages (message_id, message_seq) VALUES (?, ?)", messageID, messageSeq) + // if err != nil { + // If messageID already exists, update messageSeq + _, err := db.Exec("UPDATE messages SET message_seq = ? WHERE message_id = ?", messageSeq, messageID) + if err != nil { + return err + // } + } + + return nil +}