mysql_transfer.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/dm/model"
  5. "go-common/library/database/sql"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _InstTrJobSQL = "INSERT INTO dm_transfer_job(from_cid,to_cid,mid,offset,state) VALUES(?,?,?,?,?)"
  10. _countTransList = "SELECT count(*) FROM dm_transfer_job WHERE to_cid=?"
  11. _countTransListState = "SELECT count(*) FROM dm_transfer_job WHERE to_cid=? AND state=?"
  12. _TransList = "SELECT id,from_cid,to_cid,state,ctime FROM dm_transfer_job WHERE to_cid=? limit ?,?"
  13. _TransListState = "SELECT id,from_cid,to_cid,state,ctime FROM dm_transfer_job WHERE to_cid=? AND state=? limit ?, ?"
  14. _selTransferID = "SELECT id,from_cid,to_cid,mid,offset,state,ctime,mtime from dm_transfer_job WHERE id=?"
  15. _uptTransferSQL = "UPDATE dm_transfer_job SET state=? WHERE id=?"
  16. )
  17. // InsertTransferJob add transfer job
  18. func (d *Dao) InsertTransferJob(c context.Context, from, to, mid int64, offset float64, state int8) (affect int64, err error) {
  19. row, err := d.biliDM.Exec(c, _InstTrJobSQL, from, to, mid, offset, state)
  20. if err != nil {
  21. log.Error("biliDM.Exec(%s, %d %d %d %v) error(%v)", _InstTrJobSQL, from, to, mid, offset, err)
  22. return
  23. }
  24. return row.LastInsertId()
  25. }
  26. // TransferList transfer list
  27. func (d *Dao) TransferList(c context.Context, cid, state, pn, ps int64) (res []*model.TransList, total int64, err error) {
  28. var rows *sql.Rows
  29. res = make([]*model.TransList, 0)
  30. if state == int64(model.TransferJobStateAll) {
  31. countRow := d.biliDM.QueryRow(c, _countTransList, cid)
  32. if err = countRow.Scan(&total); err != nil {
  33. log.Error("row.ScanCount error(%v)", err)
  34. return
  35. }
  36. rows, err = d.biliDM.Query(c, _TransList, cid, (pn-1)*ps, ps)
  37. if err != nil {
  38. log.Error("biliDM.Query(%s, %d ) error(%v)", _TransList, cid, err)
  39. return
  40. }
  41. } else {
  42. countRow := d.biliDM.QueryRow(c, _countTransListState, cid, state)
  43. if err = countRow.Scan(&total); err != nil {
  44. log.Error("row.ScanCount error(%v)", err)
  45. return
  46. }
  47. rows, err = d.biliDM.Query(c, _TransListState, cid, state, (pn-1)*ps, ps)
  48. if err != nil {
  49. log.Error("biliDM.Query(%s, %d, %d) error(%v)", _TransList, cid, state, err)
  50. return
  51. }
  52. }
  53. defer rows.Close()
  54. for rows.Next() {
  55. dm := &model.TransList{}
  56. if err = rows.Scan(&dm.ID, &dm.From, &dm.To, &dm.State, &dm.Ctime); err != nil {
  57. log.Error("rows.Scan error(%v)", err)
  58. return
  59. }
  60. res = append(res, dm)
  61. }
  62. return
  63. }
  64. // CheckTransferID check transfer job state by id
  65. func (d *Dao) CheckTransferID(c context.Context, id int64) (job *model.TransferJobInfo, err error) {
  66. job = new(model.TransferJobInfo)
  67. row := d.biliDM.QueryRow(c, _selTransferID, id)
  68. if err = row.Scan(&job.ID, &job.FromCID, &job.ToCID, &job.MID, &job.Offset, &job.State, &job.Ctime, &job.Mtime); err != nil {
  69. if err == sql.ErrNoRows {
  70. job = nil
  71. err = nil
  72. } else {
  73. log.Error("row.Scan error(%v)", err)
  74. }
  75. }
  76. return
  77. }
  78. // SetTransferState change transfer state
  79. func (d *Dao) SetTransferState(c context.Context, id int64, state int8) (affect int64, err error) {
  80. row, err := d.biliDM.Exec(c, _uptTransferSQL, state, id)
  81. if err != nil {
  82. log.Error("d.biliDM.Exec(%s,%d) error(%v)", _uptTransferSQL, id, err)
  83. return
  84. }
  85. return row.RowsAffected()
  86. }