mysql.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/figure/model"
  6. xsql "go-common/library/database/sql"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _shard = 100
  11. _figureInfo = "SELECT id, mid, score, lawful_score, wide_score, friendly_score, bounty_score, creativity_score, ver, ctime, mtime FROM figure_user_%02d WHERE mid=? ORDER BY id DESC LIMIT 1"
  12. _rank = `SELECT score_from,score_to,percentage FROM figure_rank ORDER BY percentage ASC`
  13. )
  14. func hit(mid int64) int64 {
  15. return mid % _shard
  16. }
  17. // FigureInfo get user figure info
  18. func (d *Dao) FigureInfo(c context.Context, mid int64) (res *model.Figure, err error) {
  19. row := d.db.QueryRow(c, fmt.Sprintf(_figureInfo, hit(mid)), mid)
  20. res = &model.Figure{}
  21. if err = row.Scan(&res.ID, &res.Mid, &res.Score, &res.LawfulScore, &res.WideScore, &res.FriendlyScore, &res.BountyScore, &res.CreativityScore, &res.Ver, &res.Ctime, &res.Mtime); err != nil {
  22. if err == xsql.ErrNoRows {
  23. err = nil
  24. res = nil
  25. return
  26. }
  27. err = errors.WithStack(err)
  28. }
  29. return
  30. }
  31. // Ranks get figure score rank by ver
  32. func (d *Dao) Ranks(c context.Context) (ranks []*model.Rank, err error) {
  33. var (
  34. rows *xsql.Rows
  35. )
  36. if rows, err = d.db.Query(c, _rank); err != nil {
  37. return
  38. }
  39. defer rows.Close()
  40. for rows.Next() {
  41. var rank = &model.Rank{}
  42. if err = rows.Scan(&rank.ScoreFrom, &rank.ScoreTo, &rank.Percentage); err != nil {
  43. ranks = nil
  44. return
  45. }
  46. ranks = append(ranks, rank)
  47. }
  48. err = rows.Err()
  49. return
  50. }