mysql.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "go-common/app/interface/main/playlist/model"
  9. xsql "go-common/library/database/sql"
  10. "go-common/library/log"
  11. "go-common/library/xstr"
  12. )
  13. const (
  14. _plArcSub = 100
  15. _plArcSQL = "SELECT aid,sort,`desc` FROM playlist_archive_%s WHERE pid = ? AND aid = ?"
  16. _plArcsSQL = "SELECT aid,sort,`desc` FROM playlist_archive_%s WHERE pid = ? ORDER BY sort"
  17. _plArcAddSQL = "INSERT INTO playlist_archive_%s (pid,aid,sort,`desc`) VALUES (?,?,?,?)"
  18. _plArcBatchAddSQL = "INSERT INTO playlist_archive_%s (pid,aid,sort,`desc`) VALUES %s"
  19. _plArcDelSQL = "DELETE FROM playlist_archive_%s WHERE pid = ? AND aid = ?"
  20. _plArcDelByPidSQL = "DELETE FROM playlist_archive_%s WHERE pid = ?"
  21. _plArcBatchDelSQL = "DELETE FROM playlist_archive_%s WHERE pid = ? AND aid in (%s)"
  22. _plArcDescEditSQL = "UPDATE playlist_archive_%s SET `desc` = ? WHERE pid = ? AND aid = ?"
  23. _plArcSortEditSQL = "UPDATE playlist_archive_%s SET sort = ? WHERE pid = ? AND aid = ?"
  24. _plArcSortBatchEditSQL = "UPDATE playlist_archive_%s SET sort = CASE %s END WHERE pid = ? AND aid IN (%s)"
  25. _plAddSQL = "INSERT INTO playlist_stat (mid,fid,is_deleted,view,reply,fav,share) VALUES (?,?,0,0,0,0,0) ON DUPLICATE KEY UPDATE mid=?,fid=?,is_deleted=0,view=0,reply=0,fav=0,share=0"
  26. _plEditSQL = "UPDATE playlist_stat SET mtime = ? WHERE id = ?"
  27. _plDelSQL = "UPDATE playlist_stat set is_deleted = 1 WHERE id = ?"
  28. _plByMidSQL = "SELECT id,mid,fid,view,reply,fav,`share`,mtime FROM playlist_stat WHERE is_deleted = 0 AND mid = ?"
  29. _plByPidsSQL = "SELECT id,mid,fid,view,reply,fav,`share`,mtime FROM playlist_stat WHERE id in (%s)"
  30. _plByPidSQL = "SELECT id,mid,fid,view,reply,fav,`share`,mtime FROM playlist_stat WHERE id = ?"
  31. )
  32. func plArcHit(pid int64) string {
  33. return fmt.Sprintf("%02d", pid%_plArcSub)
  34. }
  35. // Video get video by pid and aid
  36. func (d *Dao) Video(c context.Context, pid, aid int64) (res *model.ArcSort, err error) {
  37. res = &model.ArcSort{}
  38. row := d.db.QueryRow(c, fmt.Sprintf(_plArcSQL, plArcHit(pid)), pid, aid)
  39. if err = row.Scan(&res.Aid, &res.Sort, &res.Desc); err != nil {
  40. if err == sql.ErrNoRows {
  41. err = nil
  42. } else {
  43. log.Error("Video:row.Scan error(%v)", err)
  44. }
  45. }
  46. return
  47. }
  48. // Videos get playlist videos.
  49. func (d *Dao) Videos(c context.Context, pid int64) (res []*model.ArcSort, err error) {
  50. var rows *xsql.Rows
  51. if rows, err = d.videosStmt[plArcHit(pid)].Query(c, pid); err != nil {
  52. log.Error("d.videosStmt[%s].Query(%d) error(%v)", plArcHit(pid), pid, err)
  53. return
  54. }
  55. defer rows.Close()
  56. for rows.Next() {
  57. r := new(model.ArcSort)
  58. if err = rows.Scan(&r.Aid, &r.Sort, &r.Desc); err != nil {
  59. log.Error("row.Scan() error(%v)", err)
  60. return
  61. }
  62. res = append(res, r)
  63. }
  64. if err = rows.Err(); err != nil {
  65. log.Error("rows.Err() error(%v)", err)
  66. }
  67. return
  68. }
  69. // AddArc add archive to playlist.
  70. func (d *Dao) AddArc(c context.Context, pid, aid, sort int64, desc string) (lastID int64, err error) {
  71. var res sql.Result
  72. if res, err = d.db.Exec(c, fmt.Sprintf(_plArcAddSQL, plArcHit(pid)), pid, aid, desc, sort); err != nil {
  73. log.Error("AddArc: db.Exec(%d,%d,%d,%s) error(%v)", pid, aid, sort, desc, err)
  74. return
  75. }
  76. return res.LastInsertId()
  77. }
  78. // BatchAddArc add archives to playlist.
  79. func (d *Dao) BatchAddArc(c context.Context, pid int64, arcSorts []*model.ArcSort) (lastID int64, err error) {
  80. var (
  81. res sql.Result
  82. values []string
  83. )
  84. for _, v := range arcSorts {
  85. values = append(values, fmt.Sprintf("(%d,%d,%d,'%s')", pid, v.Aid, v.Sort, v.Desc))
  86. }
  87. if res, err = d.db.Exec(c, fmt.Sprintf(_plArcBatchAddSQL, plArcHit(pid), strings.Join(values, ","))); err != nil {
  88. log.Error("BatchAddArc: db.Exec(%d) error(%v)", pid, err)
  89. return
  90. }
  91. return res.LastInsertId()
  92. }
  93. // DelArc delete playlist archive.
  94. func (d *Dao) DelArc(c context.Context, pid, aid int64) (affected int64, err error) {
  95. var res sql.Result
  96. if res, err = d.db.Exec(c, fmt.Sprintf(_plArcDelSQL, plArcHit(pid)), pid, aid); err != nil {
  97. log.Error("DelArc: db.Exec(%d,%d) error(%v)", pid, aid, err)
  98. return
  99. }
  100. return res.RowsAffected()
  101. }
  102. // BatchDelArc delete archives from playlist.
  103. func (d *Dao) BatchDelArc(c context.Context, pid int64, aids []int64) (affected int64, err error) {
  104. var res sql.Result
  105. if res, err = d.db.Exec(c, fmt.Sprintf(_plArcBatchDelSQL, plArcHit(pid), xstr.JoinInts(aids)), pid); err != nil {
  106. log.Error("BatchDelArc: db.Exec(%d,%v) error(%v)", pid, aids, err)
  107. return
  108. }
  109. return res.RowsAffected()
  110. }
  111. // UpdateArcDesc update playlist arc desc.
  112. func (d *Dao) UpdateArcDesc(c context.Context, pid, aid int64, desc string) (affected int64, err error) {
  113. var res sql.Result
  114. if res, err = d.db.Exec(c, fmt.Sprintf(_plArcDescEditSQL, plArcHit(pid)), desc, pid, aid); err != nil {
  115. log.Error("UpdateArcDesc: db.Exec(%d,%d,%s) error(%v)", pid, aid, desc, err)
  116. return
  117. }
  118. return res.RowsAffected()
  119. }
  120. // UpdateArcSort update playlist arc sort.
  121. func (d *Dao) UpdateArcSort(c context.Context, pid, aid, sort int64) (affected int64, err error) {
  122. var res sql.Result
  123. if res, err = d.db.Exec(c, fmt.Sprintf(_plArcSortEditSQL, plArcHit(pid)), sort, pid, aid); err != nil {
  124. log.Error("UpdateArcSort: db.Exec(%d,%d,%d) error(%v)", pid, aid, sort, err)
  125. return
  126. }
  127. return res.RowsAffected()
  128. }
  129. // BatchUpdateArcSort batch update playlist arc sort.
  130. func (d *Dao) BatchUpdateArcSort(c context.Context, pid int64, arcSorts []*model.ArcSort) (affected int64, err error) {
  131. var (
  132. caseStr string
  133. aids []int64
  134. res sql.Result
  135. )
  136. for _, v := range arcSorts {
  137. caseStr = fmt.Sprintf("%s WHEN aid = %d THEN %d", caseStr, v.Aid, v.Sort)
  138. aids = append(aids, v.Aid)
  139. }
  140. if res, err = d.db.Exec(c, fmt.Sprintf(_plArcSortBatchEditSQL, plArcHit(pid), caseStr, xstr.JoinInts(aids)), pid); err != nil {
  141. log.Error("BatchUpdateArcSort: db.Exec(%d,%s,%v) error(%v)", pid, caseStr, aids, err)
  142. return
  143. }
  144. return res.RowsAffected()
  145. }
  146. //Add playlist stat.
  147. func (d *Dao) Add(c context.Context, mid, fid int64) (lastID int64, err error) {
  148. var res sql.Result
  149. if res, err = d.db.Exec(c, _plAddSQL, mid, fid, mid, fid); err != nil {
  150. log.Error("Add:db.Exec(%d,%d) error(%v)", mid, fid, err)
  151. return
  152. }
  153. return res.LastInsertId()
  154. }
  155. // Del playlist stat.
  156. func (d *Dao) Del(c context.Context, pid int64) (affected int64, err error) {
  157. var (
  158. res sql.Result
  159. tx *xsql.Tx
  160. )
  161. if tx, err = d.db.Begin(c); err != nil {
  162. log.Error("d.db.Begin error(%v)", err)
  163. return
  164. }
  165. if res, err = tx.Exec(_plDelSQL, pid); err != nil {
  166. tx.Rollback()
  167. log.Error("DelPlaylist: db.Exec(%d) error(%v)", pid, err)
  168. return
  169. }
  170. if _, err = d.db.Exec(c, fmt.Sprintf(_plArcDelByPidSQL, plArcHit(pid)), pid); err != nil {
  171. tx.Rollback()
  172. log.Error("DelArc: db.Exec(%d) error(%v)", pid, err)
  173. return
  174. }
  175. if err = tx.Commit(); err != nil {
  176. log.Error("tx.Commit error(%v)", err)
  177. return
  178. }
  179. return res.RowsAffected()
  180. }
  181. // Update playlist stat.
  182. func (d *Dao) Update(c context.Context, pid int64) (affected int64, err error) {
  183. var res sql.Result
  184. if res, err = d.db.Exec(c, _plEditSQL, time.Now(), pid); err != nil {
  185. log.Error("Update mtime: db.Exec(%d) error(%v)", pid, err)
  186. return
  187. }
  188. return res.RowsAffected()
  189. }
  190. // PlsByMid get playlist by mid.
  191. func (d *Dao) PlsByMid(c context.Context, mid int64) (res []*model.PlStat, err error) {
  192. var (
  193. rows *xsql.Rows
  194. )
  195. if rows, err = d.db.Query(c, _plByMidSQL, mid); err != nil {
  196. log.Error("PlsByMid:d.db.Query(%d) error(%v)", mid, err)
  197. return
  198. }
  199. defer rows.Close()
  200. for rows.Next() {
  201. r := new(model.PlStat)
  202. if err = rows.Scan(&r.ID, &r.Mid, &r.Fid, &r.View, &r.Reply, &r.Fav, &r.Share, &r.MTime); err != nil {
  203. log.Error("PlsByMid:row.Scan() error(%v)", err)
  204. return
  205. }
  206. res = append(res, r)
  207. }
  208. if err = rows.Err(); err != nil {
  209. log.Error("rows.Err() error(%v)", err)
  210. }
  211. return
  212. }
  213. // PlsByPid get playlist stat by pids.
  214. func (d *Dao) PlsByPid(c context.Context, pids []int64) (res []*model.PlStat, err error) {
  215. var (
  216. rows *xsql.Rows
  217. )
  218. if rows, err = d.db.Query(c, fmt.Sprintf(_plByPidsSQL, xstr.JoinInts(pids))); err != nil {
  219. log.Error("PlsByPid: db.Exec(%s) error(%v)", xstr.JoinInts(pids), err)
  220. return
  221. }
  222. defer rows.Close()
  223. for rows.Next() {
  224. r := new(model.PlStat)
  225. if err = rows.Scan(&r.ID, &r.Mid, &r.Fid, &r.View, &r.Reply, &r.Fav, &r.Share, &r.MTime); err != nil {
  226. log.Error("PlsByPid:row.Scan() error(%v)", err)
  227. return
  228. }
  229. res = append(res, r)
  230. }
  231. if err = rows.Err(); err != nil {
  232. log.Error("rows.Err() error(%v)", err)
  233. }
  234. return
  235. }
  236. // PlByPid get playlist by pid.
  237. func (d *Dao) PlByPid(c context.Context, pid int64) (res *model.PlStat, err error) {
  238. res = &model.PlStat{}
  239. row := d.db.QueryRow(c, _plByPidSQL, pid)
  240. if err = row.Scan(&res.ID, &res.Mid, &res.Fid, &res.View, &res.Reply, &res.Fav, &res.Share, &res.MTime); err != nil {
  241. if err == sql.ErrNoRows {
  242. err = nil
  243. } else {
  244. log.Error("PlByPid:row.Scan error(%v)", err)
  245. }
  246. }
  247. return
  248. }