最低价格约束
This commit is contained in:
@@ -757,6 +757,7 @@ create table product_sku (
|
||||
code text not null unique,
|
||||
name text not null,
|
||||
price decimal not null,
|
||||
price_min decimal not null,
|
||||
status int not null default 1,
|
||||
created_at timestamptz default current_timestamp,
|
||||
updated_at timestamptz default current_timestamp,
|
||||
@@ -773,7 +774,8 @@ comment on column product_sku.product_id is '产品ID';
|
||||
comment on column product_sku.discount_id is '折扣ID';
|
||||
comment on column product_sku.code is 'SKU 代码:格式为 key=value,key=value,...,其中,key:value 是 SKU 的属性,多个属性用逗号分隔';
|
||||
comment on column product_sku.name is 'SKU 可读名称';
|
||||
comment on column product_sku.price is '定价';
|
||||
comment on column product_sku.price_min is '最低价格';
|
||||
comment on column product_sku.min is '最小购买量';
|
||||
comment on column product_sku.status is 'SKU状态:0-禁用,1-正常';
|
||||
comment on column product_sku.created_at is '创建时间';
|
||||
comment on column product_sku.updated_at is '更新时间';
|
||||
|
||||
@@ -14,6 +14,7 @@ type ProductSku struct {
|
||||
Code string `json:"code" gorm:"column:code"` // SSKU 代码:格式为 key=value,key=value,...,其中,key:value 是 SKU 的属性,多个属性用逗号分隔
|
||||
Name string `json:"name" gorm:"column:name"` // SKU 可读名称
|
||||
Price decimal.Decimal `json:"price" gorm:"column:price"` // 定价
|
||||
PriceMin decimal.Decimal `json:"price_min" gorm:"column:price_min"` // 最低价格
|
||||
Status SkuStatus `json:"status" gorm:"column:status"` // SKU 状态:0-禁用,1-正常
|
||||
|
||||
Product *Product `json:"product,omitempty" gorm:"foreignKey:ProductID"`
|
||||
|
||||
@@ -36,6 +36,7 @@ func newProductSku(db *gorm.DB, opts ...gen.DOOption) productSku {
|
||||
_productSku.Code = field.NewString(tableName, "code")
|
||||
_productSku.Name = field.NewString(tableName, "name")
|
||||
_productSku.Price = field.NewField(tableName, "price")
|
||||
_productSku.PriceMin = field.NewField(tableName, "price_min")
|
||||
_productSku.Status = field.NewInt32(tableName, "status")
|
||||
_productSku.Product = productSkuBelongsToProduct{
|
||||
db: db.Session(&gorm.Session{}),
|
||||
@@ -67,6 +68,7 @@ type productSku struct {
|
||||
Code field.String
|
||||
Name field.String
|
||||
Price field.Field
|
||||
PriceMin field.Field
|
||||
Status field.Int32
|
||||
Product productSkuBelongsToProduct
|
||||
|
||||
@@ -96,6 +98,7 @@ func (p *productSku) updateTableName(table string) *productSku {
|
||||
p.Code = field.NewString(table, "code")
|
||||
p.Name = field.NewString(table, "name")
|
||||
p.Price = field.NewField(table, "price")
|
||||
p.PriceMin = field.NewField(table, "price_min")
|
||||
p.Status = field.NewInt32(table, "status")
|
||||
|
||||
p.fillFieldMap()
|
||||
@@ -113,7 +116,7 @@ func (p *productSku) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
}
|
||||
|
||||
func (p *productSku) fillFieldMap() {
|
||||
p.fieldMap = make(map[string]field.Expr, 12)
|
||||
p.fieldMap = make(map[string]field.Expr, 13)
|
||||
p.fieldMap["id"] = p.ID
|
||||
p.fieldMap["created_at"] = p.CreatedAt
|
||||
p.fieldMap["updated_at"] = p.UpdatedAt
|
||||
@@ -123,6 +126,7 @@ func (p *productSku) fillFieldMap() {
|
||||
p.fieldMap["code"] = p.Code
|
||||
p.fieldMap["name"] = p.Name
|
||||
p.fieldMap["price"] = p.Price
|
||||
p.fieldMap["price_min"] = p.PriceMin
|
||||
p.fieldMap["status"] = p.Status
|
||||
|
||||
}
|
||||
|
||||
@@ -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