vip.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/admin/main/vip/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. xtime "go-common/library/time"
  10. "github.com/pkg/errors"
  11. )
  12. // Drawback Drawback
  13. func (s *Service) Drawback(c context.Context, days int, mid int64, usename, remark string) (err error) {
  14. var (
  15. userInfo *model.VipUserInfo
  16. history *model.VipChangeHistory
  17. ot time.Time
  18. tx *sql.Tx
  19. affected int64
  20. )
  21. if userInfo, err = s.dao.SelVipUserInfo(c, mid); err != nil {
  22. return
  23. }
  24. if userInfo == nil {
  25. err = ecode.VipUserInfoNotExit
  26. return
  27. }
  28. ct := time.Now()
  29. overdueTIme := userInfo.VipOverdueTime
  30. ot = overdueTIme.Time().AddDate(0, 0, -days)
  31. if ot.Before(ct) {
  32. ot = ct
  33. userInfo.VipStatus = model.VipStatusOverTime
  34. }
  35. userInfo.VipOverdueTime = xtime.Time(ot.Unix())
  36. if userInfo.AnnualVipOverdueTime.Time().After(ot) && userInfo.VipType == model.AnnualVip {
  37. userInfo.VipType = model.Vip
  38. userInfo.AnnualVipOverdueTime = xtime.Time(ot.Unix())
  39. }
  40. history = new(model.VipChangeHistory)
  41. history.Mid = mid
  42. history.ChangeTime = xtime.Time(time.Now().Unix())
  43. history.Days = -days
  44. history.ChangeType = model.ChangeTypeSystemDrawback
  45. history.OperatorID = usename
  46. history.Remark = remark
  47. if tx, err = s.dao.BeginTran(context.TODO()); err != nil {
  48. return
  49. }
  50. defer func() {
  51. if err == nil {
  52. if err = tx.Commit(); err != nil {
  53. tx.Rollback()
  54. }
  55. } else {
  56. tx.Rollback()
  57. }
  58. }()
  59. if affected, err = s.dao.UpdateVipUserInfo(tx, userInfo); err != nil {
  60. return
  61. }
  62. if affected > 0 {
  63. if _, err = s.dao.InsertVipChangeHistory(tx, history); err != nil {
  64. return
  65. }
  66. if err = s.dao.DelBcoinSalary(tx, mid, userInfo.VipOverdueTime); err != nil {
  67. return
  68. }
  69. }
  70. return
  71. }
  72. // ExchangeVipByDays exchange vip days
  73. //func (s *Service) exchangeVip(c context.Context, tx *sql.Tx, mid, batchID, unit int, remark, username string) (hv *inModel.HandlerVip, err error) {
  74. // var r = new(inModel.VipChangeBo)
  75. // if mid == 0 {
  76. // err = ecode.VipMidErr
  77. // return
  78. // }
  79. // if len(remark) == 0 || len(remark) > 200 {
  80. // err = ecode.VipRemarkErr
  81. // return
  82. // }
  83. // r.BatchID = int64(batchID)
  84. // r.ChangeType = model.ChangeTypeSystem
  85. // r.Remark = remark
  86. // r.OperatorID = username
  87. // r.Days = int32(unit)
  88. // r.Mid = int64(mid)
  89. // log.Info("%+v ", r)
  90. // if hv, err = s.vipRPC.UpdateVipWithHistory(context.TODO(), r); err != nil {
  91. // fmt.Printf("rpc %+v err(%+v) \n", hv, err)
  92. // log.Error("rpc %+v err(%+v) ", hv, err)
  93. // return
  94. // }
  95. // return
  96. //}
  97. // HistoryPage history page.
  98. func (s *Service) HistoryPage(c context.Context, u *model.UserChangeHistoryReq) (res []*model.VipChangeHistory, count int, err error) {
  99. if count, err = s.dao.HistoryCount(c, u); err != nil {
  100. log.Error("%+v", err)
  101. return
  102. }
  103. if count == 0 {
  104. return
  105. }
  106. if res, err = s.dao.HistoryList(c, u); err != nil {
  107. log.Error("%+v", err)
  108. return
  109. }
  110. return
  111. }
  112. // VipInfo .
  113. func (s *Service) VipInfo(c context.Context, mid int64) (res *model.VipUserInfo, err error) {
  114. if res, err = s.dao.SelVipUserInfo(c, mid); err != nil {
  115. err = errors.WithStack(err)
  116. return
  117. }
  118. return
  119. }