business.go 3.0 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/vip/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. "github.com/pkg/errors"
  13. )
  14. // BusinessList business list.
  15. func (s *Service) BusinessList(c context.Context, pn, ps, status int) (res []*model.VipBusinessInfo, count int64, err error) {
  16. if count, err = s.dao.BussinessCount(c, status); err != nil {
  17. log.Error("%+v", err)
  18. return
  19. }
  20. if count == 0 {
  21. return
  22. }
  23. if res, err = s.dao.BussinessList(c, pn, ps, status); err != nil {
  24. log.Error("%+v", err)
  25. return
  26. }
  27. return
  28. }
  29. // UpdateBusinessInfo update business info.
  30. func (s *Service) UpdateBusinessInfo(c context.Context, req *model.VipBusinessInfo) (err error) {
  31. var (
  32. business *model.VipBusinessInfo
  33. appkey string
  34. businessName string
  35. )
  36. if business, err = s.dao.SelBusiness(c, req.ID); err != nil {
  37. err = errors.WithStack(err)
  38. return
  39. }
  40. if business == nil {
  41. err = ecode.VipBusinessNotExitErr
  42. return
  43. }
  44. req.Secret = business.Secret
  45. appkey = business.AppKey
  46. businessName = business.BusinessName
  47. arg := new(model.QueryBusinessInfo)
  48. if appkey != req.AppKey {
  49. arg.Appkey = req.AppKey
  50. if business, err = s.dao.SelBusinessByQuery(c, arg); err != nil {
  51. err = errors.WithStack(err)
  52. return
  53. }
  54. if business != nil {
  55. err = ecode.VipAppkeyExitErr
  56. return
  57. }
  58. req.Secret = s.makeSecret(req.AppKey)
  59. }
  60. if businessName != req.BusinessName {
  61. arg = new(model.QueryBusinessInfo)
  62. arg.Name = req.BusinessName
  63. if business, err = s.dao.SelBusinessByQuery(c, arg); err != nil {
  64. err = errors.WithStack(err)
  65. return
  66. }
  67. if business != nil {
  68. err = ecode.VipBusinessNameExitErr
  69. return
  70. }
  71. }
  72. if _, err = s.dao.UpdateBusiness(c, req); err != nil {
  73. log.Error("%+v", err)
  74. return
  75. }
  76. return
  77. }
  78. // AddBusinessInfo add business info.
  79. func (s *Service) AddBusinessInfo(c context.Context, req *model.VipBusinessInfo) (err error) {
  80. var (
  81. business *model.VipBusinessInfo
  82. )
  83. arg := new(model.QueryBusinessInfo)
  84. arg.Appkey = req.AppKey
  85. if business, err = s.dao.SelBusinessByQuery(c, arg); err != nil {
  86. err = errors.WithStack(err)
  87. return
  88. }
  89. if business != nil {
  90. err = ecode.VipAppkeyExitErr
  91. return
  92. }
  93. arg = new(model.QueryBusinessInfo)
  94. arg.Name = req.BusinessName
  95. if business, err = s.dao.SelBusinessByQuery(c, arg); err != nil {
  96. err = errors.WithStack(err)
  97. return
  98. }
  99. if business != nil {
  100. err = ecode.VipBusinessNameExitErr
  101. return
  102. }
  103. req.Secret = s.makeSecret(req.AppKey)
  104. if _, err = s.dao.AddBusiness(c, req); err != nil {
  105. log.Error("%+v", err)
  106. return
  107. }
  108. return
  109. }
  110. func (s *Service) makeSecret(appkey string) string {
  111. key := fmt.Sprintf("%v,%v,%v", appkey, time.Now().UnixNano(), rand.Intn(100000000))
  112. hash := md5.New()
  113. hash.Write([]byte(key))
  114. sum := hash.Sum(nil)
  115. return hex.EncodeToString(sum)
  116. }
  117. // BusinessInfo business info
  118. func (s *Service) BusinessInfo(c context.Context, id int) (r *model.VipBusinessInfo, err error) {
  119. if r, err = s.dao.SelBusiness(c, id); err != nil {
  120. log.Error("%+v", err)
  121. return
  122. }
  123. return
  124. }