重构优惠券表结构与功能
This commit is contained in:
@@ -3,6 +3,7 @@ package services
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"platform/pkg/u"
|
||||
"platform/web/core"
|
||||
m "platform/web/models"
|
||||
q "platform/web/queries"
|
||||
@@ -27,36 +28,32 @@ func (s *couponService) Page(req *core.PageReq) (result []*m.Coupon, count int64
|
||||
|
||||
func (s *couponService) Create(data CreateCouponData) error {
|
||||
return q.Coupon.Create(&m.Coupon{
|
||||
UserID: data.UserID,
|
||||
Code: data.Code,
|
||||
Remark: data.Remark,
|
||||
Amount: data.Amount,
|
||||
MinAmount: data.MinAmount,
|
||||
Status: m.CouponStatusUnused,
|
||||
ExpireAt: data.ExpireAt,
|
||||
Name: data.Name,
|
||||
Amount: data.Amount,
|
||||
MinAmount: data.MinAmount,
|
||||
Count: int32(u.Else(data.Count, 1)),
|
||||
Status: u.Else(data.Status, m.CouponStatusEnabled),
|
||||
ExpireType: u.Else(data.ExpireType, m.CouponExpireTypeNever),
|
||||
ExpireAt: data.ExpireAt,
|
||||
ExpireIn: data.ExpireIn,
|
||||
})
|
||||
}
|
||||
|
||||
type CreateCouponData struct {
|
||||
UserID *int32 `json:"user_id"`
|
||||
Code string `json:"code" validate:"required"`
|
||||
Remark *string `json:"remark"`
|
||||
Amount decimal.Decimal `json:"amount" validate:"required"`
|
||||
MinAmount decimal.Decimal `json:"min_amount"`
|
||||
ExpireAt *time.Time `json:"expire_at"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Amount decimal.Decimal `json:"amount" validate:"required"`
|
||||
MinAmount decimal.Decimal `json:"min_amount"`
|
||||
Count *int `json:"count"`
|
||||
Status *m.CouponStatus `json:"status"`
|
||||
ExpireType *m.CouponExpireType `json:"expire_type"`
|
||||
ExpireAt *time.Time `json:"expire_at"`
|
||||
ExpireIn *int `json:"expire_in"`
|
||||
}
|
||||
|
||||
func (s *couponService) Update(data UpdateCouponData) error {
|
||||
do := make([]field.AssignExpr, 0)
|
||||
|
||||
if data.UserID != nil {
|
||||
do = append(do, q.Coupon.UserID.Value(*data.UserID))
|
||||
}
|
||||
if data.Code != nil {
|
||||
do = append(do, q.Coupon.Code.Value(*data.Code))
|
||||
}
|
||||
if data.Remark != nil {
|
||||
do = append(do, q.Coupon.Remark.Value(*data.Remark))
|
||||
if data.Name != nil {
|
||||
do = append(do, q.Coupon.Name.Value(*data.Name))
|
||||
}
|
||||
if data.Amount != nil {
|
||||
do = append(do, q.Coupon.Amount.Value(*data.Amount))
|
||||
@@ -64,26 +61,36 @@ func (s *couponService) Update(data UpdateCouponData) error {
|
||||
if data.MinAmount != nil {
|
||||
do = append(do, q.Coupon.MinAmount.Value(*data.MinAmount))
|
||||
}
|
||||
if data.Count != nil {
|
||||
do = append(do, q.Coupon.Count_.Value(int32(*data.Count)))
|
||||
}
|
||||
if data.Status != nil {
|
||||
do = append(do, q.Coupon.Status.Value(int(*data.Status)))
|
||||
}
|
||||
if data.ExpireType != nil {
|
||||
do = append(do, q.Coupon.ExpireType.Value(int(*data.ExpireType)))
|
||||
}
|
||||
if data.ExpireAt != nil {
|
||||
do = append(do, q.Coupon.ExpireAt.Value(*data.ExpireAt))
|
||||
}
|
||||
if data.ExpireIn != nil {
|
||||
do = append(do, q.Coupon.ExpireIn.Value(*data.ExpireIn))
|
||||
}
|
||||
|
||||
_, err := q.Coupon.Where(q.Coupon.ID.Eq(data.ID)).UpdateSimple(do...)
|
||||
return err
|
||||
}
|
||||
|
||||
type UpdateCouponData struct {
|
||||
ID int32 `json:"id" validate:"required"`
|
||||
UserID *int32 `json:"user_id"`
|
||||
Code *string `json:"code"`
|
||||
Remark *string `json:"remark"`
|
||||
Amount *decimal.Decimal `json:"amount"`
|
||||
MinAmount *decimal.Decimal `json:"min_amount"`
|
||||
Status *m.CouponStatus `json:"status"`
|
||||
ExpireAt *time.Time `json:"expire_at"`
|
||||
ID int32 `json:"id" validate:"required"`
|
||||
Name *string `json:"name"`
|
||||
Amount *decimal.Decimal `json:"amount"`
|
||||
MinAmount *decimal.Decimal `json:"min_amount"`
|
||||
Count *int `json:"count"`
|
||||
Status *m.CouponStatus `json:"status"`
|
||||
ExpireType *m.CouponExpireType `json:"expire_type"`
|
||||
ExpireAt *time.Time `json:"expire_at"`
|
||||
ExpireIn *int `json:"expire_in"`
|
||||
}
|
||||
|
||||
func (s *couponService) Delete(id int32) error {
|
||||
@@ -91,14 +98,14 @@ func (s *couponService) Delete(id int32) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *couponService) GetCouponAvailableByCode(code string, amount decimal.Decimal, uid *int32) (*m.Coupon, error) {
|
||||
// GetUserCoupon 获取用户的指定优惠券
|
||||
func (s *couponService) GetUserCoupon(uid int32, cuid int32, amount decimal.Decimal) (*m.CouponUser, error) {
|
||||
// 获取优惠券
|
||||
coupon, err := q.Coupon.Where(
|
||||
q.Coupon.Code.Eq(code),
|
||||
q.Coupon.Status.Eq(int(m.CouponStatusUnused)),
|
||||
q.Coupon.
|
||||
Where(q.Coupon.ExpireAt.Gt(time.Now())).
|
||||
Or(q.Coupon.ExpireAt.IsNull()),
|
||||
assigned, err := q.CouponUser.Joins(q.CouponUser.Coupon).Where(
|
||||
q.CouponUser.ID.Eq(cuid),
|
||||
q.CouponUser.UserID.Eq(uid),
|
||||
q.CouponUser.Status.Eq(int(m.CouponUserStatusUnused)),
|
||||
q.CouponUser.ExpireAt.Gt(time.Now()),
|
||||
).Take()
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, core.NewBizErr("优惠券不存在或已失效")
|
||||
@@ -108,32 +115,22 @@ func (s *couponService) GetCouponAvailableByCode(code string, amount decimal.Dec
|
||||
}
|
||||
|
||||
// 检查最小使用额度
|
||||
if amount.Cmp(coupon.MinAmount) < 0 {
|
||||
return nil, core.NewBizErr(fmt.Sprintf("使用此优惠券的最小额度为 %s", coupon.MinAmount))
|
||||
if amount.Cmp(assigned.Coupon.MinAmount) < 0 {
|
||||
return nil, core.NewBizErr(fmt.Sprintf("使用此优惠券的最小额度为 %s", assigned.Coupon.MinAmount))
|
||||
}
|
||||
|
||||
// 检查所属
|
||||
if coupon.UserID != nil {
|
||||
if uid == nil {
|
||||
return nil, core.NewBizErr("检查优惠券所属用户失败")
|
||||
}
|
||||
if *coupon.UserID != *uid {
|
||||
return nil, core.NewBizErr("优惠券不属于当前用户")
|
||||
}
|
||||
}
|
||||
|
||||
return coupon, nil
|
||||
return assigned, nil
|
||||
}
|
||||
|
||||
func (s *couponService) UseCoupon(q *q.Query, id int32) error {
|
||||
_, err := q.Coupon.
|
||||
func (s *couponService) UseCoupon(q *q.Query, cuid int32) error {
|
||||
_, err := q.CouponUser.
|
||||
Where(
|
||||
q.Coupon.ID.Eq(id),
|
||||
q.Coupon.Status.Eq(int(m.CouponStatusUnused)),
|
||||
q.Coupon.ExpireAt.Gt(time.Now()),
|
||||
q.CouponUser.ID.Eq(cuid),
|
||||
q.CouponUser.Status.Eq(int(m.CouponUserStatusUnused)),
|
||||
).
|
||||
UpdateSimple(
|
||||
q.Coupon.Status.Value(int(m.CouponStatusUsed)),
|
||||
q.CouponUser.Status.Value(int(m.CouponUserStatusUsed)),
|
||||
q.CouponUser.UsedAt.Value(time.Now()),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user