-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
}, | ||
} | ||
|
||
} |