实现产品查询和修改接口 & 修复套餐查询接口问题
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user