prize.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "go-common/app/service/main/coupon/model"
  6. "go-common/library/database/sql"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _userCardSQL = "SELECT card_type,state,batch_token,coupon_token,act_id FROM coupon_user_card WHERE mid=? AND act_id=? AND card_type=?"
  11. _userCardsSQL = "SELECT card_type,state,batch_token,coupon_token,act_id FROM coupon_user_card WHERE mid=? AND act_id=?"
  12. _addUserCardSQL = "INSERT INTO coupon_user_card(mid,card_type,state,batch_token,coupon_token,act_id) VALUES (?,?,?,?,?,?)"
  13. _updateUserCardSQL = "UPDATE coupon_user_card SET state=? WHERE mid=? AND coupon_token=?"
  14. )
  15. // UserCard .
  16. func (d *Dao) UserCard(c context.Context, mid, actID int64, cardType int8) (r *model.CouponUserCard, err error) {
  17. var row *sql.Row
  18. r = &model.CouponUserCard{}
  19. row = d.db.QueryRow(c, _userCardSQL, mid, actID, cardType)
  20. if err = row.Scan(&r.CardType, &r.State, &r.BatchToken, &r.CouponToken, &r.ActID); err != nil {
  21. if err == sql.ErrNoRows {
  22. err = nil
  23. r = nil
  24. return
  25. }
  26. err = errors.WithStack(err)
  27. return
  28. }
  29. return
  30. }
  31. // UserCards .
  32. func (d *Dao) UserCards(c context.Context, mid, actID int64) (res []*model.CouponUserCard, err error) {
  33. var rows *sql.Rows
  34. if rows, err = d.db.Query(c, _userCardsSQL, mid, actID); err != nil {
  35. err = errors.WithStack(err)
  36. return
  37. }
  38. defer rows.Close()
  39. for rows.Next() {
  40. r := &model.CouponUserCard{}
  41. if err = rows.Scan(&r.CardType, &r.State, &r.BatchToken, &r.CouponToken, &r.ActID); err != nil {
  42. err = errors.WithStack(err)
  43. res = nil
  44. return
  45. }
  46. res = append(res, r)
  47. }
  48. err = rows.Err()
  49. return
  50. }
  51. // AddUserCard .
  52. func (d *Dao) AddUserCard(c context.Context, tx *sql.Tx, uc *model.CouponUserCard) (a int64, err error) {
  53. var res xsql.Result
  54. if res, err = tx.Exec(_addUserCardSQL, uc.MID, uc.CardType, uc.State, uc.BatchToken, uc.CouponToken, uc.ActID); err != nil {
  55. err = errors.WithStack(err)
  56. return
  57. }
  58. if a, err = res.RowsAffected(); err != nil {
  59. err = errors.WithStack(err)
  60. return
  61. }
  62. return
  63. }
  64. // UpdateUserCard .
  65. func (d *Dao) UpdateUserCard(c context.Context, mid int64, state int8, couponToken string) (a int64, err error) {
  66. var res xsql.Result
  67. if res, err = d.db.Exec(c, _updateUserCardSQL, state, mid, couponToken); err != nil {
  68. err = errors.WithStack(err)
  69. return
  70. }
  71. if a, err = res.RowsAffected(); err != nil {
  72. err = errors.WithStack(err)
  73. return
  74. }
  75. return
  76. }