refresh.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package resource
  2. import (
  3. "context"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "go-common/library/log"
  8. )
  9. // checkDiff check diff between ads and ads_active
  10. func (s *Service) checkDiff() {
  11. now := time.Now()
  12. s.activeVideos(context.Background(), now)
  13. }
  14. // activeVideos check if VideoAds active need to update
  15. func (s *Service) activeVideos(c context.Context, now time.Time) {
  16. amtime, err := s.resdao.ActVideoMTimeCount(c)
  17. if err != nil {
  18. log.Error("resdao.ActVideoMTimeCount error(%v)", err)
  19. return
  20. }
  21. dmtime, err := s.resdao.AdVideoMTimeCount(c, now)
  22. if err != nil {
  23. log.Error("resdao.AdVideoMTimeCount error(%v)", err)
  24. return
  25. }
  26. if amtime == dmtime {
  27. log.Info("all video active ad are same")
  28. return
  29. }
  30. log.Info("video active avg mtime(%d), ads avg mtime(%d)", amtime, dmtime)
  31. if err = s.resdao.DelAllVideo(c); err != nil {
  32. log.Error("sdDao.DelAllVideo(), err (%v)", err)
  33. return
  34. }
  35. ads, err := s.resdao.AllVideoActive(c, now)
  36. if err != nil {
  37. log.Error("resdao.AllVideoActive(%v), err (%v)", now, err)
  38. return
  39. }
  40. tx, err := s.resdao.BeginTran(c)
  41. if err != nil {
  42. log.Error("BeginTran(), err (%v)", err)
  43. return
  44. }
  45. for _, ad := range ads {
  46. aids := strings.Split(ad.AidS, ",")
  47. for _, aid := range aids {
  48. i, e := strconv.ParseInt(aid, 10, 64)
  49. if e != nil {
  50. log.Error("strconv.ParseInt() error(%v)", e)
  51. continue
  52. }
  53. ad.Aid = i
  54. ad.MTime = dmtime
  55. if err = s.resdao.TxInsertVideo(tx, ad); err != nil {
  56. if err = tx.Rollback(); err != nil {
  57. log.Error("tx.Rollback(), err (%v)", err)
  58. }
  59. log.Error("resdao.TxInsertVideo(tx, %v), err(%v)", ad, err)
  60. return
  61. }
  62. }
  63. }
  64. if err = tx.Commit(); err != nil {
  65. log.Error("tx.Commit(), error(%v)", err)
  66. }
  67. }