user.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/bbq/user/api"
  6. "go-common/app/service/bbq/user/internal/model"
  7. accountv1 "go-common/app/service/main/account/api"
  8. "go-common/library/log"
  9. "go-common/library/time"
  10. "go-common/library/xstr"
  11. )
  12. const (
  13. _userLikeNum = 128
  14. _userFollowNum = 128
  15. _userFanNum = 128
  16. )
  17. //calcTableID .
  18. func (d *Dao) calcTableID(num, mid int64) string {
  19. id := mid % 100
  20. return fmt.Sprintf("%02d", id)
  21. }
  22. func (d *Dao) getTableIndex(mid int64) int64 {
  23. return mid % 100
  24. }
  25. //userLikeSQL .
  26. func (d *Dao) userLikeSQL(mid int64, sql string) string {
  27. tableName := "user_like_" + d.calcTableID(_userLikeNum, mid)
  28. return fmt.Sprintf(sql, tableName)
  29. }
  30. //userFollowSQL .
  31. func (d *Dao) userFollowSQL(mid int64, sql string) string {
  32. tableName := "user_follow_" + d.calcTableID(_userFollowNum, mid)
  33. return fmt.Sprintf(sql, tableName)
  34. }
  35. //userFanSQL .
  36. func (d *Dao) userFanSQL(mid int64, sql string) string {
  37. tableName := "user_fan_" + d.calcTableID(_userFanNum, mid)
  38. return fmt.Sprintf(sql, tableName)
  39. }
  40. // isMidIn 获取mid的关注up主
  41. // 如果key在map中,那么value值肯定为1
  42. func (d *Dao) isMidIn(c context.Context, mid int64, candidateMIDs []int64, sql string) (MIDMap map[int64]bool) {
  43. if len(candidateMIDs) == 0 {
  44. return
  45. }
  46. MIDMap = make(map[int64]bool)
  47. tableName := d.getTableIndex(mid)
  48. midstr := xstr.JoinInts(candidateMIDs)
  49. querySQL := fmt.Sprintf(sql, tableName, midstr)
  50. log.V(1).Infov(c, log.KV("event", "fetch_list"), log.KV("sql", querySQL))
  51. rows, err := d.db.Query(c, querySQL, mid)
  52. if err != nil {
  53. log.Errorv(c, log.KV("event", "mysql_select"), log.KV("sql", querySQL))
  54. return
  55. }
  56. defer rows.Close()
  57. for rows.Next() {
  58. var followedMid int64
  59. if err = rows.Scan(&followedMid); err != nil {
  60. log.Errorv(c, log.KV("event", "mysql_scan"), log.KV("sql", querySQL))
  61. continue
  62. }
  63. MIDMap[followedMid] = true
  64. }
  65. log.Infov(c, log.KV("event", "mysql_select"), log.KV("sql", querySQL), log.KV("mid", mid),
  66. log.KV("req_size", len(candidateMIDs)), log.KV("rsp_size", len(MIDMap)))
  67. return MIDMap
  68. }
  69. // fetchPartRelationUserList 获取相关的用户列表,可以是关注列表也可以是粉丝列表,根据sql区别
  70. func (d *Dao) fetchPartRelationUserList(c context.Context, mid int64, cursor model.CursorValue, sql string) (
  71. MID2IDMap map[int64]time.Time, relationMIDs []int64, err error) {
  72. MID2IDMap = make(map[int64]time.Time)
  73. querySQL := fmt.Sprintf(sql, d.getTableIndex(mid), model.UserListLen)
  74. log.V(1).Infov(c, log.KV("event", "fetch_follow_list"), log.KV("sql", querySQL))
  75. rows, err := d.db.Query(c, querySQL, mid, cursor.CursorTime)
  76. if err != nil {
  77. log.Errorv(c, log.KV("event", "mysql_select"), log.KV("sql", querySQL))
  78. return
  79. }
  80. defer rows.Close()
  81. conflict := bool(true)
  82. for rows.Next() {
  83. var relationMID int64
  84. var mtime time.Time
  85. if err = rows.Scan(&relationMID, &mtime); err != nil {
  86. log.Errorv(c, log.KV("event", "mysql_scan"), log.KV("sql", querySQL))
  87. return
  88. }
  89. // 为了解决同一个mtime的冲突问题
  90. if mtime == cursor.CursorTime && conflict {
  91. if relationMID == cursor.CursorID {
  92. conflict = false
  93. }
  94. continue
  95. }
  96. relationMIDs = append(relationMIDs, relationMID)
  97. MID2IDMap[relationMID] = mtime
  98. }
  99. log.Infov(c, log.KV("event", "mysql_select"), log.KV("sql", querySQL),
  100. log.KV("mid", mid), log.KV("relation_num", len(relationMIDs)))
  101. return
  102. }
  103. //GetUserBProfile 获取用户全量b站信息
  104. func (d *Dao) GetUserBProfile(c context.Context, in *api.PhoneCheckReq) (res *accountv1.ProfileReply, err error) {
  105. req := &accountv1.MidReq{
  106. Mid: in.Mid,
  107. RealIp: "",
  108. }
  109. res, err = d.accountClient.Profile3(c, req)
  110. return
  111. }