package dashboard import ( "context" "fmt" "github.com/suyuan32/simple-admin-common/msg/errormsg" "github.com/suyuan32/simple-admin-common/utils/pointy" "time" "wechat-api/ent" "wechat-api/ent/usagestatisticday" "wechat-api/ent/usagestatistichour" "wechat-api/ent/wx" "wechat-api/internal/utils/dberrorhandler" "wechat-api/internal/svc" "wechat-api/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type GetWxsLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewGetWxsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWxsLogic { return &GetWxsLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx} } 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 } else { organizationId = l.ctx.Value("organizationId").(uint64) } // 解析起始和截止时间 layouts := []string{ "2006-01", // 对应 "2024-01" "2006-01-02", // 对应 "2024-01-01" } var layoutsType int var endTime time.Time for i, layout := range layouts { endTime, err = time.Parse(layout, *req.EndDate) layoutsType = i 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() } wxs, err := l.svcCtx.DB.Wx.Query().Where(wx.OrganizationID(organizationId)).Page(l.ctx, req.Page, req.PageSize) if err != nil { return nil, dberrorhandler.DefaultEntError(l.Logger, err, req) } resp := &types.WxResp{} resp.Msg = errormsg.Success resp.Data.Total = wxs.PageDetails.Total var wxIds []string wxSet := make(map[string]*ent.Wx) for _, w := range wxs.List { wxIds = append(wxIds, w.Wxid) wxSet[w.Wxid] = w } var wxList []types.WxData if isCurrentDay { // 返回当日每小时的数据 lastHourData, err := l.svcCtx.DB.UsageStatisticHour.Query(). Where( usagestatistichour.OrganizationID(organizationId), usagestatistichour.BotIDIn(wxIds...), ). Order(ent.Desc(usagestatistichour.FieldAddtime)). First(l.ctx) if err != nil { return nil, err } if lastHourData == nil { return nil, err } usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query(). Where( usagestatistichour.OrganizationID(organizationId), usagestatistichour.BotIDIn(wxIds...), usagestatistichour.Addtime(lastHourData.Addtime), ).All(l.ctx) if err != nil { return nil, err } for _, hourData := range usageStatisticHour { wxList = append(wxList, types.WxData{ Nickname: wxSet[hourData.BotID].Nickname, InteractionRate: float32(hourData.ActiveUser) / float32(hourData.TotalFriend), TotalFriend: hourData.TotalFriend, TotalGroup: hourData.TotalGroup, }) } } else { // 返回当日每小时的数据 lastDayData, err := l.svcCtx.DB.UsageStatisticDay.Query(). Where( usagestatisticday.OrganizationID(organizationId), usagestatisticday.BotIDIn(wxIds...), ). Order(ent.Desc(usagestatisticday.FieldAddtime)). First(l.ctx) if err != nil { return nil, err } if lastDayData == nil { return nil, err } usageStatisticDay, err := l.svcCtx.DB.UsageStatisticDay.Query(). Where( usagestatisticday.OrganizationID(organizationId), usagestatisticday.BotIDIn(wxIds...), usagestatisticday.Addtime(lastDayData.Addtime), ).All(l.ctx) if err != nil { return nil, err } for _, dayData := range usageStatisticDay { wxList = append(wxList, types.WxData{ Nickname: wxSet[dayData.BotID].Nickname, InteractionRate: float32(dayData.ActiveUser) / float32(dayData.TotalFriend), TotalFriend: dayData.TotalFriend, TotalGroup: dayData.TotalGroup, }) } } resp.Data.Data = wxList return resp, nil }