code.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package service
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "fmt"
  7. "math/rand"
  8. "time"
  9. "go-common/app/admin/main/coupon/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. )
  13. // CodePage code page.
  14. func (s *Service) CodePage(c context.Context, a *model.ArgCouponCode) (res *model.CodePage, err error) {
  15. res = new(model.CodePage)
  16. var (
  17. count int64
  18. b *model.CouponBatchInfo
  19. now = time.Now().Unix()
  20. )
  21. if count, err = s.dao.CountCode(c, a); err != nil {
  22. return
  23. }
  24. if count <= 0 {
  25. return
  26. }
  27. if res.CodeList, err = s.dao.CodeList(c, a); err != nil {
  28. return
  29. }
  30. for _, v := range res.CodeList {
  31. if b, err = s.BatchInfo(c, v.BatchToken); err != nil {
  32. return
  33. }
  34. if b.ExpireDay == -1 && b.ExpireTime <= now {
  35. v.State = model.CodeStateExpire
  36. }
  37. }
  38. return
  39. }
  40. // CodeBlock code block.
  41. func (s *Service) CodeBlock(c context.Context, a *model.ArgCouponCode) (err error) {
  42. var code *model.CouponCode
  43. if code, err = s.dao.CodeByID(c, a.ID); err != nil {
  44. return
  45. }
  46. if code == nil || code.State != model.CodeStateNotUse {
  47. return ecode.RequestErr
  48. }
  49. return s.dao.UpdateCodeBlock(c, &model.CouponCode{ID: a.ID, State: model.CodeStateBlock, Ver: code.Ver})
  50. }
  51. // CodeUnBlock code un block.
  52. func (s *Service) CodeUnBlock(c context.Context, a *model.ArgCouponCode) (err error) {
  53. var code *model.CouponCode
  54. if code, err = s.dao.CodeByID(c, a.ID); err != nil {
  55. return
  56. }
  57. if code == nil || code.State != model.CodeStateBlock {
  58. return ecode.RequestErr
  59. }
  60. return s.dao.UpdateCodeBlock(c, &model.CouponCode{ID: a.ID, State: model.CodeStateNotUse, Ver: code.Ver})
  61. }
  62. // ExportCode export code.
  63. func (s *Service) ExportCode(c context.Context, a *model.ArgCouponCode) (res []string, err error) {
  64. if a.BatchToken == "" {
  65. return nil, ecode.RequestErr
  66. }
  67. var cs []*model.CouponCode
  68. a.Ps = 1000
  69. a.Pn = 1
  70. for {
  71. if cs, err = s.dao.CodeList(context.Background(), a); err != nil {
  72. return
  73. }
  74. if len(cs) == 0 {
  75. break
  76. }
  77. for _, v := range cs {
  78. res = append(res, v.Code)
  79. }
  80. a.Pn++
  81. }
  82. return
  83. }
  84. // InitCodes init codes
  85. func (s *Service) InitCodes(c context.Context, token string) (err error) {
  86. var info *model.CouponBatchInfo
  87. if info, err = s.dao.BatchInfo(c, token); err != nil {
  88. return
  89. }
  90. if info == nil {
  91. return
  92. }
  93. if info.MaxCount == 0 || info.MaxCount > model.BatchCodeMaxCount {
  94. return
  95. }
  96. // init code.
  97. go func() {
  98. log.Info("init code start arg[%s,%d]", token, info.MaxCount)
  99. cs := []*model.CouponCode{}
  100. for i := int64(0); i < info.MaxCount; i++ {
  101. cs = append(cs, &model.CouponCode{
  102. BatchToken: token,
  103. State: model.CodeStateNotUse,
  104. Code: codeToken(i),
  105. CouponType: model.CouponAllowance,
  106. })
  107. if len(cs) == int(info.MaxCount) || len(cs)%model.BatchAddCodeSlice == 0 {
  108. if err = s.dao.BatchAddCode(context.Background(), cs); err != nil {
  109. log.Error("init code error[%s,%v]", token, err)
  110. return
  111. }
  112. log.Info("init code ing arg[%s,%d,%d]", token, info.MaxCount, i)
  113. time.Sleep(50 * time.Millisecond)
  114. cs = []*model.CouponCode{}
  115. }
  116. }
  117. log.Info("init code end arg[%s,%d]", token, info.MaxCount)
  118. }()
  119. return
  120. }
  121. func codeToken(i int64) string {
  122. hash := md5.New()
  123. unix := time.Now().UnixNano()
  124. key := fmt.Sprintf("%v,%v,%v", unix, i, rand.Intn(100000000))
  125. hash.Write([]byte(key))
  126. sum := hash.Sum(nil)
  127. code := hex.EncodeToString(sum)
  128. return code[12:24]
  129. }