加入黑名单机制,在黑名单的Id不参与水群统计排行

This commit is contained in:
李寻欢 2023-10-08 14:33:38 +08:00
parent eb541a1b70
commit 011057b85e
2 changed files with 28 additions and 3 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ cache
log log
dist dist
*.log *.log
blacklist.txt

View File

@ -5,6 +5,7 @@ import (
"go-wechat/client" "go-wechat/client"
"go-wechat/entity" "go-wechat/entity"
"go-wechat/utils" "go-wechat/utils"
"io"
"log" "log"
"os" "os"
"strings" "strings"
@ -28,9 +29,24 @@ func yesterday() {
func dealYesterday(gid string) { func dealYesterday(gid string) {
notifyMsgs := []string{"#昨日水群排行榜"} notifyMsgs := []string{"#昨日水群排行榜"}
// 读取黑名单文件名单内的Id不上榜
var blacklist []string
file, err := os.Open("blacklist.txt")
if err != nil {
log.Printf("读取黑名单失败: %v", err)
} else {
defer file.Close()
var content []byte
if content, err = io.ReadAll(file); err != nil {
log.Printf("读取黑名单失败: %v", err)
} else {
blacklist = strings.Split(string(content), "\n")
}
}
// 获取昨日消息总数 // 获取昨日消息总数
var yesterdayMsgCount int64 var yesterdayMsgCount int64
err := client.MySQL.Model(&entity.Message{}). err = client.MySQL.Model(&entity.Message{}).
Where("from_user = ?", gid). Where("from_user = ?", gid).
Where("DATEDIFF(create_at,NOW()) = -1"). Where("DATEDIFF(create_at,NOW()) = -1").
Count(&yesterdayMsgCount).Error Count(&yesterdayMsgCount).Error
@ -54,14 +70,22 @@ func dealYesterday(gid string) {
} }
var records []record var records []record
err = client.MySQL.Table("t_message AS tm"). tx := client.MySQL.Table("t_message AS tm").
Joins("LEFT JOIN t_group_user AS tgu ON tgu.wxid = tm.group_user AND tm.from_user = tgu.group_id"). Joins("LEFT JOIN t_group_user AS tgu ON tgu.wxid = tm.group_user AND tm.from_user = tgu.group_id").
Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`"). Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`").
Where("tm.from_user = ?", gid). Where("tm.from_user = ?", gid).
Where("tm.type < 10000"). Where("tm.type < 10000").
Where("DATEDIFF(tm.create_at,NOW()) = -1"). Where("DATEDIFF(tm.create_at,NOW()) = -1").
Group("tm.group_user, tgu.nickname").Order("`count` DESC"). Group("tm.group_user, tgu.nickname").Order("`count` DESC").
Limit(5).Find(&records).Error Limit(10)
// 如果有黑名单,过滤掉
if len(blacklist) > 0 {
tx.Where("tm.group_user NOT IN (?)", blacklist)
}
err = tx.Find(&records).Error
if err != nil { if err != nil {
log.Printf("获取昨日消息失败, 错误信息: %v", err) log.Printf("获取昨日消息失败, 错误信息: %v", err)
return return