get_wxs_logic.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package dashboard
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  6. "strconv"
  7. "time"
  8. "wechat-api/ent"
  9. "wechat-api/ent/predicate"
  10. "wechat-api/ent/usagestatisticday"
  11. "wechat-api/ent/usagestatistichour"
  12. "wechat-api/ent/wx"
  13. "wechat-api/internal/utils/dberrorhandler"
  14. "wechat-api/internal/svc"
  15. "wechat-api/internal/types"
  16. "github.com/zeromicro/go-zero/core/logx"
  17. )
  18. type GetWxsLogic struct {
  19. logx.Logger
  20. ctx context.Context
  21. svcCtx *svc.ServiceContext
  22. }
  23. func NewGetWxsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWxsLogic {
  24. return &GetWxsLogic{
  25. Logger: logx.WithContext(ctx),
  26. ctx: ctx,
  27. svcCtx: svcCtx}
  28. }
  29. func (l *GetWxsLogic) GetWxs(req *types.WxReq) (resp *types.WxResp, err error) {
  30. // 获取组织id
  31. var organizationId uint64 = 0
  32. isAdmin := l.ctx.Value("isAdmin").(bool)
  33. if isAdmin {
  34. if req.OrganizationId != nil && *req.OrganizationId != 0 {
  35. organizationId = *req.OrganizationId
  36. }
  37. } else {
  38. organizationId = l.ctx.Value("organizationId").(uint64)
  39. }
  40. // 解析起始和截止时间
  41. layouts := []string{
  42. "2006-01", // 对应 "2024-01"
  43. "2006-01-02", // 对应 "2024-01-01"
  44. }
  45. var layoutsType int
  46. var endTime time.Time
  47. for i, layout := range layouts {
  48. endTime, err = time.Parse(layout, *req.EndDate)
  49. layoutsType = i
  50. if err == nil {
  51. break
  52. }
  53. }
  54. if err != nil {
  55. fmt.Println("解析结束时间失败:", err)
  56. return
  57. }
  58. // 判断截止日期是否包含当前日
  59. var isCurrentDay bool
  60. now := time.Now()
  61. if layoutsType == 0 {
  62. isCurrentDay = endTime.Year() == now.Year() && endTime.Month() == now.Month()
  63. } else {
  64. isCurrentDay = endTime.Year() == now.Year() && endTime.Month() == now.Month() && endTime.Day() == now.Day()
  65. }
  66. var predicates []predicate.Wx
  67. if organizationId != 0 {
  68. predicates = append(predicates, wx.OrganizationID(organizationId))
  69. }
  70. wxs, err := l.svcCtx.DB.Wx.Query().Where(predicates...).All(l.ctx)
  71. if err != nil {
  72. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  73. }
  74. resp = &types.WxResp{}
  75. resp.Msg = errormsg.Success
  76. var wxIds []string
  77. wxSet := make(map[string]*ent.Wx)
  78. for _, w := range wxs {
  79. wxIds = append(wxIds, w.Wxid)
  80. wxSet[w.Wxid] = w
  81. }
  82. var wxList []types.WxData
  83. if isCurrentDay {
  84. // 返回当日每小时的数据
  85. var predicatesH []predicate.UsageStatisticHour
  86. if organizationId != 0 {
  87. predicatesH = append(predicatesH, usagestatistichour.OrganizationID(organizationId))
  88. predicatesH = append(predicatesH, usagestatistichour.BotIDIn(wxIds...))
  89. }
  90. lastHourData, err := l.svcCtx.DB.UsageStatisticHour.Query().
  91. Where(predicatesH...).
  92. Order(ent.Desc(usagestatistichour.FieldAddtime)).
  93. First(l.ctx)
  94. if err != nil {
  95. return nil, err
  96. }
  97. if lastHourData == nil {
  98. return nil, err
  99. }
  100. var predicatesHour []predicate.UsageStatisticHour
  101. if organizationId != 0 {
  102. predicatesHour = append(predicatesHour, usagestatistichour.OrganizationID(organizationId))
  103. predicatesHour = append(predicatesHour, usagestatistichour.BotIDIn(wxIds...))
  104. }
  105. predicatesHour = append(predicatesHour, usagestatistichour.Addtime(lastHourData.Addtime))
  106. usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query().Where(predicatesHour...).Page(l.ctx, req.Page, req.PageSize)
  107. if err != nil {
  108. return nil, err
  109. }
  110. if usageStatisticHour != nil && usageStatisticHour.List != nil {
  111. resp.Data.Total = usageStatisticHour.PageDetails.Total
  112. for _, hourData := range usageStatisticHour.List {
  113. wxList = append(wxList, types.WxData{
  114. Nickname: wxSet[hourData.BotID].Nickname,
  115. InteractionRate: float32(hourData.ActiveUser) / float32(hourData.TotalFriend),
  116. TotalFriend: hourData.TotalFriend,
  117. TotalGroup: hourData.TotalGroup,
  118. })
  119. }
  120. }
  121. } else {
  122. endAddTimeString := endTime.Format("20060102")
  123. endAddTime, err := strconv.ParseUint(endAddTimeString, 10, 64)
  124. if err != nil {
  125. fmt.Println("转换截止时间失败:", err)
  126. return nil, err
  127. }
  128. var predicatesDay []predicate.UsageStatisticDay
  129. if organizationId != 0 {
  130. predicatesDay = append(predicatesDay, usagestatisticday.OrganizationID(organizationId))
  131. predicatesDay = append(predicatesDay, usagestatisticday.BotIDIn(wxIds...))
  132. }
  133. predicatesDay = append(predicatesDay, usagestatisticday.Addtime(endAddTime))
  134. usageStatisticDay, err := l.svcCtx.DB.UsageStatisticDay.Query().
  135. Where(predicatesDay...).Page(l.ctx, req.Page, req.PageSize)
  136. if err != nil {
  137. return nil, err
  138. }
  139. if usageStatisticDay != nil && usageStatisticDay.List != nil {
  140. resp.Data.Total = usageStatisticDay.PageDetails.Total
  141. for _, dayData := range usageStatisticDay.List {
  142. rate := float32(0)
  143. if dayData.TotalFriend != 0 {
  144. rate = float32(dayData.ActiveUser) / float32(dayData.TotalFriend)
  145. }
  146. wxList = append(wxList, types.WxData{
  147. Nickname: wxSet[dayData.BotID].Nickname,
  148. InteractionRate: rate,
  149. TotalFriend: dayData.TotalFriend,
  150. TotalGroup: dayData.TotalGroup,
  151. })
  152. }
  153. }
  154. }
  155. resp.Data.Data = wxList
  156. return resp, nil
  157. }