up.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package up
  2. import (
  3. "context"
  4. "sync"
  5. "go-common/app/interface/main/creative/model/up"
  6. upapi "go-common/app/service/main/up/api/v1"
  7. upmdl "go-common/app/service/main/up/model"
  8. "go-common/library/log"
  9. "go-common/library/sync/errgroup"
  10. "net/url"
  11. "strconv"
  12. )
  13. // UpInfo rpc
  14. func (d *Dao) UpInfo(c context.Context, mid int64, from int, ip string) (res *upmdl.UpInfo, err error) {
  15. var arg = &upmdl.ArgInfo{
  16. Mid: mid,
  17. From: from,
  18. }
  19. if res, err = d.up.Info(c, arg); err != nil {
  20. log.Error("d.up.Info error(%v)", err)
  21. }
  22. return
  23. }
  24. // UpSwitch get switch
  25. func (d *Dao) UpSwitch(c context.Context, mid int64, from int, ip string) (res *upmdl.PBUpSwitch, err error) {
  26. var arg = &upmdl.ArgUpSwitch{
  27. Mid: mid,
  28. From: from,
  29. IP: ip,
  30. }
  31. if res, err = d.up.UpSwitch(c, arg); err != nil {
  32. log.Error("d.up.UpSwitch error(%v)", err)
  33. }
  34. return
  35. }
  36. // SetUpSwitch set switch
  37. func (d *Dao) SetUpSwitch(c context.Context, mid int64, state, from int, ip string) (res *upmdl.PBSetUpSwitchRes, err error) {
  38. var arg = &upmdl.ArgUpSwitch{
  39. Mid: mid,
  40. From: from,
  41. State: state,
  42. IP: ip,
  43. }
  44. if res, err = d.up.SetUpSwitch(c, arg); err != nil {
  45. log.Error("d.up.SetUpSwitch error(%v)", err)
  46. }
  47. return
  48. }
  49. // UpSpecialGroups 获取UP主的特殊用户组
  50. func (d *Dao) UpSpecialGroups(c context.Context, mid int64) (groups map[int64]*up.SpecialGroup, err error) {
  51. groups = make(map[int64]*up.SpecialGroup)
  52. params := url.Values{}
  53. params.Set("mid", strconv.FormatInt(mid, 10))
  54. var res struct {
  55. Code int `json:"code"`
  56. Msg string `json:"message"`
  57. Data []*up.SpecialGroup `json:"data"`
  58. }
  59. if err = d.httpClient.Get(c, d.c.Host.API+_upSpecialGroupURI, "", params, &res); err != nil {
  60. log.Error("d.UpSpecialGroups() error(%v)", err)
  61. return
  62. }
  63. if res.Data == nil {
  64. log.Warn("UpSpecialGroups(%d) error when get up groups", mid)
  65. return
  66. }
  67. for _, v := range res.Data {
  68. groups[v.GroupID] = v
  69. }
  70. return
  71. }
  72. // UpSpecial 获取UP主的特殊用户组
  73. func (d *Dao) UpSpecial(c context.Context, gpid int64) (ups map[int64]int64, err error) {
  74. var (
  75. res *upapi.UpGroupMidsReply
  76. page int
  77. g errgroup.Group
  78. l sync.RWMutex
  79. )
  80. if res, err = d.UpClient.UpGroupMids(c, &upapi.UpGroupMidsReq{
  81. GroupID: gpid,
  82. Pn: 1,
  83. Ps: 1,
  84. }); err != nil {
  85. log.Error("UpSpecial d.UpSpecial gpid(%d)|error(%v)", gpid, err)
  86. return
  87. }
  88. log.Warn("UpSpecial get total: gpid(%d)|total(%d)", gpid, res.Total)
  89. if res.Total <= 0 {
  90. return
  91. }
  92. ups = make(map[int64]int64, res.Total)
  93. ps := int(10000)
  94. pageNum := res.Total / ps
  95. if res.Total%ps != 0 {
  96. pageNum++
  97. }
  98. for page = 1; page <= pageNum; page++ {
  99. tmpPage := page
  100. g.Go(func() (err error) {
  101. resgg, err := d.UpClient.UpGroupMids(c, &upapi.UpGroupMidsReq{
  102. GroupID: gpid,
  103. Pn: tmpPage,
  104. Ps: ps,
  105. })
  106. if err != nil {
  107. log.Error("d.UpGroupMids gg (%d,%d,%d) error(%v) ", gpid, tmpPage, ps, err)
  108. err = nil
  109. return
  110. }
  111. for _, mid := range resgg.Mids {
  112. l.Lock()
  113. ups[mid] = mid
  114. l.Unlock()
  115. }
  116. return
  117. })
  118. }
  119. g.Wait()
  120. log.Warn("UpSpecial get result: gpid,total,midslen,upslens (%d)|(%d)|(%d)", gpid, res.Total, len(ups))
  121. return
  122. }