archive.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package archive
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/admin/main/videoup/model/archive"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. xtime "go-common/library/time"
  11. "go-common/library/xstr"
  12. )
  13. const (
  14. _upArcSQL = "UPDATE archive SET title=?,content=?,copyright=?,cover=?,note=?,pubtime=?,mtime=? WHERE id=?"
  15. _upArcTpSQL = "UPDATE archive SET typeid=? WHERE id=?"
  16. _upArcRound = "UPDATE archive SET round=? WHERE id=?"
  17. _upArcState = "UPDATE archive SET state=? WHERE id=?"
  18. _upAccessSQL = "UPDATE archive SET access=? WHERE id=?"
  19. _upAuthorSQL = "UPDATE archive SET mid=?,author=? WHERE id=?"
  20. _upPTimeSQL = "UPDATE archive SET pubtime=? WHERE id=?"
  21. _upArcReasonSQL = "UPDATE archive SET reject_reason=?,forward=? WHERE id=?"
  22. _upArcAttrSQL = "UPDATE archive SET attribute=attribute&(~(1<<?))|(?<<?) WHERE id=?"
  23. _upArcNote = "UPDATE archive SET note=? WHERE id=?"
  24. _upArcCopyright = "UPDATE archive SET copyright=? WHERE id=?"
  25. _upArcMtime = "UPDATE archive SET mtime=? WHERE id=?"
  26. _arcSQL = "SELECT id,mid,title,access,attribute,reject_reason,tag,forward,round,state,copyright,cover,content,typeid,pubtime,ctime,mtime FROM archive WHERE id=?"
  27. _arcsSQL = "SELECT id,mid,title,access,attribute,reject_reason,tag,forward,round,state,copyright,cover,content,typeid,pubtime,ctime,mtime FROM archive WHERE id in (%s)"
  28. _arcStatesSQL = "SELECT id,state FROM archive WHERE id IN (%s)"
  29. _upArcTagSQL = "UPDATE archive SET tag=? WHERE id=?"
  30. )
  31. // TxUpArchive update archive by aid.
  32. func (d *Dao) TxUpArchive(tx *xsql.Tx, aid int64, title, content, cover, note string, copyright int8, pTime xtime.Time) (rows int64, err error) {
  33. res, err := tx.Exec(_upArcSQL, title, content, copyright, cover, note, pTime, time.Now(), aid)
  34. if err != nil {
  35. log.Error("d.TxUpArchive.tx.Exec error(%v)", err)
  36. return
  37. }
  38. rows, err = res.RowsAffected()
  39. return
  40. }
  41. // TxUpArcTypeID update archive type_id by aid
  42. func (d *Dao) TxUpArcTypeID(tx *xsql.Tx, aid int64, typeID int16) (rows int64, err error) {
  43. res, err := tx.Exec(_upArcTpSQL, typeID, aid)
  44. if err != nil {
  45. log.Error("d.TxUpArcTypeID.tx.Exec error(%v) ", err)
  46. return
  47. }
  48. rows, err = res.RowsAffected()
  49. return
  50. }
  51. // TxUpArcRound update archive round by aid
  52. func (d *Dao) TxUpArcRound(tx *xsql.Tx, aid int64, round int8) (rows int64, err error) {
  53. res, err := tx.Exec(_upArcRound, round, aid)
  54. if err != nil {
  55. log.Error("d.TxUpArcRound.tx.Exec error(%v) ", err)
  56. return
  57. }
  58. rows, err = res.RowsAffected()
  59. return
  60. }
  61. // TxUpArcState update archive state by aid
  62. func (d *Dao) TxUpArcState(tx *xsql.Tx, aid int64, state int8) (rows int64, err error) {
  63. res, err := tx.Exec(_upArcState, state, aid)
  64. if err != nil {
  65. log.Error("d.TxUpArcState.tx.Exec error(%v)", err)
  66. }
  67. rows, err = res.RowsAffected()
  68. return
  69. }
  70. // TxUpArcAccess update archive by aid.
  71. func (d *Dao) TxUpArcAccess(tx *xsql.Tx, aid int64, access int16) (rows int64, err error) {
  72. res, err := tx.Exec(_upAccessSQL, access, aid)
  73. if err != nil {
  74. log.Error("d.TxUpArcAccess.tx.Exec error(%v)", err)
  75. return
  76. }
  77. rows, err = res.RowsAffected()
  78. return
  79. }
  80. // TxUpArcAuthor update archive mid && author by aid.
  81. func (d *Dao) TxUpArcAuthor(tx *xsql.Tx, aid, mid int64, author string) (rows int64, err error) {
  82. res, err := tx.Exec(_upAuthorSQL, mid, author, aid)
  83. if err != nil {
  84. log.Error("d.TxUpArcAuthor.tx.Exec error(%v)", err)
  85. return
  86. }
  87. rows, err = res.RowsAffected()
  88. return
  89. }
  90. // TxUpArcPTime update ptime by aid
  91. func (d *Dao) TxUpArcPTime(tx *xsql.Tx, aid int64, pTime xtime.Time) (rows int64, err error) {
  92. res, err := tx.Exec(_upPTimeSQL, pTime, aid)
  93. if err != nil {
  94. log.Error("tx.Exec(%s, %d, %v) error(%v)", _upPTimeSQL, pTime, aid, err)
  95. return
  96. }
  97. rows, err = res.RowsAffected()
  98. return
  99. }
  100. // TxUpArcReason update archive reject_reason && forward_id by aid
  101. func (d *Dao) TxUpArcReason(tx *xsql.Tx, aid, forward int64, reason string) (rows int64, err error) {
  102. res, err := tx.Exec(_upArcReasonSQL, reason, forward, aid)
  103. if err != nil {
  104. log.Error("d.TxUpArcReason.tx.Exec error(%v)", err)
  105. return
  106. }
  107. rows, err = res.RowsAffected()
  108. return
  109. }
  110. // TxUpArcAttr update attribute by aid.
  111. func (d *Dao) TxUpArcAttr(tx *xsql.Tx, aid int64, bit uint, val int32) (rows int64, err error) {
  112. res, err := tx.Exec(_upArcAttrSQL, bit, val, bit, aid)
  113. if err != nil {
  114. log.Error("d.upArcAttr.Exec() error(%v)", err)
  115. return
  116. }
  117. rows, err = res.RowsAffected()
  118. return
  119. }
  120. // TxUpArcNote update note by aid.
  121. func (d *Dao) TxUpArcNote(tx *xsql.Tx, aid int64, note string) (rows int64, err error) {
  122. res, err := tx.Exec(_upArcNote, note, aid)
  123. if err != nil {
  124. log.Error("d.upArcNote.Exec() error(%v)", err)
  125. return
  126. }
  127. rows, err = res.RowsAffected()
  128. return
  129. }
  130. // TxUpArcCopyRight update copyright by aid.
  131. func (d *Dao) TxUpArcCopyRight(tx *xsql.Tx, aid int64, copyright int8) (rows int64, err error) {
  132. res, err := tx.Exec(_upArcCopyright, copyright, aid)
  133. if err != nil {
  134. log.Error("d.TxUpArcCopyRight.Exec() error(%v)", err)
  135. return
  136. }
  137. rows, err = res.RowsAffected()
  138. return
  139. }
  140. // TxUpArcMtime update mtime by aid.
  141. func (d *Dao) TxUpArcMtime(tx *xsql.Tx, aid int64) (rows int64, err error) {
  142. res, err := tx.Exec(_upArcMtime, time.Now(), aid)
  143. if err != nil {
  144. log.Error("d.upArcNote.Exec() error(%v)", err)
  145. return
  146. }
  147. rows, err = res.RowsAffected()
  148. return
  149. }
  150. // Archive get archive by aid
  151. func (d *Dao) Archive(c context.Context, aid int64) (a *archive.Archive, err error) {
  152. var (
  153. row = d.rddb.QueryRow(c, _arcSQL, aid)
  154. reason, tag sql.NullString
  155. )
  156. a = &archive.Archive{}
  157. if err = row.Scan(&a.Aid, &a.Mid, &a.Title, &a.Access, &a.Attribute, &reason, &tag, &a.Forward, &a.Round, &a.State,
  158. &a.Copyright, &a.Cover, &a.Desc, &a.TypeID, &a.PTime, &a.CTime, &a.MTime); err != nil {
  159. if err == xsql.ErrNoRows {
  160. a = nil
  161. err = nil
  162. } else {
  163. log.Error("row.Scan error(%v)", err)
  164. }
  165. return
  166. }
  167. a.RejectReason = reason.String
  168. a.Tag = tag.String
  169. return
  170. }
  171. // Archives get archives by aids
  172. func (d *Dao) Archives(c context.Context, aids []int64) (am map[int64]*archive.Archive, err error) {
  173. am = make(map[int64]*archive.Archive)
  174. if len(aids) == 0 {
  175. return
  176. }
  177. var reason, tag sql.NullString
  178. rows, err := d.rddb.Query(c, fmt.Sprintf(_arcsSQL, xstr.JoinInts(aids)))
  179. if err != nil {
  180. log.Error("db.Query() error(%v)", err)
  181. return
  182. }
  183. defer rows.Close()
  184. for rows.Next() {
  185. a := &archive.Archive{}
  186. if err = rows.Scan(&a.Aid, &a.Mid, &a.Title, &a.Access, &a.Attribute, &reason, &tag, &a.Forward, &a.Round, &a.State,
  187. &a.Copyright, &a.Cover, &a.Desc, &a.TypeID, &a.PTime, &a.CTime, &a.MTime); err != nil {
  188. log.Error("rows.Scan error(%v)", err)
  189. return
  190. }
  191. a.RejectReason = reason.String
  192. a.Tag = tag.String
  193. am[a.Aid] = a
  194. }
  195. return
  196. }
  197. // ArcStateMap get archive id and state map
  198. func (d *Dao) ArcStateMap(c context.Context, aids []int64) (sMap map[int64]int, err error) {
  199. sMap = make(map[int64]int)
  200. if len(aids) == 0 {
  201. return
  202. }
  203. rows, err := d.rddb.Query(c, fmt.Sprintf(_arcStatesSQL, xstr.JoinInts(aids)))
  204. if err != nil {
  205. log.Error("db.Query() error(%v)", err)
  206. return
  207. }
  208. defer rows.Close()
  209. for rows.Next() {
  210. a := struct {
  211. ID int64
  212. State int
  213. }{}
  214. if err = rows.Scan(&a.ID, &a.State); err != nil {
  215. log.Error("rows.Scan error(%v)", err)
  216. return
  217. }
  218. sMap[a.ID] = a.State
  219. }
  220. return
  221. }
  222. //TxUpTag update archive tag
  223. func (d *Dao) TxUpTag(tx *xsql.Tx, aid int64, tags string) (id int64, err error) {
  224. res, err := tx.Exec(_upArcTagSQL, tags, aid)
  225. if err != nil {
  226. log.Error("TxUpTag tx.Exec error(%v) aid(%d) tags(%s)", err, aid, tags)
  227. return
  228. }
  229. return res.LastInsertId()
  230. }