104 lines
2.6 KiB
Go
104 lines
2.6 KiB
Go
package tasks
|
||
|
||
import (
|
||
"fmt"
|
||
"go-wechat/client"
|
||
"go-wechat/entity"
|
||
"go-wechat/utils"
|
||
"io"
|
||
"log"
|
||
"os"
|
||
"strings"
|
||
)
|
||
|
||
// 水群排行榜
|
||
|
||
// yesterday
|
||
// @description: 昨日排行榜
|
||
func yesterday() {
|
||
// 从环境变量读取需要处理的群Id
|
||
gid := strings.Split(os.Getenv("GROUP_ID"), ",")
|
||
for _, id := range gid {
|
||
dealYesterday(id)
|
||
}
|
||
}
|
||
|
||
// dealYesterday
|
||
// @description: 处理请求
|
||
// @param gid
|
||
func dealYesterday(gid 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
|
||
err = client.MySQL.Model(&entity.Message{}).
|
||
Where("from_user = ?", gid).
|
||
Where("DATEDIFF(create_at,NOW()) = -1").
|
||
Count(&yesterdayMsgCount).Error
|
||
if err != nil {
|
||
log.Printf("获取昨日消息总数失败, 错误信息: %v", err)
|
||
return
|
||
}
|
||
log.Printf("昨日消息总数: %d", yesterdayMsgCount)
|
||
if yesterdayMsgCount == 0 {
|
||
return
|
||
}
|
||
|
||
notifyMsgs = append(notifyMsgs, " ")
|
||
notifyMsgs = append(notifyMsgs, fmt.Sprintf("昨日消息总数: %d", yesterdayMsgCount))
|
||
|
||
// 返回数据
|
||
type record struct {
|
||
GroupUser string
|
||
Nickname string
|
||
Count int64
|
||
}
|
||
|
||
var records []record
|
||
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").
|
||
Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`").
|
||
Where("tm.from_user = ?", gid).
|
||
Where("tm.type < 10000").
|
||
Where("DATEDIFF(tm.create_at,NOW()) = -1").
|
||
Group("tm.group_user, tgu.nickname").Order("`count` DESC").
|
||
Limit(10)
|
||
|
||
// 如果有黑名单,过滤掉
|
||
if len(blacklist) > 0 {
|
||
tx.Where("tm.group_user NOT IN (?)", blacklist)
|
||
}
|
||
|
||
err = tx.Find(&records).Error
|
||
|
||
if err != nil {
|
||
log.Printf("获取昨日消息失败, 错误信息: %v", err)
|
||
return
|
||
}
|
||
notifyMsgs = append(notifyMsgs, " ")
|
||
for i, r := range records {
|
||
log.Printf("账号: %s[%s] -> %d", r.Nickname, r.GroupUser, r.Count)
|
||
notifyMsgs = append(notifyMsgs, fmt.Sprintf("#%d: %s -> %d条", i+1, r.Nickname, r.Count))
|
||
}
|
||
|
||
notifyMsgs = append(notifyMsgs, " \n请未上榜的群友多多反思。")
|
||
|
||
log.Printf("排行榜: \n%s", strings.Join(notifyMsgs, "\n"))
|
||
go utils.SendMessage(gid, "", strings.Join(notifyMsgs, "\n"), 0)
|
||
}
|