From 011057b85ee7e0b2861c15520c7dac67e1a449f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Sun, 8 Oct 2023 14:33:38 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E5=8A=A0=E5=85=A5=E9=BB=91?= =?UTF-8?q?=E5=90=8D=E5=8D=95=E6=9C=BA=E5=88=B6=EF=BC=8C=E5=9C=A8=E9=BB=91?= =?UTF-8?q?=E5=90=8D=E5=8D=95=E7=9A=84Id=E4=B8=8D=E5=8F=82=E4=B8=8E?= =?UTF-8?q?=E6=B0=B4=E7=BE=A4=E7=BB=9F=E8=AE=A1=E6=8E=92=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + tasks/water_group.go | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 1e7bd868..45a581b1 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ cache log dist *.log +blacklist.txt \ No newline at end of file diff --git a/tasks/water_group.go b/tasks/water_group.go index 92383c39..02c04687 100644 --- a/tasks/water_group.go +++ b/tasks/water_group.go @@ -5,6 +5,7 @@ import ( "go-wechat/client" "go-wechat/entity" "go-wechat/utils" + "io" "log" "os" "strings" @@ -28,9 +29,24 @@ func yesterday() { 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{}). + err = client.MySQL.Model(&entity.Message{}). Where("from_user = ?", gid). Where("DATEDIFF(create_at,NOW()) = -1"). Count(&yesterdayMsgCount).Error @@ -54,14 +70,22 @@ func dealYesterday(gid string) { } 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"). 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(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 { log.Printf("获取昨日消息失败, 错误信息: %v", err) return