boweniac hai 3 meses
pai
achega
2f31117031

+ 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
 			}

+ 34 - 28
internal/logic/dashboard/get_wxs_logic.go

@@ -4,8 +4,10 @@ import (
 	"context"
 	"fmt"
 	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+	"strconv"
 	"time"
 	"wechat-api/ent"
+	"wechat-api/ent/predicate"
 	"wechat-api/ent/usagestatisticday"
 	"wechat-api/ent/usagestatistichour"
 	"wechat-api/ent/wx"
@@ -34,8 +36,10 @@ func (l *GetWxsLogic) GetWxs(req *types.WxReq) (resp *types.WxResp, err error) {
 	// 获取组织id
 	var organizationId uint64 = 0
 	isAdmin := l.ctx.Value("isAdmin").(bool)
-	if isAdmin && req.OrganizationId != nil && *req.OrganizationId != 0 {
-		organizationId = *req.OrganizationId
+	if isAdmin {
+		if req.OrganizationId != nil && *req.OrganizationId != 0 {
+			organizationId = *req.OrganizationId
+		}
 	} else {
 		organizationId = l.ctx.Value("organizationId").(uint64)
 	}
@@ -71,7 +75,11 @@ func (l *GetWxsLogic) GetWxs(req *types.WxReq) (resp *types.WxResp, err error) {
 		isCurrentDay = endTime.Year() == now.Year() && endTime.Month() == now.Month() && endTime.Day() == now.Day()
 	}
 
-	wxs, err := l.svcCtx.DB.Wx.Query().Where(wx.OrganizationID(organizationId)).All(l.ctx)
+	var predicates []predicate.Wx
+	if organizationId != 0 {
+		predicates = append(predicates, wx.OrganizationID(organizationId))
+	}
+	wxs, err := l.svcCtx.DB.Wx.Query().Where(predicates...).All(l.ctx)
 
 	if err != nil {
 		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
@@ -91,11 +99,13 @@ func (l *GetWxsLogic) GetWxs(req *types.WxReq) (resp *types.WxResp, err error) {
 
 	if isCurrentDay {
 		// 返回当日每小时的数据
+		var predicatesH []predicate.UsageStatisticHour
+		if organizationId != 0 {
+			predicatesH = append(predicatesH, usagestatistichour.OrganizationID(organizationId))
+			predicatesH = append(predicatesH, usagestatistichour.BotIDIn(wxIds...))
+		}
 		lastHourData, err := l.svcCtx.DB.UsageStatisticHour.Query().
-			Where(
-				usagestatistichour.OrganizationID(organizationId),
-				usagestatistichour.BotIDIn(wxIds...),
-			).
+			Where(predicatesH...).
 			Order(ent.Desc(usagestatistichour.FieldAddtime)).
 			First(l.ctx)
 		if err != nil {
@@ -104,12 +114,14 @@ func (l *GetWxsLogic) GetWxs(req *types.WxReq) (resp *types.WxResp, err error) {
 		if lastHourData == nil {
 			return nil, err
 		}
-		usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query().
-			Where(
-				usagestatistichour.OrganizationID(organizationId),
-				usagestatistichour.BotIDIn(wxIds...),
-				usagestatistichour.Addtime(lastHourData.Addtime),
-			).Page(l.ctx, req.Page, req.PageSize)
+
+		var predicatesHour []predicate.UsageStatisticHour
+		if organizationId != 0 {
+			predicatesHour = append(predicatesHour, usagestatistichour.OrganizationID(organizationId))
+			predicatesHour = append(predicatesHour, usagestatistichour.BotIDIn(wxIds...))
+		}
+		predicatesHour = append(predicatesHour, usagestatistichour.Addtime(lastHourData.Addtime))
+		usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query().Where(predicatesHour...).Page(l.ctx, req.Page, req.PageSize)
 		if err != nil {
 			return nil, err
 		}
@@ -125,26 +137,20 @@ func (l *GetWxsLogic) GetWxs(req *types.WxReq) (resp *types.WxResp, err error) {
 			}
 		}
 	} else {
-		// 返回当日每小时的数据
-		lastDayData, err := l.svcCtx.DB.UsageStatisticDay.Query().
-			Where(
-				usagestatisticday.OrganizationID(organizationId),
-				usagestatisticday.BotIDIn(wxIds...),
-			).
-			Order(ent.Desc(usagestatisticday.FieldAddtime)).
-			First(l.ctx)
+		endAddTimeString := endTime.Format("20060102")
+		endAddTime, err := strconv.ParseUint(endAddTimeString, 10, 64)
 		if err != nil {
+			fmt.Println("转换截止时间失败:", err)
 			return nil, err
 		}
-		if lastDayData == nil {
-			return nil, err
+		var predicatesDay []predicate.UsageStatisticDay
+		if organizationId != 0 {
+			predicatesDay = append(predicatesDay, usagestatisticday.OrganizationID(organizationId))
+			predicatesDay = append(predicatesDay, usagestatisticday.BotIDIn(wxIds...))
 		}
+		predicatesDay = append(predicatesDay, usagestatisticday.Addtime(endAddTime))
 		usageStatisticDay, err := l.svcCtx.DB.UsageStatisticDay.Query().
-			Where(
-				usagestatisticday.OrganizationID(organizationId),
-				usagestatisticday.BotIDIn(wxIds...),
-				usagestatisticday.Addtime(lastDayData.Addtime),
-			).Page(l.ctx, req.Page, req.PageSize)
+			Where(predicatesDay...).Page(l.ctx, req.Page, req.PageSize)
 		if err != nil {
 			return nil, err
 		}