137 lines
3.0 KiB
Go
137 lines
3.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) AllProductSaleInfos() ([]*m.Product, error) {
|
|
products, err := q.Product.
|
|
Select(
|
|
q.Product.ID,
|
|
q.Product.Code,
|
|
q.Product.Name,
|
|
q.Product.Description,
|
|
q.Product.Sort,
|
|
).
|
|
Where(
|
|
q.Product.Status.Eq(int(m.ProductStatusEnabled)),
|
|
).
|
|
Order(q.Product.Sort).
|
|
Find()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pids := make([]int32, len(products))
|
|
for i, p := range products {
|
|
pids[i] = p.ID
|
|
}
|
|
|
|
skus, err := q.ProductSku.
|
|
Select(
|
|
q.ProductSku.ID,
|
|
q.ProductSku.ProductID,
|
|
q.ProductSku.Name,
|
|
q.ProductSku.Code,
|
|
q.ProductSku.Price,
|
|
).
|
|
Where(
|
|
q.ProductSku.ProductID.In(pids...),
|
|
q.ProductSku.Status.Eq(int32(m.SkuStatusEnabled)),
|
|
).
|
|
Order(q.ProductSku.Sort).
|
|
Find()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pmap := make(map[int32]*m.Product, len(products))
|
|
for _, p := range products {
|
|
pmap[p.ID] = p
|
|
p.Skus = make([]*m.ProductSku, 0)
|
|
}
|
|
for _, s := range skus {
|
|
if p, ok := pmap[s.ProductID]; ok {
|
|
p.Skus = append(p.Skus, s)
|
|
}
|
|
}
|
|
|
|
return products, nil
|
|
}
|
|
|
|
// 新增产品
|
|
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
|
|
}
|