mysql.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "go-common/app/service/main/archive/api"
  9. "go-common/app/service/main/archive/model/archive"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _sharding = 100
  14. _statSQL = "SELECT aid,click,fav,share,reply,coin,dm,now_rank,his_rank,likes,dislike FROM archive_stat_%s WHERE aid=%d"
  15. _upStatSQL = `INSERT INTO archive_stat_%s (aid,click,fav,share,reply,coin,dm,now_rank,his_rank,ctime,mtime,likes,dislike) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)
  16. ON DUPLICATE KEY UPDATE click=?,fav=?,share=?,reply=?,coin=?,dm=?,now_rank=?,his_rank=?,mtime=?,likes=?,dislike=?`
  17. _upMStatSQL = `REPLACE INTO archive_stat_%02d (aid,click,fav,share,reply,coin,dm,now_rank,his_rank,mtime,likes,dislike) VALUES %s`
  18. _clickSQL = "SELECT aid,web,h5,outside,ios,android FROM archive_click_%02d WHERE aid=?"
  19. )
  20. func (d *Dao) hit(id int64) string {
  21. return fmt.Sprintf("%02d", id%_sharding)
  22. }
  23. func statTbl(aid int64) int64 {
  24. return aid % _sharding
  25. }
  26. // Stat returns stat info
  27. func (d *Dao) Stat(c context.Context, aid int64) (stat *api.Stat, err error) {
  28. stat = &api.Stat{}
  29. err = d.db.QueryRow(c, fmt.Sprintf(_statSQL, d.hit(aid), aid)).Scan(&stat.Aid, &stat.View, &stat.Fav, &stat.Share, &stat.Reply, &stat.Coin, &stat.Danmaku, &stat.NowRank, &stat.HisRank, &stat.Like, &stat.DisLike)
  30. if err == sql.ErrNoRows {
  31. err = nil
  32. stat = nil
  33. } else if err != nil {
  34. log.Error("Stat(%v) error(%v)", aid, err)
  35. }
  36. return
  37. }
  38. // Update update stat's fields
  39. func (d *Dao) Update(c context.Context, stat *api.Stat) (rows int64, err error) {
  40. now := time.Now()
  41. res, err := d.db.Exec(c, fmt.Sprintf(_upStatSQL, d.hit(stat.Aid)), stat.Aid, stat.View, stat.Fav, stat.Share, stat.Reply, stat.Coin, stat.Danmaku, stat.NowRank, stat.HisRank, now, now, stat.Like, stat.DisLike,
  42. stat.View, stat.Fav, stat.Share, stat.Reply, stat.Coin, stat.Danmaku, stat.NowRank, stat.HisRank, now, stat.Like, stat.DisLike)
  43. if err != nil {
  44. log.Error("UpdateStat(%d,%v) error(%v)", stat.Aid, stat, err)
  45. return
  46. }
  47. rows, err = res.RowsAffected()
  48. return
  49. }
  50. // MultiUpdate update some stat's fields
  51. func (d *Dao) MultiUpdate(c context.Context, yu int64, stats ...*api.Stat) (rows int64, err error) {
  52. if len(stats) == 0 {
  53. return
  54. }
  55. const field = `(%d,%d,%d,%d,%d,%d,%d,%d,%d,'%s',%d,%d)`
  56. var (
  57. fsqls = make([]string, 0, len(stats))
  58. now = time.Now().Format("2006-01-02 15:04:05")
  59. )
  60. for _, stat := range stats {
  61. fsqls = append(fsqls, fmt.Sprintf(field, stat.Aid, stat.View, stat.Fav, stat.Share, stat.Reply, stat.Coin, stat.Danmaku, stat.NowRank, stat.HisRank, now, stat.Like, stat.DisLike))
  62. }
  63. res, err := d.db.Exec(c, fmt.Sprintf(_upMStatSQL, yu, strings.Join(fsqls, ",")))
  64. if err != nil {
  65. log.Error("upMstat error(%v)", err)
  66. return
  67. }
  68. rows, err = res.RowsAffected()
  69. return
  70. }
  71. // Click archive click.
  72. func (d *Dao) Click(c context.Context, aid int64) (cl *archive.Click3, err error) {
  73. row := d.clickDB.QueryRow(c, fmt.Sprintf(_clickSQL, statTbl(aid)), aid)
  74. cl = &archive.Click3{}
  75. if err = row.Scan(&cl.Aid, &cl.Web, &cl.H5, &cl.Outter, &cl.Ios, &cl.Android); err != nil {
  76. if err == sql.ErrNoRows {
  77. cl = nil
  78. err = nil
  79. } else {
  80. log.Error("row.Scan error(%v)", err)
  81. }
  82. }
  83. return
  84. }