delay.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package archive
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/service/main/videoup/model/archive"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. xtime "go-common/library/time"
  9. )
  10. const (
  11. //insert
  12. _inDelaySQL = "INSERT INTO archive_delay (mid,aid,state,type,dtime) VALUES (?,?,?,?,?)"
  13. //update
  14. _upDelaySQL = "INSERT INTO archive_delay (mid,aid,state,type,dtime,ctime) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE dtime=?,deleted_at='0000-00-00 00:00:00'"
  15. //delete
  16. _delDelaySQL = "UPDATE archive_delay SET deleted_at = ? WHERE aid=? AND type=?"
  17. //select
  18. _dTimeSQL = "SELECT aid,dtime,state FROM archive_delay WHERE aid=? AND type=? AND deleted_at = 0"
  19. )
  20. // TxAddDelay insert delay.
  21. func (d *Dao) TxAddDelay(tx *sql.Tx, mid int64, aid int64, state, tp int8, dTime xtime.Time) (rows int64, err error) {
  22. res, err := tx.Exec(_inDelaySQL, mid, aid, state, tp, dTime)
  23. if err != nil {
  24. log.Error("d.inDelay.Exec() error(%v)", err)
  25. return
  26. }
  27. rows, err = res.RowsAffected()
  28. return
  29. }
  30. // TxUpDelay update delay
  31. func (d *Dao) TxUpDelay(tx *sql.Tx, mid, aid int64, state, tp int8, dTime xtime.Time) (rows int64, err error) {
  32. var now = time.Now()
  33. res, err := tx.Exec(_upDelaySQL, mid, aid, state, tp, dTime, now, dTime)
  34. if err != nil {
  35. log.Error("d.TxUpDelay.Exec() error(%v)", err)
  36. return
  37. }
  38. rows, err = res.RowsAffected()
  39. return
  40. }
  41. // TxDelDelay delete delay
  42. func (d *Dao) TxDelDelay(tx *sql.Tx, aid int64, tp int8) (rows int64, err error) {
  43. res, err := tx.Exec(_delDelaySQL, time.Now(), aid, tp)
  44. if err != nil {
  45. log.Error("d.TxDelDelay.Exec() error(%v)", err)
  46. return
  47. }
  48. rows, err = res.RowsAffected()
  49. return
  50. }
  51. // Delay get a delay time by avid.
  52. func (d *Dao) Delay(c context.Context, aid int64, tp int8) (dl *archive.Delay, err error) {
  53. row := d.rddb.QueryRow(c, _dTimeSQL, aid, tp)
  54. dl = &archive.Delay{}
  55. if err = row.Scan(&dl.Aid, &dl.DTime, &dl.State); err != nil {
  56. if err == sql.ErrNoRows {
  57. dl = nil
  58. err = nil
  59. } else {
  60. log.Error("row.Scan error(%v)", err)
  61. }
  62. }
  63. return
  64. }