mysql.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/card/model"
  6. xsql "go-common/library/database/sql"
  7. "go-common/library/xstr"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _selUserEquip = "SELECT mid,card_id,expire_time FROM user_card_equip WHERE mid=? AND deleted = 0;"
  12. _selUserEquips = "SELECT mid,card_id,expire_time FROM user_card_equip WHERE mid IN (%s) AND deleted = 0;"
  13. _selEffectiveCard = "SELECT id,name,state,is_hot,card_url,big_crad_url,card_type,order_num,group_id,ctime,mtime FROM card_info WHERE state = 0 AND deleted = 0 ORDER BY order_num DESC;"
  14. _selEffectiveCardGroup = "SELECT id,name,state,ctime,mtime,order_num FROM card_group WHERE state = 0 AND deleted = 0;"
  15. _cardEquip = "INSERT INTO user_card_equip(mid,card_id,expire_time)VALUES(?,?,?) ON DUPLICATE KEY UPDATE card_id =?,expire_time=?,deleted=0;"
  16. _cardDemount = "UPDATE user_card_equip SET deleted = 1 WHERE mid = ?;"
  17. )
  18. // RawEquip get user equip info.
  19. func (d *Dao) RawEquip(c context.Context, mid int64) (r *model.UserEquip, err error) {
  20. r = new(model.UserEquip)
  21. row := d.db.QueryRow(c, _selUserEquip, mid)
  22. if err = row.Scan(&r.Mid, &r.CardID, &r.ExpireTime); err != nil {
  23. if err == xsql.ErrNoRows {
  24. err = nil
  25. r = nil
  26. return
  27. }
  28. r = nil
  29. err = errors.Wrapf(err, "dao equip mid(%d)", mid)
  30. }
  31. return
  32. }
  33. // RawEquips get user equip infos.
  34. func (d *Dao) RawEquips(c context.Context, mids []int64) (res map[int64]*model.UserEquip, err error) {
  35. var rows *xsql.Rows
  36. res = make(map[int64]*model.UserEquip, len(mids))
  37. midStr := xstr.JoinInts(mids)
  38. if rows, err = d.db.Query(c, fmt.Sprintf(_selUserEquips, midStr)); err != nil {
  39. err = errors.Wrapf(err, "dao equips mids(%s)", midStr)
  40. return
  41. }
  42. defer rows.Close()
  43. for rows.Next() {
  44. r := new(model.UserEquip)
  45. if err = rows.Scan(&r.Mid, &r.CardID, &r.ExpireTime); err != nil {
  46. err = errors.Wrapf(err, "dao equips scan mids(%s)", midStr)
  47. res = nil
  48. return
  49. }
  50. res[r.Mid] = r
  51. }
  52. err = rows.Err()
  53. return
  54. }
  55. // EffectiveCards query all effective cards .
  56. func (d *Dao) EffectiveCards(c context.Context) (res []*model.Card, err error) {
  57. var rows *xsql.Rows
  58. if rows, err = d.db.Query(c, _selEffectiveCard); err != nil {
  59. err = errors.WithStack(err)
  60. return
  61. }
  62. defer rows.Close()
  63. for rows.Next() {
  64. r := new(model.Card)
  65. if err = rows.Scan(&r.ID, &r.Name, &r.State, &r.IsHot, &r.CardURL, &r.BigCradURL, &r.CardType, &r.OrderNum,
  66. &r.GroupID, &r.Ctime, &r.Mtime); err != nil {
  67. err = errors.WithStack(err)
  68. res = nil
  69. return
  70. }
  71. res = append(res, r)
  72. }
  73. err = rows.Err()
  74. return
  75. }
  76. // EffectiveGroups query all effective groups .
  77. func (d *Dao) EffectiveGroups(c context.Context) (res []*model.CardGroup, err error) {
  78. var rows *xsql.Rows
  79. if rows, err = d.db.Query(c, _selEffectiveCardGroup); err != nil {
  80. err = errors.WithStack(err)
  81. return
  82. }
  83. defer rows.Close()
  84. for rows.Next() {
  85. r := new(model.CardGroup)
  86. if err = rows.Scan(&r.ID, &r.Name, &r.State, &r.Ctime, &r.Mtime, &r.OrderNum); err != nil {
  87. err = errors.WithStack(err)
  88. res = nil
  89. return
  90. }
  91. res = append(res, r)
  92. }
  93. err = rows.Err()
  94. return
  95. }
  96. // CardEquip card equip.
  97. func (d *Dao) CardEquip(c context.Context, e *model.UserEquip) (err error) {
  98. if _, err = d.db.Exec(c, _cardEquip, e.Mid, e.CardID, e.ExpireTime, e.CardID, e.ExpireTime); err != nil {
  99. err = errors.WithStack(err)
  100. return
  101. }
  102. return
  103. }
  104. // DeleteEquip delete card equip.
  105. func (d *Dao) DeleteEquip(c context.Context, mid int64) (err error) {
  106. if _, err = d.db.Exec(c, _cardDemount, mid); err != nil {
  107. err = errors.WithStack(err)
  108. return
  109. }
  110. return
  111. }