mysql_advance.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. _countAdvsSQLAll = "SELECT count(*) FROM dm_advancecomment WHERE dm_inid=? "
  10. _countAdvsSQLMode = "SELECT count(*) FROM dm_advancecomment WHERE dm_inid=? AND mode=? "
  11. _countAdvsSQLType = "SELECT count(*) FROM dm_advancecomment WHERE dm_inid=? AND type=? "
  12. _countAdvsSQLTypeMode = "SELECT count(*) FROM dm_advancecomment WHERE dm_inid=? AND type=? AND mode=? "
  13. _selAdvsSQLAll = "SELECT buy_id,type,mode,mid,timestamp FROM dm_advancecomment WHERE dm_inid=? ORDER BY buy_id DESC limit ?,?"
  14. _selAdvsSQLMode = "SELECT buy_id,type,mode,mid,timestamp FROM dm_advancecomment WHERE dm_inid=? AND mode=? ORDER BY buy_id DESC limit ?,?"
  15. _selAdvsSQLType = "SELECT buy_id,type,mode,mid,timestamp FROM dm_advancecomment WHERE dm_inid=? AND type=? ORDER BY buy_id DESC limit ?,?"
  16. _selAdvsSQLTypeMode = "SELECT buy_id,type,mode,mid,timestamp FROM dm_advancecomment WHERE dm_inid=? AND type=? AND mode=? ORDER BY buy_id DESC ?,?"
  17. )
  18. // Advances 获取高级弹幕申请
  19. func (d *Dao) Advances(c context.Context, dmInid int64, typ, mode string, pn, ps int64) (res []*model.Advance, total int64, err error) {
  20. var rows *sql.Rows
  21. res = make([]*model.Advance, 0)
  22. if typ == model.AdvTypeAll {
  23. if mode == model.AdvModeAll {
  24. countRow := d.biliDM.QueryRow(c, _countAdvsSQLAll, dmInid)
  25. if err = countRow.Scan(&total); err != nil {
  26. log.Error("row.ScanCount error(%v)", err)
  27. return
  28. }
  29. rows, err = d.biliDM.Query(c, _selAdvsSQLAll, dmInid, (pn-1)*ps, ps)
  30. if err != nil {
  31. log.Error("d.dbDM.Query(%s,%d,%d,%d) error(%v)", _selAdvsSQLAll, dmInid, (pn-1)*ps, ps, err)
  32. return
  33. }
  34. } else {
  35. countRow := d.biliDM.QueryRow(c, _countAdvsSQLMode, dmInid, mode)
  36. if err = countRow.Scan(&total); err != nil {
  37. log.Error("row.ScanCount error(%v)", err)
  38. return
  39. }
  40. rows, err = d.biliDM.Query(c, _selAdvsSQLMode, dmInid, mode, (pn-1)*ps, ps)
  41. if err != nil {
  42. log.Error("d.dbDM.Query(%s,%d,%s,%d,%d) error(%v)", _selAdvsSQLMode, dmInid, mode, (pn-1)*ps, ps, err)
  43. return
  44. }
  45. }
  46. } else {
  47. if mode == model.AdvModeAll {
  48. countRow := d.biliDM.QueryRow(c, _countAdvsSQLType, dmInid, typ)
  49. if err = countRow.Scan(&total); err != nil {
  50. log.Error("row.ScanCount error(%v)", err)
  51. return
  52. }
  53. rows, err = d.biliDM.Query(c, _selAdvsSQLType, dmInid, typ, (pn-1)*ps, ps)
  54. if err != nil {
  55. log.Error("d.dbDM.Query(%s,%d,%s,%d,%d) error(%v)", _selAdvsSQLType, dmInid, typ, (pn-1)*ps, ps, err)
  56. return
  57. }
  58. } else {
  59. countRow := d.biliDM.QueryRow(c, _countAdvsSQLTypeMode, dmInid, typ)
  60. if err = countRow.Scan(&total); err != nil {
  61. log.Error("row.ScanCount error(%v)", err)
  62. return
  63. }
  64. rows, err = d.biliDM.Query(c, _selAdvsSQLTypeMode, dmInid, typ, mode, (pn-1)*ps, ps)
  65. if err != nil {
  66. log.Error("d.dbDM.Query(%s,%d,%s,%s,%d,%d) error(%v)", _selAdvsSQLTypeMode, dmInid, typ, mode, (pn-1)*ps, ps, err)
  67. return
  68. }
  69. }
  70. }
  71. defer rows.Close()
  72. for rows.Next() {
  73. adv := &model.Advance{}
  74. if err = rows.Scan(&adv.ID, &adv.Type, &adv.Mode, &adv.Mid, &adv.Timestamp); err != nil {
  75. log.Error("rows.Scan error(%v)", err)
  76. return
  77. }
  78. res = append(res, adv)
  79. }
  80. return
  81. }