mysql.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package dao
  2. import (
  3. "context"
  4. plmdl "go-common/app/interface/main/playlist/model"
  5. "go-common/app/job/main/playlist/model"
  6. )
  7. const (
  8. _statSQL = "SELECT id,mid,fid,view,reply,fav,share,mtime FROM `playlist_stat` WHERE id = ?"
  9. _upViewSQL = "UPDATE playlist_stat SET view= ?, mtime=mtime WHERE id = ?"
  10. _upFavSQL = "UPDATE playlist_stat SET fav= ?, mtime=mtime WHERE id = ?"
  11. _upReplySQL = "UPDATE playlist_stat SET reply= ?, mtime=mtime WHERE id = ?"
  12. _upShareSQL = "UPDATE playlist_stat SET share= ?, mtime=mtime WHERE id = ?"
  13. )
  14. // Update updates stat in db.
  15. func (d *Dao) Update(c context.Context, stat *model.StatM, tp string) (rows int64, err error) {
  16. var tmpSQL string
  17. switch tp {
  18. case model.ViewCountType:
  19. tmpSQL = _upViewSQL
  20. case model.FavCountType:
  21. tmpSQL = _upFavSQL
  22. case model.ReplyCountType:
  23. tmpSQL = _upReplySQL
  24. case model.ShareCountType:
  25. tmpSQL = _upShareSQL
  26. }
  27. res, err := d.db.Exec(c, tmpSQL, *stat.Count, stat.ID)
  28. if err != nil {
  29. PromError("db:更新计数", tp+" Update(%d,%+v) error(%v)", stat.ID, stat, err)
  30. return
  31. }
  32. rows, err = res.RowsAffected()
  33. return
  34. }
  35. // Stat returns stat info.
  36. func (d *Dao) Stat(c context.Context, pid int64) (stat *plmdl.PlStat, err error) {
  37. row := d.db.QueryRow(c, _statSQL, pid)
  38. stat = &plmdl.PlStat{}
  39. if err = row.Scan(&stat.ID, &stat.Mid, &stat.Fid, &stat.View, &stat.Reply, &stat.Fav, &stat.Share, &stat.MTime); err != nil {
  40. PromError("db:读取计数", "Stat(%v) error(%v)", pid, err)
  41. }
  42. return
  43. }