coupon.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "runtime/debug"
  7. "time"
  8. "go-common/app/admin/main/coupon/model"
  9. col "go-common/app/service/main/coupon/model"
  10. coumol "go-common/app/service/main/coupon/model"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. "go-common/library/net/metadata"
  14. xtime "go-common/library/time"
  15. "go-common/library/xstr"
  16. "github.com/pkg/errors"
  17. )
  18. // AddBatchInfo add batch info.
  19. func (s *Service) AddBatchInfo(c context.Context, b *model.CouponBatchInfo) (err error) {
  20. if b.StartTime >= b.ExpireTime {
  21. err = ecode.CouPonBatchTimeErr
  22. return
  23. }
  24. b.BatchToken = s.token()
  25. b.Ctime = xtime.Time(time.Now().Unix())
  26. if _, err = s.dao.AddBatchInfo(c, b); err != nil {
  27. err = errors.WithStack(err)
  28. }
  29. return
  30. }
  31. // BatchList batch list.
  32. func (s *Service) BatchList(c context.Context, arg *model.ArgBatchList) (res []*model.CouponBatchResp, err error) {
  33. var bs []*model.CouponBatchInfo
  34. if bs, err = s.dao.BatchList(c, arg.AppID, arg.Type); err != nil {
  35. err = errors.WithStack(err)
  36. return
  37. }
  38. for _, v := range bs {
  39. r := new(model.CouponBatchResp)
  40. r.ID = v.ID
  41. r.AppID = v.AppID
  42. r.AppName = s.allAppInfo[v.AppID]
  43. r.Name = v.Name
  44. r.BatchToken = v.BatchToken
  45. r.MaxCount = v.MaxCount
  46. r.CurrentCount = v.CurrentCount
  47. r.StartTime = v.StartTime
  48. r.ExpireTime = v.ExpireTime
  49. r.ExpireDay = v.ExpireDay
  50. r.Operator = v.Operator
  51. r.LimitCount = v.LimitCount
  52. r.UseLimitExplain = model.NoLimitExplain
  53. r.Amount = v.Amount
  54. r.FullAmount = v.FullAmount
  55. if r.PlatfromLimit, err = xstr.SplitInts(v.PlatformLimit); err != nil {
  56. log.Error("xstr.SplitInts() err[%+v] ", v.PlatformLimit, err)
  57. err = nil
  58. }
  59. if r.PlatfromLimit == nil {
  60. r.PlatfromLimit = []int64{}
  61. }
  62. r.ProdLimExplainFmt(v.ProdLimMonth, v.ProdLimRenewal) //ProductLimitExplain
  63. r.ProdLimMonth = v.ProdLimMonth
  64. r.ProdLimRenewal = v.ProdLimRenewal
  65. r.State = batchState(v)
  66. res = append(res, r)
  67. }
  68. return
  69. }
  70. func batchState(v *model.CouponBatchInfo) (state int8) {
  71. state = v.State
  72. now := time.Now().Unix()
  73. if v.ExpireDay == -1 {
  74. if v.ExpireTime <= now {
  75. state = model.CodeBatchExpire
  76. }
  77. }
  78. return
  79. }
  80. // get coupon token
  81. func (s *Service) token() string {
  82. var b bytes.Buffer
  83. b.WriteString(fmt.Sprintf("%07d", s.r.Int63n(9999999)))
  84. b.WriteString(fmt.Sprintf("%03d", time.Now().UnixNano()/1e6%1000))
  85. b.WriteString(time.Now().Format("20060102150405"))
  86. return b.String()
  87. }
  88. // AllAppInfo app app info.
  89. func (s *Service) AllAppInfo(c context.Context) map[int64]string {
  90. return s.allAppInfo
  91. }
  92. // SalaryCoupon salary coupon.
  93. func (s *Service) SalaryCoupon(c context.Context, mid int64, ct int64, count int, token string) (err error) {
  94. arg := new(coumol.ArgSalaryCoupon)
  95. arg.Count = count
  96. arg.CouponType = ct
  97. arg.Mid = mid
  98. arg.Origin = model.SystemAdminSalary
  99. arg.BatchToken = token
  100. if err = s.couRPC.SalaryCoupon(c, arg); err != nil {
  101. err = errors.WithStack(err)
  102. }
  103. return
  104. }
  105. //RunSalaryCoupon run salary coupon.
  106. func (s *Service) RunSalaryCoupon(c context.Context, mids []int64, token string, appID int64, couponType int64, origin int64, mt string) {
  107. go func() {
  108. var (
  109. err error
  110. msgmids = []int64{}
  111. )
  112. defer func() {
  113. if x := recover(); x != nil {
  114. log.Error("RunSalaryCoupon.GoRun arg[%s] panic[%+v]", token, x)
  115. log.Error("%s", debug.Stack())
  116. }
  117. }()
  118. for _, v := range mids {
  119. for i := 0; i < _maxretry; i++ {
  120. if err = s.couRPC.SalaryCoupon(context.Background(), &col.ArgSalaryCoupon{
  121. Mid: v,
  122. CouponType: couponType,
  123. Origin: origin,
  124. Count: 1,
  125. BatchToken: token,
  126. AppID: appID,
  127. }); err != nil {
  128. time.Sleep(200 * time.Millisecond)
  129. continue
  130. }
  131. break
  132. }
  133. if err != nil {
  134. log.Error("RunSalaryCoupon faild arg[%s,%d] err[%+v] ", token, v, err)
  135. continue
  136. }
  137. time.Sleep(10 * time.Millisecond)
  138. log.Info("RunSalaryCoupon suc arg[%s,%d]", token, v)
  139. msgmids = append(msgmids, v)
  140. }
  141. if len(mt) > 0 && s.c.Prop.SalaryNormalMsgOpen && len(msgmids) > 0 {
  142. if cerr := s.msgchan.Do(c, func(c context.Context) {
  143. s.sendMsg(msgmids, metadata.String(c, metadata.RemoteIP), 1, true, mt)
  144. }); cerr != nil {
  145. log.Error("s.sendMsg err(%+v)", cerr)
  146. }
  147. }
  148. }()
  149. }