get_wxs_logic.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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)).Page(l.ctx, req.Page, req.PageSize)
  63. if err != nil {
  64. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  65. }
  66. resp = &types.WxResp{}
  67. resp.Msg = errormsg.Success
  68. resp.Data.Total = wxs.PageDetails.Total
  69. var wxIds []string
  70. wxSet := make(map[string]*ent.Wx)
  71. for _, w := range wxs.List {
  72. wxIds = append(wxIds, w.Wxid)
  73. wxSet[w.Wxid] = w
  74. }
  75. var wxList []types.WxData
  76. if isCurrentDay {
  77. // 返回当日每小时的数据
  78. lastHourData, err := l.svcCtx.DB.UsageStatisticHour.Query().
  79. Where(
  80. usagestatistichour.OrganizationID(organizationId),
  81. usagestatistichour.BotIDIn(wxIds...),
  82. ).
  83. Order(ent.Desc(usagestatistichour.FieldAddtime)).
  84. First(l.ctx)
  85. if err != nil {
  86. return nil, err
  87. }
  88. if lastHourData == nil {
  89. return nil, err
  90. }
  91. usageStatisticHour, err := l.svcCtx.DB.UsageStatisticHour.Query().
  92. Where(
  93. usagestatistichour.OrganizationID(organizationId),
  94. usagestatistichour.BotIDIn(wxIds...),
  95. usagestatistichour.Addtime(lastHourData.Addtime),
  96. ).All(l.ctx)
  97. if err != nil {
  98. return nil, err
  99. }
  100. for _, hourData := range usageStatisticHour {
  101. wxList = append(wxList, types.WxData{
  102. Nickname: wxSet[hourData.BotID].Nickname,
  103. InteractionRate: float32(hourData.ActiveUser) / float32(hourData.TotalFriend),
  104. TotalFriend: hourData.TotalFriend,
  105. TotalGroup: hourData.TotalGroup,
  106. })
  107. }
  108. } else {
  109. // 返回当日每小时的数据
  110. lastDayData, err := l.svcCtx.DB.UsageStatisticDay.Query().
  111. Where(
  112. usagestatisticday.OrganizationID(organizationId),
  113. usagestatisticday.BotIDIn(wxIds...),
  114. ).
  115. Order(ent.Desc(usagestatisticday.FieldAddtime)).
  116. First(l.ctx)
  117. l.Infof("----------------lastDayData--------------: %+v", lastDayData)
  118. if err != nil {
  119. return nil, err
  120. }
  121. if lastDayData == nil {
  122. return nil, err
  123. }
  124. usageStatisticDay, err := l.svcCtx.DB.UsageStatisticDay.Query().
  125. Where(
  126. usagestatisticday.OrganizationID(organizationId),
  127. usagestatisticday.BotIDIn(wxIds...),
  128. usagestatisticday.Addtime(lastDayData.Addtime),
  129. ).All(l.ctx)
  130. if err != nil {
  131. return nil, err
  132. }
  133. for _, dayData := range usageStatisticDay {
  134. l.Infof("----------------dayData--------------: %+v", dayData)
  135. rate := float32(0)
  136. if dayData.TotalFriend != 0 {
  137. rate = float32(dayData.ActiveUser) / float32(dayData.TotalFriend)
  138. }
  139. wxList = append(wxList, types.WxData{
  140. Nickname: wxSet[dayData.BotID].Nickname,
  141. InteractionRate: rate,
  142. TotalFriend: dayData.TotalFriend,
  143. TotalGroup: dayData.TotalGroup,
  144. })
  145. }
  146. }
  147. l.Infof("----------------wxList--------------: %+v", wxList)
  148. resp.Data.Data = wxList
  149. return resp, nil
  150. }