tips.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "unicode/utf8"
  6. "go-common/app/admin/main/vip/model"
  7. "go-common/library/ecode"
  8. xtime "go-common/library/time"
  9. "github.com/pkg/errors"
  10. )
  11. // TipList tip list.
  12. func (s *Service) TipList(c context.Context, platform int8, state int8, position int8) (ts []*model.Tips, err error) {
  13. var now = time.Now().Unix()
  14. if ts, err = s.dao.TipList(c, platform, state, now, position); err != nil {
  15. err = errors.WithStack(err)
  16. }
  17. for _, v := range ts {
  18. v.TipState(v.StartTime, v.EndTime, now)
  19. }
  20. return
  21. }
  22. // TipByID tip by id.
  23. func (s *Service) TipByID(c context.Context, id int64) (t *model.Tips, err error) {
  24. if t, err = s.dao.TipByID(c, id); err != nil {
  25. err = errors.WithStack(err)
  26. }
  27. if t == nil {
  28. err = ecode.VipTipNotFoundErr
  29. return
  30. }
  31. t.TipState(t.StartTime, t.EndTime, time.Now().Unix())
  32. return
  33. }
  34. // AddTip add tip.
  35. func (s *Service) AddTip(c context.Context, t *model.Tips) (err error) {
  36. if t.StartTime >= t.EndTime {
  37. err = ecode.RequestErr
  38. return
  39. }
  40. if utf8.RuneCountInString(t.Tip) > _maxTipLen {
  41. err = ecode.VipTipTooLoogErr
  42. return
  43. }
  44. t.Ctime = xtime.Time(time.Now().Unix())
  45. if _, err = s.dao.AddTip(c, t); err != nil {
  46. err = errors.WithStack(err)
  47. }
  48. return
  49. }
  50. // TipUpdate update tip.
  51. func (s *Service) TipUpdate(c context.Context, t *model.Tips) (err error) {
  52. var (
  53. old *model.Tips
  54. now = time.Now().Unix()
  55. )
  56. if t.ID == 0 {
  57. err = ecode.RequestErr
  58. return
  59. }
  60. if old, err = s.TipByID(c, t.ID); err != nil {
  61. err = errors.WithStack(err)
  62. return
  63. }
  64. if t.StartTime >= t.EndTime {
  65. err = ecode.VipTipTimeErr
  66. return
  67. }
  68. if utf8.RuneCountInString(t.Tip) > _maxTipLen {
  69. err = ecode.VipTipTooLoogErr
  70. return
  71. }
  72. if old.StartTime != t.StartTime && old.StartTime < now {
  73. err = ecode.VipTipStartTimeCatNotModifyErr
  74. return
  75. }
  76. if old.EndTime != t.EndTime && old.EndTime < now {
  77. err = ecode.VipTipEndTimeCatNotModifyErr
  78. return
  79. }
  80. if _, err = s.dao.TipUpdate(c, t); err != nil {
  81. err = errors.WithStack(err)
  82. }
  83. return
  84. }
  85. // DeleteTip delete tip.
  86. func (s *Service) DeleteTip(c context.Context, id int64, operator string) (err error) {
  87. var (
  88. old *model.Tips
  89. now = time.Now().Unix()
  90. )
  91. if old, err = s.TipByID(c, id); err != nil {
  92. err = errors.WithStack(err)
  93. return
  94. }
  95. if old.StartTime <= now {
  96. err = ecode.VipTipCatNotDeleteErr
  97. return
  98. }
  99. if _, err = s.dao.DeleteTip(c, id, model.Delete, operator); err != nil {
  100. err = errors.WithStack(err)
  101. }
  102. return
  103. }
  104. // ExpireTip expire tip.
  105. func (s *Service) ExpireTip(c context.Context, id int64, operator string) (err error) {
  106. var (
  107. old *model.Tips
  108. now = time.Now().Unix()
  109. )
  110. if old, err = s.TipByID(c, id); err != nil {
  111. err = errors.WithStack(err)
  112. return
  113. }
  114. if old.StartTime > now || old.EndTime < now {
  115. err = ecode.VipTipCatNotExpireErr
  116. return
  117. }
  118. if _, err = s.dao.ExpireTip(c, id, operator, now); err != nil {
  119. err = errors.WithStack(err)
  120. }
  121. return
  122. }