You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.0 KiB

6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
  1. package coupon
  2. import (
  3. "CouponBackendGo/api/v1/coupon"
  4. "CouponBackendGo/internal/dao"
  5. "CouponBackendGo/internal/model/do"
  6. "CouponBackendGo/internal/model/dto"
  7. "CouponBackendGo/internal/service"
  8. "context"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/gogf/gf/v2/frame/g"
  12. "github.com/gogf/gf/v2/os/gtime"
  13. )
  14. type (
  15. sCoupon struct{}
  16. )
  17. func init() {
  18. service.RegisterCoupon(New())
  19. }
  20. func New() service.ICoupon {
  21. return &sCoupon{}
  22. }
  23. func (s *sCoupon) GetCouponList(ctx context.Context, pageNo int, pageSize int) (couponList []*dto.DtoCoupon, err error) {
  24. // 从Redis中获取数据
  25. value, _ := g.Redis().Get(ctx, fmt.Sprintf("%d-%d couponlist", pageNo, pageSize))
  26. if value.String() != "" {
  27. // 如果Redis中有数据,尝试解析为Coupon列表
  28. err = json.Unmarshal(value.Bytes(), &couponList) //反序列化
  29. couponlist, cnt := UpdateCoupon(ctx, couponList)
  30. if cnt > 0 {
  31. // 将修改后的卡券列表序列化并存储到Redis,更新频繁变更的数据
  32. couponListJson, _ := json.Marshal(couponlist)
  33. _, err = g.Redis().Set(ctx, fmt.Sprintf("%d-%d couponlist", pageNo, pageSize), couponListJson)
  34. }
  35. if err != nil {
  36. return nil, err
  37. }
  38. return couponlist, err
  39. }
  40. // 如果Redis中没有数据,查询数据库
  41. err = dao.Coupon.Ctx(ctx).Where("deleted", 0).Order("updated_at desc, end_time desc").Page(pageNo, pageSize).Scan(&couponList)
  42. couponList = InitCoupon(ctx, couponList)
  43. couponList, _ = UpdateCoupon(ctx, couponList)
  44. if err != nil {
  45. return nil, err
  46. }
  47. // 将查询到的卡券列表序列化并存储到Redis,更新频繁变更的数据
  48. couponListJson, _ := json.Marshal(couponList)
  49. _, err = g.Redis().Set(ctx, fmt.Sprintf("%d-%d couponlist", pageNo, pageSize), couponListJson)
  50. return
  51. }
  52. func InitCoupon(ctx context.Context, couponList []*dto.DtoCoupon) []*dto.DtoCoupon {
  53. for _, v := range couponList {
  54. v.Status = 0 //待使用期
  55. if v.StartTime.Unix() <= gtime.Now().Unix() {
  56. if v.EndTime.Unix() <= gtime.Now().Unix() {
  57. v.Status = 2 //使用期后
  58. v.Count, _ = dao.CouponUsers.Ctx(ctx).Where("coupon_id", v.Id).Count()
  59. } else {
  60. v.Status = 1 //使用期中
  61. }
  62. }
  63. }
  64. return couponList
  65. }
  66. func UpdateCoupon(ctx context.Context, couponList []*dto.DtoCoupon) ([]*dto.DtoCoupon, int) {
  67. cnt := 0
  68. for _, v := range couponList {
  69. if v.Status != 2 {
  70. if v.StartTime.Unix() > gtime.Now().Unix() {
  71. v.Status = 0
  72. } else if v.EndTime.Unix() <= gtime.Now().Unix() {
  73. v.Status = 2
  74. cnt++
  75. } else {
  76. v.Status = 1
  77. }
  78. v.Count, _ = dao.CouponUsers.Ctx(ctx).Where("coupon_id", v.Id).Count()
  79. }
  80. }
  81. return couponList, cnt
  82. }
  83. func (s *sCoupon) GetCouponListTotal(ctx context.Context) (total int, err error) {
  84. total, err = dao.Coupon.Ctx(ctx).Where("deleted", 0).Count()
  85. if err != nil {
  86. return 0, err
  87. }
  88. return
  89. }
  90. func (s *sCoupon) GetCoupon(ctx context.Context, id int) (coupon *dto.DtoCoupon, err error) {
  91. err = dao.Coupon.Ctx(ctx).WherePri(id).Scan(&coupon)
  92. if err != nil {
  93. return nil, err
  94. }
  95. return
  96. }
  97. func (s *sCoupon) DeleteCoupon(ctx context.Context, id int) (err error) {
  98. _, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{
  99. Deleted: 1,
  100. }).WherePri(id).Update()
  101. if err != nil {
  102. return err
  103. }
  104. // 软删除数据后,删除所有缓存
  105. keys, err := g.Redis().Keys(ctx, "*-* couponlist")
  106. if len(keys) > 0 {
  107. _, err = g.Redis().Del(ctx, keys...)
  108. }
  109. return
  110. }
  111. func (s *sCoupon) InsertCoupon(ctx context.Context, req *coupon.InsertCouponReq) (err error) {
  112. if req.Id == 0 {
  113. _, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{
  114. Title: req.Title,
  115. Cover: req.Image,
  116. ImgUrl: req.Image,
  117. StartTime: req.StartTime.Unix(),
  118. EndTime: req.EndTime.Unix(),
  119. }).Insert()
  120. } else {
  121. _, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{
  122. Title: req.Title,
  123. Cover: req.Image,
  124. ImgUrl: req.Image,
  125. StartTime: req.StartTime.Unix(),
  126. EndTime: req.EndTime.Unix(),
  127. }).WherePri(req.Id).Update()
  128. }
  129. if err != nil {
  130. return err
  131. }
  132. // 新增&编辑数据后,删除所有缓存
  133. keys, err := g.Redis().Keys(ctx, "*-* couponlist")
  134. if len(keys) > 0 {
  135. _, err = g.Redis().Del(ctx, keys...)
  136. }
  137. return
  138. }