65 lines
2.0 KiB
Go
Executable File
65 lines
2.0 KiB
Go
Executable File
package model
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
|
)
|
|
|
|
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, productType int, isDel int, isShelf int, sort string) ([]FsProduct, error)
|
|
GetAllProductList(ctx context.Context, isDel int, isShelf int, sort string) (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, productType int, isDel int, isShelf int, sort string) (resp []FsProduct, err error) {
|
|
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)
|
|
}
|
|
err = m.conn.QueryRowsCtx(ctx, &resp, query, productType, isDel, isShelf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
func (m *defaultFsProductModel) GetAllProductList(ctx context.Context, isDel int, isShelf int, sort string) (resp []FsProduct, err error) {
|
|
query := fmt.Sprintf("select %s from %s where `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)
|
|
}
|
|
err = m.conn.QueryRowsCtx(ctx, &resp, query, isDel, isShelf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|