boweniac 3 месяцев назад
Родитель
Сommit
e7bd3cf034
1 измененных файлов с 29 добавлено и 18 удалено
  1. 29 18
      internal/logic/dashboard/get_charts_logic.go

+ 29 - 18
internal/logic/dashboard/get_charts_logic.go

@@ -8,6 +8,7 @@ import (
 	"time"
 	"wechat-api/ent/custom_types"
 	"wechat-api/ent/label"
+	"wechat-api/ent/predicate"
 	"wechat-api/ent/usagestatisticday"
 	"wechat-api/ent/usagestatistichour"
 	"wechat-api/internal/svc"
@@ -44,7 +45,9 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 	var organizationId uint64 = 0
 	isAdmin := l.ctx.Value("isAdmin").(bool)
 	if isAdmin && req.OrganizationId != nil && *req.OrganizationId != 0 {
-		organizationId = *req.OrganizationId
+		if req.OrganizationId != nil && *req.OrganizationId != 0 {
+			organizationId = *req.OrganizationId
+		}
 	} else {
 		organizationId = l.ctx.Value("organizationId").(uint64)
 	}
@@ -113,11 +116,14 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 			fmt.Println("转换开始时间失败:", err)
 			return nil, err
 		}
-		usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query().Where(
-			usagestatistichour.OrganizationID(organizationId),
-			usagestatistichour.BotIDIsNil(),
-			usagestatistichour.AddtimeGTE(startAddTime),
-		).All(l.ctx)
+
+		var predicates []predicate.UsageStatisticHour
+		if organizationId != 0 {
+			predicates = append(predicates, usagestatistichour.OrganizationID(organizationId))
+		}
+		predicates = append(predicates, usagestatistichour.BotIDIsNil())
+		predicates = append(predicates, usagestatistichour.AddtimeGTE(startAddTime))
+		usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query().Where(predicates...).All(l.ctx)
 		if err != nil {
 			return nil, err
 		}
@@ -177,13 +183,15 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 			fmt.Println("转换截止时间失败:", err)
 			return nil, err
 		}
-		usageStatisticDay, err := l.svcCtx.DB.UsageStatisticDay.Query().
-			Where(
-				usagestatisticday.OrganizationID(organizationId),
-				usagestatisticday.BotIDIsNil(),
-				usagestatisticday.AddtimeGTE(startAddTime),
-				usagestatisticday.AddtimeLTE(endAddTime),
-			).All(l.ctx)
+
+		var predicatesDay []predicate.UsageStatisticDay
+		if organizationId != 0 {
+			predicatesDay = append(predicatesDay, usagestatisticday.OrganizationID(organizationId))
+		}
+		predicatesDay = append(predicatesDay, usagestatisticday.BotIDIsNil())
+		predicatesDay = append(predicatesDay, usagestatisticday.AddtimeGTE(startAddTime))
+		predicatesDay = append(predicatesDay, usagestatisticday.AddtimeLTE(endAddTime))
+		usageStatisticDay, err := l.svcCtx.DB.UsageStatisticDay.Query().Where(predicatesDay...).All(l.ctx)
 		if err != nil {
 			return nil, err
 		}
@@ -237,11 +245,14 @@ func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp
 				fmt.Println("转换开始时间失败:", err)
 				return nil, err
 			}
-			usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query().Where(
-				usagestatistichour.OrganizationID(organizationId),
-				usagestatistichour.BotIDIsNil(),
-				usagestatistichour.AddtimeGTE(startOfDayAddTime),
-			).All(l.ctx)
+
+			var predicates []predicate.UsageStatisticHour
+			if organizationId != 0 {
+				predicates = append(predicates, usagestatistichour.OrganizationID(organizationId))
+			}
+			predicates = append(predicates, usagestatistichour.BotIDIsNil())
+			predicates = append(predicates, usagestatistichour.AddtimeGTE(startOfDayAddTime))
+			usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query().Where(predicates...).All(l.ctx)
 			if err != nil {
 				return nil, err
 			}