重构优惠券表结构与功能
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user