From 6f859fd76a4c8def2307827318d2ddfb91018ba1 Mon Sep 17 00:00:00 2001 From: majun <3060162534@qq.com> Date: Sat, 28 Dec 2024 19:40:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/logic/coupon/coupon.go | 63 +---------------------------------------- 1 file changed, 1 insertion(+), 62 deletions(-) diff --git a/internal/logic/coupon/coupon.go b/internal/logic/coupon/coupon.go index 6d3f1d3..e131d02 100644 --- a/internal/logic/coupon/coupon.go +++ b/internal/logic/coupon/coupon.go @@ -7,9 +7,6 @@ import ( "CouponBackendGo/internal/model/dto" "CouponBackendGo/internal/service" "context" - "encoding/json" - "fmt" - "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gtime" ) @@ -26,37 +23,11 @@ func New() service.ICoupon { } func (s *sCoupon) GetCouponList(ctx context.Context, pageNo int, pageSize int) (couponList []*dto.DtoCoupon, err error) { - // 从Redis中获取数据 - value, _ := g.Redis().Get(ctx, fmt.Sprintf("%d-%d couponlist", pageNo, pageSize)) - if value.String() != "" { - // 如果Redis中有数据,尝试解析为Coupon列表 - err = json.Unmarshal(value.Bytes(), &couponList) //反序列化 - couponlist, cnt := UpdateCoupon(ctx, couponList) - - if cnt > 0 { - // 将修改后的卡券列表序列化并存储到Redis,更新频繁变更的数据 - couponListJson, _ := json.Marshal(couponlist) - _, err = g.Redis().Set(ctx, fmt.Sprintf("%d-%d couponlist", pageNo, pageSize), couponListJson) - } - - if err != nil { - return nil, err - } - return couponlist, err - } - - // 如果Redis中没有数据,查询数据库 err = dao.Coupon.Ctx(ctx).Where("deleted", 0).Order("updated_at desc, end_time desc").Page(pageNo, pageSize).Scan(&couponList) couponList = InitCoupon(ctx, couponList) - couponList, _ = UpdateCoupon(ctx, couponList) if err != nil { return nil, err } - - // 将查询到的卡券列表序列化并存储到Redis,更新频繁变更的数据 - couponListJson, _ := json.Marshal(couponList) - _, err = g.Redis().Set(ctx, fmt.Sprintf("%d-%d couponlist", pageNo, pageSize), couponListJson) - return } @@ -66,33 +37,15 @@ func InitCoupon(ctx context.Context, couponList []*dto.DtoCoupon) []*dto.DtoCoup if v.StartTime.Unix() <= gtime.Now().Unix() { if v.EndTime.Unix() <= gtime.Now().Unix() { v.Status = 2 //使用期后 - v.Count, _ = dao.CouponUsers.Ctx(ctx).Where("coupon_id", v.Id).Count() } else { v.Status = 1 //使用期中 } } + v.Count, _ = dao.CouponUsers.Ctx(ctx).Where("coupon_id", v.Id).Count() } return couponList } -func UpdateCoupon(ctx context.Context, couponList []*dto.DtoCoupon) ([]*dto.DtoCoupon, int) { - cnt := 0 - for _, v := range couponList { - if v.Status != 2 { - if v.StartTime.Unix() > gtime.Now().Unix() { - v.Status = 0 - } else if v.EndTime.Unix() <= gtime.Now().Unix() { - v.Status = 2 - cnt++ - } else { - v.Status = 1 - } - v.Count, _ = dao.CouponUsers.Ctx(ctx).Where("coupon_id", v.Id).Count() - } - } - return couponList, cnt -} - func (s *sCoupon) GetCouponListTotal(ctx context.Context) (total int, err error) { total, err = dao.Coupon.Ctx(ctx).Where("deleted", 0).Count() if err != nil { @@ -116,13 +69,6 @@ func (s *sCoupon) DeleteCoupon(ctx context.Context, id int) (err error) { if err != nil { return err } - - // 软删除数据后,删除所有缓存 - keys, err := g.Redis().Keys(ctx, "*-* couponlist") - if len(keys) > 0 { - _, err = g.Redis().Del(ctx, keys...) - } - return } @@ -147,12 +93,5 @@ func (s *sCoupon) InsertCoupon(ctx context.Context, req *coupon.InsertCouponReq) if err != nil { return err } - - // 新增&编辑数据后,删除所有缓存 - keys, err := g.Redis().Keys(ctx, "*-* couponlist") - if len(keys) > 0 { - _, err = g.Redis().Del(ctx, keys...) - } - return }