mysql.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/relation/model/i64b"
  6. sml "go-common/app/service/main/relation/model"
  7. "go-common/library/database/sql"
  8. )
  9. const (
  10. _shard = 500
  11. _tagUserShard = 500
  12. // following
  13. _getRelationSQL = "SELECT r.attribute,r.mtime,t.tag FROM user_relation_mid_%03d AS r join user_relation_tag_user_%03d AS t ON t.mid=r.mid AND t.fid=r.fid WHERE r.mid=? AND r.fid=? AND r.status=0 "
  14. _UserSetAchieveFlag = "INSERT INTO user_addit (mid,achieve_flags) VALUES (?,?) ON DUPLICATE KEY UPDATE achieve_flags=achieve_flags|VALUES(achieve_flags)"
  15. )
  16. func hit(id int64) int64 {
  17. return id % _shard
  18. }
  19. func tagUserHit(id int64) int64 {
  20. return id % _tagUserShard
  21. }
  22. // UserRelation get user relation attr.
  23. func (d *Dao) UserRelation(c context.Context, mid, fid int64) (f *sml.Following, err error) {
  24. row := d.db.QueryRow(c, fmt.Sprintf(_getRelationSQL, hit(mid), tagUserHit(mid)), mid, fid)
  25. f = new(sml.Following)
  26. var ttag i64b.Int64Bytes
  27. if err = row.Scan(&f.Attribute, &f.MTime, &ttag); err != nil {
  28. if err == sql.ErrNoRows {
  29. err = nil
  30. f = nil
  31. }
  32. return
  33. }
  34. f.Mid = fid
  35. f.Tag = []int64(ttag)
  36. for _, id := range f.Tag {
  37. if id == -10 {
  38. f.Special = 1
  39. }
  40. }
  41. return
  42. }
  43. // UserSetAchieveFlag is
  44. func (d *Dao) UserSetAchieveFlag(ctx context.Context, mid int64, flag uint64) (int64, error) {
  45. res, err := d.db.Exec(ctx, _UserSetAchieveFlag, mid, flag)
  46. if err != nil {
  47. return 0, err
  48. }
  49. return res.RowsAffected()
  50. }