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-06 09:36:11 +00:00
GetPriceList ( ctx context . Context , productIds [ ] string ) ( [ ] GetPriceListRsp , error )
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" `
}
func ( m * defaultFsProductPriceModel ) GetPriceList ( ctx context . Context , productIds [ ] string ) ( resp [ ] GetPriceListRsp , err error ) {
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
}
}