archive.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package dao
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "go-common/app/service/bbq/video/model"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/ecode"
  9. "go-common/library/queue/databus"
  10. "github.com/json-iterator/go"
  11. )
  12. const (
  13. _queryCmsVideoRepository = "select id from `video_repository` where `cid` = ?;"
  14. _insertCmsVideoRepository = "insert into `video_repository`(`avid`, `cid`, `mid`, `title`, `from`, `content`, `pubtime`, `duration`, `state`, `tid`, `sub_tid`, `svid`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"
  15. )
  16. // ArchiveSub .
  17. func (d *Dao) ArchiveSub() (*model.Archive, error) {
  18. if d.archiveSub == nil {
  19. return nil, ecode.ArchiveDatabusNilErr
  20. }
  21. msg, ok := <-d.archiveSub.Messages()
  22. if !ok {
  23. return nil, errors.New("chan <- failed")
  24. }
  25. return d.archiveProcess(msg)
  26. }
  27. func (d *Dao) archiveProcess(msg *databus.Message) (a *model.Archive, err error) {
  28. defer msg.Commit()
  29. an := new(model.ArchiveNotify)
  30. if err = jsoniter.Unmarshal(msg.Value, an); err != nil {
  31. return
  32. }
  33. if an.Action == "update" && an.New.Videos <= an.Old.Videos {
  34. return
  35. }
  36. if an.Table != "archive" || an.New == nil {
  37. return
  38. }
  39. if d.archiveFilters.DoFilter(an.New) {
  40. return
  41. }
  42. a = an.New
  43. return
  44. }
  45. // ArchiveKickOff .
  46. func (d *Dao) ArchiveKickOff(c context.Context, svid int64, a *model.Archive) (err error) {
  47. row := d.cmsdb.QueryRow(c, _queryCmsVideoRepository, a.CID)
  48. tmp := 0
  49. if err = row.Scan(&tmp); err != nil && err != xsql.ErrNoRows {
  50. return
  51. }
  52. if tmp != 0 {
  53. err = fmt.Errorf("ArchiveKickOff cid existed [%d]", a.CID)
  54. return
  55. }
  56. _, err = d.cmsdb.Exec(c, _insertCmsVideoRepository, a.AID, a.CID, a.MID, a.Title, 0, a.Content, a.PubTime, a.Duration, a.State, a.TID, a.SubTID, svid)
  57. return
  58. }