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.

123 lines
3.2 KiB

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