2023-06-05 02:26:25 +00:00
package model
2023-06-06 09:36:11 +00:00
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"strings"
)
2023-06-05 02:26:25 +00:00
var _ FsProductPriceModel = ( * customFsProductPriceModel ) ( nil )
type (
// FsProductPriceModel is an interface to be customized, add more methods here,
// and implement the added methods in customFsProductPriceModel.
FsProductPriceModel interface {
fsProductPriceModel
2023-06-07 09:27:17 +00:00
GetPriceListByProductIds ( ctx context . Context , productIds [ ] string ) ( resp [ ] GetPriceListRsp , err error )
GetPriceListBySizeIds ( ctx context . Context , sizeIds [ ] string ) ( resp [ ] FsProductPrice , err error )
2023-06-06 09:36:11 +00:00
FindOneByProductIdMaterialIdSizeId ( ctx context . Context , productId int64 , materialId int64 , sizeId int64 ) ( * FsProductPrice , error )
2023-06-05 02:26:25 +00:00
}
customFsProductPriceModel struct {
* defaultFsProductPriceModel
}
)
// NewFsProductPriceModel returns a model for the database table.
func NewFsProductPriceModel ( conn sqlx . SqlConn ) FsProductPriceModel {
return & customFsProductPriceModel {
defaultFsProductPriceModel : newFsProductPriceModel ( conn ) ,
}
}
2023-06-06 09:36:11 +00:00
type GetPriceListRsp struct {
ProductId int64 ` json:"product_id" `
Price string ` json:"price" `
}
2023-06-07 09:27:17 +00:00
func ( m * defaultFsProductPriceModel ) GetPriceListByProductIds ( ctx context . Context , productIds [ ] string ) ( resp [ ] GetPriceListRsp , err error ) {
2023-06-06 09:36:11 +00:00
if len ( productIds ) == 0 {
return nil , nil
}
query := fmt . Sprintf ( "select %s from %s where `product_id` in (?) and `status` = ? group by product_id" , "product_id,group_concat(step_price) as price " , m . table )
if err = m . conn . QueryRowsCtx ( ctx , & resp , query , strings . Join ( productIds , "," ) , 1 ) ; err != nil {
return nil , err
}
return
}
func ( m * defaultFsProductPriceModel ) FindOneByProductIdMaterialIdSizeId ( ctx context . Context , productId int64 , materialId int64 , sizeId int64 ) ( * FsProductPrice , error ) {
var resp FsProductPrice
query := fmt . Sprintf ( "select %s from %s where `product_id` = ? and `material_id` = ? and `size_id` = ? limit 1" , fsProductPriceRows , m . table )
err := m . conn . QueryRowCtx ( ctx , & resp , query , productId , materialId , sizeId )
switch err {
case nil :
return & resp , nil
case sqlc . ErrNotFound :
return nil , ErrNotFound
default :
return nil , err
}
}
2023-06-07 09:27:17 +00:00
func ( m * defaultFsProductPriceModel ) GetPriceListBySizeIds ( ctx context . Context , sizeIds [ ] string ) ( resp [ ] FsProductPrice , err error ) {
if len ( sizeIds ) == 0 {
return nil , nil
}
query := fmt . Sprintf ( "select %s from %s where `size_id` in (?) and `status` = ? " , fsProductPriceRows , m . table )
if err = m . conn . QueryRowsCtx ( ctx , & resp , query , strings . Join ( sizeIds , "," ) , 1 ) ; err != nil {
return nil , err
}
return
}