recheck.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package archive
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/videoup/model/archive"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _recheckByAid = "SELECT id,type,aid,uid,state,ctime,mtime FROM archive_recheck WHERE aid =? and type = ?"
  12. _recheckBatchIDByAid = "SELECT id,aid FROM archive_recheck WHERE aid IN (%s) AND type=?"
  13. _recheckBatchStateByAid = "SELECT aid,state FROM archive_recheck WHERE aid IN (%s) AND type=?"
  14. _upRecheckState = "UPDATE archive_recheck SET state=? WHERE aid =? and type = ?"
  15. )
  16. // TxUpRecheckState update recheck state
  17. func (d *Dao) TxUpRecheckState(tx *sql.Tx, tp int, aid int64, state int8) (err error) {
  18. if _, err = tx.Exec(_upRecheckState, state, aid, tp); err != nil {
  19. log.Error("TxUpRecheckState Exec(%d,%d,%d) error(%v)", state, tp, aid, err)
  20. return
  21. }
  22. return
  23. }
  24. // RecheckByAid find archive recheck
  25. func (d *Dao) RecheckByAid(c context.Context, tp int, aid int64) (recheck *archive.Recheck, err error) {
  26. row := d.db.QueryRow(c, _recheckByAid, aid, tp)
  27. recheck = &archive.Recheck{}
  28. if err = row.Scan(&recheck.ID, &recheck.Type, &recheck.AID, &recheck.UID, &recheck.State, &recheck.CTime, &recheck.MTime); err != nil {
  29. if err == sql.ErrNoRows {
  30. err = nil
  31. recheck = nil
  32. } else {
  33. log.Error("RecheckByAid row.Scan(%d,%d) error(%v)", tp, aid, err)
  34. }
  35. return
  36. }
  37. return
  38. }
  39. //RecheckIDByAID find states by ids
  40. func (d *Dao) RecheckIDByAID(c context.Context, tp int, aids []int64) (ids []int64, existAID []int64, err error) {
  41. var (
  42. rows *sql.Rows
  43. id, aid int64
  44. )
  45. aidstr := xstr.JoinInts(aids)
  46. ids = []int64{}
  47. existAID = []int64{}
  48. if rows, err = d.db.Query(c, fmt.Sprintf(_recheckBatchIDByAid, aidstr), tp); err != nil {
  49. log.Error("RecheckIDByAID d.db.Query error(%v) type(%d) aids(%s)", err, tp, aidstr)
  50. return
  51. }
  52. defer rows.Close()
  53. for rows.Next() {
  54. if err = rows.Scan(&id, &aid); err != nil {
  55. log.Error("RecheckIDByAID rows.Scan error(%v) type(%d) aids(%s)", err, tp, aidstr)
  56. return
  57. }
  58. ids = append(ids, id)
  59. existAID = append(existAID, aid)
  60. }
  61. return
  62. }
  63. func (d *Dao) RecheckStateMap(c context.Context, tp int, aids []int64) (m map[int64]int8, err error) {
  64. var (
  65. rows *sql.Rows
  66. aid int64
  67. state int8
  68. )
  69. m = make(map[int64]int8)
  70. if len(aids) == 0 {
  71. return
  72. }
  73. str := xstr.JoinInts(aids)
  74. if rows, err = d.db.Query(c, fmt.Sprintf(_recheckBatchStateByAid, str), tp); err != nil {
  75. log.Error("RecheckStateMap d.db.Query error(%v) type(%d) aids(%s)", err, tp, str)
  76. return
  77. }
  78. defer rows.Close()
  79. for rows.Next() {
  80. if err = rows.Scan(&aid, &state); err != nil {
  81. log.Error("RecheckStateMap rows.Scan error(%v) type(%d) aids(%s)", err, tp, str)
  82. return
  83. }
  84. m[aid] = state
  85. }
  86. return
  87. }