From 49705a03a854819a793312ac91b869ebe96c854d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Thu, 7 Mar 2024 09:42:59 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20=E6=B0=B4=E7=BE=A4=E6=8E=92=E8=A1=8C?= =?UTF-8?q?=E6=A6=9C=E6=96=B0=E5=A2=9E=E7=BE=A4=E6=B4=BB=E8=B7=83=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tasks/watergroup/month.go | 19 +++++++++++++++++++ tasks/watergroup/utils.go | 2 +- tasks/watergroup/week.go | 19 +++++++++++++++++++ tasks/watergroup/year.go | 19 +++++++++++++++++++ tasks/watergroup/yesterday.go | 19 +++++++++++++++++++ 5 files changed, 77 insertions(+), 1 deletion(-) diff --git a/tasks/watergroup/month.go b/tasks/watergroup/month.go index 1a40cf5..fd7703b 100644 --- a/tasks/watergroup/month.go +++ b/tasks/watergroup/month.go @@ -2,7 +2,9 @@ package watergroup import ( "fmt" + "go-wechat/client" "go-wechat/config" + "go-wechat/entity" "go-wechat/service" "go-wechat/utils" "log" @@ -55,6 +57,20 @@ func dealMonth(gid string) { log.Printf("上月群[%s]无对话记录", gid) return } + + // 查询群成员总数 + var groupUsers int64 + err = client.MySQL.Model(&entity.GroupUser{}).Where("group_id = ?", gid).Count(&groupUsers).Error + if err != nil { + log.Printf("查询群成员总数失败, 错误信息: %v", err) + } + // 计算活跃度 + showActivity := err != nil && groupUsers > 0 + activity := "0.00" + if groupUsers > 0 { + activity = fmt.Sprintf("%.2f", (float64(len(records))/float64(groupUsers))*100) + } + // 计算消息总数 var msgCount int64 for _, v := range records { @@ -63,6 +79,9 @@ func dealMonth(gid string) { // 组装消息总数推送信息 notifyMsgs = append(notifyMsgs, " ") notifyMsgs = append(notifyMsgs, fmt.Sprintf("🗣️ %s本群 %d 位朋友共产生 %d 条发言", monthStr, len(records), msgCount)) + if showActivity { + notifyMsgs = append(notifyMsgs, fmt.Sprintf("🎭 活跃度: %s%", activity)) + } notifyMsgs = append(notifyMsgs, "\n🏵 活跃用户排行榜 🏵") notifyMsgs = append(notifyMsgs, " ") diff --git a/tasks/watergroup/utils.go b/tasks/watergroup/utils.go index 87cdb53..510653b 100644 --- a/tasks/watergroup/utils.go +++ b/tasks/watergroup/utils.go @@ -18,7 +18,7 @@ type rankUser struct { // @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"). + 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 AND is_member = 1"). Select("tm.group_user", "tgu.nickname", "count( 1 ) AS `count`"). Where("tm.from_user = ?", groupId). Where("tm.type < 10000"). diff --git a/tasks/watergroup/week.go b/tasks/watergroup/week.go index 084310d..a27298a 100644 --- a/tasks/watergroup/week.go +++ b/tasks/watergroup/week.go @@ -2,7 +2,9 @@ package watergroup import ( "fmt" + "go-wechat/client" "go-wechat/config" + "go-wechat/entity" "go-wechat/service" "go-wechat/utils" "log" @@ -54,6 +56,20 @@ func dealWeek(gid string) { log.Printf("上周群[%s]无对话记录", gid) return } + + // 查询群成员总数 + var groupUsers int64 + err = client.MySQL.Model(&entity.GroupUser{}).Where("group_id = ?", gid).Count(&groupUsers).Error + if err != nil { + log.Printf("查询群成员总数失败, 错误信息: %v", err) + } + // 计算活跃度 + showActivity := err != nil && groupUsers > 0 + activity := "0.00" + if groupUsers > 0 { + activity = fmt.Sprintf("%.2f", (float64(len(records))/float64(groupUsers))*100) + } + // 计算消息总数 var msgCount int64 for _, v := range records { @@ -62,6 +78,9 @@ func dealWeek(gid string) { // 组装消息总数推送信息 notifyMsgs = append(notifyMsgs, " ") notifyMsgs = append(notifyMsgs, fmt.Sprintf("🗣️ 上周本群 %d 位朋友共产生 %d 条发言", len(records), msgCount)) + if showActivity { + notifyMsgs = append(notifyMsgs, fmt.Sprintf("🎭 活跃度: %s%", activity)) + } notifyMsgs = append(notifyMsgs, "\n🏵 活跃用户排行榜 🏵") notifyMsgs = append(notifyMsgs, " ") diff --git a/tasks/watergroup/year.go b/tasks/watergroup/year.go index 838d2fd..255fa8c 100644 --- a/tasks/watergroup/year.go +++ b/tasks/watergroup/year.go @@ -2,7 +2,9 @@ package watergroup import ( "fmt" + "go-wechat/client" "go-wechat/config" + "go-wechat/entity" "go-wechat/service" "go-wechat/utils" "log" @@ -54,6 +56,20 @@ func dealYear(gid string) { log.Printf("去年本群[%s]无对话记录", gid) return } + + // 查询群成员总数 + var groupUsers int64 + err = client.MySQL.Model(&entity.GroupUser{}).Where("group_id = ?", gid).Count(&groupUsers).Error + if err != nil { + log.Printf("查询群成员总数失败, 错误信息: %v", err) + } + // 计算活跃度 + showActivity := err != nil && groupUsers > 0 + activity := "0.00" + if groupUsers > 0 { + activity = fmt.Sprintf("%.2f", (float64(len(records))/float64(groupUsers))*100) + } + // 计算消息总数 var msgCount int64 for _, v := range records { @@ -70,6 +86,9 @@ func dealYear(gid string) { notifyMsgs = append(notifyMsgs, fmt.Sprintf("祝福你们新年快乐!让我们一起迎接%d年的到来!", time.Now().Local().Year())) notifyMsgs = append(notifyMsgs, " ") notifyMsgs = append(notifyMsgs, fmt.Sprintf("🗣️ 去年本群 %d 位朋友共产生 %d 条发言", len(records), msgCount)) + if showActivity { + notifyMsgs = append(notifyMsgs, fmt.Sprintf("🎭 活跃度: %s%", activity)) + } notifyMsgs = append(notifyMsgs, "\n🏵 活跃用户排行榜 🏵") notifyMsgs = append(notifyMsgs, " ") diff --git a/tasks/watergroup/yesterday.go b/tasks/watergroup/yesterday.go index 5a3f0d8..77adc5a 100644 --- a/tasks/watergroup/yesterday.go +++ b/tasks/watergroup/yesterday.go @@ -2,7 +2,9 @@ package watergroup import ( "fmt" + "go-wechat/client" "go-wechat/config" + "go-wechat/entity" "go-wechat/service" "go-wechat/utils" "log" @@ -56,6 +58,20 @@ func dealYesterday(gid string) { log.Printf("昨日群[%s]无对话记录", gid) return } + + // 查询群成员总数 + var groupUsers int64 + err = client.MySQL.Model(&entity.GroupUser{}).Where("group_id = ?", gid).Count(&groupUsers).Error + if err != nil { + log.Printf("查询群成员总数失败, 错误信息: %v", err) + } + // 计算活跃度 + showActivity := err != nil && groupUsers > 0 + activity := "0.00" + if groupUsers > 0 { + activity = fmt.Sprintf("%.2f", (float64(len(records))/float64(groupUsers))*100) + } + // 计算消息总数 var msgCount int64 for _, v := range records { @@ -64,6 +80,9 @@ func dealYesterday(gid string) { // 组装消息总数推送信息 notifyMsgs = append(notifyMsgs, " ") notifyMsgs = append(notifyMsgs, fmt.Sprintf("🗣️ 昨日本群 %d 位朋友共产生 %d 条发言", len(records), msgCount)) + if showActivity { + notifyMsgs = append(notifyMsgs, fmt.Sprintf("🎭 活跃度: %s%", activity)) + } notifyMsgs = append(notifyMsgs, "\n🏵 活跃用户排行榜 🏵") notifyMsgs = append(notifyMsgs, " ")