package dashboard import ( "context" "fmt" "github.com/suyuan32/simple-admin-common/msg/errormsg" "strconv" "time" "wechat-api/ent" "wechat-api/ent/usagestatisticday" "wechat-api/ent/usagestatistichour" "wechat-api/internal/svc" "wechat-api/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type GetChartsLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewGetChartsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetChartsLogic { return &GetChartsLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx} } type SumUint struct { Addtime uint64 `json:"addtime"` Value uint64 `json:"value"` } type SumInt struct { Addtime uint64 `json:"addtime"` Value int64 `json:"value"` } func (l *GetChartsLogic) GetCharts(req *types.ChartsReq) (resp *types.ChartsResp, err error) { // 获取组织id var organizationId uint64 = 0 isAdmin := l.ctx.Value("isAdmin").(bool) if isAdmin && req.OrganizationId != nil && *req.OrganizationId != 0 { organizationId = *req.OrganizationId } else { organizationId = l.ctx.Value("organizationId").(uint64) } // 解析起始和截止时间 layouts := []string{ "2006-01", // 对应 "2024-01" "2006-01-02", // 对应 "2024-01-01" } var layoutsType int var startTime time.Time for i, layout := range layouts { startTime, err = time.Parse(layout, *req.StartDate) layoutsType = i if err == nil { break } } l.Infof("----------------layoutsType--------------: %d", layoutsType) if err != nil { fmt.Println("解析开始时间失败:", err) return } var endTime time.Time for _, layout := range layouts { endTime, err = time.Parse(layout, *req.EndDate) if err == nil { break } } if err != nil { fmt.Println("解析结束时间失败:", err) return } // 判断截止日期是否包含当前日 var isCurrentDay bool now := time.Now() if layoutsType == 0 { isCurrentDay = endTime.Year() == now.Year() && endTime.Month() == now.Month() } else { isCurrentDay = endTime.Year() == now.Year() && endTime.Month() == now.Month() && endTime.Day() == now.Day() } // 定义变量 var aiResponse types.ChartsUint var sopRun types.ChartsUint var totalFriend types.ChartsUint var totalGroup types.ChartsUint var accountBalance types.ChartsUint var consumeToken types.ChartsUint var activeUser types.ChartsUint var newUser types.ChartsInt var aiResponseSum []SumUint var sopRunSum []SumUint var totalFriendSum []SumUint var totalGroupSum []SumUint var consumeTokenSum []SumUint var activeUserSum []SumUint var newUserSum []SumInt if isCurrentDay && layoutsType == 1 && req.StartDate == req.EndDate { // 返回当日每小时的数据 startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC) startAddTimeString := startOfDay.Format("2006010200") startAddTime, err := strconv.ParseUint(startAddTimeString, 10, 64) if err != nil { fmt.Println("转换开始时间失败:", err) return nil, err } usageStatisticHour := l.svcCtx.DB.UsageStatisticHour.Query().Where( usagestatistichour.OrganizationID(organizationId), usagestatistichour.AddtimeGTE(startAddTime), ).GroupBy(usagestatistichour.FieldAddtime) _ = usageStatisticHour.Aggregate(ent.As(ent.Sum(usagestatistichour.FieldAiResponse), "value")).Scan(l.ctx, &aiResponseSum) _ = usageStatisticHour.Aggregate(ent.As(ent.Sum(usagestatistichour.FieldSopRun), "value")).Scan(l.ctx, &sopRunSum) _ = usageStatisticHour.Aggregate(ent.As(ent.Sum(usagestatistichour.FieldTotalFriend), "value")).Scan(l.ctx, &totalFriendSum) _ = usageStatisticHour.Aggregate(ent.As(ent.Sum(usagestatistichour.FieldTotalGroup), "value")).Scan(l.ctx, &totalGroupSum) _ = usageStatisticHour.Aggregate(ent.As(ent.Sum(usagestatistichour.FieldConsumeToken), "value")).Scan(l.ctx, &consumeTokenSum) _ = usageStatisticHour.Aggregate(ent.As(ent.Sum(usagestatistichour.FieldActiveUser), "value")).Scan(l.ctx, &activeUserSum) _ = usageStatisticHour.Aggregate(ent.As(ent.Sum(usagestatistichour.FieldNewUser), "value")).Scan(l.ctx, &newUserSum) for _, ar := range aiResponseSum { aiResponse = types.ChartsUint{ Count: aiResponse.Count + ar.Value, Val: append(aiResponse.Val, ar.Value), } } for _, sr := range sopRunSum { sopRun = types.ChartsUint{ Count: sopRun.Count + sr.Value, Val: append(sopRun.Val, sr.Value), } } for _, tf := range totalFriendSum { totalFriend = types.ChartsUint{ Count: tf.Value, Val: append(totalFriend.Val, tf.Value), } } for _, tg := range totalGroupSum { totalGroup = types.ChartsUint{ Count: tg.Value, Val: append(totalGroup.Val, tg.Value), } } for _, ct := range consumeTokenSum { addtimeLastTwoDigits := ct.Addtime % 100 consumeToken = types.ChartsUint{ Count: consumeToken.Count + ct.Value, Val: append(consumeToken.Val, ct.Value), Label: append(consumeToken.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits)), } } for _, au := range activeUserSum { activeUser = types.ChartsUint{ Count: au.Value, Val: append(activeUser.Val, au.Value), } } for _, nu := range newUserSum { addtimeLastTwoDigits := nu.Addtime % 100 newUser = types.ChartsInt{ Count: nu.Value, Val: append(newUser.Val, nu.Value), Label: append(newUser.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits)), } } } else { // 从天表统计数据 startAddTimeString := startTime.Format("20060102") startAddTime, err := strconv.ParseUint(startAddTimeString, 10, 64) if err != nil { fmt.Println("转换开始时间失败:", err) return nil, err } endAddTimeString := endTime.Format("20060102") endAddTime, err := strconv.ParseUint(endAddTimeString, 10, 64) if err != nil { fmt.Println("转换截止时间失败:", err) return nil, err } usageStatisticDay := l.svcCtx.DB.UsageStatisticDay.Query(). Where( usagestatisticday.OrganizationID(organizationId), usagestatisticday.AddtimeGTE(startAddTime), usagestatisticday.AddtimeLTE(endAddTime), ). GroupBy(usagestatisticday.FieldAddtime) _ = usageStatisticDay.Aggregate(ent.As(ent.Sum(usagestatisticday.FieldAiResponse), "value")).Scan(l.ctx, &aiResponseSum) _ = usageStatisticDay.Aggregate(ent.As(ent.Sum(usagestatisticday.FieldSopRun), "value")).Scan(l.ctx, &sopRunSum) _ = usageStatisticDay.Aggregate(ent.As(ent.Sum(usagestatisticday.FieldTotalFriend), "value")).Scan(l.ctx, &totalFriendSum) _ = usageStatisticDay.Aggregate(ent.As(ent.Sum(usagestatisticday.FieldTotalGroup), "value")).Scan(l.ctx, &totalGroupSum) _ = usageStatisticDay.Aggregate(ent.As(ent.Sum(usagestatisticday.FieldConsumeToken), "value")).Scan(l.ctx, &consumeTokenSum) _ = usageStatisticDay.Aggregate(ent.As(ent.Sum(usagestatisticday.FieldActiveUser), "value")).Scan(l.ctx, &activeUserSum) _ = usageStatisticDay.Aggregate(ent.As(ent.Sum(usagestatisticday.FieldNewUser), "value")).Scan(l.ctx, &newUserSum) for _, ar := range aiResponseSum { aiResponse = types.ChartsUint{ Count: aiResponse.Count + ar.Value, Val: append(aiResponse.Val, ar.Value), } } for _, sr := range sopRunSum { sopRun = types.ChartsUint{ Count: sopRun.Count + sr.Value, Val: append(sopRun.Val, sr.Value), } } for _, tf := range totalFriendSum { totalFriend = types.ChartsUint{ Count: tf.Value, Val: append(totalFriend.Val, tf.Value), } } for _, tg := range totalGroupSum { totalGroup = types.ChartsUint{ Count: tg.Value, Val: append(totalGroup.Val, tg.Value), } } for _, ct := range consumeTokenSum { addtimeLastTwoDigits := ct.Addtime % 100 consumeToken = types.ChartsUint{ Count: consumeToken.Count + ct.Value, Val: append(consumeToken.Val, ct.Value), Label: append(consumeToken.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits)), } } for _, au := range activeUserSum { activeUser = types.ChartsUint{ Count: au.Value, Val: append(activeUser.Val, au.Value), } } for _, nu := range newUserSum { addtimeLastTwoDigits := nu.Addtime % 100 newUser = types.ChartsInt{ Count: nu.Value, Val: append(newUser.Val, nu.Value), Label: append(newUser.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits)), } } if isCurrentDay { // 从小时表统计当天数据 startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC) startOfDayAddTimeString := startOfDay.Format("2006010200") startOfDayAddTime, err := strconv.ParseUint(startOfDayAddTimeString, 10, 64) if err != nil { fmt.Println("转换开始时间失败:", err) return nil, err } usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query().Where( usagestatistichour.OrganizationID(organizationId), usagestatistichour.AddtimeGTE(startOfDayAddTime), ).All(l.ctx) if err != nil { return nil, err } hourLen := len(usageStatisticHour) if hourLen > 0 { var aiResponseOfDay uint64 var sopRunOfDay uint64 var totalFriendOfDay uint64 var totalGroupOfDay uint64 var consumeTokenOfDay uint64 var activeUserOfDay uint64 var newUserOfDay int64 for _, hourData := range usageStatisticHour { aiResponseOfDay += hourData.AiResponse sopRunOfDay += hourData.SopRun consumeTokenOfDay += hourData.ConsumeToken } totalFriendOfDay = usageStatisticHour[hourLen-1].TotalFriend totalGroupOfDay = usageStatisticHour[hourLen-1].TotalGroup activeUserOfDay = usageStatisticHour[hourLen-1].ActiveUser newUserOfDay = usageStatisticHour[hourLen-1].NewUser aiResponse = types.ChartsUint{ Count: aiResponse.Count + aiResponseOfDay, Val: append(aiResponse.Val, aiResponseOfDay), } sopRun = types.ChartsUint{ Count: sopRun.Count + sopRunOfDay, Val: append(sopRun.Val, sopRunOfDay), } totalFriend = types.ChartsUint{ Count: totalFriendOfDay, Val: append(totalFriend.Val, totalFriendOfDay), } totalGroup = types.ChartsUint{ Count: totalGroupOfDay, Val: append(totalGroup.Val, totalGroupOfDay), } consumeToken = types.ChartsUint{ Count: consumeToken.Count + consumeTokenOfDay, Val: append(consumeToken.Val, consumeTokenOfDay), Label: append(consumeToken.Label, "今日"), } activeUser = types.ChartsUint{ Count: activeUserOfDay, Val: append(activeUser.Val, activeUserOfDay), } newUser = types.ChartsInt{ Count: newUserOfDay, Val: append(newUser.Val, newUserOfDay), Label: append(newUser.Label, "今日"), } } } } chartsData := types.ChartsData{ AiResponse: &aiResponse, SopRun: &sopRun, TotalFriend: &totalFriend, TotalGroup: &totalGroup, AccountBalance: &accountBalance, ConsumeToken: &consumeToken, ActiveUser: &activeUser, NewUser: &newUser, } return &types.ChartsResp{BaseDataInfo: types.BaseDataInfo{Msg: errormsg.UpdateSuccess}, Data: &chartsData}, nil }