dao.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package account
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-intl/conf"
  5. account "go-common/app/service/main/account/model"
  6. accrpc "go-common/app/service/main/account/rpc/client"
  7. "go-common/library/log"
  8. "go-common/library/net/metadata"
  9. "github.com/pkg/errors"
  10. )
  11. // Dao is archive dao.
  12. type Dao struct {
  13. // rpc
  14. accRPC *accrpc.Service3
  15. }
  16. // New new a archive dao.
  17. func New(c *conf.Config) (d *Dao) {
  18. d = &Dao{
  19. // rpc
  20. accRPC: accrpc.New3(c.AccountRPC),
  21. }
  22. return
  23. }
  24. // Relations3 relatons
  25. func (d *Dao) Relations3(c context.Context, owners []int64, mid int64) (follows map[int64]bool) {
  26. if len(owners) == 0 {
  27. return nil
  28. }
  29. follows = make(map[int64]bool, len(owners))
  30. for _, owner := range owners {
  31. follows[owner] = false
  32. }
  33. var (
  34. am map[int64]*account.Relation
  35. err error
  36. )
  37. ip := metadata.String(c, metadata.RemoteIP)
  38. arg := &account.ArgRelations{Owners: owners, Mid: mid, RealIP: ip}
  39. if am, err = d.accRPC.Relations3(c, arg); err != nil {
  40. log.Error("%+v", err)
  41. return
  42. }
  43. for i, a := range am {
  44. if _, ok := follows[i]; ok {
  45. follows[i] = a.Following
  46. }
  47. }
  48. return
  49. }
  50. // IsAttention is
  51. func (d *Dao) IsAttention(c context.Context, owners []int64, mid int64) (isAtten map[int64]int8) {
  52. if len(owners) == 0 || mid == 0 {
  53. return
  54. }
  55. ip := metadata.String(c, metadata.RemoteIP)
  56. arg := &account.ArgRelations{Owners: owners, Mid: mid, RealIP: ip}
  57. res, err := d.accRPC.Relations3(c, arg)
  58. if err != nil {
  59. log.Error("%+v", err)
  60. return
  61. }
  62. isAtten = make(map[int64]int8, len(res))
  63. for mid, rel := range res {
  64. if rel.Following {
  65. isAtten[mid] = 1
  66. }
  67. }
  68. return
  69. }
  70. // Card3 get card info by mid
  71. func (d *Dao) Card3(c context.Context, mid int64) (res *account.Card, err error) {
  72. arg := &account.ArgMid{Mid: mid}
  73. if res, err = d.accRPC.Card3(c, arg); err != nil {
  74. err = errors.Wrapf(err, "%v", arg)
  75. }
  76. return
  77. }
  78. // Cards3 get cards info by mids
  79. func (d *Dao) Cards3(c context.Context, mids []int64) (res map[int64]*account.Card, err error) {
  80. arg := &account.ArgMids{Mids: mids}
  81. if res, err = d.accRPC.Cards3(c, arg); err != nil {
  82. err = errors.Wrapf(err, "%v", arg)
  83. }
  84. return
  85. }
  86. // Following3 following.
  87. func (d *Dao) Following3(c context.Context, mid, owner int64) (follow bool, err error) {
  88. ip := metadata.String(c, metadata.RemoteIP)
  89. arg := &account.ArgRelation{Mid: mid, Owner: owner, RealIP: ip}
  90. rl, err := d.accRPC.Relation3(c, arg)
  91. if err != nil {
  92. err = errors.Wrapf(err, "%v", arg)
  93. return
  94. }
  95. if rl != nil {
  96. follow = rl.Following
  97. }
  98. return
  99. }
  100. // Infos3 rpc info get by mids .
  101. func (d *Dao) Infos3(c context.Context, mids []int64) (res map[int64]*account.Info, err error) {
  102. arg := &account.ArgMids{Mids: mids}
  103. if res, err = d.accRPC.Infos3(c, arg); err != nil {
  104. err = errors.Wrapf(err, "%v", arg)
  105. }
  106. return
  107. }