delete.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package ugc
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/job/main/tv/model/ugc"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _delArc = "UPDATE ugc_archive SET deleted = 1, submit = 1 WHERE aid = ? AND deleted = 0"
  12. _delVideos = "UPDATE ugc_video SET deleted = 1, submit = 1 WHERE aid = ? AND deleted = 0"
  13. _delVideo = "UPDATE ugc_video SET deleted = 1, submit = 1 WHERE cid = ? AND deleted = 0"
  14. _delVideoCids = "UPDATE ugc_video SET deleted = 1, submit = 1 WHERE cid IN (%s) AND deleted = 0"
  15. _checkVideos = "SELECT cid FROM ugc_video WHERE aid = ? AND deleted = 0 LIMIT 1"
  16. )
  17. // TxDelArc deletes an arc
  18. func (d *Dao) TxDelArc(tx *sql.Tx, aid int64) (err error) {
  19. if _, err = tx.Exec(_delArc, aid); err != nil {
  20. log.Error("TxDelArc, failed to update: (%v), Error: %v", aid, err)
  21. }
  22. return
  23. }
  24. // DelVideos delete one archive all videos
  25. func (d *Dao) DelVideos(ctx context.Context, aid int64) (err error) {
  26. if _, err = d.DB.Exec(ctx, _delVideos, aid); err != nil {
  27. log.Error("DelVideos, failed to update: (%v), Error: %v", aid, err)
  28. return
  29. }
  30. log.Info("Aid %d is deleted, delete its videos", aid)
  31. return
  32. }
  33. // TxDelVideos deletes the videos of an arc
  34. func (d *Dao) TxDelVideos(tx *sql.Tx, aid int64) (err error) {
  35. if _, err = tx.Exec(_delVideos, aid); err != nil {
  36. log.Error("TxDelVideos, failed to update: (%v), Error: %v", aid, err)
  37. }
  38. return
  39. }
  40. // TxDelVideo deletes a video
  41. func (d *Dao) TxDelVideo(tx *sql.Tx, cid int64) (err error) {
  42. if _, err = tx.Exec(_delVideo, cid); err != nil {
  43. log.Error("TxDelVideo, failed to update: (%v), Error: %v", cid, err)
  44. }
  45. return
  46. }
  47. // DelVideoArc deletes some videos of an archive, if the archive is empty, also delete it
  48. func (d *Dao) DelVideoArc(ctx context.Context, req *ugc.DelVideos) (arcValid bool, err error) {
  49. var cid int64
  50. arcValid = true
  51. if _, err = d.DB.Exec(ctx, fmt.Sprintf(_delVideoCids, xstr.JoinInts(req.CIDs))); err != nil {
  52. log.Error("DelVideos Cids %v, Aid %d, Error: %v", req.CIDs, req.AID, err)
  53. return
  54. }
  55. if err = d.DB.QueryRow(ctx, _checkVideos, req.AID).Scan(&cid); err != nil { // if no active videos, delete the arc
  56. if err == sql.ErrNoRows {
  57. err = nil
  58. arcValid = false
  59. if _, err = d.DB.Exec(ctx, _delArc, req.AID); err != nil {
  60. log.Error("DelVideos DelArc Cids %v, Aid %d, Error: %v", req.CIDs, req.AID, err)
  61. return
  62. }
  63. log.Info("DelArc Aid %d Because No Active Video", req.AID)
  64. } else {
  65. log.Error("DelVideos Cids %v, Aid %d, Error: %v", req.CIDs, req.AID, err)
  66. return
  67. }
  68. }
  69. return
  70. }