kfc.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package kfc
  2. import (
  3. "context"
  4. "go-common/app/interface/main/activity/conf"
  5. "go-common/app/interface/main/activity/dao/kfc"
  6. kfcmdl "go-common/app/interface/main/activity/model/kfc"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/sync/pipeline/fanout"
  10. )
  11. // Service struct
  12. type Service struct {
  13. c *conf.Config
  14. dao *kfc.Dao
  15. cache *fanout.Fanout
  16. }
  17. // Close service
  18. func (s *Service) Close() {
  19. s.dao.Close()
  20. s.cache.Close()
  21. }
  22. // New Service
  23. func New(c *conf.Config) (s *Service) {
  24. s = &Service{
  25. c: c,
  26. dao: kfc.New(c),
  27. cache: fanout.New("cache", fanout.Worker(5), fanout.Buffer(10240)),
  28. }
  29. return
  30. }
  31. // KfcInfo .
  32. func (s *Service) KfcInfo(c context.Context, id, mid int64) (kfcInfo *kfcmdl.BnjKfcCoupon, err error) {
  33. var (
  34. res *kfcmdl.BnjKfcCoupon
  35. )
  36. if res, err = s.dao.KfcCoupon(c, id); err != nil {
  37. log.Error("s.dao.KfcCoupon(%d) error(%+v)", id, err)
  38. return
  39. }
  40. if res.ID == 0 {
  41. err = ecode.NothingFound
  42. return
  43. }
  44. if res.Mid == 0 {
  45. var uid int64
  46. if uid, err = s.kfcRecall(c, id); err == nil && uid > 0 {
  47. if uid == mid {
  48. res.Mid = uid
  49. }
  50. }
  51. }
  52. if res.Mid != 0 && res.Mid == mid {
  53. kfcInfo = res
  54. } else {
  55. err = ecode.NothingFound
  56. }
  57. return
  58. }
  59. // kfcRecall .
  60. func (s *Service) kfcRecall(c context.Context, id int64) (uid int64, err error) {
  61. if uid, err = s.dao.KfcWinner(c, id); err != nil {
  62. log.Error("s.dao.KfcWinner(%d) error(%+v)", id, err)
  63. return
  64. }
  65. if uid > 0 {
  66. s.DeliverKfc(c, id, uid)
  67. }
  68. return
  69. }
  70. // KfcUse .
  71. func (s *Service) KfcUse(c context.Context, code string) (kfcID int64, err error) {
  72. var (
  73. kfcInfo *kfcmdl.BnjKfcCoupon
  74. effectRows int64
  75. )
  76. if kfcID, err = s.dao.CacheKfcCode(c, code); err != nil {
  77. log.Error("s.dao.CacheKfcCode(%s) error(%+v)", code, err)
  78. return
  79. }
  80. if kfcID > 0 {
  81. err = ecode.ActivityKfcHasUsed
  82. return
  83. }
  84. if kfcInfo, err = s.dao.RawKfcCode(c, code); err != nil {
  85. log.Error("s.dao.RawKfcCode(%s) error(%+v)", code, err)
  86. return
  87. }
  88. if kfcInfo.ID == 0 {
  89. err = ecode.ActivityKfcNotExist
  90. return
  91. }
  92. if kfcInfo.Mid == 0 {
  93. err = ecode.ActivityKfcNotGiveOut
  94. return
  95. }
  96. if kfcInfo.State == int64(kfc.KfcCodeUsed) {
  97. s.cache.Do(c, func(c context.Context) {
  98. s.dao.AddCacheKfcCode(c, code, kfcInfo.ID)
  99. })
  100. err = ecode.ActivityKfcHasUsed
  101. return
  102. }
  103. if effectRows, err = s.dao.KfcCodeGiveOut(c, kfcInfo.ID); err != nil {
  104. log.Error("s.dao.KfcCodeGiveOut(%d) error(%+v)", kfcInfo.ID, err)
  105. return
  106. }
  107. if effectRows == 0 {
  108. err = ecode.ActivityKfcSqlError
  109. }
  110. kfcID = kfcInfo.ID
  111. s.cache.Do(c, func(c context.Context) {
  112. s.dao.AddCacheKfcCode(c, code, kfcInfo.ID)
  113. s.dao.DelCacheKfcCoupon(c, kfcInfo.ID)
  114. })
  115. return
  116. }
  117. // DeliverKfc .
  118. func (s *Service) DeliverKfc(c context.Context, id, mid int64) (err error) {
  119. effectID, err := s.dao.KfcDeliver(c, id, mid)
  120. if err != nil {
  121. log.Error("s.dao.KfcDeliver(%d,%d) error(%+v)", id, mid, err)
  122. return
  123. }
  124. if effectID > 0 {
  125. s.cache.Do(c, func(c context.Context) {
  126. if e := s.dao.DelCacheKfcCoupon(c, id); e == nil {
  127. s.dao.KfcCoupon(c, id)
  128. }
  129. })
  130. } else {
  131. err = ecode.ActivityKfcSqlError
  132. log.Error("DeliverKfc mysql effect o rows (%d,%d)", id, mid)
  133. }
  134. return
  135. }