mysql.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/job/main/figure-timer/model"
  8. xsql "go-common/library/database/sql"
  9. "github.com/pkg/errors"
  10. )
  11. const (
  12. _shard = 100
  13. _selFigure = `SELECT id,mid,score,lawful_score,wide_score,friendly_score,bounty_score,creativity_score,ver,ctime,mtime FROM figure_user_%02d WHERE mid=? LIMIT 1`
  14. _selFigures = `SELECT id,mid,score,lawful_score,wide_score,friendly_score,bounty_score,creativity_score,ver,ctime,mtime FROM figure_user_%02d WHERE mid>? LIMIT ?`
  15. _upsertFigure = `INSERT INTO figure_user_%02d (mid,score,lawful_score,wide_score,friendly_score,bounty_score,creativity_score,ver,ctime,mtime) VALUES (?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE score=?,lawful_score=?,wide_score=?,friendly_score=?,bounty_score=?,creativity_score=?,ver=?,mtime=?`
  16. _insertRank = `INSERT INTO figure_rank (score_from,score_to,percentage) VALUES(?,?,?) ON DUPLICATE KEY UPDATE score_from=?,score_to=?,percentage=?`
  17. _insertRankHistory = `INSERT INTO figure_rank_history (score_from,score_to,percentage,ver) VALUES(?,?,?,?)`
  18. )
  19. func hit(mid int64) int64 {
  20. return mid % _shard
  21. }
  22. // Figure get Figure from db
  23. func (d *Dao) Figure(c context.Context, mid int64) (figure *model.Figure, err error) {
  24. row := d.mysql.QueryRow(c, fmt.Sprintf(_selFigure, hit(mid)), mid)
  25. figure = &model.Figure{}
  26. if err = row.Scan(&figure.ID, &figure.Mid, &figure.Score, &figure.LawfulScore, &figure.WideScore, &figure.FriendlyScore, &figure.BountyScore, &figure.CreativityScore, &figure.Ver, &figure.Ctime, &figure.Mtime); err != nil {
  27. if err == xsql.ErrNoRows {
  28. err = nil
  29. figure = nil
  30. return
  31. }
  32. err = errors.WithStack(err)
  33. return
  34. }
  35. return
  36. }
  37. // Figures get all figure info from formMid
  38. func (d *Dao) Figures(c context.Context, fromMid int64, limit int) (figures []*model.Figure, end bool, err error) {
  39. if limit <= 0 {
  40. return
  41. }
  42. var (
  43. rows *xsql.Rows
  44. )
  45. if rows, err = d.mysql.Query(c, fmt.Sprintf(_selFigures, hit(fromMid)), fromMid, limit); err != nil {
  46. err = errors.WithStack(err)
  47. return
  48. }
  49. defer rows.Close()
  50. for rows.Next() {
  51. figure := &model.Figure{}
  52. if err = rows.Scan(&figure.ID, &figure.Mid, &figure.Score, &figure.LawfulScore, &figure.WideScore, &figure.FriendlyScore, &figure.BountyScore, &figure.CreativityScore, &figure.Ver, &figure.Ctime, &figure.Mtime); err != nil {
  53. if err == xsql.ErrNoRows {
  54. err = nil
  55. end = true
  56. return
  57. }
  58. return
  59. }
  60. figures = append(figures, figure)
  61. }
  62. if len(figures) < limit {
  63. end = true
  64. }
  65. err = errors.WithStack(rows.Err())
  66. return
  67. }
  68. // UpsertFigure insert or update(if mid duplicated) Figure
  69. func (d *Dao) UpsertFigure(c context.Context, figure *model.Figure) (id int64, err error) {
  70. var (
  71. result sql.Result
  72. now = time.Now()
  73. )
  74. if result, err = d.mysql.Exec(c, fmt.Sprintf(_upsertFigure, hit(figure.Mid)), figure.Mid, figure.Score, figure.LawfulScore, figure.WideScore, figure.FriendlyScore, figure.BountyScore, figure.CreativityScore, figure.Ver, now, now, figure.Score, figure.LawfulScore, figure.WideScore, figure.FriendlyScore, figure.BountyScore, figure.CreativityScore, figure.Ver, now); err != nil {
  75. return
  76. }
  77. if id, err = result.LastInsertId(); err != nil {
  78. err = errors.WithStack(err)
  79. }
  80. return
  81. }
  82. // InsertRankHistory insert figure rank history to db
  83. func (d *Dao) InsertRankHistory(c context.Context, rank *model.Rank) (id int64, err error) {
  84. var (
  85. res sql.Result
  86. )
  87. if res, err = d.mysql.Exec(c, _insertRankHistory, rank.ScoreFrom, rank.ScoreTo, rank.Percentage, rank.Ver); err != nil {
  88. return
  89. }
  90. if id, err = res.LastInsertId(); err != nil {
  91. err = errors.WithStack(err)
  92. return
  93. }
  94. return
  95. }
  96. // UpsertRank insert or update figure rank to db
  97. func (d *Dao) UpsertRank(c context.Context, rank *model.Rank) (id int64, err error) {
  98. var (
  99. res sql.Result
  100. )
  101. if res, err = d.mysql.Exec(c, _insertRank, rank.ScoreFrom, rank.ScoreTo, rank.Percentage, rank.ScoreFrom, rank.ScoreTo, rank.Percentage); err != nil {
  102. return
  103. }
  104. if id, err = res.LastInsertId(); err != nil {
  105. err = errors.WithStack(err)
  106. return
  107. }
  108. return
  109. }