package dashboard import ( "context" "fmt" "github.com/suyuan32/simple-admin-common/msg/errormsg" "strconv" "time" "wechat-api/ent/custom_types" "wechat-api/ent/label" "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 } } 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() } // 定义变量 aiResponse := types.ChartsUint{} sopRun := types.ChartsUint{} totalFriend := types.ChartsUint{} totalGroup := types.ChartsUint{} accountBalance := types.ChartsUint{} consumeToken := types.ChartsUint{} activeUser := types.ChartsUint{} newUser := types.ChartsInt{} var labelDists []custom_types.LabelDist 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, err := l.svcCtx.DB.UsageStatisticHour.Query().Where( usagestatistichour.OrganizationID(organizationId), usagestatistichour.BotIDIsNil(), usagestatistichour.AddtimeGTE(startAddTime), ).All(l.ctx) if err != nil { return nil, err } for _, hourData := range usageStatisticHour { addtimeLastTwoDigits := hourData.Addtime % 100 aiResponse.Count = aiResponse.Count + hourData.AiResponse aiResponse.Val = append(aiResponse.Val, hourData.AiResponse) sopRun.Count = sopRun.Count + hourData.SopRun sopRun.Val = append(sopRun.Val, hourData.SopRun) totalFriend.Count = hourData.TotalFriend totalFriend.Val = append(totalFriend.Val, hourData.TotalFriend) totalGroup.Count = hourData.TotalGroup totalGroup.Val = append(totalGroup.Val, hourData.TotalGroup) consumeToken.Count = consumeToken.Count + hourData.ConsumeToken consumeToken.Val = append(consumeToken.Val, hourData.ConsumeToken) consumeToken.Label = append(consumeToken.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits)) activeUser.Count = hourData.ActiveUser activeUser.Val = append(activeUser.Val, hourData.ActiveUser) newUser.Count = hourData.NewUser newUser.Val = append(newUser.Val, hourData.NewUser) newUser.Label = append(newUser.Label, fmt.Sprintf("%02d", addtimeLastTwoDigits)) } hourLen := len(usageStatisticHour) if hourLen > 0 { if usageStatisticHour[0].TotalFriend > 0 { totalFriend.Rate = float32((usageStatisticHour[hourLen-1].TotalFriend - usageStatisticHour[0].TotalFriend) / usageStatisticHour[0].TotalFriend) } if usageStatisticHour[0].TotalGroup > 0 { totalGroup.Rate = float32((usageStatisticHour[hourLen-1].TotalGroup - usageStatisticHour[0].TotalGroup) / usageStatisticHour[0].TotalGroup) } if usageStatisticHour[0].ActiveUser > 0 { activeUser.Rate = float32((usageStatisticHour[hourLen-1].ActiveUser - usageStatisticHour[0].ActiveUser) / usageStatisticHour[0].ActiveUser) } if usageStatisticHour[0].NewUser > 0 { newUser.Rate = float32((usageStatisticHour[hourLen-1].NewUser - usageStatisticHour[0].NewUser) / usageStatisticHour[0].NewUser) } labelDists = usageStatisticHour[hourLen-1].LabelDist } } 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, err := l.svcCtx.DB.UsageStatisticDay.Query(). Where( usagestatisticday.OrganizationID(organizationId), usagestatisticday.BotIDIsNil(), usagestatisticday.AddtimeGTE(startAddTime), usagestatisticday.AddtimeLTE(endAddTime), ).All(l.ctx) if err != nil { return nil, err } for _, dayData := range usageStatisticDay { aiResponse.Count = aiResponse.Count + dayData.AiResponse aiResponse.Val = append(aiResponse.Val, dayData.AiResponse) sopRun.Count = sopRun.Count + dayData.SopRun sopRun.Val = append(sopRun.Val, dayData.SopRun) totalFriend.Count = dayData.TotalFriend totalFriend.Val = append(totalFriend.Val, dayData.TotalFriend) totalGroup.Count = dayData.TotalGroup totalGroup.Val = append(totalGroup.Val, dayData.TotalGroup) consumeToken.Count = consumeToken.Count + dayData.ConsumeToken consumeToken.Val = append(consumeToken.Val, dayData.ConsumeToken) consumeToken.Label = append(consumeToken.Label, fmt.Sprintf("%d", dayData.Addtime)) activeUser.Count = dayData.ActiveUser activeUser.Val = append(activeUser.Val, dayData.ActiveUser) newUser.Count = dayData.NewUser newUser.Val = append(newUser.Val, dayData.NewUser) newUser.Label = append(newUser.Label, fmt.Sprintf("%d", dayData.Addtime)) } dayLen := len(usageStatisticDay) if dayLen > 0 { if usageStatisticDay[0].TotalFriend > 0 { totalFriend.Rate = float32((usageStatisticDay[dayLen-1].TotalFriend - usageStatisticDay[0].TotalFriend) / usageStatisticDay[0].TotalFriend) } if usageStatisticDay[0].TotalGroup > 0 { totalGroup.Rate = float32((usageStatisticDay[dayLen-1].TotalGroup - usageStatisticDay[0].TotalGroup) / usageStatisticDay[0].TotalGroup) } if usageStatisticDay[0].ActiveUser > 0 { activeUser.Rate = float32((usageStatisticDay[dayLen-1].ActiveUser - usageStatisticDay[0].ActiveUser) / usageStatisticDay[0].ActiveUser) } if usageStatisticDay[0].NewUser > 0 { newUser.Rate = float32((usageStatisticDay[dayLen-1].NewUser - usageStatisticDay[0].NewUser) / usageStatisticDay[0].NewUser) } labelDists = usageStatisticDay[dayLen-1].LabelDist } 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.BotIDIsNil(), 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.Count = aiResponse.Count + aiResponseOfDay aiResponse.Val = append(aiResponse.Val, aiResponseOfDay) sopRun.Count = sopRun.Count + sopRunOfDay sopRun.Val = append(sopRun.Val, sopRunOfDay) totalFriend.Count = totalFriendOfDay totalFriend.Val = append(totalFriend.Val, totalFriendOfDay) tfLen := len(totalFriend.Val) if tfLen > 0 && totalFriend.Val[0] > 0 { totalFriend.Rate = float32((totalFriend.Val[tfLen-1] - totalFriend.Val[0]) / totalFriend.Val[0]) } totalGroup.Count = totalGroupOfDay totalGroup.Val = append(totalGroup.Val, totalGroupOfDay) tgLen := len(totalGroup.Val) if tgLen > 0 && totalGroup.Val[0] > 0 { totalGroup.Rate = float32((totalGroup.Val[tgLen-1] - totalGroup.Val[0]) / totalGroup.Val[0]) } consumeToken.Count = consumeToken.Count + consumeTokenOfDay consumeToken.Val = append(consumeToken.Val, consumeTokenOfDay) consumeToken.Label = append(consumeToken.Label, "今日") activeUser.Count = activeUserOfDay activeUser.Val = append(activeUser.Val, activeUserOfDay) auLen := len(activeUser.Val) if auLen > 0 && activeUser.Val[0] > 0 { activeUser.Rate = float32((activeUser.Val[auLen-1] - activeUser.Val[0]) / activeUser.Val[0]) } newUser.Count = newUserOfDay newUser.Val = append(newUser.Val, newUserOfDay) newUser.Label = append(newUser.Label, "今日") nuLen := len(newUser.Val) if nuLen > 0 && newUser.Val[0] > 0 { newUser.Rate = float32((newUser.Val[nuLen-1] - newUser.Val[0]) / newUser.Val[0]) } labelDists = usageStatisticHour[hourLen-1].LabelDist } } } var labelIds []uint64 var labelsData []types.LabelsData labelSet := make(map[uint64]uint64) for _, labelDist := range labelDists { if labelDist.Count != 0 { labelIds = append(labelIds, labelDist.LabelID) labelSet[labelDist.LabelID] = labelDist.Count } } labelInfos, err := l.svcCtx.DB.Label.Query().Where(label.IDIn(labelIds...)).All(l.ctx) for _, labelInfo := range labelInfos { labelsData = append(labelsData, types.LabelsData{ Value: labelSet[labelInfo.ID], Name: labelInfo.Name, }) } chartsData := types.ChartsData{ AiResponse: &aiResponse, SopRun: &sopRun, TotalFriend: &totalFriend, TotalGroup: &totalGroup, AccountBalance: &accountBalance, ConsumeToken: &consumeToken, ActiveUser: &activeUser, NewUser: &newUser, LabelDist: labelsData, } return &types.ChartsResp{BaseDataInfo: types.BaseDataInfo{Msg: errormsg.UpdateSuccess}, Data: &chartsData}, nil }