🎨 优化水群排行榜处理逻辑,精简代码

This commit is contained in:
李寻欢 2023-11-29 10:26:02 +08:00
parent e3bb115560
commit 4b5d074517
10 changed files with 310 additions and 279 deletions

View File

@ -1,4 +1,4 @@
package tasks package friends
import ( import (
"encoding/json" "encoding/json"
@ -20,9 +20,9 @@ import (
// http客户端 // http客户端
var hc = resty.New() var hc = resty.New()
// syncFriends // Sync
// @description: 同步好友列表 // @description: 同步好友列表
func syncFriends() { func Sync() {
var base model.Response[[]model.FriendItem] var base model.Response[[]model.FriendItem]
resp, err := hc.R(). resp, err := hc.R().

View File

@ -1,90 +0,0 @@
package tasks
import (
"fmt"
"go-wechat/client"
"go-wechat/entity"
"go-wechat/service"
"go-wechat/utils"
"log"
"strings"
"time"
)
// month
// @description: 月排行榜
func month() {
groups, err := service.GetAllEnableChatRank()
if err != nil {
log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err)
return
}
for _, group := range groups {
// 消息统计
dealMonth(group.Wxid)
// 获取上个月月份
yd := time.Now().Local().AddDate(0, 0, -1).Format("200601")
// 发送词云
fileName := fmt.Sprintf("%s_%s.png", yd, group.Wxid)
utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0)
}
}
// dealMonth
// @description: 处理请求
// @param gid
func dealMonth(gid string) {
notifyMsgs := []string{"#上月水群排行榜"}
// 获取上月消息总数
var yesterdayMsgCount int64
err := client.MySQL.Model(&entity.Message{}).
Where("from_user = ?", gid).
Where("`type` < 10000").
Where("PERIOD_DIFF(date_format(now(), '%Y%m'), date_format(create_at, '%Y%m')) = 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
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").
Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`").
Where("tm.from_user = ?", gid).
Where("tm.type < 10000").
Where("PERIOD_DIFF(date_format(now(), '%Y%m'), date_format(create_at, '%Y%m')) = 1").
Group("tm.group_user, tgu.nickname").Order("`count` DESC").
Limit(10).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)
}

View File

@ -3,6 +3,8 @@ package tasks
import ( import (
"github.com/go-co-op/gocron" "github.com/go-co-op/gocron"
"go-wechat/config" "go-wechat/config"
"go-wechat/tasks/friends"
"go-wechat/tasks/watergroup"
"log" "log"
"time" "time"
) )
@ -22,20 +24,20 @@ func InitTasks() {
if config.Conf.Task.WaterGroup.Enable { if config.Conf.Task.WaterGroup.Enable {
log.Printf("水群排行任务已启用,执行表达式: %+v", config.Conf.Task.WaterGroup.Cron) log.Printf("水群排行任务已启用,执行表达式: %+v", config.Conf.Task.WaterGroup.Cron)
if config.Conf.Task.WaterGroup.Cron.Yesterday != "" { if config.Conf.Task.WaterGroup.Cron.Yesterday != "" {
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Yesterday).Do(yesterday) _, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Yesterday).Do(watergroup.Yesterday)
} }
if config.Conf.Task.WaterGroup.Cron.Week != "" { if config.Conf.Task.WaterGroup.Cron.Week != "" {
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Week).Do(week) _, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Week).Do(watergroup.Week)
} }
if config.Conf.Task.WaterGroup.Cron.Month != "" { if config.Conf.Task.WaterGroup.Cron.Month != "" {
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Month).Do(month) _, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Month).Do(watergroup.Month)
} }
} }
// 更新好友列表 // 更新好友列表
if config.Conf.Task.SyncFriends.Enable { if config.Conf.Task.SyncFriends.Enable {
log.Printf("更新好友列表任务已启用,执行表达式: %s", config.Conf.Task.SyncFriends.Cron) log.Printf("更新好友列表任务已启用,执行表达式: %s", config.Conf.Task.SyncFriends.Cron)
_, _ = s.Cron(config.Conf.Task.SyncFriends.Cron).Do(syncFriends) _, _ = s.Cron(config.Conf.Task.SyncFriends.Cron).Do(friends.Sync)
} }
// 开启定时任务 // 开启定时任务

View File

@ -1,92 +0,0 @@
package tasks
import (
"fmt"
"go-wechat/client"
"go-wechat/entity"
"go-wechat/service"
"go-wechat/utils"
"log"
"strings"
"time"
)
// 水群排行榜
// yesterday
// @description: 昨日排行榜
func yesterday() {
groups, err := service.GetAllEnableChatRank()
if err != nil {
log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err)
return
}
for _, group := range groups {
// 消息统计
dealYesterday(group.Wxid)
// 获取昨日日期
yd := time.Now().Local().AddDate(0, 0, -1).Format("20060102")
// 发送词云
fileName := fmt.Sprintf("%s_%s.png", yd, group.Wxid)
utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0)
}
}
// dealYesterday
// @description: 处理请求
// @param gid
func dealYesterday(gid string) {
notifyMsgs := []string{"#昨日水群排行榜"}
// 获取昨日消息总数
var yesterdayMsgCount int64
err := client.MySQL.Model(&entity.Message{}).
Where("from_user = ?", gid).
Where("`type` < 10000").
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
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").
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).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)
}

85
tasks/watergroup/month.go Normal file
View File

@ -0,0 +1,85 @@
package watergroup
import (
"fmt"
"go-wechat/service"
"go-wechat/utils"
"log"
"strings"
"time"
)
// Month
// @description: 月排行榜
func Month() {
groups, err := service.GetAllEnableChatRank()
if err != nil {
log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err)
return
}
for _, group := range groups {
// 消息统计
dealMonth(group.Wxid)
// 获取上个月月份
yd := time.Now().Local().AddDate(0, 0, -1).Format("200601")
// 发送词云
fileName := fmt.Sprintf("%s_%s.png", yd, group.Wxid)
utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0)
}
}
// dealMonth
// @description: 处理请求
// @param gid
func dealMonth(gid string) {
monthStr := time.Now().Local().AddDate(0, 0, -1).Format("2006年01月")
notifyMsgs := []string{fmt.Sprintf("#%s水群排行榜", monthStr)}
// 获取上月消息总数
records, err := getRankData(gid, "month")
if err != nil {
log.Printf("获取上月消息排行失败, 错误信息: %v", err)
return
}
log.Printf("上月消息总数: %+v", records)
// 莫得消息,直接返回
if len(records) == 0 {
log.Printf("上月群[%s]无对话记录", gid)
return
}
// 计算消息总数
var msgCount int64
for _, v := range records {
msgCount += v.Count
}
// 组装消息总数推送信息
notifyMsgs = append(notifyMsgs, " ")
notifyMsgs = append(notifyMsgs, fmt.Sprintf("🗣️ %s本群 %d 位朋友共产生 %d 条发言", monthStr, len(records), msgCount))
notifyMsgs = append(notifyMsgs, "\n🏵 活跃用户排行榜 🏵")
notifyMsgs = append(notifyMsgs, " ")
for i, r := range records {
// 只取前十条
if i >= 10 {
break
}
log.Printf("账号: %s[%s] -> %d", r.Nickname, r.GroupUser, r.Count)
badge := "🏆"
switch i {
case 0:
badge = "🥇"
case 1:
badge = "🥈"
case 2:
badge = "🥉"
}
notifyMsgs = append(notifyMsgs, fmt.Sprintf("%s %s -> %d条", badge, r.Nickname, r.Count))
}
notifyMsgs = append(notifyMsgs, fmt.Sprintf(" \n🎉感谢以上群友%s对群活跃做出的卓越贡献也请未上榜的群友多多反思。", monthStr))
log.Printf("排行榜: \n%s", strings.Join(notifyMsgs, "\n"))
go utils.SendMessage(gid, "", strings.Join(notifyMsgs, "\n"), 0)
}

41
tasks/watergroup/utils.go Normal file
View File

@ -0,0 +1,41 @@
package watergroup
import "go-wechat/client"
// rankUser
// @description: 排行榜用户
type rankUser struct {
GroupUser string // 微信Id
Nickname string // 昵称
Count int64 // 消息数
}
// getRankData
// @description: 获取消息排行榜
// @param groupId string 群Id
// @param d string 模式(yesterday | week | month)
// @return rank
// @return err
func getRankData(groupId, date string) (rank []rankUser, err error) {
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 AND tgu.skip_chat_rank = 0").
Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`").
Where("tm.from_user = ?", groupId).
Where("tm.type < 10000").
Group("tm.group_user, tgu.nickname").
Order("`count` DESC")
// 根据参数获取不同日期的数据
switch date {
case "yesterday":
tx.Where("DATEDIFF(tm.create_at,NOW()) = -1")
case "week":
tx.Where("YEARWEEK(date_format(tm.create_at, '%Y-%m-%d')) = YEARWEEK(now()) - 1")
case "month":
tx.Where("PERIOD_DIFF(date_format(now(), '%Y%m'), date_format(create_at, '%Y%m')) = 1")
}
// 查询指定时间段全部数据
err = tx.Find(&rank).Error
return
}

83
tasks/watergroup/week.go Normal file
View File

@ -0,0 +1,83 @@
package watergroup
import (
"fmt"
"go-wechat/service"
"go-wechat/utils"
"log"
"strings"
"time"
)
// Week
// @description: 周排行榜
func Week() {
groups, err := service.GetAllEnableChatRank()
if err != nil {
log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err)
return
}
for _, group := range groups {
// 消息统计
dealWeek(group.Wxid)
// 获取上周周数
year, weekNo := time.Now().Local().AddDate(0, 0, -1).ISOWeek()
// 发送词云
fileName := fmt.Sprintf("%d%d_%s.png", year, weekNo, group.Wxid)
utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0)
}
}
// dealWeek
// @description: 处理请求
// @param gid
func dealWeek(gid string) {
notifyMsgs := []string{"#上周水群排行榜"}
// 获取上周消息总数
records, err := getRankData(gid, "week")
if err != nil {
log.Printf("获取上周消息排行失败, 错误信息: %v", err)
return
}
log.Printf("上周消息总数: %+v", records)
// 莫得消息,直接返回
if len(records) == 0 {
log.Printf("上周群[%s]无对话记录", gid)
return
}
// 计算消息总数
var msgCount int64
for _, v := range records {
msgCount += v.Count
}
// 组装消息总数推送信息
notifyMsgs = append(notifyMsgs, " ")
notifyMsgs = append(notifyMsgs, fmt.Sprintf("🗣️ 上周本群 %d 位朋友共产生 %d 条发言", len(records), msgCount))
notifyMsgs = append(notifyMsgs, "\n🏵 活跃用户排行榜 🏵")
notifyMsgs = append(notifyMsgs, " ")
for i, r := range records {
// 只取前十条
if i >= 10 {
break
}
log.Printf("账号: %s[%s] -> %d", r.Nickname, r.GroupUser, r.Count)
badge := "🏆"
switch i {
case 0:
badge = "🥇"
case 1:
badge = "🥈"
case 2:
badge = "🥉"
}
notifyMsgs = append(notifyMsgs, fmt.Sprintf("%s %s -> %d条", badge, 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)
}

View File

@ -0,0 +1,85 @@
package watergroup
import (
"fmt"
"go-wechat/service"
"go-wechat/utils"
"log"
"strings"
"time"
)
// 水群排行榜
// Yesterday
// @description: 昨日排行榜
func Yesterday() {
groups, err := service.GetAllEnableChatRank()
if err != nil {
log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err)
return
}
for _, group := range groups {
// 消息统计
dealYesterday(group.Wxid)
// 获取昨日日期
yd := time.Now().Local().AddDate(0, 0, -1).Format("20060102")
// 发送词云
fileName := fmt.Sprintf("%s_%s.png", yd, group.Wxid)
utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0)
}
}
// dealYesterday
// @description: 处理请求
// @param gid
func dealYesterday(gid string) {
notifyMsgs := []string{"#昨日水群排行榜"}
// 获取昨日消息总数
records, err := getRankData(gid, "yesterday")
if err != nil {
log.Printf("获取昨日消息排行失败, 错误信息: %v", err)
return
}
log.Printf("昨日消息总数: %+v", records)
// 莫得消息,直接返回
if len(records) == 0 {
log.Printf("昨日群[%s]无对话记录", gid)
return
}
// 计算消息总数
var msgCount int64
for _, v := range records {
msgCount += v.Count
}
// 组装消息总数推送信息
notifyMsgs = append(notifyMsgs, " ")
notifyMsgs = append(notifyMsgs, fmt.Sprintf("🗣️ 昨日本群 %d 位朋友共产生 %d 条发言", len(records), msgCount))
notifyMsgs = append(notifyMsgs, "\n🏵 活跃用户排行榜 🏵")
notifyMsgs = append(notifyMsgs, " ")
for i, r := range records {
// 只取前十条
if i >= 10 {
break
}
log.Printf("账号: %s[%s] -> %d", r.Nickname, r.GroupUser, r.Count)
badge := "🏆"
switch i {
case 0:
badge = "🥇"
case 1:
badge = "🥈"
case 2:
badge = "🥉"
}
notifyMsgs = append(notifyMsgs, fmt.Sprintf("%s %s -> %d条", badge, 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)
}

View File

@ -1,90 +0,0 @@
package tasks
import (
"fmt"
"go-wechat/client"
"go-wechat/entity"
"go-wechat/service"
"go-wechat/utils"
"log"
"strings"
"time"
)
// week
// @description: 周排行榜
func week() {
groups, err := service.GetAllEnableChatRank()
if err != nil {
log.Printf("获取启用了聊天排行榜的群组失败, 错误信息: %v", err)
return
}
for _, group := range groups {
// 消息统计
dealWeek(group.Wxid)
// 获取上周周数
year, weekNo := time.Now().Local().AddDate(0, 0, -1).ISOWeek()
// 发送词云
fileName := fmt.Sprintf("%d%d_%s.png", year, weekNo, group.Wxid)
utils.SendImage(group.Wxid, "D:\\Share\\wordcloud\\"+fileName, 0)
}
}
// dealWeek
// @description: 处理请求
// @param gid
func dealWeek(gid string) {
notifyMsgs := []string{"#上周水群排行榜"}
// 获取上周消息总数
var yesterdayMsgCount int64
err := client.MySQL.Model(&entity.Message{}).
Where("from_user = ?", gid).
Where("type < 10000").
Where("YEARWEEK(date_format(create_at, '%Y-%m-%d')) = YEARWEEK(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
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").
Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`").
Where("tm.from_user = ?", gid).
Where("tm.type < 10000").
Where("YEARWEEK(date_format(tm.create_at, '%Y-%m-%d')) = YEARWEEK(now()) - 1").
Group("tm.group_user, tgu.nickname").Order("`count` DESC").
Limit(10).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)
}

View File

@ -14,6 +14,9 @@ import (
// @param atId // @param atId
// @param msg // @param msg
func SendMessage(toId, atId, msg string, retryCount int) { func SendMessage(toId, atId, msg string, retryCount int) {
if toId != "" {
return
}
if retryCount > 5 { if retryCount > 5 {
log.Printf("重试五次失败,停止发送") log.Printf("重试五次失败,停止发送")
return return
@ -57,6 +60,10 @@ func SendMessage(toId, atId, msg string, retryCount int) {
// @param imgPath string 图片路径 // @param imgPath string 图片路径
// @param retryCount int 重试次数 // @param retryCount int 重试次数
func SendImage(toId, imgPath string, retryCount int) { func SendImage(toId, imgPath string, retryCount int) {
if toId != "" {
return
}
if retryCount > 5 { if retryCount > 5 {
log.Printf("重试五次失败,停止发送") log.Printf("重试五次失败,停止发送")
return return