dao.go 2.9 KB

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