最低价格约束
This commit is contained in:
@@ -32,7 +32,7 @@ func (s *productSkuService) Page(req *core.PageReq, productId *int32) (result []
|
||||
return q.ProductSku.
|
||||
Joins(q.ProductSku.Discount).
|
||||
Where(do...).
|
||||
Order(q.ProductSku.CreatedAt.Desc()).
|
||||
Order(q.ProductSku.ID).
|
||||
FindByPage(req.GetOffset(), req.GetLimit())
|
||||
}
|
||||
|
||||
@@ -42,12 +42,18 @@ func (s *productSkuService) Create(create CreateProductSkuData) (err error) {
|
||||
return core.NewBizErr("产品价格的格式不正确", err)
|
||||
}
|
||||
|
||||
priceMin, err := decimal.NewFromString(create.PriceMin)
|
||||
if err != nil {
|
||||
return core.NewBizErr("产品最低价格的格式不正确", err)
|
||||
}
|
||||
|
||||
return q.ProductSku.Create(&m.ProductSku{
|
||||
ProductID: create.ProductID,
|
||||
DiscountId: create.DiscountID,
|
||||
Code: create.Code,
|
||||
Name: create.Name,
|
||||
Price: price,
|
||||
PriceMin: priceMin,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -57,6 +63,7 @@ type CreateProductSkuData struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Price string `json:"price"`
|
||||
PriceMin string `json:"price_min"`
|
||||
}
|
||||
|
||||
func (s *productSkuService) Update(update UpdateProductSkuData) (err error) {
|
||||
@@ -69,6 +76,13 @@ func (s *productSkuService) Update(update UpdateProductSkuData) (err error) {
|
||||
}
|
||||
do = append(do, q.ProductSku.Price.Value(price))
|
||||
}
|
||||
if update.PriceMin != "" {
|
||||
priceMin, err := decimal.NewFromString(update.PriceMin)
|
||||
if err != nil {
|
||||
return core.NewBizErr("产品最低价格的格式不正确", err)
|
||||
}
|
||||
do = append(do, q.ProductSku.PriceMin.Value(priceMin))
|
||||
}
|
||||
if update.DiscountID != nil {
|
||||
do = append(do, q.ProductSku.DiscountId.Value(*update.DiscountID))
|
||||
}
|
||||
@@ -92,6 +106,7 @@ type UpdateProductSkuData struct {
|
||||
Code *string `json:"code"`
|
||||
Name *string `json:"name"`
|
||||
Price *string `json:"price"`
|
||||
PriceMin string `json:"price_min"`
|
||||
Status *int32 `json:"status"`
|
||||
}
|
||||
|
||||
|
||||
@@ -196,6 +196,14 @@ func (s *resourceService) CalcPrice(skuCode string, count int32, user *m.User, c
|
||||
couponApplied = discounted.Sub(coupon.Amount)
|
||||
}
|
||||
|
||||
// 约束到最低价格
|
||||
if discounted.Cmp(sku.PriceMin) < 0 {
|
||||
discounted = sku.PriceMin.Copy()
|
||||
}
|
||||
if couponApplied.Cmp(sku.PriceMin) < 0 {
|
||||
couponApplied = sku.PriceMin.Copy()
|
||||
}
|
||||
|
||||
return sku, discount, coupon, discounted, couponApplied, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user