delay.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package archive
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/videoup/model/archive"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. )
  11. const (
  12. _delaysSQL = "SELECT id,aid,dtime,type,state FROM archive_delay WHERE aid=? AND deleted_at = 0 ORDER BY dtime DESC LIMIT 1"
  13. _getNowDelaysSQL = "SELECT id,aid,dtime,type,state FROM archive_delay WHERE dtime<=? AND deleted_at = 0"
  14. _delAdminDelaySQL = "UPDATE archive_delay SET deleted_at = ? WHERE aid=? AND type=1"
  15. _delDelayByIdsSQL = "UPDATE archive_delay SET deleted_at = ? WHERE id IN(%s)"
  16. )
  17. // Delay get delay by aid
  18. func (d *Dao) Delay(c context.Context, aid int64) (delay *archive.Delay, err error) {
  19. rows := d.db.QueryRow(c, _delaysSQL, aid)
  20. delay = &archive.Delay{}
  21. if err = rows.Scan(&delay.ID, &delay.Aid, &delay.DTime, &delay.Type, &delay.State); err != nil {
  22. if err == sql.ErrNoRows {
  23. delay = nil
  24. err = nil
  25. } else {
  26. log.Error("row.Scan error(%v)", err)
  27. }
  28. }
  29. return
  30. }
  31. // NowDelays get current minute dalay archive
  32. func (d *Dao) NowDelays(c context.Context, dtime time.Time) (delays []*archive.Delay, err error) {
  33. rows, err := d.db.Query(c, _getNowDelaysSQL, dtime)
  34. if err != nil {
  35. log.Error("d.db.Query(%s) error(%v)", dtime, err)
  36. return
  37. }
  38. defer rows.Close()
  39. for rows.Next() {
  40. v := &archive.Delay{}
  41. if err = rows.Scan(&v.ID, &v.Aid, &v.DTime, &v.Type, &v.State); err != nil {
  42. log.Error("rows.Scan error(%v)", err)
  43. return
  44. }
  45. delays = append(delays, v)
  46. }
  47. return
  48. }
  49. // DelAdminDelay delete admin delay by aid
  50. func (d *Dao) DelAdminDelay(c context.Context, aid int64) (rows int64, err error) {
  51. res, err := d.db.Exec(c, _delAdminDelaySQL, time.Now(), aid)
  52. if err != nil {
  53. log.Error("d.db.Exec(%d) error(%v)", aid, err)
  54. return
  55. }
  56. rows, err = res.RowsAffected()
  57. return
  58. }
  59. // DelDelayByIds delete delays by ids
  60. func (d *Dao) DelDelayByIds(c context.Context, ids []int64) (rows int64, err error) {
  61. if len(ids) == 0 {
  62. return
  63. }
  64. res, err := d.db.Exec(c, fmt.Sprintf(_delDelayByIdsSQL, xstr.JoinInts(ids)), time.Now())
  65. if err != nil {
  66. log.Error("d.DelDelayByIds() error(%v)", err)
  67. return
  68. }
  69. rows, err = res.RowsAffected()
  70. return
  71. }