kfc.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package kfc
  2. import (
  3. "context"
  4. "database/sql"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/activity/model/kfc"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/ecode"
  10. "go-common/library/net/metadata"
  11. "github.com/pkg/errors"
  12. )
  13. // KfcCodeUsed kfcDao const.
  14. var (
  15. KfcCodeUsed = 1
  16. KfcCodeNotGiveOut = 0
  17. _kfcCouponSQL = "select `id`,`mid`,`ctime`,`mtime`,`coupon_code`,`desc`,`state`,`delete_time` from bnj_kfc_coupon where id = ?"
  18. _kfcCodeSQL = "select `id`,`mid`,`ctime`,`mtime`,`coupon_code`,`desc`,`state`,`delete_time` from bnj_kfc_coupon where coupon_code = ?"
  19. _kfcCodeGiveOuteSQL = "update `bnj_kfc_coupon` set `state` = ? where `id` = ? and `state` = ?"
  20. _kfcDeliverSQL = "update `bnj_kfc_coupon` set mid = ? where id = ? and mid = 0"
  21. )
  22. // RawKfcCoupon get coupon .
  23. func (d *Dao) RawKfcCoupon(c context.Context, id int64) (res *kfc.BnjKfcCoupon, err error) {
  24. res = &kfc.BnjKfcCoupon{}
  25. row := d.db.QueryRow(c, _kfcCouponSQL, id)
  26. if err = row.Scan(&res.ID, &res.Mid, &res.Ctime, &res.Mtime, &res.CouponCode, &res.Desc, &res.State, &res.DeleteTime); err != nil {
  27. if err == xsql.ErrNoRows {
  28. err = nil
  29. } else {
  30. err = errors.Wrap(err, "RawKfcCoupon:row.Scan()")
  31. }
  32. }
  33. return
  34. }
  35. // RawKfcCode .
  36. func (d *Dao) RawKfcCode(c context.Context, code string) (res *kfc.BnjKfcCoupon, err error) {
  37. res = &kfc.BnjKfcCoupon{}
  38. row := d.db.QueryRow(c, _kfcCodeSQL, code)
  39. if err = row.Scan(&res.ID, &res.Mid, &res.Ctime, &res.Mtime, &res.CouponCode, &res.Desc, &res.State, &res.DeleteTime); err != nil {
  40. if err == xsql.ErrNoRows {
  41. err = nil
  42. } else {
  43. err = errors.Wrap(err, "RawKfcCode:row.Scan()")
  44. }
  45. }
  46. return
  47. }
  48. // KfcCodeGiveOut .
  49. func (d *Dao) KfcCodeGiveOut(c context.Context, id int64) (res int64, err error) {
  50. var (
  51. sqlRes sql.Result
  52. )
  53. if sqlRes, err = d.db.Exec(c, _kfcCodeGiveOuteSQL, KfcCodeUsed, id, KfcCodeNotGiveOut); err != nil {
  54. err = errors.Wrap(err, "d.db.Exec()")
  55. return
  56. }
  57. return sqlRes.RowsAffected()
  58. }
  59. // KfcDeliver .
  60. func (d *Dao) KfcDeliver(c context.Context, id, mid int64) (res int64, err error) {
  61. var (
  62. sqlRes sql.Result
  63. )
  64. if sqlRes, err = d.db.Exec(c, _kfcDeliverSQL, mid, id); err != nil {
  65. err = errors.Wrap(err, "d.db.Exec()")
  66. return
  67. }
  68. return sqlRes.RowsAffected()
  69. }
  70. // KfcWinner .
  71. func (d *Dao) KfcWinner(c context.Context, id int64) (uid int64, err error) {
  72. params := url.Values{}
  73. params.Set("id", strconv.FormatInt(id, 10))
  74. var res struct {
  75. Code int `json:"code"`
  76. Data struct {
  77. UID int64 `json:"uid"`
  78. } `json:"data"`
  79. }
  80. if err = d.client.Get(c, d.kfcWinnerURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  81. err = errors.Wrapf(err, "d.client.Get(%s)", d.kfcWinnerURL+"?"+params.Encode())
  82. return
  83. }
  84. if res.Code != ecode.OK.Code() {
  85. err = ecode.Int(res.Code)
  86. return
  87. }
  88. uid = res.Data.UID
  89. return
  90. }