bgm.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/growup/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _bgmCountSQL = "SELECT count(distinct sid) FROM background_music WHERE mid=?"
  12. _bgmTitleSQL = "SELECT sid,title FROM background_music WHERE sid IN (%s)"
  13. _bgmWhiteSQL = "SELECT COUNT(*) FROM bgm_white_list WHERE mid = ?"
  14. _bgmIncomeByMIDSQL = "SELECT sid,income,total_income,date FROM bgm_income WHERE mid = ? AND date >= ? AND date <= ?"
  15. _bgmIncomeBySIDSQL = "SELECT aid,income,date FROM bgm_income WHERE sid = ? AND date <= ?"
  16. )
  17. // BgmWhiteList bgm_white_list
  18. func (d *Dao) BgmWhiteList(c context.Context, mid int64) (count int, err error) {
  19. row := d.db.QueryRow(c, _bgmWhiteSQL, mid)
  20. if err = row.Scan(&count); err != nil {
  21. if err == sql.ErrNoRows {
  22. err = nil
  23. } else {
  24. log.Error("row scan error(%v)", err)
  25. }
  26. }
  27. return
  28. }
  29. // BGMCount get up bgm count
  30. func (d *Dao) BGMCount(c context.Context, mid int64) (count int, err error) {
  31. row := d.db.QueryRow(c, _bgmCountSQL, mid)
  32. if err = row.Scan(&count); err != nil {
  33. if err == sql.ErrNoRows {
  34. err = nil
  35. } else {
  36. log.Error("row scan error(%v)", err)
  37. }
  38. }
  39. return
  40. }
  41. // GetBgmTitle get bgm titles
  42. func (d *Dao) GetBgmTitle(c context.Context, ids []int64) (titles map[int64]string, err error) {
  43. titles = make(map[int64]string)
  44. rows, err := d.db.Query(c, fmt.Sprintf(_bgmTitleSQL, xstr.JoinInts(ids)))
  45. if err != nil {
  46. return
  47. }
  48. defer rows.Close()
  49. for rows.Next() {
  50. var sid int64
  51. var title string
  52. err = rows.Scan(&sid, &title)
  53. if err != nil {
  54. log.Error("rows.Scan error(%v)", err)
  55. return
  56. }
  57. titles[sid] = title
  58. }
  59. return
  60. }
  61. // ListBgmIncome list bgm income
  62. func (d *Dao) ListBgmIncome(c context.Context, mid int64, startTime, endTime string) (bgms []*model.ArchiveIncome, err error) {
  63. bgms = make([]*model.ArchiveIncome, 0)
  64. rows, err := d.db.Query(c, _bgmIncomeByMIDSQL, mid, startTime, endTime)
  65. if err != nil {
  66. return
  67. }
  68. defer rows.Close()
  69. for rows.Next() {
  70. bgm := &model.ArchiveIncome{}
  71. err = rows.Scan(&bgm.ArchiveID, &bgm.Income, &bgm.TotalIncome, &bgm.Date)
  72. if err != nil {
  73. log.Error("ListColumnIncome rows.Scan error(%v)", err)
  74. return
  75. }
  76. bgms = append(bgms, bgm)
  77. }
  78. err = rows.Err()
  79. return
  80. }
  81. // ListBgmIncomeByID list bgm_income by sid
  82. func (d *Dao) ListBgmIncomeByID(c context.Context, id int64, endTime string) (bgms []*model.ArchiveIncome, err error) {
  83. bgms = make([]*model.ArchiveIncome, 0)
  84. rows, err := d.db.Query(c, _bgmIncomeBySIDSQL, id, endTime)
  85. if err != nil {
  86. log.Error("ListBgmIncomeByID d.db.Query error(%v)", err)
  87. return
  88. }
  89. defer rows.Close()
  90. for rows.Next() {
  91. bgm := &model.ArchiveIncome{}
  92. err = rows.Scan(&bgm.ArchiveID, &bgm.Income, &bgm.Date)
  93. if err != nil {
  94. log.Error("ListBgmIncomeByID rows.Scan error(%v)", err)
  95. return
  96. }
  97. bgms = append(bgms, bgm)
  98. }
  99. err = rows.Err()
  100. return
  101. }