forked from lxh/go-wxhelper
✨ 水群排行榜新增周榜和月榜
This commit is contained in:
parent
500e241f8d
commit
b4470b0888
@ -22,7 +22,10 @@ task:
|
||||
cron: '*/5 * * * *' # 五分钟一次
|
||||
waterGroup:
|
||||
enable: false
|
||||
cron: '30 9 * * *'
|
||||
cron:
|
||||
yesterday: '30 9 * * *' # 每天9:30
|
||||
week: '30 9 * * 1' # 每周一9:30
|
||||
month: '30 9 1 * *' # 每月1号9:30
|
||||
# 需要发送水群排行榜的群Id
|
||||
groups:
|
||||
- '18958257758@chatroom'
|
||||
|
@ -10,19 +10,3 @@ type Config struct {
|
||||
Wechat wechat `json:"wechat" yaml:"wechat"` // 微信助手
|
||||
Ai ai `json:"ai" yaml:"ai"` // AI配置
|
||||
}
|
||||
|
||||
// task
|
||||
// @description: 定时任务
|
||||
type task struct {
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用
|
||||
SyncFriends struct {
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用
|
||||
Cron string `json:"cron" yaml:"cron"` // 定时任务表达式
|
||||
} `json:"syncFriends" yaml:"syncFriends"` // 同步好友
|
||||
WaterGroup struct {
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用
|
||||
Cron string `json:"cron" yaml:"cron"` // 定时任务表达式
|
||||
Groups []string `json:"groups" yaml:"groups"` // 启用的群Id
|
||||
Blacklist []string `json:"blacklist" yaml:"blacklist"` // 黑名单
|
||||
} `json:"waterGroup" yaml:"waterGroup"` // 水群排行榜
|
||||
}
|
||||
|
33
config/task.go
Normal file
33
config/task.go
Normal file
@ -0,0 +1,33 @@
|
||||
package config
|
||||
|
||||
// task
|
||||
// @description: 定时任务
|
||||
type task struct {
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用
|
||||
SyncFriends syncFriends `json:"syncFriends" yaml:"syncFriends"` // 同步好友
|
||||
WaterGroup waterGroup `json:"waterGroup" yaml:"waterGroup"` // 水群排行榜
|
||||
}
|
||||
|
||||
// syncFriends
|
||||
// @description: 同步好友
|
||||
type syncFriends struct {
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用
|
||||
Cron string `json:"cron" yaml:"cron"` // 定时任务表达式
|
||||
}
|
||||
|
||||
// waterGroup
|
||||
// @description: 水群排行榜
|
||||
type waterGroup struct {
|
||||
Enable bool `json:"enable" yaml:"enable"` // 是否启用
|
||||
Cron waterGroupCron `json:"cron" yaml:"cron"` // 定时任务表达式
|
||||
Groups []string `json:"groups" yaml:"groups"` // 启用的群Id
|
||||
Blacklist []string `json:"blacklist" yaml:"blacklist"` // 黑名单
|
||||
}
|
||||
|
||||
// waterGroupCron
|
||||
// @description: 水群排行榜定时任务
|
||||
type waterGroupCron struct {
|
||||
Yesterday string `json:"yesterday" yaml:"yesterday"` // 昨日排行榜
|
||||
Week string `json:"week" yaml:"week"` // 周排行榜
|
||||
Month string `json:"month" yaml:"month"` // 月排行榜
|
||||
}
|
86
tasks/month.go
Normal file
86
tasks/month.go
Normal file
@ -0,0 +1,86 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go-wechat/client"
|
||||
"go-wechat/config"
|
||||
"go-wechat/entity"
|
||||
"go-wechat/utils"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// month
|
||||
// @description: 月排行榜
|
||||
func month() {
|
||||
for _, id := range config.Conf.Task.WaterGroup.Groups {
|
||||
dealMonth(id)
|
||||
}
|
||||
}
|
||||
|
||||
// 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("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
|
||||
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("YEARWEEK(date_format(tm.create_at, '%Y-%m-%d')) = YEARWEEK(now()) - 1").
|
||||
Group("tm.group_user, tgu.nickname").Order("`count` DESC").
|
||||
Limit(10)
|
||||
|
||||
// 黑名单
|
||||
blacklist := config.Conf.Task.WaterGroup.Blacklist
|
||||
// 如果有黑名单,过滤掉
|
||||
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)
|
||||
}
|
@ -20,8 +20,16 @@ func InitTasks() {
|
||||
|
||||
// 水群排行
|
||||
if config.Conf.Task.WaterGroup.Enable {
|
||||
log.Printf("水群排行任务已启用,执行表达式: %s", config.Conf.Task.WaterGroup.Cron)
|
||||
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron).Do(yesterday)
|
||||
log.Printf("水群排行任务已启用,执行表达式: %+v", config.Conf.Task.WaterGroup.Cron)
|
||||
if config.Conf.Task.WaterGroup.Cron.Yesterday != "" {
|
||||
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Yesterday).Do(yesterday)
|
||||
}
|
||||
if config.Conf.Task.WaterGroup.Cron.Week != "" {
|
||||
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Week).Do(week)
|
||||
}
|
||||
if config.Conf.Task.WaterGroup.Cron.Month != "" {
|
||||
_, _ = s.Cron(config.Conf.Task.WaterGroup.Cron.Month).Do(month)
|
||||
}
|
||||
}
|
||||
|
||||
// 更新好友列表
|
||||
|
@ -30,6 +30,7 @@ func dealYesterday(gid 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 {
|
||||
|
86
tasks/week.go
Normal file
86
tasks/week.go
Normal file
@ -0,0 +1,86 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go-wechat/client"
|
||||
"go-wechat/config"
|
||||
"go-wechat/entity"
|
||||
"go-wechat/utils"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// week
|
||||
// @description: 周排行榜
|
||||
func week() {
|
||||
for _, id := range config.Conf.Task.WaterGroup.Groups {
|
||||
dealWeek(id)
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
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("YEARWEEK(date_format(tm.create_at, '%Y-%m-%d')) = YEARWEEK(now()) - 1").
|
||||
Group("tm.group_user, tgu.nickname").Order("`count` DESC").
|
||||
Limit(10)
|
||||
|
||||
// 黑名单
|
||||
blacklist := config.Conf.Task.WaterGroup.Blacklist
|
||||
// 如果有黑名单,过滤掉
|
||||
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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user