From 78b80f9098c168729d3319ff5beeeaca09cef604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=A5=E6=B5=B7?= Date: Tue, 5 Mar 2024 23:05:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81top=E7=BE=A4=E8=81=8A?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbase/message/extra.go | 61 ++++++++++++++++++++++++++++++++++++ wclient/robot/handler.go | 1 + wclient/robot/handler_top.go | 37 ++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 dbase/message/extra.go create mode 100644 wclient/robot/handler_top.go diff --git a/dbase/message/extra.go b/dbase/message/extra.go new file mode 100644 index 00000000..ce4e3e94 --- /dev/null +++ b/dbase/message/extra.go @@ -0,0 +1,61 @@ +package message + +import ( + "time" + + "github.com/opentdp/go-helper/dborm" +) + +type TopItem struct { + Sender string `json:"sender"` + RecordCount int32 `json:"record_count"` +} + +func TodayUnix() int64 { + + now := time.Now() + today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) + + return today.Unix() + +} + +func TalkTop10(roomid string) []*TopItem { + + var result []*TopItem + + sql := ` + SELECT sender, COUNT(sender) AS record_count + FROM message + WHERE created_at >= ? AND roomid = ? + GROUP BY sender + ORDER BY record_count DESC + LIMIT 10 + ` + + timestamp := TodayUnix() + dborm.Db.Raw(sql, timestamp, roomid).Scan(&result) + + return result + +} + +func ImageTop10(roomid string) []*TopItem { + + var result []*TopItem + + sql := ` + SELECT sender, COUNT(sender) AS record_count + FROM message + WHERE created_at >= ? AND roomid = ? AND type = 3 + GROUP BY sender + ORDER BY record_count DESC + LIMIT 10 + ` + + timestamp := TodayUnix() + dborm.Db.Raw(sql, timestamp, roomid).Scan(&result) + + return result + +} diff --git a/wclient/robot/handler.go b/wclient/robot/handler.go index 2bf7f5ff..644d494e 100644 --- a/wclient/robot/handler.go +++ b/wclient/robot/handler.go @@ -28,6 +28,7 @@ func setupHandlers() { apiHandler() badHandler() banHandler() + topHandler() roomHandler() wgetHandler() diff --git a/wclient/robot/handler_top.go b/wclient/robot/handler_top.go new file mode 100644 index 00000000..ec9292ef --- /dev/null +++ b/wclient/robot/handler_top.go @@ -0,0 +1,37 @@ +package robot + +import ( + "fmt" + "strings" + + "github.com/opentdp/wechat-rest/dbase/message" + "github.com/opentdp/wechat-rest/wcferry" +) + +func topHandler() { + + handlers["/top"] = &Handler{ + Level: 7, + Order: 50, + ChatAble: false, + RoomAble: true, + Describe: "获取群聊统计信息", + Callback: func(msg *wcferry.WxMsg) string { + text := []string{} + // 聊天统计 + text = append(text, "", "今日灌水排行", "----------------") + for _, v := range message.TalkTop10(msg.Roomid) { + u := wc.CmdClient.GetAliasInChatRoom(v.Sender, msg.Roomid) + text = append(text, fmt.Sprintf("%s: %d 次", u, v.RecordCount)) + } + // 图片统计 + text = append(text, "", "今日斗图排行", "----------------") + for _, v := range message.ImageTop10(msg.Roomid) { + u := wc.CmdClient.GetAliasInChatRoom(v.Sender, msg.Roomid) + text = append(text, fmt.Sprintf("%s: %d 张", u, v.RecordCount)) + } + return strings.Join(text, "\n") + }, + } + +}