58 lines
1.8 KiB
Go
Executable File
58 lines
1.8 KiB
Go
Executable File
package model
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
"strings"
|
|
)
|
|
|
|
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
|
|
GetProductListByConditions(ctx context.Context, productTypes []string, sort string) ([]FsProduct, error)
|
|
GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error)
|
|
}
|
|
|
|
customFsProductModel struct {
|
|
*defaultFsProductModel
|
|
}
|
|
)
|
|
|
|
// NewFsProductModel returns a model for the database table.
|
|
func NewFsProductModel(conn sqlx.SqlConn) FsProductModel {
|
|
return &customFsProductModel{
|
|
defaultFsProductModel: newFsProductModel(conn),
|
|
}
|
|
}
|
|
func (m *defaultFsProductModel) GetProductListByConditions(ctx context.Context, productTypes []string, sort string) (resp []FsProduct, err error) {
|
|
query := fmt.Sprintf("select %s from %s where `type` in (?) and `is_del` =? and `is_shelf` = ? and `status` =?",
|
|
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)
|
|
}
|
|
err = m.conn.QueryRowsCtx(ctx, &resp, query, strings.Join(productTypes, ","), 0, 1, 1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
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 ?",
|
|
fsProductRows, m.table)
|
|
err = m.conn.QueryRowsCtx(ctx, &resp, query, 0, 1, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|