2023-09-21 17:33:59 +08:00
|
|
|
package tasks
|
|
|
|
|
|
|
|
import (
|
2023-09-25 14:16:59 +08:00
|
|
|
"fmt"
|
2023-09-21 17:33:59 +08:00
|
|
|
"go-wechat/client"
|
|
|
|
"go-wechat/entity"
|
2023-11-28 16:09:36 +08:00
|
|
|
"go-wechat/service"
|
2023-10-11 09:51:33 +08:00
|
|
|
"go-wechat/utils"
|
2023-09-21 17:33:59 +08:00
|
|
|
"log"
|
2023-09-25 14:16:59 +08:00
|
|
|
"strings"
|
2023-11-22 10:55:40 +08:00
|
|
|
"time"
|
2023-09-21 17:33:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 水群排行榜
|
|
|
|
|
|
|
|
// yesterday
|
|
|
|
// @description: 昨日排行榜
|
|
|
|
func yesterday() {
|
2023-11-28 16:09:36 +08:00
|
|
|
groups, err := service.GetAllEnableChatRank()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, group := range groups {
|
2023-11-22 10:55:40 +08:00
|
|
|
// 消息统计
|
2023-11-28 16:09:36 +08:00
|
|
|
dealYesterday(group.Wxid)
|
2023-11-22 10:55:40 +08:00
|
|
|
// 获取昨日日期
|
|
|
|
yd := time.Now().Local().AddDate(0, 0, -1).Format("20060102")
|
|
|
|
// 发送词云
|
2023-11-28 16:09:36 +08:00
|
|
|
fileName := fmt.Sprintf("%s_%s.png", yd, group.Wxid)
|
|
|
|
utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0)
|
2023-09-25 14:16:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dealYesterday
|
|
|
|
// @description: 处理请求
|
|
|
|
// @param gid
|
|
|
|
func dealYesterday(gid string) {
|
|
|
|
notifyMsgs := []string{"#昨日水群排行榜"}
|
|
|
|
|
2023-09-21 17:33:59 +08:00
|
|
|
// 获取昨日消息总数
|
|
|
|
var yesterdayMsgCount int64
|
2023-10-11 09:21:07 +08:00
|
|
|
err := client.MySQL.Model(&entity.Message{}).
|
2023-09-25 14:16:59 +08:00
|
|
|
Where("from_user = ?", gid).
|
2023-11-19 11:36:15 +08:00
|
|
|
Where("`type` < 10000").
|
2023-09-21 17:33:59 +08:00
|
|
|
Where("DATEDIFF(create_at,NOW()) = -1").
|
|
|
|
Count(&yesterdayMsgCount).Error
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("获取昨日消息总数失败, 错误信息: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("昨日消息总数: %d", yesterdayMsgCount)
|
2023-09-25 14:16:59 +08:00
|
|
|
if yesterdayMsgCount == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyMsgs = append(notifyMsgs, " ")
|
|
|
|
notifyMsgs = append(notifyMsgs, fmt.Sprintf("昨日消息总数: %d", yesterdayMsgCount))
|
2023-09-21 17:33:59 +08:00
|
|
|
|
|
|
|
// 返回数据
|
|
|
|
type record struct {
|
|
|
|
GroupUser string
|
2023-09-25 14:16:59 +08:00
|
|
|
Nickname string
|
2023-09-21 17:33:59 +08:00
|
|
|
Count int64
|
|
|
|
}
|
|
|
|
|
|
|
|
var records []record
|
2023-11-28 16:09:36 +08:00
|
|
|
err = 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 AND tgu.skip_chat_rank = 0").
|
2023-09-25 14:16:59 +08:00
|
|
|
Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`").
|
|
|
|
Where("tm.from_user = ?", gid).
|
2023-10-07 09:41:40 +08:00
|
|
|
Where("tm.type < 10000").
|
2023-09-25 14:16:59 +08:00
|
|
|
Where("DATEDIFF(tm.create_at,NOW()) = -1").
|
|
|
|
Group("tm.group_user, tgu.nickname").Order("`count` DESC").
|
2023-11-28 16:09:36 +08:00
|
|
|
Limit(10).Find(&records).Error
|
2023-10-08 14:33:38 +08:00
|
|
|
|
2023-09-21 17:33:59 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("获取昨日消息失败, 错误信息: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-09-25 14:16:59 +08:00
|
|
|
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))
|
2023-09-21 17:33:59 +08:00
|
|
|
}
|
2023-09-25 14:16:59 +08:00
|
|
|
|
|
|
|
notifyMsgs = append(notifyMsgs, " \n请未上榜的群友多多反思。")
|
|
|
|
|
|
|
|
log.Printf("排行榜: \n%s", strings.Join(notifyMsgs, "\n"))
|
2023-10-11 09:51:33 +08:00
|
|
|
go utils.SendMessage(gid, "", strings.Join(notifyMsgs, "\n"), 0)
|
2023-09-21 17:33:59 +08:00
|
|
|
}
|