mysql_subtitle.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/admin/main/dm/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. )
  11. const (
  12. _subtitleSharding = 100
  13. _getSubtitles = "SELECT id,oid,type,lan,aid,mid,author_mid,up_mid,is_sign,is_lock,status,subtitle_url,pub_time,mtime FROM subtitle_%02d WHERE id in (%s)"
  14. _getSubtitle = "SELECT id,oid,type,lan,aid,mid,author_mid,up_mid,is_sign,is_lock,status,subtitle_url,pub_time,mtime FROM subtitle_%02d WHERE id=?"
  15. _countSubtitleDraft = "SELECT COUNT(id) FROM subtitle_%02d WHERE oid=? AND type=? AND lan=? AND mid=? AND pub_time=0"
  16. _updateSubtitle = "UPDATE subtitle_%02d set status=?,pub_time=? where id=?"
  17. _getSubtitlePubID = "SELECT id FROM subtitle_%02d WHERE oid=? AND type=? AND lan=? AND status=5 ORDER BY pub_time DESC limit 1"
  18. _addSubtitlePub = "INSERT INTO subtitle_pub(oid,type,lan,subtitle_id,is_delete) VALUES(?,?,?,?,?) ON DUPLICATE KEY UPDATE subtitle_id=?,is_delete=?"
  19. _getSubtitleLans = "SELECT code,lan,doc_zh FROM subtitle_language WHERE is_delete=0"
  20. _addSubtitleSubject = "INSERT INTO subtitle_subject(aid,allow,attr,lan_code) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE allow=?,attr=?,lan_code=?"
  21. _getSubtitleSubject = "SELECT aid,allow,attr,lan_code from subtitle_subject WHERE aid=?"
  22. )
  23. func (d *Dao) hitSubtitle(oid int64) int64 {
  24. return oid % _subtitleSharding
  25. }
  26. // GetSubtitles .
  27. func (d *Dao) GetSubtitles(c context.Context, oid int64, subtitleIds []int64) (subtitles []*model.Subtitle, err error) {
  28. rows, err := d.biliDM.Query(c, fmt.Sprintf(_getSubtitles, d.hitSubtitle(oid), xstr.JoinInts(subtitleIds)))
  29. if err != nil {
  30. log.Error("params(subtitleIds:%v),error(%v)", subtitleIds, err)
  31. return
  32. }
  33. defer rows.Close()
  34. for rows.Next() {
  35. subtitle := &model.Subtitle{}
  36. var t time.Time
  37. if err = rows.Scan(&subtitle.ID, &subtitle.Oid, &subtitle.Type, &subtitle.Lan, &subtitle.Aid, &subtitle.Mid, &subtitle.AuthorID, &subtitle.UpMid, &subtitle.IsSign, &subtitle.IsLock, &subtitle.Status, &subtitle.SubtitleURL, &subtitle.PubTime, &t); err != nil {
  38. log.Error("params(subtitleIds:%v),error(%v)", subtitleIds, err)
  39. return
  40. }
  41. subtitle.Mtime = t.Unix()
  42. subtitles = append(subtitles, subtitle)
  43. }
  44. if err = rows.Err(); err != nil {
  45. log.Error("params(subtitleIds:%v),error(%v)", subtitleIds, err)
  46. return
  47. }
  48. return
  49. }
  50. // GetSubtitle .
  51. func (d *Dao) GetSubtitle(c context.Context, oid, subtitleID int64) (subtitle *model.Subtitle, err error) {
  52. row := d.biliDM.QueryRow(c, fmt.Sprintf(_getSubtitle, d.hitSubtitle(oid)), subtitleID)
  53. subtitle = &model.Subtitle{}
  54. var t time.Time
  55. if err = row.Scan(&subtitle.ID, &subtitle.Oid, &subtitle.Type, &subtitle.Lan, &subtitle.Aid, &subtitle.Mid, &subtitle.AuthorID, &subtitle.UpMid, &subtitle.IsSign, &subtitle.IsLock, &subtitle.Status, &subtitle.SubtitleURL, &subtitle.PubTime, &t); err != nil {
  56. if err == sql.ErrNoRows {
  57. err = nil
  58. subtitle = nil
  59. return
  60. }
  61. log.Error("params(subtitleIds:%v),error(%v)", subtitleID, err)
  62. return
  63. }
  64. subtitle.Mtime = t.Unix()
  65. return
  66. }
  67. // UpdateSubtitle .
  68. func (d *Dao) UpdateSubtitle(c context.Context, subtitle *model.Subtitle) (err error) {
  69. if _, err = d.biliDM.Exec(c, fmt.Sprintf(_updateSubtitle, d.hitSubtitle(subtitle.Oid)), subtitle.Status, subtitle.PubTime, subtitle.ID); err != nil {
  70. log.Error("UpdateSubtitle.params(subtitle:%+v),error(%v)", subtitle, err)
  71. return
  72. }
  73. return
  74. }
  75. // CountSubtitleDraft .
  76. func (d *Dao) CountSubtitleDraft(c context.Context, oid int64, mid int64, lan uint8, tp int32) (count int64, err error) {
  77. row := d.biliDM.QueryRow(c, fmt.Sprintf(_countSubtitleDraft, d.hitSubtitle(oid)), oid, tp, lan, mid)
  78. if err = row.Scan(&count); err != nil {
  79. if err == sql.ErrNoRows {
  80. err = nil
  81. count = 0
  82. return
  83. }
  84. log.Error("params(oid:%v, tp:%v, mid:%v, lan:%v),error(%v)", oid, tp, mid, lan, err)
  85. return
  86. }
  87. return
  88. }
  89. // TxUpdateSubtitle .
  90. func (d *Dao) TxUpdateSubtitle(tx *sql.Tx, subtitle *model.Subtitle) (err error) {
  91. if _, err = tx.Exec(fmt.Sprintf(_updateSubtitle, d.hitSubtitle(subtitle.Oid)), subtitle.Status, subtitle.PubTime, subtitle.ID); err != nil {
  92. log.Error("TxUpdateSubtitle.params(subtitle:%+v),error(%v)", subtitle, err)
  93. return
  94. }
  95. return
  96. }
  97. // TxGetSubtitleID .
  98. func (d *Dao) TxGetSubtitleID(tx *sql.Tx, oid int64, tp int32, lan uint8) (subtitleID int64, err error) {
  99. row := tx.QueryRow(fmt.Sprintf(_getSubtitlePubID, d.hitSubtitle(oid)), oid, tp, lan)
  100. if err = row.Scan(&subtitleID); err != nil {
  101. if err == sql.ErrNoRows {
  102. err = nil
  103. subtitleID = 0
  104. return
  105. }
  106. log.Error("TxGetSubtitleID.Scac(err:%v)", err)
  107. return
  108. }
  109. return
  110. }
  111. // TxUpdateSubtitlePub .
  112. func (d *Dao) TxUpdateSubtitlePub(tx *sql.Tx, subtitlePub *model.SubtitlePub) (err error) {
  113. if _, err = tx.Exec(_addSubtitlePub, subtitlePub.Oid, subtitlePub.Type, subtitlePub.Lan, subtitlePub.SubtitleID, subtitlePub.IsDelete, subtitlePub.SubtitleID, subtitlePub.IsDelete); err != nil {
  114. log.Error("params(%+v),error(%v)", subtitlePub, err)
  115. return
  116. }
  117. return
  118. }
  119. // SubtitleLans .
  120. func (d *Dao) SubtitleLans(c context.Context) (subtitleLans []*model.SubtitleLan, err error) {
  121. rows, err := d.biliDM.Query(c, _getSubtitleLans)
  122. if err != nil {
  123. log.Error("params(query:%v),error(%v)", _getSubtitleLans, err)
  124. return
  125. }
  126. defer rows.Close()
  127. for rows.Next() {
  128. subtitleLan := new(model.SubtitleLan)
  129. if err = rows.Scan(&subtitleLan.Code, &subtitleLan.Lan, &subtitleLan.DocZh); err != nil {
  130. log.Error("params.Sacn(query:%v),error(%v)", _getSubtitleLans, err)
  131. return
  132. }
  133. subtitleLans = append(subtitleLans, subtitleLan)
  134. }
  135. if err = rows.Err(); err != nil {
  136. log.Error("params.Err(query:%v),error(%v)", _getSubtitleLans, err)
  137. return
  138. }
  139. return
  140. }
  141. // AddSubtitleSubject .
  142. func (d *Dao) AddSubtitleSubject(c context.Context, subtitleSubject *model.SubtitleSubject) (err error) {
  143. if _, err = d.biliDM.Exec(c, _addSubtitleSubject, subtitleSubject.Aid, subtitleSubject.Allow, subtitleSubject.Attr, subtitleSubject.Lan, subtitleSubject.Allow, subtitleSubject.Attr, subtitleSubject.Lan); err != nil {
  144. log.Error("params(subtitleSubject:%+v),error(%v)", subtitleSubject, err)
  145. return
  146. }
  147. return
  148. }
  149. // GetSubtitleSubject .
  150. func (d *Dao) GetSubtitleSubject(c context.Context, aid int64) (subtitleSubject *model.SubtitleSubject, err error) {
  151. subtitleSubject = new(model.SubtitleSubject)
  152. row := d.biliDM.QueryRow(c, _getSubtitleSubject, aid)
  153. if err = row.Scan(&subtitleSubject.Aid, &subtitleSubject.Allow, &subtitleSubject.Attr, &subtitleSubject.Lan); err != nil {
  154. if err == sql.ErrNoRows {
  155. err = nil
  156. subtitleSubject = nil
  157. return
  158. }
  159. log.Error("params(aid:%v),error(%v)", aid, err)
  160. return
  161. }
  162. return
  163. }