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.Order(q.ProductDiscount.CreatedAt.Desc()).Find() } func (s *productDiscountService) Page(req *core.PageReq) (result []*m.ProductDiscount, count int64, err error) { return q.ProductDiscount.Order(q.ProductDiscount.CreatedAt.Desc()).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 }