mysql.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/figure/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _shard = 100
  12. _insertFigure = "INSERT INTO figure_user_%02d (mid,score,lawful_score,wide_score,friendly_score,bounty_score,creativity_score,ver,ctime,mtime) VALUES (?,?,?,?,?,?,?,?,?,?)"
  13. _existFigure = "SELECT id FROM figure_user_%02d WHERE mid=? LIMIT 1"
  14. )
  15. func hit(mid int64) int64 {
  16. return mid % _shard
  17. }
  18. // ExistFigure exist user figure info
  19. func (d *Dao) ExistFigure(c context.Context, mid int64) (id int64, err error) {
  20. res := d.db.QueryRow(c, fmt.Sprintf(_existFigure, hit(mid)), mid)
  21. if err = res.Scan(&id); err != nil {
  22. if err == sql.ErrNoRows {
  23. err = nil
  24. return
  25. }
  26. errors.Wrapf(err, "row.Scan(%d) error", mid)
  27. }
  28. return
  29. }
  30. // SaveFigure init user figure info
  31. func (d *Dao) SaveFigure(c context.Context, f *model.Figure) (id int64, err error) {
  32. res, err := d.db.Exec(c, fmt.Sprintf(_insertFigure, hit(f.Mid)), f.Mid, f.Score, f.LawfulScore, f.WideScore, f.FriendlyScore, f.BountyScore, f.CreativityScore, f.Ver, f.Ctime, f.Mtime)
  33. if err != nil {
  34. errors.Wrapf(err, "init user(%d) Figure info error(%v)", f.Mid, err)
  35. return
  36. }
  37. if id, err = res.LastInsertId(); err != nil {
  38. log.Error("res.LastInsertId() error(%v)", err)
  39. }
  40. return
  41. }