dao.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package account
  2. import (
  3. "context"
  4. "sync"
  5. "github.com/pkg/errors"
  6. "go-common/app/admin/main/credit/conf"
  7. creditMDL "go-common/app/admin/main/credit/model"
  8. blocked "go-common/app/admin/main/credit/model/blocked"
  9. accgrpc "go-common/app/service/main/account/api"
  10. "go-common/library/log"
  11. "go-common/library/sync/errgroup"
  12. )
  13. // Dao is account dao.
  14. type Dao struct {
  15. // grpc
  16. accountClient accgrpc.AccountClient
  17. }
  18. // New is initial for account .
  19. func New(c *conf.Config) (d *Dao) {
  20. d = &Dao{}
  21. var err error
  22. if d.accountClient, err = accgrpc.NewClient(c.AccClient); err != nil {
  23. panic(errors.WithMessage(err, "Failed to dial account service"))
  24. }
  25. return
  26. }
  27. // RPCInfo rpc info get by muti mid .
  28. func (d *Dao) RPCInfo(c context.Context, mid int64) (res *accgrpc.InfoReply, err error) {
  29. arg := &accgrpc.MidReq{Mid: mid}
  30. if res, err = d.accountClient.Info3(c, arg); err != nil {
  31. log.Error("d.accountClient.Info3 error(%v)", err)
  32. }
  33. return
  34. }
  35. // RPCInfos rpc info get by muti mid .
  36. func (d *Dao) RPCInfos(c context.Context, mids []int64) (res map[int64]*accgrpc.Info, err error) {
  37. var (
  38. g errgroup.Group
  39. l sync.RWMutex
  40. args *accgrpc.MidsReq
  41. accRes *accgrpc.InfosReply
  42. )
  43. mids = creditMDL.ArrayUnique(mids)
  44. total := len(mids)
  45. pageNum := total / blocked.AccMaxPageSize
  46. if total%blocked.AccMaxPageSize != 0 {
  47. pageNum++
  48. }
  49. res = make(map[int64]*accgrpc.Info, total)
  50. for i := 0; i < pageNum; i++ {
  51. start := i * blocked.AccMaxPageSize
  52. end := (i + 1) * blocked.AccMaxPageSize
  53. if end > total {
  54. end = total
  55. }
  56. g.Go(func() (err error) {
  57. args = &accgrpc.MidsReq{Mids: mids[start:end]}
  58. if accRes, err = d.accountClient.Infos3(c, args); err != nil {
  59. log.Error("d.accountClient.Infos3(%+v) error(%v)", mids[start:end], err)
  60. err = nil
  61. return
  62. }
  63. for mid, info := range accRes.Infos {
  64. l.Lock()
  65. res[mid] = info
  66. l.Unlock()
  67. }
  68. return
  69. })
  70. }
  71. if err = g.Wait(); err != nil {
  72. log.Error("g.Wait error(%v)", err)
  73. }
  74. return
  75. }