From 252cdcb1158ee0303122a4b7d0248230d458787c Mon Sep 17 00:00:00 2001 From: shoopea Date: Thu, 9 May 2019 18:36:45 +0800 Subject: [PATCH] test --- def.go | 33 +++++++++++++++++---------------- main.go | 2 +- sql.go | 18 ++++++++---------- workers.go | 3 +-- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/def.go b/def.go index 8558c82..21991db 100644 --- a/def.go +++ b/def.go @@ -2,28 +2,29 @@ package main import ( "regexp" + "time" ) type ChatWarsMessage struct { - UserID64 int64 `json:"user_id"` - SenderUserID64 int64 `json:"sender_user_id"` - Date int32 `json:"date"` - ID64 int64 `json:"id"` - ChatID64 int64 `json:"chat_id"` - Text string `json:"text"` + UserID64 int64 `json:"user_id"` + SenderUserID64 int64 `json:"sender_user_id"` + Date time.Time `json:"date"` + ID64 int64 `json:"id"` + ChatID64 int64 `json:"chat_id"` + Text string `json:"text"` } type ChatWarsMessageAuctionAnnounce struct { - MsgID64 int64 `json:"msg_id"` - LotID int32 `json:"lot_id"` - Item string `json:"item"` - Cond string `json:"cond"` - Quality string `json:"quality"` - Seller string `json:"seller"` - Buyer string `json:"buyer"` - Price int32 `json:"price"` - Status string `json:"status"` - End int64 `json:"end"` + MsgID64 int64 `json:"msg_id"` + LotID int32 `json:"lot_id"` + Item string `json:"item"` + Cond string `json:"cond"` + Quality string `json:"quality"` + Seller string `json:"seller"` + Buyer string `json:"buyer"` + Price int32 `json:"price"` + Status string `json:"status"` + End time.Time `json:"end"` } type MessageParsingRule struct { diff --git a/main.go b/main.go index 42f549f..41d719a 100644 --- a/main.go +++ b/main.go @@ -68,7 +68,7 @@ func main() { // Connecting to DB switch cfg.SQL.Driver { case "mysql": - db, err = sql.Open("mysql", cfg.SQL.Username+":"+cfg.SQL.Password+"@"+cfg.SQL.Type+"("+cfg.SQL.Address+")/"+cfg.SQL.Database) + db, err = sql.Open("mysql", cfg.SQL.Username+":"+cfg.SQL.Password+"@"+cfg.SQL.Type+"("+cfg.SQL.Address+")/"+cfg.SQL.Database+"?parseTime=true") if err != nil { log.Fatal(err) } diff --git a/sql.go b/sql.go index fdfa8cb..949dda9 100644 --- a/sql.go +++ b/sql.go @@ -95,8 +95,8 @@ func initDB() { _, err = db.Exec(`CREATE TABLE obj_war ( obj_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT - ,start_time TIMESTAMP NOT NULL - ,end_time TIMESTAMP NOT NULL + ,start_time DATETIME NOT NULL + ,end_time DATETIME NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) failOnError(err, "initDB : create table obj_war") @@ -107,7 +107,7 @@ func initDB() { ,chat_id BIGINT NOT NULL ,user_id BIGINT NOT NULL ,sender_user_id BIGINT NOT NULL - ,date TIMESTAMP NOT NULL + ,date DATETIME NOT NULL ,text VARCHAR(4096) NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,UNIQUE KEY (msg_id, chat_id, sender_user_id) @@ -149,7 +149,7 @@ func initDB() { ,buyer VARCHAR(32) ,price SMALLINT NOT NULL ,status VARCHAR(32) - ,end TIMESTAMP NOT NULL + ,end DATETIME NOT NULL ,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE ,UNIQUE KEY (lot_id) ) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`) @@ -1188,8 +1188,8 @@ func putUnprocessedMsg(m ChatWarsMessage) (int64, error) { return 0, err } - stmt, err := db.Prepare(`INSERT INTO obj_msg (obj_id, msg_id, chat_id, user_id, sender_user_id, date , text) - VALUES (?, ?, ?, ?, ?, FROM_UNIXTIME(?), ?);`) + stmt, err := db.Prepare(`INSERT INTO obj_msg (obj_id, msg_id, chat_id, user_id, sender_user_id, date, text) + VALUES (?, ?, ?, ?, ?, ?, ?);`) if err != nil { return 0, err } @@ -1206,7 +1206,7 @@ func putUnprocessedMsg(m ChatWarsMessage) (int64, error) { func getMsg(objId int64) (ChatWarsMessage, error) { var m ChatWarsMessage - stmt, err := db.Prepare(`SELECT om.msg_id, om.chat_id, om.sender_user_id, UNIX_TIMESTAMP(om.date), om.text FROM obj_msg om WHERE om.obj_id = ?`) + stmt, err := db.Prepare(`SELECT om.msg_id, om.chat_id, om.sender_user_id, om.date, om.text FROM obj_msg om WHERE om.obj_id = ?`) if err != nil { return m, err } @@ -1234,8 +1234,6 @@ func getObjSubTypeId(objId int64) (int64, error) { return 0, err } - log.Printf("getObjSubTypeId(%d) : %d\n", objId, objSubTypeId) - return objSubTypeId, nil } @@ -1283,7 +1281,7 @@ func insertMsgAuctionAnnounce(m *ChatWarsMessageAuctionAnnounce) error { } stmt, err := db.Prepare(`INSERT INTO obj_msg_auction_announce (obj_id, lot_id, item, cond, quality, seller, buyer, price, status, end) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, FROM_UNIXTIME(?));`) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`) if err != nil { return err } diff --git a/workers.go b/workers.go index 8d02510..81c511f 100644 --- a/workers.go +++ b/workers.go @@ -113,8 +113,7 @@ func SQLIdentifyMsgWorker(id int, objIds <-chan int64) { p, _ := strconv.ParseInt(r.ReplaceAllString(m.Text, "${Price}"), 10, 32) cwm.Price = int32(p) cwm.Status = r.ReplaceAllString(m.Text, "${Status}") - t, _ := fromChatWarsDate(r.ReplaceAllString(m.Text, "${End}")) - cwm.End = t.Unix() + cwm.End, _ := fromChatWarsDate(r.ReplaceAllString(m.Text, "${End}")) err = insertMsgAuctionAnnounce(&cwm) logOnError(err, "SQLIdentifyMsgWorker["+strconv.Itoa(id)+"] : AuctionAnnounce") case objSubTypeMessageTime: