From caa48b88f52edfb83faa584a0fa244e63d78c8c3 Mon Sep 17 00:00:00 2001 From: chenlin Date: Tue, 4 Mar 2025 16:34:10 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BC=98=E5=8C=96=E8=90=A5=E6=94=B6?= =?UTF-8?q?=E5=88=86=E6=9E=90=EF=BC=8C=E4=BF=AE=E5=A4=8D=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E6=9C=88=E6=B2=A1=E6=9C=89=E6=8E=A8=E5=B9=BF=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E5=BD=93=E5=89=8D=E6=9C=88=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=B8=8D=E6=98=BE=E7=A4=BA=E7=9A=84=E7=BC=BA=E9=99=B7=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/apis/migumanage/migu_admin.go | 54 +++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/app/admin/apis/migumanage/migu_admin.go b/app/admin/apis/migumanage/migu_admin.go index fc004d9..1b608a3 100644 --- a/app/admin/apis/migumanage/migu_admin.go +++ b/app/admin/apis/migumanage/migu_admin.go @@ -2165,19 +2165,55 @@ func (e MiGuDeployService) CalculateRevenueAnalysis(c *gin.Context) { // return //} + // 确保有数据 + if len(result) <= 0 { + resp := models.RetentionMonthsResp{} + response.OK(c, resp, "查询成功") + } + earliestMonth := result[0].Month // 从查询结果中获取最早的月份 + currentMonth := time.Now().Format("2006-01") // 获取当前月份 + + // 生成所有月份列表 + allMonths := generateMonthRange(earliestMonth, currentMonth) + + // 用 map 记录已有数据 + monthMap := make(map[string]models.MonthlyRetention) + for _, data := range result { + monthMap[data.Month] = data + } + + // 确保所有月份都有数据 + var completeResult []models.MonthlyRetention + for _, month := range allMonths { + if data, exists := monthMap[month]; exists { + completeResult = append(completeResult, data) + } else { + // 补全没有数据的月份 + completeResult = append(completeResult, models.MonthlyRetention{ + Month: month, + NewUserCount: 0, + ValidUsersCount: 0, + RetainedUsersCount: 0, + TotalValidUsersCount: 0, + }) + } + } + + result = completeResult // 更新最终结果 + // 查询每个月的上个月未退订用户数 for i := 0; i < len(result); i++ { - currentMonth := result[i].Month + tempCurrentMonth := result[i].Month // 查询当前月之前所有月份未退订的用户数 - totalValidUsers, err := GetTotalValidUsers(e.Orm, currentMonth) + totalValidUsers, err := GetTotalValidUsers(e.Orm, tempCurrentMonth) if err != nil { response.Error(c, http.StatusInternalServerError, err, "未退订用户查询失败") return } // 查询当前月之前所有月份在本月2号之后退订的用户数 - totalUnsubscribedUsers, err := GetTotalUnsubscribedUsers(e.Orm, currentMonth) + totalUnsubscribedUsers, err := GetTotalUnsubscribedUsers(e.Orm, tempCurrentMonth) if err != nil { response.Error(c, http.StatusInternalServerError, err, "退订用户查询失败") return @@ -2237,6 +2273,18 @@ func (e MiGuDeployService) CalculateRevenueAnalysis(c *gin.Context) { response.OK(c, resp, "查询成功") } +// 生成从 start 到 end 之间的所有月份 +func generateMonthRange(start, end string) []string { + startTime, _ := time.Parse("2006-01", start) + endTime, _ := time.Parse("2006-01", end) + + var months []string + for t := startTime; !t.After(endTime); t = t.AddDate(0, 1, 0) { + months = append(months, t.Format("2006-01")) + } + return months +} + // GetTotalValidUsers 计算所有之前月份的未退订用户总数 func GetTotalValidUsers(db *gorm.DB, currentMonth string) (int, error) { var totalValidUsers int