relation.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/relation/model"
  6. relationPB "go-common/app/service/main/relation/api"
  7. "go-common/library/log"
  8. "go-common/library/net/metadata"
  9. )
  10. const (
  11. _shard = 500
  12. _maxSize = 20000
  13. )
  14. func midTable(mid int64) string {
  15. return fmt.Sprintf("user_relation_mid_%03d", mid%_shard)
  16. }
  17. func fidTable(fid int64) string {
  18. return fmt.Sprintf("user_relation_fid_%03d", fid%_shard)
  19. }
  20. // Followers is
  21. func (d *Dao) Followers(ctx context.Context, fid int64, mid int64) (model.RelationList, error) {
  22. list := model.RelationList{}
  23. db := d.ReadORM.Table(fidTable(fid)).Where("status=?", 0).Where("fid=?", fid).Limit(_maxSize).Order("id desc")
  24. if mid > 0 {
  25. db = db.Where("mid=?", mid).Limit(1)
  26. }
  27. if err := db.Find(&list).Error; err != nil {
  28. return nil, err
  29. }
  30. for _, f := range list {
  31. f.ParseRelation()
  32. }
  33. return list, nil
  34. }
  35. // Followings is
  36. func (d *Dao) Followings(ctx context.Context, mid int64, fid int64) (model.RelationList, error) {
  37. list := model.RelationList{}
  38. db := d.ReadORM.Table(midTable(mid)).Where("status=?", 0).Where("mid=?", mid).Limit(_maxSize).Order("id desc")
  39. if fid > 0 {
  40. db = db.Where("fid=?", fid).Limit(1)
  41. }
  42. if err := db.Find(&list).Error; err != nil {
  43. return nil, err
  44. }
  45. for _, f := range list {
  46. f.ParseRelation()
  47. }
  48. return list, nil
  49. }
  50. // Stat is
  51. func (d *Dao) Stat(ctx context.Context, mid int64) (*relationPB.StatReply, error) {
  52. stat, err := d.relationClient.Stat(ctx, &relationPB.MidReq{
  53. Mid: mid,
  54. RealIp: metadata.String(ctx, metadata.RemoteIP),
  55. })
  56. if err != nil {
  57. log.Error("d.relationRPC.Stat err(%+v)", err)
  58. return nil, err
  59. }
  60. return stat, nil
  61. }
  62. // Stats is
  63. func (d *Dao) Stats(ctx context.Context, mids []int64) (map[int64]*relationPB.StatReply, error) {
  64. statReply, err := d.relationClient.Stats(ctx, &relationPB.MidsReq{
  65. Mids: mids,
  66. RealIp: metadata.String(ctx, metadata.RemoteIP),
  67. })
  68. if err != nil {
  69. log.Error("d.relationRPC.Stats err(%+v)", err)
  70. return nil, err
  71. }
  72. if len(statReply.StatReplyMap) == 0 {
  73. return nil, nil
  74. }
  75. return statReply.StatReplyMap, nil
  76. }