实现产品查询和修改接口 & 修复套餐查询接口问题
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
package services
|
||||
|
||||
import q "platform/web/queries"
|
||||
import (
|
||||
"platform/web/core"
|
||||
m "platform/web/models"
|
||||
q "platform/web/queries"
|
||||
"time"
|
||||
|
||||
"gorm.io/gen/field"
|
||||
)
|
||||
|
||||
var Product = &productService{}
|
||||
|
||||
@@ -10,3 +17,68 @@ type productService struct{}
|
||||
func (s *productService) GetPrice(code string) {
|
||||
q.ProductSku.Where(q.ProductSku.Code.Eq(code)).Find()
|
||||
}
|
||||
|
||||
// 获取所有产品
|
||||
func (s *productService) AllProducts() ([]*m.Product, error) {
|
||||
return q.Product.Find()
|
||||
}
|
||||
|
||||
// 新增产品
|
||||
func (s *productService) CreateProduct(create *CreateProductData) error {
|
||||
return q.Product.Create(&m.Product{
|
||||
Code: create.Code,
|
||||
Name: create.Name,
|
||||
Description: create.Description,
|
||||
Sort: create.Sort,
|
||||
Status: m.ProductStatus(create.Status),
|
||||
})
|
||||
}
|
||||
|
||||
type CreateProductData struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
Sort int32 `json:"sort"`
|
||||
Status int `json:"status"`
|
||||
}
|
||||
|
||||
// 更新产品
|
||||
func (s *productService) UpdateProduct(update *UpdateProductData) error {
|
||||
if update == nil {
|
||||
return core.NewBizErr("更新数据不存在")
|
||||
}
|
||||
|
||||
do := make([]field.AssignExpr, 0, 5)
|
||||
if update.Code != nil {
|
||||
do = append(do, q.Product.Code.Value(*update.Code))
|
||||
}
|
||||
if update.Name != nil {
|
||||
do = append(do, q.Product.Name.Value(*update.Name))
|
||||
}
|
||||
if update.Description != nil {
|
||||
do = append(do, q.Product.Description.Value(*update.Description))
|
||||
}
|
||||
if update.Sort != nil {
|
||||
do = append(do, q.Product.Sort.Value(*update.Sort))
|
||||
}
|
||||
if update.Status != nil {
|
||||
do = append(do, q.Product.Status.Value(*update.Status))
|
||||
}
|
||||
_, err := q.Product.Where(q.Product.ID.Eq(update.Id)).UpdateSimple(do...)
|
||||
return err
|
||||
}
|
||||
|
||||
type UpdateProductData struct {
|
||||
Id int32 `json:"id"`
|
||||
Code *string `json:"code"`
|
||||
Name *string `json:"name"`
|
||||
Description *string `json:"description"`
|
||||
Sort *int32 `json:"sort"`
|
||||
Status *int `json:"status"`
|
||||
}
|
||||
|
||||
// 删除产品
|
||||
func (s *productService) DeleteProduct(id int32) error {
|
||||
_, err := q.Product.Where(q.Product.ID.Eq(id)).UpdateColumn(q.Product.DeletedAt, time.Now())
|
||||
return err
|
||||
}
|
||||
|
||||
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