get_wxs_logic.go 5.2 KB

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