You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
package coupon
|
|
|
|
import (
|
|
"cls/internal/domain/coupon"
|
|
"cls/pkg/xorm_engine"
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
// CouponRepository 优惠券仓储实现
|
|
type CouponRepositoryORM struct {
|
|
engine *xorm_engine.Engine
|
|
}
|
|
|
|
var _ coupon.CouponRepository = (*CouponRepositoryORM)(nil)
|
|
|
|
// NewCouponRepository 创建优惠券仓储
|
|
func NewCouponRepository(engine *xorm_engine.Engine) coupon.CouponRepository {
|
|
return &CouponRepositoryORM{
|
|
engine: engine,
|
|
}
|
|
}
|
|
|
|
// Create 创建优惠券
|
|
func (r *CouponRepositoryORM) Create(coupon *coupon.Coupon) error {
|
|
_, err := r.engine.Insert(coupon)
|
|
return err
|
|
}
|
|
|
|
// GetByID 根据ID获取优惠券
|
|
func (r *CouponRepositoryORM) GetByID(id uint64) (*coupon.Coupon, error) {
|
|
var coupon coupon.Coupon
|
|
has, err := r.engine.ID(id).Get(&coupon)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !has {
|
|
return nil, nil
|
|
}
|
|
return &coupon, nil
|
|
}
|
|
|
|
// GetByCode 根据优惠券码获取优惠券
|
|
func (r *CouponRepositoryORM) GetByCode(code string) (*coupon.Coupon, error) {
|
|
var coupon coupon.Coupon
|
|
has, err := r.engine.Where("code = ?", code).Get(&coupon)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !has {
|
|
return nil, nil
|
|
}
|
|
return &coupon, nil
|
|
}
|
|
|
|
// List 获取优惠券列表
|
|
func (r *CouponRepositoryORM) List(page, size int) ([]*coupon.Coupon, int64, error) {
|
|
var coupons []*coupon.Coupon
|
|
total, err := r.engine.Count(&coupon.Coupon{})
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
err = r.engine.Limit(size, (page-1)*size).Find(&coupons)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return coupons, total, nil
|
|
}
|
|
|
|
// Update 更新优惠券
|
|
func (r *CouponRepositoryORM) Update(coupon *coupon.Coupon) error {
|
|
_, err := r.engine.ID(coupon.ID).Update(coupon)
|
|
return err
|
|
}
|
|
|
|
// Delete 删除优惠券
|
|
func (r *CouponRepositoryORM) Delete(id uint64) error {
|
|
_, err := r.engine.ID(id).Delete(&coupon.Coupon{})
|
|
return err
|
|
}
|
|
func (r *CouponRepositoryORM) ListByUserID(userID uint64) ([]*coupon.Coupon, error) {
|
|
data := make([]*coupon.Coupon, 0)
|
|
return data, r.engine.Where(builder.Eq{"user_id": userID}).Find(&data)
|
|
}
|