bgm.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package charge
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. model "go-common/app/job/main/growup/model/charge"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _getBGMSQL = "SELECT id,mid,sid,aid,cid,join_at,title FROM background_music WHERE id > ? ORDER BY id LIMIT ?"
  11. _bgmChargeSQL = "SELECT id,mid,sid,aid,cid,join_at,inc_charge,date FROM %s WHERE id > ? AND date = ? AND inc_charge > 0 ORDER BY id LIMIT ?"
  12. _bgmStatisSQL = "SELECT id,mid,sid,aid,cid,total_charge FROM bgm_charge_statis WHERE id > ? ORDER BY id LIMIT ?"
  13. _inBgmChargeTableSQL = "INSERT INTO %s(sid,aid,mid,cid,title,inc_charge,date,join_at) VALUES %s ON DUPLICATE KEY UPDATE inc_charge=VALUES(inc_charge)"
  14. _inBgmStatisSQL = "INSERT INTO bgm_charge_statis(sid,aid,mid,cid,title,total_charge,join_at) VALUES %s ON DUPLICATE KEY UPDATE total_charge=VALUES(total_charge)"
  15. )
  16. // GetBgm get bgms
  17. func (d *Dao) GetBgm(c context.Context, id int64, limit int64) (bs []*model.Bgm, last int64, err error) {
  18. rows, err := d.db.Query(c, _getBGMSQL, id, limit)
  19. if err != nil {
  20. return
  21. }
  22. defer rows.Close()
  23. for rows.Next() {
  24. b := &model.Bgm{}
  25. err = rows.Scan(&last, &b.MID, &b.SID, &b.AID, &b.CID, &b.JoinAt, &b.Title)
  26. if err != nil {
  27. return
  28. }
  29. bs = append(bs, b)
  30. }
  31. return
  32. }
  33. // BgmCharge get bgm charge by date
  34. func (d *Dao) BgmCharge(c context.Context, date time.Time, id int64, limit int, table string) (bgms []*model.BgmCharge, err error) {
  35. bgms = make([]*model.BgmCharge, 0)
  36. rows, err := d.db.Query(c, fmt.Sprintf(_bgmChargeSQL, table), id, date, limit)
  37. if err != nil {
  38. log.Error("BgmCharge d.db.Query error(%v)", err)
  39. return
  40. }
  41. defer rows.Close()
  42. for rows.Next() {
  43. bgm := &model.BgmCharge{}
  44. err = rows.Scan(&bgm.ID, &bgm.MID, &bgm.SID, &bgm.AID, &bgm.CID, &bgm.JoinAt, &bgm.IncCharge, &bgm.Date)
  45. if err != nil {
  46. log.Error("BgmCharge rows.Scan error(%v)", err)
  47. return
  48. }
  49. bgms = append(bgms, bgm)
  50. }
  51. return
  52. }
  53. // BgmStatis bgm statis
  54. func (d *Dao) BgmStatis(c context.Context, id int64, limit int) (bgms []*model.BgmStatis, err error) {
  55. bgms = make([]*model.BgmStatis, 0)
  56. rows, err := d.db.Query(c, _bgmStatisSQL, id, limit)
  57. if err != nil {
  58. log.Error("BgmStatis d.db.Query error(%v)", err)
  59. return
  60. }
  61. defer rows.Close()
  62. for rows.Next() {
  63. bgm := &model.BgmStatis{}
  64. err = rows.Scan(&bgm.ID, &bgm.MID, &bgm.SID, &bgm.AID, &bgm.CID, &bgm.TotalCharge)
  65. if err != nil {
  66. log.Error("BgmStatis rows.Scan error(%v)", err)
  67. return
  68. }
  69. bgms = append(bgms, bgm)
  70. }
  71. return
  72. }
  73. // InsertBgmChargeTable insert bgm charge
  74. func (d *Dao) InsertBgmChargeTable(c context.Context, vals, table string) (rows int64, err error) {
  75. if vals == "" {
  76. return
  77. }
  78. res, err := d.db.Exec(c, fmt.Sprintf(_inBgmChargeTableSQL, table, vals))
  79. if err != nil {
  80. log.Error("InsertBgmChargeTable(%s) tx.Exec error(%v)", table, err)
  81. return
  82. }
  83. return res.RowsAffected()
  84. }
  85. // InsertBgmStatisBatch insert bgm statis
  86. func (d *Dao) InsertBgmStatisBatch(c context.Context, vals string) (rows int64, err error) {
  87. if vals == "" {
  88. return
  89. }
  90. res, err := d.db.Exec(c, fmt.Sprintf(_inBgmStatisSQL, vals))
  91. if err != nil {
  92. log.Error("InsertBgmStatisBatch tx.Exec error(%v)", err)
  93. return
  94. }
  95. return res.RowsAffected()
  96. }