package gmodel import ( "context" "errors" "gorm.io/gorm" ) type GetPriceListByProductIdsRsp struct { ProductId int64 `json:"product_id"` Price string `json:"price"` } func (p *FsProductPriceModel) GetSimplePriceListByProductIds(ctx context.Context, productIds []int64) (resp []GetPriceListByProductIdsRsp, err error) { if len(productIds) == 0 { return nil, nil } db := p.db.WithContext(ctx).Model(&FsProductPrice{}).Select("product_id,group_concat(step_price) as price"). Where("`product_id` in (?) and `status` = ? group by product_id", productIds, 1) if err = db.Find(&resp).Error; err != nil { return nil, err } return } func (p *FsProductPriceModel) GetPriceListBySizeIds(ctx context.Context, sizeIds []int64) (resp []FsProductPrice, err error) { if len(sizeIds) == 0 { return } err = p.db.WithContext(ctx).Model(&FsProductPrice{}).Where("`size_id` in (?) and `status` = ? ", sizeIds, 1).Find(&resp).Error if err != nil { return nil, err } return } type FindOneProductPriceByParamsReq struct { ProductId *int64 MaterialId *int64 SizeId *int64 Status *int64 } func (p *FsProductPriceModel) FindOneProductPriceByParams(ctx context.Context, req FindOneProductPriceByParamsReq) (resp FsProductPrice, err error) { db := p.db.WithContext(ctx).Model(&FsProductPrice{}) if req.ProductId != nil { db = db.Where("`product_id` = ?", *req.ProductId) } if req.MaterialId != nil { db = db.Where("`material_id` = ?", *req.MaterialId) } if req.SizeId != nil { db = db.Where("`size_id` = ?", *req.SizeId) } if req.Status != nil { db = db.Where("`status` = ?", *req.Status) } if err = db.First(&resp).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { return FsProductPrice{}, err } return resp, nil } func (p *FsProductPriceModel) GetPriceListByProductIds(ctx context.Context, productIds []int64) (resp []FsProductPrice, err error) { if len(productIds) == 0 { return nil, nil } db := p.db.WithContext(ctx).Model(&FsProductPrice{}). Where("`product_id` in (?) and `status` = ?", productIds, 1) if err = db.Find(&resp).Error; err != nil { return nil, err } return } // 产品价格 type ProductPrice struct { Id int64 `json:"id"` MinBuyNum int64 `json:"min_buy_num"` StepNum interface{} `json:"step_num"` StepPrice interface{} `json:"step_price"` ProductId int64 `json:"product_id"` SizeId int64 `json:"size_id"` EachBoxNum int64 `json:"each_box_num"` } func (c *FsProductPriceModel) GetAllSelectBySizeId(ctx context.Context, sizeIds []int64) (prices []*ProductPrice, err error) { err = c.db.WithContext(ctx).Model(&ProductPrice{}).Where("size_id IN (?) AND status = ?", sizeIds, 1).Select("id, min_buy_num, step_num, step_price, product_id, size_id, each_box_num").Find(&prices).Error return prices, err }