vdoad.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package resource
  2. import (
  3. "context"
  4. "database/sql"
  5. "time"
  6. "go-common/app/interface/main/web-show/model/resource"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. xtime "go-common/library/time"
  10. )
  11. const (
  12. _selAdVdoActSQL = `SELECT id,name,aid,ad_cid,ad_url,skipable,ad_strategy,mtime FROM video_ads WHERE platform=0 and type=0 and state=0 AND verified=1 AND starttime<? AND endtime>?`
  13. _selAdMtCntVdoSQL = `SELECT FROM_UNIXTIME(ROUND(AVG(UNIX_TIMESTAMP(mtime)))) FROM video_ads WHERE platform=0 and type=0 and state=0 AND verified=1 AND starttime<? AND endtime>?`
  14. )
  15. func (dao *Dao) initAd() {
  16. dao.selAdVdoActStmt = dao.videodb.Prepared(_selAdVdoActSQL)
  17. dao.selAdMtCntVdoStmt = dao.videodb.Prepared(_selAdMtCntVdoSQL)
  18. }
  19. // AllVideoActive dao
  20. func (dao *Dao) AllVideoActive(c context.Context, now time.Time) (ads []resource.VideoAD, err error) {
  21. var rows *xsql.Rows
  22. if rows, err = dao.selAdVdoActStmt.Query(c, now, now); err != nil {
  23. log.Error("dao..Exec(%v, %v), err (%v)", now, now, err)
  24. return
  25. }
  26. defer rows.Close()
  27. ads = make([]resource.VideoAD, 0, 100)
  28. for rows.Next() {
  29. ad := resource.VideoAD{}
  30. if err = rows.Scan(&ad.ID, &ad.Name, &ad.AidS, &ad.Cid, &ad.URL, &ad.Skipable, &ad.Strategy, &ad.MTime); err != nil {
  31. log.Error("rows.Scan(), err (%v)", err)
  32. ads = nil
  33. return
  34. }
  35. ads = append(ads, ad)
  36. }
  37. return
  38. }
  39. // AdVideoMTimeCount dao
  40. func (dao *Dao) AdVideoMTimeCount(c context.Context, now time.Time) (mtime xtime.Time, err error) {
  41. row := dao.selAdMtCntVdoStmt.QueryRow(c, now, now)
  42. if err = row.Scan(&mtime); err != nil {
  43. if err == sql.ErrNoRows {
  44. err = nil
  45. } else {
  46. log.Error("mysqlDB error(%v)", err)
  47. }
  48. }
  49. return
  50. }