vdoactive.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package resource
  2. import (
  3. "context"
  4. "database/sql"
  5. "go-common/app/interface/main/web-show/model/resource"
  6. xsql "go-common/library/database/sql"
  7. "go-common/library/log"
  8. xtime "go-common/library/time"
  9. )
  10. const (
  11. _inVdoActSQL = "INSERT IGNORE INTO video_ads_active (name,aid,cid,url,skipable,strategy,mtime) VALUES(?,?,?,?,?,?,?)"
  12. _selAllVdoActSQL = "SELECT name,aid,cid,url,skipable,strategy FROM video_ads_active ORDER BY id"
  13. _selVdoMTCntActSQL = `SELECT FROM_UNIXTIME(ROUND(AVG(UNIX_TIMESTAMP(mtime)))) FROM video_ads_active`
  14. _delAllVdoActSQL = "DELETE FROM video_ads_active"
  15. )
  16. // initActive init
  17. func (dao *Dao) initActive() {
  18. dao.selAllVdoActStmt = dao.videodb.Prepared(_selAllVdoActSQL)
  19. dao.selVdoActMTCntStmt = dao.videodb.Prepared(_selVdoMTCntActSQL)
  20. dao.delAllVdoActStmt = dao.videodb.Prepared(_delAllVdoActSQL)
  21. }
  22. // TxInsertVideo dao
  23. func (dao *Dao) TxInsertVideo(tx *xsql.Tx, vad resource.VideoAD) (err error) {
  24. if _, err = tx.Exec(_inVdoActSQL, vad.Name, vad.Aid, vad.Cid, vad.URL, vad.Skipable, vad.Strategy, vad.MTime); err != nil {
  25. log.Error("tx.Stmt(dao.inStmt).Exec(), err (%v)", err)
  26. }
  27. return
  28. }
  29. //VideoAds dao
  30. func (dao *Dao) VideoAds(c context.Context) (vads map[int64][]*resource.VideoAD, err error) {
  31. rows, err := dao.selAllVdoActStmt.Query(c)
  32. if err != nil {
  33. log.Error("dao.selAllVdoStmt query error (%v)", err)
  34. return
  35. }
  36. defer rows.Close()
  37. vads = map[int64][]*resource.VideoAD{}
  38. for rows.Next() {
  39. vad := &resource.VideoAD{}
  40. if err = rows.Scan(&vad.Name, &vad.Aid, &vad.Cid, &vad.URL, &vad.Skipable, &vad.Strategy); err != nil {
  41. log.Error("rows.Scan err (%v)", err)
  42. vads = nil
  43. return
  44. }
  45. vads[vad.Aid] = append(vads[vad.Aid], vad)
  46. }
  47. return
  48. }
  49. // ActVideoMTimeCount dao
  50. func (dao *Dao) ActVideoMTimeCount(c context.Context) (mtime xtime.Time, err error) {
  51. row := dao.selVdoActMTCntStmt.QueryRow(c)
  52. if err = row.Scan(&mtime); err != nil {
  53. if err == sql.ErrNoRows {
  54. err = nil
  55. } else {
  56. log.Error("row.Scan(), err (%v)", err)
  57. }
  58. }
  59. return
  60. }
  61. // DelAllVideo dao
  62. func (dao *Dao) DelAllVideo(c context.Context) (err error) {
  63. if _, err = dao.delAllVdoActStmt.Exec(c); err != nil {
  64. log.Error("mysqlDB.Exec error(%v)", err)
  65. }
  66. return
  67. }