manager.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "sync"
  7. "go-common/app/admin/main/usersuit/model"
  8. "go-common/library/log"
  9. "go-common/library/sync/errgroup"
  10. )
  11. // Managers get manager users info.
  12. func (d *Dao) Managers(c context.Context) (manMap map[int64]string, err error) {
  13. var (
  14. count, page int64
  15. g errgroup.Group
  16. l sync.RWMutex
  17. )
  18. if count, err = d.ManagerTotal(c); err != nil {
  19. log.Error("d.ManagerTotal error(%v)", err)
  20. return
  21. }
  22. if count <= 0 {
  23. return
  24. }
  25. manMap = make(map[int64]string, count)
  26. ps := int64(500)
  27. pageNum := count / ps
  28. if count%ps != 0 {
  29. pageNum++
  30. }
  31. for page = 1; page <= pageNum; page++ {
  32. tmpPage := page
  33. g.Go(func() (err error) {
  34. mi, err := d.Manager(c, tmpPage, ps)
  35. if err != nil {
  36. log.Error("d.Manager(%d,%d) error(%v) ", tmpPage, ps, err)
  37. err = nil
  38. return
  39. }
  40. for _, v := range mi {
  41. l.Lock()
  42. manMap[v.OID] = v.Uname
  43. l.Unlock()
  44. }
  45. return
  46. })
  47. }
  48. g.Wait()
  49. return
  50. }
  51. // Manager get manager users.
  52. func (d *Dao) Manager(c context.Context, pn, ps int64) (mi []*model.MangerInfo, err error) {
  53. params := url.Values{}
  54. params.Set("pn", strconv.FormatInt(pn, 10))
  55. params.Set("ps", strconv.FormatInt(ps, 10))
  56. var res struct {
  57. Code int `json:"code"`
  58. Data *struct {
  59. Items []*model.MangerInfo `json:"items"`
  60. } `json:"data"`
  61. }
  62. if err = d.client.Get(c, d.managersURL, "", params, &res); err != nil {
  63. log.Error("Manager(%s) error(%v)", d.managersURL+"?"+params.Encode(), err)
  64. return
  65. }
  66. if res.Code != 0 {
  67. log.Warn("Manager(%s) code(%d) data(%+v)", d.managersURL+"?"+params.Encode(), res.Code, res.Data)
  68. return
  69. }
  70. if res.Data != nil {
  71. mi = res.Data.Items
  72. }
  73. return
  74. }
  75. // ManagerTotal get manager user total.
  76. func (d *Dao) ManagerTotal(c context.Context) (count int64, err error) {
  77. params := url.Values{}
  78. var res struct {
  79. Code int `json:"code"`
  80. Data *struct {
  81. Total int64 `json:"total"`
  82. } `json:"data"`
  83. }
  84. if err = d.client.Get(c, d.managerTotalURL, "", params, &res); err != nil {
  85. log.Error("ManagerTotal(%s) error(%v)", d.managerTotalURL+"?"+params.Encode(), err)
  86. return
  87. }
  88. if res.Code != 0 {
  89. log.Warn("ManagerTotal(%s) code(%d) data(%+v)", d.managerTotalURL+"?"+params.Encode(), res.Code, res.Data)
  90. return
  91. }
  92. if res.Data != nil {
  93. count = res.Data.Total
  94. }
  95. return
  96. }