82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
package services
|
|
|
|
import (
|
|
"platform/web/core"
|
|
m "platform/web/models"
|
|
q "platform/web/queries"
|
|
"time"
|
|
|
|
"gorm.io/gen/field"
|
|
)
|
|
|
|
var Product = &productService{}
|
|
|
|
type productService struct{}
|
|
|
|
// 获取所有产品
|
|
func (s *productService) AllProducts() ([]*m.Product, error) {
|
|
return q.Product.
|
|
Order(q.Product.Sort.Asc(), q.Product.CreatedAt.Desc()).
|
|
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
|
|
}
|