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.
148 lines
3.8 KiB
148 lines
3.8 KiB
package coupon
|
|
|
|
import (
|
|
"CouponBackendGo/api/v1/coupon"
|
|
"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"
|
|
)
|
|
|
|
type (
|
|
sCoupon struct{}
|
|
)
|
|
|
|
func init() {
|
|
service.RegisterCoupon(New())
|
|
}
|
|
|
|
func New() service.ICoupon {
|
|
return &sCoupon{}
|
|
}
|
|
|
|
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 {
|
|
return 0, err
|
|
}
|
|
return
|
|
}
|
|
|
|
func (s *sCoupon) GetCoupon(ctx context.Context, id int) (coupon *dto.DtoCoupon, err error) {
|
|
err = dao.Coupon.Ctx(ctx).WherePri(id).Scan(&coupon)
|
|
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()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 软删除数据后,删除所有缓存
|
|
keys, err := g.Redis().Keys(ctx, "*-* couponlist")
|
|
if len(keys) > 0 {
|
|
_, err = g.Redis().Del(ctx, keys...)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (s *sCoupon) InsertCoupon(ctx context.Context, req *coupon.InsertCouponReq) (err error) {
|
|
_, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{
|
|
Title: req.Title,
|
|
Cover: req.Image,
|
|
ImgUrl: req.Image,
|
|
StartTime: req.StartTime.Unix(),
|
|
EndTime: req.EndTime.Unix(),
|
|
}).Insert()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 新增数据后,删除所有缓存
|
|
keys, err := g.Redis().Keys(ctx, "*-* couponlist")
|
|
if len(keys) > 0 {
|
|
_, err = g.Redis().Del(ctx, keys...)
|
|
}
|
|
|
|
return
|
|
}
|