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.
99 lines
2.3 KiB
99 lines
2.3 KiB
package coupon
|
|
|
|
import (
|
|
"CouponBackendGo/api/v1/coupon"
|
|
"CouponBackendGo/internal/dao"
|
|
"CouponBackendGo/internal/model/do"
|
|
"CouponBackendGo/internal/model/dto"
|
|
"CouponBackendGo/internal/service"
|
|
"context"
|
|
"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) {
|
|
err = dao.Coupon.Ctx(ctx).Where("deleted", 0).Order("updated_at desc, end_time desc").Page(pageNo, pageSize).Scan(&couponList)
|
|
couponList = InitCoupon(ctx, couponList)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
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 //使用期后
|
|
} else {
|
|
v.Status = 1 //使用期中
|
|
}
|
|
}
|
|
v.Count, _ = dao.CouponUsers.Ctx(ctx).Where("coupon_id", v.Id).Count()
|
|
}
|
|
return couponList
|
|
}
|
|
|
|
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
|
|
}
|
|
return
|
|
}
|
|
|
|
func (s *sCoupon) InsertCoupon(ctx context.Context, req *coupon.InsertCouponReq) (err error) {
|
|
if req.Id == 0 {
|
|
_, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{
|
|
Title: req.Title,
|
|
Cover: req.Image,
|
|
ImgUrl: req.Image,
|
|
Type: req.Type,
|
|
StartTime: req.StartTime.Unix(),
|
|
EndTime: req.EndTime.Unix(),
|
|
}).Insert()
|
|
} else {
|
|
_, err = dao.Coupon.Ctx(ctx).Data(do.Coupon{
|
|
Title: req.Title,
|
|
Cover: req.Image,
|
|
ImgUrl: req.Image,
|
|
Type: req.Type,
|
|
StartTime: req.StartTime.Unix(),
|
|
EndTime: req.EndTime.Unix(),
|
|
}).WherePri(req.Id).Update()
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return
|
|
}
|