mysql_advance.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/dm2/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. // advance comment
  11. _addAdvSQL = "INSERT INTO dm_advancecomment (dm_inid,owner,mid,type,timestamp,mode,refund) VALUES (?,?,?,?,?,?,?)"
  12. _selAdvSQL = "SELECT buy_id,owner,dm_inid,type,mode,mid,timestamp,refund FROM dm_advancecomment WHERE owner=? AND buy_id=?"
  13. _selAdvsSQL = "SELECT buy_id,owner,dm_inid,type,mode,mid,timestamp,refund FROM dm_advancecomment WHERE owner=? ORDER BY buy_id DESC limit 100"
  14. _selAdvModeSQL = "SELECT type FROM dm_advancecomment WHERE dm_inid=? AND mid=? AND mode=?"
  15. _upAdvTypeSQL = "UPDATE dm_advancecomment SET type=? WHERE buy_id=?"
  16. _delAdvSQL = "DELETE FROM dm_advancecomment WHERE buy_id=?"
  17. _selAdvanceCmt = "SELECT buy_id,owner,dm_inid,type,mode,mid,timestamp,refund FROM dm_advancecomment WHERE dm_inid=? AND mid=? AND mode=?"
  18. )
  19. // AdvanceType get advance type by cid,mid and mode.
  20. func (d *Dao) AdvanceType(c context.Context, cid int64, mid int64, mode string) (typ string, err error) {
  21. row := d.dbDM.QueryRow(c, _selAdvModeSQL, cid, mid, mode)
  22. if err = row.Scan(&typ); err != nil {
  23. if err == sql.ErrNoRows {
  24. err = nil
  25. } else {
  26. log.Error("row.Scan error(%v)", err)
  27. }
  28. }
  29. return
  30. }
  31. // Advance 获取购买高级弹幕功能状态
  32. func (d *Dao) Advance(c context.Context, mid, id int64) (adv *model.Advance, err error) {
  33. row := d.dbDM.QueryRow(c, _selAdvSQL, mid, id)
  34. adv = &model.Advance{}
  35. if err = row.Scan(&adv.ID, &adv.Owner, &adv.Cid, &adv.Type, &adv.Mode, &adv.Mid, &adv.Timestamp, &adv.Refund); err != nil {
  36. if err == sql.ErrNoRows {
  37. err = nil
  38. adv = nil
  39. } else {
  40. log.Error("row.Scan error(%v)", err)
  41. }
  42. }
  43. return
  44. }
  45. // Advances 获取高级弹幕申请列表
  46. func (d *Dao) Advances(c context.Context, owner int64) (res []*model.Advance, err error) {
  47. rows, err := d.dbDM.Query(c, _selAdvsSQL, owner)
  48. if err != nil {
  49. log.Error("d.dbDM.Query(%s,%d) error(%v)", _selAdvsSQL, owner, err)
  50. return
  51. }
  52. defer rows.Close()
  53. for rows.Next() {
  54. adv := &model.Advance{}
  55. if err = rows.Scan(&adv.ID, &adv.Owner, &adv.Cid, &adv.Type, &adv.Mode, &adv.Mid, &adv.Timestamp, &adv.Refund); err != nil {
  56. log.Error("rows.Scan error(%v)", err)
  57. return
  58. }
  59. res = append(res, adv)
  60. }
  61. if err = rows.Err(); err != nil {
  62. log.Error("rows.Err() error(%v)", err)
  63. }
  64. return
  65. }
  66. // BuyAdvance 购买高级弹幕功能
  67. func (d *Dao) BuyAdvance(c context.Context, mid, cid, owner, refund int64, typ, mode string) (id int64, err error) {
  68. now := time.Now().Unix()
  69. res, err := d.dbDM.Exec(c, _addAdvSQL, cid, owner, mid, typ, now, mode, refund)
  70. if err != nil {
  71. log.Error("d.dbDM.Exec(cid:%d,mid:%d,type:%s) error(%v)", cid, mid, typ, err)
  72. return
  73. }
  74. return res.LastInsertId()
  75. }
  76. // UpdateAdvType 更新购买高级弹幕类型
  77. func (d *Dao) UpdateAdvType(c context.Context, id int64, typ string) (affect int64, err error) {
  78. res, err := d.dbDM.Exec(c, _upAdvTypeSQL, typ, id)
  79. if err != nil {
  80. log.Error("d.dbDM.Exec(%s,%s,%d) error(%v)", _upAdvTypeSQL, typ, id, err)
  81. return
  82. }
  83. return res.RowsAffected()
  84. }
  85. // DelAdvance 删除购买高级弹幕记录
  86. func (d *Dao) DelAdvance(c context.Context, id int64) (affect int64, err error) {
  87. res, err := d.dbDM.Exec(c, _delAdvSQL, id)
  88. if err != nil {
  89. log.Error("d.dbDM.Exec(%s,%d) error(%v)", _delAdvSQL, id, err)
  90. return
  91. }
  92. return res.RowsAffected()
  93. }
  94. // AdvanceCmt get advance comment.
  95. func (d *Dao) AdvanceCmt(c context.Context, oid, mid int64, mode string) (adv *model.AdvanceCmt, err error) {
  96. adv = &model.AdvanceCmt{}
  97. row := d.dbDM.QueryRow(c, _selAdvanceCmt, oid, mid, mode)
  98. if err = row.Scan(&adv.ID, &adv.Owner, &adv.Oid, &adv.Type, &adv.Mode, &adv.Mid, &adv.Timestamp, &adv.Refund); err != nil {
  99. if err == sql.ErrNoRows {
  100. adv = nil
  101. err = nil
  102. } else {
  103. log.Error("row.Scan() error(%v)", err)
  104. }
  105. }
  106. return
  107. }