get_wxs_logic.go 4.4 KB

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