实现产品查询和修改接口 & 修复套餐查询接口问题
This commit is contained in:
86
web/services/product_sku.go
Normal file
86
web/services/product_sku.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"platform/web/core"
|
||||
m "platform/web/models"
|
||||
q "platform/web/queries"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gen"
|
||||
"gorm.io/gen/field"
|
||||
)
|
||||
|
||||
var ProductSku = &productSkuService{}
|
||||
|
||||
type productSkuService struct{}
|
||||
|
||||
func (s *productSkuService) Page(req *core.PageReq, productId *int32) (result []*m.ProductSku, count int64, err error) {
|
||||
do := make([]gen.Condition, 0)
|
||||
if productId != nil {
|
||||
do = append(do, q.ProductSku.ProductID.Eq(*productId))
|
||||
}
|
||||
return q.ProductSku.
|
||||
Where(do...).
|
||||
FindByPage(req.GetOffset(), req.GetLimit())
|
||||
}
|
||||
|
||||
func (s *productSkuService) Create(create CreateProductSkuData) (err error) {
|
||||
price, err := decimal.NewFromString(create.Price)
|
||||
if err != nil {
|
||||
return core.NewBizErr("产品价格的格式不正确", err)
|
||||
}
|
||||
|
||||
return q.ProductSku.Create(&m.ProductSku{
|
||||
ProductID: create.ProductID,
|
||||
Code: create.Code,
|
||||
Name: create.Name,
|
||||
Price: price,
|
||||
Discount: create.Discount,
|
||||
})
|
||||
}
|
||||
|
||||
type CreateProductSkuData struct {
|
||||
ProductID int32 `json:"product_id"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Price string `json:"price"`
|
||||
Discount float32 `json:"discount"`
|
||||
}
|
||||
|
||||
func (s *productSkuService) Update(update UpdateProductSkuData) (err error) {
|
||||
do := make([]field.AssignExpr, 0)
|
||||
|
||||
if update.Price != nil {
|
||||
price, err := decimal.NewFromString(*update.Price)
|
||||
if err != nil {
|
||||
return core.NewBizErr("产品价格的格式不正确", err)
|
||||
}
|
||||
do = append(do, q.ProductSku.Price.Value(price))
|
||||
}
|
||||
if update.Discount != nil {
|
||||
do = append(do, q.ProductSku.Discount.Value(*update.Discount))
|
||||
}
|
||||
if update.Code != nil {
|
||||
do = append(do, q.ProductSku.Code.Value(*update.Code))
|
||||
}
|
||||
if update.Name != nil {
|
||||
do = append(do, q.ProductSku.Name.Value(*update.Name))
|
||||
}
|
||||
|
||||
_, err = q.ProductSku.Where(q.ProductSku.ID.Eq(update.ID)).UpdateSimple(do...)
|
||||
return err
|
||||
}
|
||||
|
||||
type UpdateProductSkuData struct {
|
||||
ID int32 `json:"id"`
|
||||
Code *string `json:"code"`
|
||||
Name *string `json:"name"`
|
||||
Price *string `json:"price"`
|
||||
Discount *float32 `json:"discount"`
|
||||
}
|
||||
|
||||
func (s *productSkuService) Delete(id int32) (err error) {
|
||||
_, err = q.ProductSku.Where(q.ProductSku.ID.Eq(id)).UpdateColumn(q.ProductSku.DeletedAt, time.Now())
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user