完善定价与套餐关联表结构
This commit is contained in:
59
web/services/product_discount.go
Normal file
59
web/services/product_discount.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"platform/web/core"
|
||||
m "platform/web/models"
|
||||
q "platform/web/queries"
|
||||
"time"
|
||||
|
||||
"gorm.io/gen/field"
|
||||
)
|
||||
|
||||
var ProductDiscount = &productDiscountService{}
|
||||
|
||||
type productDiscountService struct{}
|
||||
|
||||
func (s *productDiscountService) All() (result []*m.ProductDiscount, err error) {
|
||||
return q.ProductDiscount.Find()
|
||||
}
|
||||
|
||||
func (s *productDiscountService) Page(req *core.PageReq) (result []*m.ProductDiscount, count int64, err error) {
|
||||
return q.ProductDiscount.FindByPage(req.GetOffset(), req.GetLimit())
|
||||
}
|
||||
|
||||
func (s *productDiscountService) Create(data CreateProductDiscountData) (err error) {
|
||||
return q.ProductDiscount.Create(&m.ProductDiscount{
|
||||
Name: data.Name,
|
||||
Discount: data.Discount,
|
||||
})
|
||||
}
|
||||
|
||||
type CreateProductDiscountData struct {
|
||||
Name string `json:"name"`
|
||||
Discount int32 `json:"discount" validate:"min=0,max=100"`
|
||||
}
|
||||
|
||||
func (s *productDiscountService) Update(data UpdateProductDiscountData) (err error) {
|
||||
do := make([]field.AssignExpr, 0)
|
||||
|
||||
if data.Name != nil {
|
||||
do = append(do, q.ProductDiscount.Name.Value(*data.Name))
|
||||
}
|
||||
if data.Discount != nil {
|
||||
do = append(do, q.ProductDiscount.Discount.Value(*data.Discount))
|
||||
}
|
||||
|
||||
_, err = q.ProductDiscount.Where(q.ProductDiscount.ID.Eq(data.ID)).UpdateSimple(do...)
|
||||
return err
|
||||
}
|
||||
|
||||
type UpdateProductDiscountData struct {
|
||||
ID int32 `json:"id"`
|
||||
Name *string `json:"name"`
|
||||
Discount *int32 `json:"discount" validate:"omitempty,min=0,max=100"`
|
||||
}
|
||||
|
||||
func (s *productDiscountService) Delete(id int32) (err error) {
|
||||
_, err = q.ProductDiscount.Where(q.ProductDiscount.ID.Eq(id)).UpdateColumn(q.ProductDiscount.DeletedAt, time.Now())
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user