fusenapi/model/fsproductmodel.go

57 lines
1.7 KiB
Go
Raw Normal View History

2023-06-01 07:32:28 +00:00
package model
2023-06-06 09:36:11 +00:00
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
2023-06-01 07:32:28 +00:00
var _ FsProductModel = (*customFsProductModel)(nil)
type (
// FsProductModel is an interface to be customized, add more methods here,
// and implement the added methods in customFsProductModel.
FsProductModel interface {
fsProductModel
2023-06-06 10:17:03 +00:00
GetProductListByConditions(ctx context.Context, productType int, sort string) ([]FsProduct, error)
GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error)
2023-06-01 07:32:28 +00:00
}
customFsProductModel struct {
*defaultFsProductModel
}
)
// NewFsProductModel returns a model for the database table.
func NewFsProductModel(conn sqlx.SqlConn) FsProductModel {
return &customFsProductModel{
defaultFsProductModel: newFsProductModel(conn),
}
}
2023-06-06 10:17:03 +00:00
func (m *defaultFsProductModel) GetProductListByConditions(ctx context.Context, productType int, sort string) (resp []FsProduct, err error) {
2023-06-06 09:36:11 +00:00
query := fmt.Sprintf("select %s from %s where `type` = ? and `is_del` =? and `is_shelf` = ?",
fsProductRows, m.table)
switch sort {
case "sort-asc":
query = fmt.Sprintf("%s order by sort ASC", query)
case "sort-desc":
query = fmt.Sprintf("%s order by sort DESC", query)
default:
query = fmt.Sprintf("%s order by sort DESC", query)
}
2023-06-06 10:17:03 +00:00
err = m.conn.QueryRowsCtx(ctx, &resp, query, productType, 0, 1)
2023-06-06 09:36:11 +00:00
if err != nil {
return nil, err
}
return
}
2023-06-06 10:17:03 +00:00
func (m *defaultFsProductModel) GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error) {
query := fmt.Sprintf("select %s from %s where `is_del` =? and `is_shelf` = ? order by RAND() limit ?",
2023-06-06 09:36:11 +00:00
fsProductRows, m.table)
2023-06-06 10:17:03 +00:00
err = m.conn.QueryRowsCtx(ctx, &resp, query, 0, 1, limit)
2023-06-06 09:36:11 +00:00
if err != nil {
return nil, err
}
return
}