dao.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package account
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-view/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 account dao.
  12. type Dao struct {
  13. // rpc
  14. accRPC *accrpc.Service3
  15. }
  16. // New account dao.
  17. func New(c *conf.Config) (d *Dao) {
  18. d = &Dao{
  19. accRPC: accrpc.New3(c.AccountRPC),
  20. }
  21. return
  22. }
  23. // Card3 get card info by mid
  24. func (d *Dao) Card3(c context.Context, mid int64) (res *account.Card, err error) {
  25. arg := &account.ArgMid{Mid: mid}
  26. if res, err = d.accRPC.Card3(c, arg); err != nil {
  27. err = errors.Wrapf(err, "%v", arg)
  28. }
  29. return
  30. }
  31. func (d *Dao) Cards3(c context.Context, mids []int64) (res map[int64]*account.Card, err error) {
  32. arg := &account.ArgMids{Mids: mids}
  33. if res, err = d.accRPC.Cards3(c, arg); err != nil {
  34. err = errors.Wrapf(err, "%v", arg)
  35. }
  36. return
  37. }
  38. // Following3 following.
  39. func (d *Dao) Following3(c context.Context, mid, owner int64) (follow bool, err error) {
  40. ip := metadata.String(c, metadata.RemoteIP)
  41. arg := &account.ArgRelation{Mid: mid, Owner: owner, RealIP: ip}
  42. rl, err := d.accRPC.Relation3(c, arg)
  43. if err != nil {
  44. err = errors.Wrapf(err, "%v", arg)
  45. return
  46. }
  47. if rl != nil {
  48. follow = rl.Following
  49. }
  50. return
  51. }
  52. // IsAttention is attention
  53. func (d *Dao) IsAttention(c context.Context, owners []int64, mid int64) (isAtten map[int64]int8) {
  54. if len(owners) == 0 || mid == 0 {
  55. return
  56. }
  57. ip := metadata.String(c, metadata.RemoteIP)
  58. arg := &account.ArgRelations{Owners: owners, Mid: mid, RealIP: ip}
  59. res, err := d.accRPC.Relations3(c, arg)
  60. if err != nil {
  61. log.Error("%+v", err)
  62. return
  63. }
  64. isAtten = make(map[int64]int8, len(res))
  65. for mid, rel := range res {
  66. if rel.Following {
  67. isAtten[mid] = 1
  68. }
  69. }
  70. return
  71. }