sv_stat.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "go-common/app/interface/bbq/app-bbq/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _insertVideoStatistics = "insert into video_statistics (`svid`,`play`,`subtitles`,`like`,`share`,`report`) values (?,?,?,?,?,?)"
  11. _incrVideoStatisticsLike = "update video_statistics set `like` = `like` + 1 where `svid` = ?"
  12. _decrVideoStatisticsLike = "update video_statistics set `like` = `like` - 1 where `svid` = ? and `like` > 0"
  13. _insertVideoStatisticsShare = "INSERT INTO video_statistics (`svid`,`share`) VALUES (?,1) ON DUPLICATE KEY UPDATE `share` = `share` + 1;"
  14. _incrVideoStatisticsShare = "UPDATE `video_statistics` SET `share` = `share` + 1 WHERE `svid` = ?;"
  15. _selectVideoStatisticsLike = "select `svid`,`play`,`subtitles`,`like`,`share`,`report` from video_statistics where `svid` = ?"
  16. _videoStatisticsShare = "select `share`+1 from video_statistics where `svid`= ?"
  17. )
  18. //TxIncrVideoStatisticsLike .
  19. func (d *Dao) TxIncrVideoStatisticsLike(tx *sql.Tx, svid int64) (num int64, err error) {
  20. var res xsql.Result
  21. if res, err = tx.Exec(_incrVideoStatisticsLike, svid); err != nil {
  22. log.Error("update video_statistics like err(%v)", err)
  23. return
  24. }
  25. return res.RowsAffected()
  26. }
  27. //TxDecrVideoStatisticsLike .
  28. func (d *Dao) TxDecrVideoStatisticsLike(tx *sql.Tx, svid int64) (num int64, err error) {
  29. var res xsql.Result
  30. if res, err = tx.Exec(_decrVideoStatisticsLike, svid); err != nil {
  31. log.Error("update video_statistics like err(%v)", err)
  32. return
  33. }
  34. return res.RowsAffected()
  35. }
  36. //IncrVideoStatisticsShare .
  37. func (d *Dao) IncrVideoStatisticsShare(ctx context.Context, svid int64) (num int32, err error) {
  38. row := d.db.QueryRow(ctx, _videoStatisticsShare, svid)
  39. err = row.Scan(&num)
  40. if err != nil || num == 0 {
  41. if _, err = d.db.Exec(ctx, _insertVideoStatisticsShare, svid); err != nil {
  42. log.Error("insert video_statistics share error: [%+v] svid: [%d]", err, svid)
  43. }
  44. num = 1
  45. } else if _, err = d.db.Exec(ctx, _incrVideoStatisticsShare, svid); err != nil {
  46. log.Error("update video_statistics share error: [%+v] svid: [%d]", err, svid)
  47. }
  48. return
  49. }
  50. //TxAddVideoStatistics .
  51. func (d *Dao) TxAddVideoStatistics(tx *sql.Tx, videoStatistics *model.VideoStatistics) (num int64, err error) {
  52. var res xsql.Result
  53. if res, err = tx.Exec(_insertVideoStatistics, videoStatistics.SVID, videoStatistics.Play, videoStatistics.Subtitles, videoStatistics.Like, videoStatistics.Share, videoStatistics.Report); err != nil {
  54. log.Error("insert video_statistics err(%v)", err)
  55. return
  56. }
  57. return res.LastInsertId()
  58. }
  59. //RawVideoStatistics .
  60. func (d *Dao) RawVideoStatistics(c context.Context, svid int64) (res *model.VideoStatistics, err error) {
  61. res = new(model.VideoStatistics)
  62. row := d.db.QueryRow(c, _selectVideoStatisticsLike, svid)
  63. err = row.Scan(&res.SVID, &res.Play, &res.Subtitles, &res.Like, &res.Share, &res.Report)
  64. if err == sql.ErrNoRows {
  65. err = nil
  66. }
  67. return
  68. }