archive_recheck.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package archive
  2. import (
  3. "context"
  4. sql2 "database/sql"
  5. "go-common/app/job/main/videoup-report/model/archive"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _recheckByAid = "SELECT id,type,aid,uid,state,ctime,mtime FROM archive_recheck WHERE aid =? and type = ?"
  11. _inRecheck = "INSERT INTO archive_recheck (type,aid) VALUES (?,?)"
  12. _upRecheckState = "UPDATE archive_recheck SET state=? WHERE aid =? and type = ?"
  13. _upMidRecheckState = "UPDATE archive_recheck as r LEFT JOIN archive AS a ON a.id = r.aid SET r.state=? WHERE a.mid =? and r.type = ? AND r.state = ?"
  14. )
  15. // AddRecheckAids add recheck aids
  16. func (d *Dao) AddRecheckAids(c context.Context, tp int, aids []int64, ignRechecked bool) (err error) {
  17. for _, aid := range aids {
  18. recheck, _ := d.RecheckByAid(c, tp, aid)
  19. if recheck != nil {
  20. if ignRechecked && recheck.State != archive.RecheckStateIgnore {
  21. continue
  22. }
  23. if recheck.State == archive.RecheckStateWait {
  24. log.Info("d.AddRecheckAids(%d) already in recheck", aid)
  25. continue
  26. }
  27. if err = d.UpdateRecheckState(c, tp, aid, archive.RecheckStateWait); err != nil {
  28. log.Error("d.UpdateRecheckState error(%v)", err)
  29. continue
  30. }
  31. } else if _, err = d.db.Exec(c, _inRecheck, tp, aid); err != nil {
  32. log.Error("d.AddRecheckAids.Exec error(%v)", err)
  33. continue
  34. }
  35. a, err := d.ArchiveByAid(c, aid)
  36. if err != nil {
  37. log.Error("d.ArchiveByAid error(%v)", err)
  38. err = nil
  39. continue
  40. }
  41. tpStr := archive.RecheckType(tp)
  42. if tpStr != "" {
  43. d.AddArchiveOper(c, aid, a.Attribute, a.TypeID, a.State, a.Round, 0, "", "待"+tpStr)
  44. }
  45. }
  46. return
  47. }
  48. // UpdateMidRecheckState 设置某个UP主的未回查稿件回查状态
  49. func (d *Dao) UpdateMidRecheckState(c context.Context, tp int, mid int64, state int8) (err error) {
  50. if _, err = d.db.Exec(c, _upMidRecheckState, state, mid, tp, archive.RecheckStateWait); err != nil {
  51. log.Error("d.updateRecheckState.Exec error(%v)", err)
  52. return
  53. }
  54. return
  55. }
  56. // TxAddRecheckAID add recheck aid to db
  57. func (d *Dao) TxAddRecheckAID(tx *sql.Tx, tp int, aid int64) (id int64, err error) {
  58. var (
  59. res sql2.Result
  60. )
  61. if res, err = tx.Exec(_inRecheck, tp, aid); err != nil {
  62. log.Error("TxAddRecheckAID error(%v) type(%d) aid(%d)", err, tp, aid)
  63. return
  64. }
  65. id, err = res.LastInsertId()
  66. return
  67. }
  68. // UpdateRecheckState update recheck state
  69. func (d *Dao) UpdateRecheckState(c context.Context, tp int, aid int64, state int8) (err error) {
  70. if _, err = d.db.Exec(c, _upRecheckState, state, aid, tp); err != nil {
  71. log.Error("d.updateRecheckState.Exec error(%v)", err)
  72. return
  73. }
  74. return
  75. }
  76. //TxUpRecheckState update recheck state
  77. func (d *Dao) TxUpRecheckState(tx *sql.Tx, tp int, aid int64, state int8) (row int64, err error) {
  78. var res sql2.Result
  79. if res, err = tx.Exec(_upRecheckState, state, aid, tp); err != nil {
  80. log.Error("d.TxUpRecheckState.Exec error(%v)", err)
  81. return
  82. }
  83. row, err = res.RowsAffected()
  84. return
  85. }
  86. // RecheckByAid find archive recheck
  87. func (d *Dao) RecheckByAid(c context.Context, tp int, aid int64) (recheck *archive.Recheck, err error) {
  88. row := d.db.QueryRow(c, _recheckByAid, aid, tp)
  89. recheck = &archive.Recheck{}
  90. if err = row.Scan(&recheck.ID, &recheck.Type, &recheck.Aid, &recheck.UID, &recheck.State, &recheck.CTime, &recheck.MTime); err != nil {
  91. if err == sql.ErrNoRows {
  92. err = nil
  93. } else {
  94. log.Error("row.Scan error(%v)", err)
  95. }
  96. recheck = nil
  97. return
  98. }
  99. return
  100. }