archive.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package archive
  2. import (
  3. "context"
  4. "database/sql"
  5. "time"
  6. "go-common/app/job/main/videoup/model/archive"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _upStateSQL = "UPDATE archive SET state=? where id=?"
  12. _upAccessSQL = "UPDATE archive SET access=? where id=?"
  13. _upRoundSQL = "UPDATE archive SET round=? where id=?"
  14. _upAttrSQL = "UPDATE archive SET attribute=attribute|? where id=?"
  15. _upCoverSQL = "UPDATE archive SET cover=? where id=?"
  16. _upDuraSQL = "UPDATE archive SET duration=? where id=?"
  17. _upAttrBitSQL = "UPDATE archive SET attribute=attribute&(~(1<<?))|(?<<?) WHERE id=?"
  18. _upPTimeSQL = "UPDATE archive SET pubtime=? WHERE id=?"
  19. _upDelayRoundSQL = "UPDATE archive SET round = ? WHERE state>=? AND round=? AND mtime >= ? AND mtime <= ?"
  20. // select
  21. _arcSQL = "SELECT id,mid,typeid,copyright,author,title,cover,content,tag,duration,round,attribute,access,state,reject_reason,pubtime,ctime,mtime,forward FROM archive WHERE id=?"
  22. _upperArcStates = "SELECT id,state FROM archive WHERE mid=?"
  23. )
  24. // Archive get a archive by avid.
  25. func (d *Dao) Archive(c context.Context, aid int64) (a *archive.Archive, err error) {
  26. var reason sql.NullString
  27. row := d.db.QueryRow(c, _arcSQL, aid)
  28. a = &archive.Archive{}
  29. if err = row.Scan(&a.Aid, &a.Mid, &a.TypeID, &a.Copyright, &a.Author, &a.Title, &a.Cover, &a.Desc, &a.Tag, &a.Duration,
  30. &a.Round, &a.Attribute, &a.Access, &a.State, &reason, &a.PTime, &a.CTime, &a.MTime, &a.Forward); err != nil {
  31. if err == sql.ErrNoRows {
  32. a = nil
  33. err = nil
  34. } else {
  35. log.Error("row.Scan error(%v)", err)
  36. }
  37. return
  38. }
  39. a.Reason = reason.String
  40. return
  41. }
  42. // UpperArcStateMap 获取UP主的稿件状态
  43. func (d *Dao) UpperArcStateMap(c context.Context, mid int64) (sMap map[int64]int8, err error) {
  44. sMap = make(map[int64]int8)
  45. rows, err := d.rdb.Query(c, _upperArcStates, mid)
  46. if err != nil {
  47. log.Error("d.rdb.Query() error(%v)", err)
  48. return
  49. }
  50. defer rows.Close()
  51. for rows.Next() {
  52. a := struct {
  53. ID int64
  54. State int8
  55. }{}
  56. if err = rows.Scan(&a.ID, &a.State); err != nil {
  57. log.Error("rows.Scan error(%v)", err)
  58. return
  59. }
  60. sMap[a.ID] = a.State
  61. }
  62. return
  63. }
  64. // UpDelayRound update round to the end by conf
  65. func (d *Dao) UpDelayRound(c context.Context, minTime, maxTime time.Time) (rows int64, err error) {
  66. res, err := d.db.Exec(c, _upDelayRoundSQL, archive.RoundEnd, archive.StateOpen, archive.RoundReviewFirstWaitTrigger, minTime, maxTime)
  67. if err != nil {
  68. log.Error("d.db.Exec error(%v)", err)
  69. return
  70. }
  71. rows, err = res.RowsAffected()
  72. return
  73. }
  74. // TxUpState update state of a archive by id.
  75. func (d *Dao) TxUpState(tx *xsql.Tx, aid int64, state int8) (rows int64, err error) {
  76. res, err := tx.Exec(_upStateSQL, state, aid)
  77. if err != nil {
  78. log.Error("tx.Exec(%d, %d) error(%v)", state, aid, err)
  79. return
  80. }
  81. rows, err = res.RowsAffected()
  82. return
  83. }
  84. // TxUpAccess update access of a archive by id.
  85. func (d *Dao) TxUpAccess(tx *xsql.Tx, aid int64, access int16) (rows int64, err error) {
  86. res, err := tx.Exec(_upAccessSQL, access, aid)
  87. if err != nil {
  88. log.Error("tx.Exec(%d, %d) error(%v)", access, aid, err)
  89. return
  90. }
  91. rows, err = res.RowsAffected()
  92. return
  93. }
  94. // TxUpRound update round of a archive by id.
  95. func (d *Dao) TxUpRound(tx *xsql.Tx, aid int64, round int8) (rows int64, err error) {
  96. res, err := tx.Exec(_upRoundSQL, round, aid)
  97. if err != nil {
  98. log.Error("tx.Exec(%d, %d) error(%v)", round, aid, err)
  99. return
  100. }
  101. rows, err = res.RowsAffected()
  102. return
  103. }
  104. // TxUpAttr update attribute value of a archive by id.
  105. func (d *Dao) TxUpAttr(tx *xsql.Tx, aid int64, attr archive.Attr) (rows int64, err error) {
  106. res, err := tx.Exec(_upAttrSQL, attr, aid)
  107. if err != nil {
  108. log.Error("tx.Exec(%d, %d) error(%v)", aid, attr, err)
  109. return
  110. }
  111. rows, err = res.RowsAffected()
  112. return
  113. }
  114. // TxUpCover update cover of a archive by id.
  115. func (d *Dao) TxUpCover(tx *xsql.Tx, aid int64, cover string) (rows int64, err error) {
  116. res, err := tx.Exec(_upCoverSQL, cover, aid)
  117. if err != nil {
  118. log.Error("tx.Exec(%d, %s) error(%v)", aid, cover, err)
  119. return
  120. }
  121. rows, err = res.RowsAffected()
  122. return
  123. }
  124. // UpCover update cover of a archive by id.
  125. func (d *Dao) UpCover(c context.Context, aid int64, cover string) (rows int64, err error) {
  126. res, err := d.db.Exec(c, _upCoverSQL, cover, aid)
  127. if err != nil {
  128. log.Error("tx.Exec(%d, %s) error(%v)", aid, cover, err)
  129. return
  130. }
  131. rows, err = res.RowsAffected()
  132. return
  133. }
  134. // TxUpArcDuration update duration of a archive by id.
  135. func (d *Dao) TxUpArcDuration(tx *xsql.Tx, aid, duration int64) (rows int64, err error) {
  136. res, err := tx.Exec(_upDuraSQL, duration, aid)
  137. if err != nil {
  138. log.Error("tx.Exec(%d, %d) error(%v)", aid, duration, err)
  139. return
  140. }
  141. rows, err = res.RowsAffected()
  142. return
  143. }
  144. // TxUpAttrBit update attribute bit value of a archive by id.
  145. func (d *Dao) TxUpAttrBit(tx *xsql.Tx, aid int64, v int32, bit uint) (rows int64, err error) {
  146. res, err := tx.Exec(_upAttrBitSQL, bit, v, bit, aid)
  147. if err != nil {
  148. log.Error("tx.Exec(%d, %d, %d) error(%v)", aid, v, bit, err)
  149. return
  150. }
  151. rows, err = res.RowsAffected()
  152. return
  153. }
  154. // TxUpPTime update ptime by aid
  155. func (d *Dao) TxUpPTime(tx *xsql.Tx, aid int64, ptime time.Time) (rows int64, err error) {
  156. res, err := tx.Exec(_upPTimeSQL, ptime, aid)
  157. if err != nil {
  158. log.Error("tx.Exec(%s, %v, %v) error(%v)", _upPTimeSQL, ptime, aid, err)
  159. return
  160. }
  161. rows, err = res.RowsAffected()
  162. return
  163. }