重构优惠券表结构与功能
This commit is contained in:
@@ -23,14 +23,14 @@ func (s *billService) CreateForBalance(q *q.Query, uid, tradeId int32, detail *T
|
||||
|
||||
func (s *billService) CreateForResource(q *q.Query, uid, resourceId int32, tradeId *int32, detail *TradeDetail) error {
|
||||
return q.Bill.Create(&m.Bill{
|
||||
UserID: uid,
|
||||
BillNo: ID.GenReadable("bil"),
|
||||
ResourceID: &resourceId,
|
||||
TradeID: tradeId,
|
||||
CouponID: detail.CouponId,
|
||||
Type: m.BillTypeConsume,
|
||||
Info: &detail.Subject,
|
||||
Amount: detail.Amount,
|
||||
Actual: detail.Actual,
|
||||
UserID: uid,
|
||||
BillNo: ID.GenReadable("bil"),
|
||||
ResourceID: &resourceId,
|
||||
TradeID: tradeId,
|
||||
CouponUserID: detail.CouponUserId,
|
||||
Type: m.BillTypeConsume,
|
||||
Info: &detail.Subject,
|
||||
Amount: detail.Amount,
|
||||
Actual: detail.Actual,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -46,8 +46,8 @@ func (s *resourceService) CreateResourceByBalance(user *m.User, data *CreateReso
|
||||
}
|
||||
|
||||
// 核销优惠券
|
||||
if detail.CouponId != nil {
|
||||
err = Coupon.UseCoupon(q, *detail.CouponId)
|
||||
if detail.CouponUserId != nil {
|
||||
err = Coupon.UseCoupon(q, *detail.CouponUserId)
|
||||
if err != nil {
|
||||
return core.NewServErr("核销优惠券失败", err)
|
||||
}
|
||||
@@ -144,7 +144,7 @@ type UpdateResourceData struct {
|
||||
Active *bool `json:"active"`
|
||||
}
|
||||
|
||||
func (s *resourceService) CalcPrice(skuCode string, count int32, user *m.User, couponCode *string) (*m.ProductSku, *m.ProductDiscount, *m.Coupon, decimal.Decimal, decimal.Decimal, error) {
|
||||
func (s *resourceService) CalcPrice(skuCode string, count int32, user *m.User, cuid *int32) (*m.ProductSku, *m.ProductDiscount, *m.CouponUser, decimal.Decimal, decimal.Decimal, error) {
|
||||
|
||||
sku, err := q.ProductSku.
|
||||
Joins(q.ProductSku.Discount).
|
||||
@@ -180,20 +180,15 @@ func (s *resourceService) CalcPrice(skuCode string, count int32, user *m.User, c
|
||||
discounted := amount.Mul(discountRate)
|
||||
|
||||
// 优惠价
|
||||
uid := (*int32)(nil)
|
||||
if user != nil {
|
||||
uid = &user.ID
|
||||
}
|
||||
|
||||
coupon := (*m.Coupon)(nil)
|
||||
coupon := (*m.CouponUser)(nil)
|
||||
couponApplied := discounted.Copy()
|
||||
if couponCode != nil {
|
||||
if user != nil && cuid != nil {
|
||||
var err error
|
||||
coupon, err = Coupon.GetCouponAvailableByCode(*couponCode, discounted, uid)
|
||||
coupon, err = Coupon.GetUserCoupon(user.ID, *cuid, discounted)
|
||||
if err != nil {
|
||||
return nil, nil, nil, decimal.Zero, decimal.Zero, err
|
||||
}
|
||||
couponApplied = discounted.Sub(coupon.Amount)
|
||||
couponApplied = discounted.Sub(coupon.Coupon.Amount)
|
||||
}
|
||||
|
||||
// 约束到最低价格
|
||||
@@ -208,10 +203,10 @@ func (s *resourceService) CalcPrice(skuCode string, count int32, user *m.User, c
|
||||
}
|
||||
|
||||
type CreateResourceData struct {
|
||||
Type m.ResourceType `json:"type" validate:"required"`
|
||||
Short *CreateShortResourceData `json:"short,omitempty"`
|
||||
Long *CreateLongResourceData `json:"long,omitempty"`
|
||||
CouponCode *string `json:"coupon,omitempty"`
|
||||
Type m.ResourceType `json:"type" validate:"required"`
|
||||
Short *CreateShortResourceData `json:"short,omitempty"`
|
||||
Long *CreateLongResourceData `json:"long,omitempty"`
|
||||
CouponId *int32 `json:"coupon,omitempty"`
|
||||
}
|
||||
|
||||
type CreateShortResourceData struct {
|
||||
@@ -265,7 +260,7 @@ func (data *CreateResourceData) Code() string {
|
||||
}
|
||||
|
||||
func (data *CreateResourceData) TradeDetail(user *m.User) (*TradeDetail, error) {
|
||||
sku, discount, coupon, amount, actual, err := Resource.CalcPrice(data.Code(), data.Count(), user, data.CouponCode)
|
||||
sku, discount, coupon, amount, actual, err := Resource.CalcPrice(data.Code(), data.Count(), user, data.CouponId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -274,17 +269,16 @@ func (data *CreateResourceData) TradeDetail(user *m.User) (*TradeDetail, error)
|
||||
if discount != nil {
|
||||
discountId = &discount.ID
|
||||
}
|
||||
var couponId *int32 = nil
|
||||
var couponUserId *int32 = nil
|
||||
if coupon != nil {
|
||||
couponId = &coupon.ID
|
||||
couponUserId = &coupon.ID
|
||||
}
|
||||
return &TradeDetail{
|
||||
data,
|
||||
m.TradeTypePurchase,
|
||||
sku.Name,
|
||||
amount, actual,
|
||||
discountId, discount,
|
||||
couponId, coupon,
|
||||
discountId, couponUserId,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -332,8 +332,8 @@ func (s *tradeService) OnCompleteTrade(user *m.User, interNo string, outerNo str
|
||||
}
|
||||
|
||||
// 核销优惠券
|
||||
if detail.CouponId != nil {
|
||||
err = Coupon.UseCoupon(q, *detail.CouponId)
|
||||
if detail.CouponUserId != nil {
|
||||
err = Coupon.UseCoupon(q, *detail.CouponUserId)
|
||||
if err != nil {
|
||||
return core.NewServErr("核销优惠券失败", err)
|
||||
}
|
||||
@@ -614,15 +614,13 @@ type ProductData interface {
|
||||
}
|
||||
|
||||
type TradeDetail struct {
|
||||
Product ProductData `json:"product"`
|
||||
Type m.TradeType `json:"type"`
|
||||
Subject string `json:"subject"`
|
||||
Amount decimal.Decimal `json:"amount"`
|
||||
Actual decimal.Decimal `json:"actual"`
|
||||
DiscountId *int32 `json:"discount_id,omitempty"`
|
||||
Discount *m.ProductDiscount `json:"-"` // 不应缓存
|
||||
CouponId *int32 `json:"coupon_id,omitempty"`
|
||||
Coupon *m.Coupon `json:"-"` // 不应缓存
|
||||
Product ProductData `json:"product"`
|
||||
Type m.TradeType `json:"type"`
|
||||
Subject string `json:"subject"`
|
||||
Amount decimal.Decimal `json:"amount"`
|
||||
Actual decimal.Decimal `json:"actual"`
|
||||
DiscountId *int32 `json:"discount_id,omitempty"`
|
||||
CouponUserId *int32 `json:"coupon_id,omitempty"`
|
||||
}
|
||||
|
||||
type TradeErr string
|
||||
|
||||
@@ -94,7 +94,6 @@ func (data *UpdateBalanceData) TradeDetail(user *m.User) (*TradeDetail, error) {
|
||||
fmt.Sprintf("账户充值 - %s元", amount.StringFixed(2)),
|
||||
amount, amount,
|
||||
nil, nil,
|
||||
nil, nil,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user