code.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "go-common/app/admin/main/coupon/model"
  7. "go-common/library/database/sql"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _codeCountSQL = "SELECT COUNT(1) FROM bilibili_coupon.coupon_code WHERE 1=1 %s"
  12. _codeListSQL = "SELECT id,batch_token,state,code,mid,coupon_token,coupon_type,ver,ctime,mtime FROM coupon_code WHERE 1=1 %s %s"
  13. _codeBlockSQL = "UPDATE coupon_code SET state = ?, ver = ver +1 WHERE id = ? AND ver =?;"
  14. _codeByIDSQL = "SELECT id,batch_token,state,code,mid,coupon_token,coupon_type,ver,ctime,mtime FROM coupon_code WHERE id = ?"
  15. _batchAddCodeSQL = "INSERT IGNORE INTO coupon_code(batch_token,state,code,coupon_type)VALUES "
  16. )
  17. // CountCode coupon code count.
  18. func (d *Dao) CountCode(c context.Context, a *model.ArgCouponCode) (count int64, err error) {
  19. sql := fmt.Sprintf(_codeCountSQL, whereSQL(a))
  20. if err = d.db.QueryRow(c, sql).Scan(&count); err != nil {
  21. err = errors.Wrapf(err, "dao code list")
  22. }
  23. return
  24. }
  25. // CodeList code list.
  26. func (d *Dao) CodeList(c context.Context, a *model.ArgCouponCode) (res []*model.CouponCode, err error) {
  27. listSQL := fmt.Sprintf(_codeListSQL, whereSQL(a), pageSQL(a.Pn, a.Ps))
  28. var rows *sql.Rows
  29. if rows, err = d.db.Query(c, listSQL); err != nil {
  30. err = errors.WithStack(err)
  31. return
  32. }
  33. defer rows.Close()
  34. for rows.Next() {
  35. r := new(model.CouponCode)
  36. if err = rows.Scan(&r.ID, &r.BatchToken, &r.State, &r.Code, &r.Mid, &r.CouponToken, &r.CouponType, &r.Ver, &r.Ctime, &r.Mtime); err != nil {
  37. err = errors.WithStack(err)
  38. res = nil
  39. return
  40. }
  41. res = append(res, r)
  42. }
  43. err = rows.Err()
  44. return
  45. }
  46. // UpdateCodeBlock update code block.
  47. func (d *Dao) UpdateCodeBlock(c context.Context, a *model.CouponCode) (err error) {
  48. if _, err = d.db.Exec(c, _codeBlockSQL, a.State, a.ID, a.Ver); err != nil {
  49. err = errors.Wrapf(err, "dao update code block(%+v)", a)
  50. }
  51. return
  52. }
  53. // CodeByID code by id.
  54. func (d *Dao) CodeByID(c context.Context, id int64) (r *model.CouponCode, err error) {
  55. r = new(model.CouponCode)
  56. if err = d.db.QueryRow(c, _codeByIDSQL, id).
  57. Scan(&r.ID, &r.BatchToken, &r.State, &r.Code, &r.Mid, &r.CouponToken, &r.CouponType, &r.Ver, &r.Ctime, &r.Mtime); err != nil {
  58. if err == sql.ErrNoRows {
  59. r = nil
  60. err = nil
  61. return
  62. }
  63. err = errors.Wrapf(err, "dao query code by id")
  64. }
  65. return
  66. }
  67. // BatchAddCode batch add code.
  68. func (d *Dao) BatchAddCode(c context.Context, cs []*model.CouponCode) (err error) {
  69. var (
  70. buf bytes.Buffer
  71. sql string
  72. )
  73. buf.WriteString(_batchAddCodeSQL)
  74. for _, v := range cs {
  75. buf.WriteString("('")
  76. buf.WriteString(v.BatchToken)
  77. buf.WriteString("',")
  78. buf.WriteString(fmt.Sprintf("%d", v.State))
  79. buf.WriteString(",'")
  80. buf.WriteString(v.Code)
  81. buf.WriteString("',")
  82. buf.WriteString(fmt.Sprintf("%d", v.CouponType))
  83. buf.WriteString("),")
  84. }
  85. sql = buf.String()
  86. if _, err = d.db.Exec(c, sql[0:len(sql)-1]); err != nil {
  87. err = errors.Wrapf(err, "dao insert codes")
  88. }
  89. return
  90. }
  91. func whereSQL(a *model.ArgCouponCode) (sql string) {
  92. if a == nil {
  93. return
  94. }
  95. if a.Mid > 0 {
  96. sql += " AND mid = " + fmt.Sprintf("%d", a.Mid)
  97. }
  98. if a.Code != "" {
  99. sql += " AND code = '" + a.Code + "'"
  100. }
  101. if a.BatchToken != "" {
  102. sql += " AND batch_token = '" + a.BatchToken + "'"
  103. }
  104. return sql
  105. }
  106. func pageSQL(pn, ps int) (sql string) {
  107. if pn <= 0 {
  108. pn = 1
  109. }
  110. if ps <= 0 {
  111. ps = 20
  112. }
  113. return " ORDER BY ID DESC LIMIT " + fmt.Sprintf("%d,%d", (pn-1)*ps, ps)
  114. }