package services import ( "platform/web/core" m "platform/web/models" q "platform/web/queries" "time" "github.com/shopspring/decimal" "gorm.io/gen" "gorm.io/gen/field" ) var ProductSku = &productSkuService{} type productSkuService struct{} func (s *productSkuService) All(product_code string) (result []*m.ProductSku, err error) { return q.ProductSku. Joins(q.ProductSku.Product). Where(q.Product.As("Product").Code.Eq(product_code)). Select(q.ProductSku.ALL). Order(q.ProductSku.Sort). Find() } func (s *productSkuService) Page(req *core.PageReq, productId *int32) (result []*m.ProductSku, count int64, err error) { do := make([]gen.Condition, 0) if productId != nil { do = append(do, q.ProductSku.ProductID.Eq(*productId)) } return q.ProductSku. Joins(q.ProductSku.Discount, q.ProductSku.Product). Where(do...). Order(q.ProductSku.Sort). FindByPage(req.GetOffset(), req.GetLimit()) } func (s *productSkuService) Create(create CreateProductSkuData) (err error) { price, err := decimal.NewFromString(create.Price) if err != nil { return core.NewBizErr("产品价格的格式不正确", err) } priceMin, err := decimal.NewFromString(create.PriceMin) if err != nil { return core.NewBizErr("产品最低价格的格式不正确", err) } countMin := int32(1) if create.CountMin != nil { countMin = *create.CountMin } return q.ProductSku.Create(&m.ProductSku{ ProductID: create.ProductID, DiscountId: create.DiscountID, Code: create.Code, Name: create.Name, Price: price, PriceMin: priceMin, Sort: create.Sort, CountMin: countMin, }) } type CreateProductSkuData struct { ProductID int32 `json:"product_id"` DiscountID int32 `json:"discount_id"` Code string `json:"code"` Name string `json:"name"` Price string `json:"price"` PriceMin string `json:"price_min"` Sort int32 `json:"sort"` CountMin *int32 `json:"count_min"` } func (s *productSkuService) Update(update UpdateProductSkuData) (err error) { do := make([]field.AssignExpr, 0) if update.Price != nil { price, err := decimal.NewFromString(*update.Price) if err != nil { return core.NewBizErr("产品价格的格式不正确", err) } do = append(do, q.ProductSku.Price.Value(price)) } if update.PriceMin != "" { priceMin, err := decimal.NewFromString(update.PriceMin) if err != nil { return core.NewBizErr("产品最低价格的格式不正确", err) } do = append(do, q.ProductSku.PriceMin.Value(priceMin)) } if update.DiscountID != nil { do = append(do, q.ProductSku.DiscountId.Value(*update.DiscountID)) } if update.Code != nil { do = append(do, q.ProductSku.Code.Value(*update.Code)) } if update.Name != nil { do = append(do, q.ProductSku.Name.Value(*update.Name)) } if update.Status != nil { do = append(do, q.ProductSku.Status.Value(*update.Status)) } if update.Sort != nil { do = append(do, q.ProductSku.Sort.Value(*update.Sort)) } if update.CountMin != nil { do = append(do, q.ProductSku.CountMin.Value(*update.CountMin)) } _, err = q.ProductSku.Where(q.ProductSku.ID.Eq(update.ID)).UpdateSimple(do...) return err } type UpdateProductSkuData struct { ID int32 `json:"id"` DiscountID *int32 `json:"discount_id"` Code *string `json:"code"` Name *string `json:"name"` Price *string `json:"price"` PriceMin string `json:"price_min"` Status *int32 `json:"status"` Sort *int32 `json:"sort"` CountMin *int32 `json:"count_min"` } func (s *productSkuService) Delete(id int32) (err error) { _, err = q.ProductSku.Where(q.ProductSku.ID.Eq(id)).UpdateColumn(q.ProductSku.DeletedAt, time.Now()) return } func (s *productSkuService) BatchUpdateDiscount(data BatchUpdateSkuDiscountData) (err error) { _, err = q.ProductSku.Where(q.ProductSku.ProductID.Eq(data.ProductID)).UpdateSimple( q.ProductSku.DiscountId.Value(data.DiscountID), ) return } type BatchUpdateSkuDiscountData struct { ProductID int32 `json:"product_id"` DiscountID int32 `json:"discount_id"` }