package model import ( "context" "fmt" "github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlx" "strings" ) 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 GetPriceListByProductIds(ctx context.Context, productIds []string) (resp []GetPriceListRsp, err error) GetPriceListBySizeIds(ctx context.Context, sizeIds []string) (resp []FsProductPrice, err error) FindOneByProductIdMaterialIdSizeId(ctx context.Context, productId int64, materialId int64, sizeId int64) (*FsProductPrice, error) } customFsProductPriceModel struct { *defaultFsProductPriceModel } ) // NewFsProductPriceModel returns a model for the database table. func NewFsProductPriceModel(conn sqlx.SqlConn) FsProductPriceModel { return &customFsProductPriceModel{ defaultFsProductPriceModel: newFsProductPriceModel(conn), } } type GetPriceListRsp struct { ProductId int64 `json:"product_id"` Price string `json:"price"` } func (m *defaultFsProductPriceModel) GetPriceListByProductIds(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 } } 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 }