|
|
@ -2,9 +2,13 @@ package coupon |
|
|
|
|
|
|
|
import ( |
|
|
|
"CouponBackendGo/internal/dao" |
|
|
|
"CouponBackendGo/internal/model/do" |
|
|
|
"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" |
|
|
|
) |
|
|
|
|
|
|
@ -21,13 +25,73 @@ 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 |
|
|
|
} |
|
|
|
|
|
|
|
func InitCoupon(ctx context.Context, couponList []*dto.DtoCoupon) []*dto.DtoCoupon { |
|
|
|
for _, v := range couponList { |
|
|
|
v.Status = 0 //待使用期
|
|
|
|
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 //使用期中
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
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 { |
|
|
@ -38,16 +102,22 @@ func (s *sCoupon) GetCouponListTotal(ctx context.Context) (total int, err error) |
|
|
|
|
|
|
|
func (s *sCoupon) GetCoupon(ctx context.Context, id int) (coupon *dto.DtoCoupon, err error) { |
|
|
|
err = dao.Coupon.Ctx(ctx).WherePri(id).Scan(&coupon) |
|
|
|
coupon.Status = 0 //待使用期
|
|
|
|
if coupon.StartTime.Unix() <= gtime.Now().Unix() { |
|
|
|
if coupon.EndTime.Unix() <= gtime.Now().Unix() { |
|
|
|
coupon.Status = 2 //使用期后
|
|
|
|
} else { |
|
|
|
coupon.Status = 1 //使用期中
|
|
|
|
} |
|
|
|
} |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
func (s *sCoupon) DeleteCoupon(ctx context.Context, id int) (err error) { |
|
|
|
_, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{ |
|
|
|
Deleted: 1, |
|
|
|
}).WherePri(id).Update() |
|
|
|
|
|
|
|
// 软删除数据后,删除所有缓存
|
|
|
|
keys, err := g.Redis().Keys(ctx, "*-* couponlist") |
|
|
|
if len(keys) > 0 { |
|
|
|
_, err = g.Redis().Del(ctx, keys...) |
|
|
|
} |
|
|
|
|
|
|
|
return |
|
|
|
} |